This commit is contained in:
aaron 2025-03-06 09:11:11 +08:00
parent 26141cce48
commit 08c648bc88
2 changed files with 29 additions and 26 deletions

View File

@ -26,41 +26,43 @@ from app.models.address import AddressDB, AddressInfo
from app.models.user import UserUpdateRoles, UserUpdateDeliveryCommission
from app.models.order import ShippingOrderDB, OrderStatus
from app.core.redis_client import redis_client
import logging
router = APIRouter()
@router.post("/send-code")
async def send_verify_code(request: VerifyCodeRequest):
"""发送验证码"""
try:
phone = request.phone
# 通过 redis 实现验证码发送频率限制
limit_time = redis_client.get_client().get(f"verify_code_limit:{phone}")
if limit_time and datetime.now() < limit_time:
return error_response(message="验证码发送频率过高")
phone = request.phone
# 通过 redis 实现验证码发送频率限制
stored_phone = redis_client.get_client().get(f"verify_code_limit:{phone}")
if phone == stored_phone:
return error_response(message="验证码发送频率过高")
# 发送验证码
code, request_id = await qcloud_manager.send_sms_code(phone)
# 存储验证码到 Redis
redis_client.get_client().setex(
f"verify_code:{phone}",
settings.VERIFICATION_CODE_EXPIRE_SECONDS,
code
)
# 设置验证码发送频率限制
redis_client.get_client().setex(
f"verify_code_limit:{phone}",
datetime.now() + timedelta(seconds=30),
True
)
return success_response(message="验证码已发送")
# 发送验证码
code, request_id = await qcloud_manager.send_sms_code(phone)
print(f"验证码发送code: {code}, request_id: {request_id}")
# 存储验证码到 Redis
redis_client.get_client().setex(
f"verify_code:{phone}",
settings.VERIFICATION_CODE_EXPIRE_SECONDS,
code
)
# 设置验证码发送频率限制
redis_client.get_client().setex(
f"verify_code_limit:{phone}",
settings.VERIFICATION_CODE_LIMIT_SECONDS,
phone,
)
except Exception as e:
return error_response(message=f"发送验证码失败: {str(e)}")
return success_response(message="验证码已发送")
# except Exception as e:
# logging.error(f"发送验证码失败: {str(e)}")
# return error_response(message=f"发送验证码失败: {str(e)}")
@router.post("/login")
async def login(

View File

@ -36,6 +36,7 @@ class Settings(BaseSettings):
REDIS_DB: int = 0
REDIS_PASSWORD: str = "redis_rJRMHr"
VERIFICATION_CODE_EXPIRE_SECONDS: int = 300 # 验证码5分钟后过期
VERIFICATION_CODE_LIMIT_SECONDS: int = 15 # 验证码发送频率限制15秒
MYSQL_HOST: str = "gz-cynosdbmysql-grp-2j1cnopr.sql.tencentcdb.com"
MYSQL_PORT: int = 27469