更换成 腾讯短信服务
This commit is contained in:
parent
c53e3903c4
commit
d8e0666d8a
@ -8,9 +8,9 @@ import random
|
||||
import string
|
||||
import redis
|
||||
from app.core.config import settings
|
||||
from unisdk.sms import UniSMS
|
||||
from unisdk.exception import UniException
|
||||
from datetime import timedelta
|
||||
from tencentcloud.common import credential
|
||||
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
|
||||
from tencentcloud.sms.v20210111 import sms_client, models
|
||||
from app.core.security import create_access_token, set_jwt_cookie, clear_jwt_cookie, get_password_hash, verify_password
|
||||
from app.core.response import success_response, error_response, ResponseModel
|
||||
from pydantic import BaseModel, Field
|
||||
@ -31,8 +31,6 @@ redis_client = redis.Redis(
|
||||
decode_responses=True
|
||||
)
|
||||
|
||||
# 初始化短信客户端
|
||||
client = UniSMS(settings.UNI_APP_ID)
|
||||
|
||||
@router.post("/send-code")
|
||||
async def send_verify_code(request: VerifyCodeRequest):
|
||||
@ -41,18 +39,28 @@ async def send_verify_code(request: VerifyCodeRequest):
|
||||
code = ''.join(random.choices(string.digits, k=6))
|
||||
|
||||
try:
|
||||
# 发送短信
|
||||
res = client.send({
|
||||
"to": phone,
|
||||
"signature": settings.UNI_SMS_SIGN,
|
||||
"templateId": settings.UNI_SMS_TEMPLATE_ID,
|
||||
"templateData": {
|
||||
"code": code
|
||||
}
|
||||
})
|
||||
# 实例化认证对象
|
||||
cred = credential.Credential(
|
||||
settings.TENCENT_SECRET_ID,
|
||||
settings.TENCENT_SECRET_KEY
|
||||
)
|
||||
|
||||
if res.code != "0":
|
||||
return error_response(message=f"短信发送失败: {res.message}")
|
||||
# 实例化短信客户端
|
||||
client = sms_client.SmsClient(cred, "ap-guangzhou")
|
||||
|
||||
# 组装请求参数
|
||||
req = models.SendSmsRequest()
|
||||
req.SmsSdkAppId = settings.SMS_SDK_APP_ID
|
||||
req.SignName = settings.SMS_SIGN_NAME
|
||||
req.TemplateId = settings.SMS_TEMPLATE_ID
|
||||
req.TemplateParamSet = [code]
|
||||
req.PhoneNumberSet = [f"+86{phone}"]
|
||||
|
||||
# 发送短信
|
||||
resp = client.SendSms(req)
|
||||
|
||||
if resp.SendStatusSet[0].Code != "Ok":
|
||||
return error_response(message=f"短信发送失败: {resp.SendStatusSet[0].Message}")
|
||||
|
||||
# 存储验证码到 Redis
|
||||
redis_client.setex(
|
||||
@ -63,7 +71,7 @@ async def send_verify_code(request: VerifyCodeRequest):
|
||||
|
||||
return success_response(message="验证码已发送")
|
||||
|
||||
except UniException as e:
|
||||
except TencentCloudSDKException as e:
|
||||
return error_response(message=f"发送验证码失败: {str(e)}")
|
||||
|
||||
@router.post("/login")
|
||||
|
||||
@ -23,18 +23,6 @@ class Settings(BaseSettings):
|
||||
REDIS_PASSWORD: str = "redis_rJRMHr"
|
||||
VERIFICATION_CODE_EXPIRE_SECONDS: int = 300 # 验证码5分钟后过期
|
||||
|
||||
# 短信配置
|
||||
UNI_APP_ID: str = "kFb5kA5EDXpnzUReadaRNpDTFf6rNmXEc45jwS2C1Mvh9Erj2"
|
||||
UNI_SMS_TEMPLATE_ID: str = "pub_verif_basic" # 验证码短信模板ID
|
||||
UNI_SMS_SIGN: str = "BAISIJI" # 短信签名
|
||||
|
||||
# MySQL配置
|
||||
# MYSQL_HOST: str = "101.36.120.145"
|
||||
# MYSQL_PORT: int = 3306
|
||||
# MYSQL_USER: str = "root"
|
||||
# MYSQL_PASSWORD: str = "mariadb_4rMwpT"
|
||||
# MYSQL_DB: str = "deliveryman"
|
||||
|
||||
MYSQL_HOST: str = "gz-cynosdbmysql-grp-2j1cnopr.sql.tencentcdb.com"
|
||||
MYSQL_PORT: int = 27469
|
||||
MYSQL_USER: str = "root"
|
||||
@ -46,9 +34,14 @@ class Settings(BaseSettings):
|
||||
def SQLALCHEMY_DATABASE_URL(self) -> str:
|
||||
return f"mysql+pymysql://{self.MYSQL_USER}:{self.MYSQL_PASSWORD}@{self.MYSQL_HOST}:{self.MYSQL_PORT}/{self.MYSQL_DB}?charset=utf8mb4"
|
||||
|
||||
# 腾讯云短信配置
|
||||
TENCENT_SECRET_ID: str = "AKIDxnbGj281iHtKallqqzvlV5YxBCrPltnS"
|
||||
TENCENT_SECRET_KEY: str = "ta6PXTMBsX7dzA7IN6uYUFn8F9uTovoU"
|
||||
SMS_SDK_APP_ID: str = "1400961527"
|
||||
SMS_SIGN_NAME: str = "蜂快到家公众号"
|
||||
SMS_TEMPLATE_ID: str = "2353143" # 验证码短信模板ID
|
||||
|
||||
# 腾讯云 COS 配置
|
||||
COS_SECRET_ID: str = "AKIDxnbGj281iHtKallqqzvlV5YxBCrPltnS"
|
||||
COS_SECRET_KEY: str = "ta6PXTMBsX7dzA7IN6uYUFn8F9uTovoU"
|
||||
COS_REGION: str = "ap-chengdu"
|
||||
COS_BUCKET: str = "dman-1311994147"
|
||||
COS_BASE_URL: str = "dman-1311994147.cos.ap-chengdu.myqcloud.com"
|
||||
@ -66,6 +59,8 @@ class Settings(BaseSettings):
|
||||
WECHAT_API_V3_KEY: str = "your-api-v3-key" # API v3密钥
|
||||
WECHAT_PLATFORM_CERT_PATH: str = "app/core/wechat_platform_cert.pem" # 平台证书路径
|
||||
|
||||
|
||||
|
||||
class Config:
|
||||
case_sensitive = True
|
||||
env_file = ".env"
|
||||
|
||||
@ -8,8 +8,8 @@ logging.basicConfig(level=logging.INFO, stream=sys.stdout)
|
||||
|
||||
cos_config = CosConfig(
|
||||
Region=settings.COS_REGION,
|
||||
SecretId=settings.COS_SECRET_ID,
|
||||
SecretKey=settings.COS_SECRET_KEY
|
||||
SecretId=settings.TENCENT_SECRET_ID,
|
||||
SecretKey=settings.TENCENT_SECRET_KEY
|
||||
)
|
||||
|
||||
cos_client = CosS3Client(cos_config)
|
||||
@ -9,8 +9,7 @@ sqlalchemy>=1.4.23
|
||||
redis==5.0.1
|
||||
pymysql==1.1.0
|
||||
SQLAlchemy==2.0.27
|
||||
unisms
|
||||
cos-python-sdk-v5==1.9.25
|
||||
tencentcloud-sdk-python==3.0.1035
|
||||
bcrypt
|
||||
aiohttp==3.9.1
|
||||
cryptography==42.0.2
|
||||
Loading…
Reference in New Issue
Block a user