Spaces:
Running
Running
Upload 2 files
Browse files- models.py +1 -0
- router_users.py +16 -0
models.py
CHANGED
|
@@ -6,6 +6,7 @@ class SendCodeRequest(BaseModel):
|
|
| 6 |
contact: str
|
| 7 |
contact_type: str # "email" 或 "phone"
|
| 8 |
action_type: str # "register" 或 "reset"
|
|
|
|
| 9 |
|
| 10 |
class UserRegister(BaseModel):
|
| 11 |
account: str
|
|
|
|
| 6 |
contact: str
|
| 7 |
contact_type: str # "email" 或 "phone"
|
| 8 |
action_type: str # "register" 或 "reset"
|
| 9 |
+
account: Optional[str] = None # 【新增】:仅重置密码时需要传,用于校验身份
|
| 10 |
|
| 11 |
class UserRegister(BaseModel):
|
| 12 |
account: str
|
router_users.py
CHANGED
|
@@ -57,6 +57,22 @@ def send_sms_code(phone: str, code: str, action: str):
|
|
| 57 |
# 【核心优化】:引入 BackgroundTasks
|
| 58 |
@router.post("/api/users/send-code")
|
| 59 |
async def send_verify_code(req: SendCodeRequest, bg_tasks: BackgroundTasks):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
code = str(random.randint(100000, 999999))
|
| 61 |
cache_key = f"{req.contact}_{req.action_type}"
|
| 62 |
|
|
|
|
| 57 |
# 【核心优化】:引入 BackgroundTasks
|
| 58 |
@router.post("/api/users/send-code")
|
| 59 |
async def send_verify_code(req: SendCodeRequest, bg_tasks: BackgroundTasks):
|
| 60 |
+
# 【新增核心逻辑】:如果是找回密码,先去数据库里核对联系方式
|
| 61 |
+
if req.action_type == "reset":
|
| 62 |
+
if not req.account:
|
| 63 |
+
raise HTTPException(status_code=400, detail="找回密码需先填写当前账号")
|
| 64 |
+
|
| 65 |
+
users_db = db.load_data("users.json", default_data={})
|
| 66 |
+
user = users_db.get(req.account)
|
| 67 |
+
if not user:
|
| 68 |
+
raise HTTPException(status_code=404, detail="该账号不存在")
|
| 69 |
+
|
| 70 |
+
if req.contact_type == "email" and user.get("email") != req.contact:
|
| 71 |
+
raise HTTPException(status_code=400, detail="填写的邮箱与该账号绑定的邮箱不一致")
|
| 72 |
+
if req.contact_type == "phone" and user.get("phone") != req.contact:
|
| 73 |
+
raise HTTPException(status_code=400, detail="填写的手机号与该账号绑定的手机号不一致")
|
| 74 |
+
|
| 75 |
+
# 原有的生成验证码和发送逻辑保持不变
|
| 76 |
code = str(random.randint(100000, 999999))
|
| 77 |
cache_key = f"{req.contact}_{req.action_type}"
|
| 78 |
|