kboss/b/customer/forgotPassword.dspy
2026-05-22 19:18:37 +08:00

71 lines
2.9 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

async def forgotPassword(ns):
"""
忘记密码:校验短信验证码后重置密码。
参数:
id (str) 用户ID找回验证码接口返回的 userid
password (str) 新密码
codeid (str) 验证码ID
vcode (str) 验证码
也可传 mobile 或 username 定位用户(未传 id 时)。
"""
import re
import traceback
if not ns.get('password'):
return {'status': False, 'msg': '新密码不能为空'}
if len(ns.get('password')) < 8 or not re.search(r'[a-zA-Z]', ns.get('password')) or not re.search(r'[0-9]', ns.get('password')):
return {'status': False, 'msg': '密码至少8位包含大小写字母、特殊字符、数字'}
if not ns.get('codeid'):
return {'status': False, 'msg': '验证码ID不能为空'}
if not ns.get('vcode'):
return {'status': False, 'msg': '验证码不能为空'}
db = DBPools()
async with db.sqlorContext('kboss') as sor:
try:
code = await sor.R('validatecode', {'id': ns.get('codeid'), 'vcode': ns.get('vcode')})
if code:
create_at = code[0]['create_at']
now = datetime.datetime.now()
create_at_dt = datetime.datetime.strptime(create_at, "%Y-%m-%d %H:%M:%S")
if (now - create_at_dt).seconds > 500:
return {'status': False, 'msg': '验证码过期'}
else:
return {'status': False, 'msg': '验证码不正确'}
user = None
if ns.get('id'):
users = await sor.R('users', {'id': ns.get('id'), 'del_flg': '0'})
if users:
user = users[0]
elif ns.get('mobile'):
users = await sor.R('users', {'mobile': ns.get('mobile'), 'del_flg': '0'})
if users:
user = users[0]
elif ns.get('username'):
users = await sor.R('users', {'username': ns.get('username'), 'del_flg': '0'})
if not users:
users = await sor.R('users', {'mobile': ns.get('username'), 'del_flg': '0'})
if users:
user = users[0]
else:
return {'status': False, 'msg': '用户标识不能为空'}
if not user:
return {'status': False, 'msg': '用户不存在'}
new_password = password_encode(ns['password'])
update_sql = """UPDATE users SET password = '%s' WHERE id = '%s';""" % (new_password, user['id'])
await sor.sqlExe(update_sql, {})
return {'status': True, 'msg': '密码重置成功'}
except Exception as error:
debug(f"forgotPassword 错误: {error}")
debug(f"forgotPassword 错误堆栈: {traceback.format_exc()}")
return {'status': False, 'msg': '密码重置失败, %s' % str(error)}
ret = await forgotPassword(params_kw)
return ret