Compare commits

..

10 Commits

Author SHA1 Message Date
0db205d0ef bugfix 2026-05-31 12:38:10 +08:00
2b639c7eaf revert: generate_sms_code returns None on failure (keep original behavior) 2026-05-31 12:26:53 +08:00
b1e08c46f5 fix: generate_sms_code raises exception with error details instead of returning None 2026-05-31 11:56:44 +08:00
8a2a7d02ed revert: remove mark_used parameter (API used by other systems) 2026-05-29 11:38:37 +08:00
bbd10a6471 fix: add mark_used parameter to check_sms_code for multi-account login flow
When multiple accounts share a phone number, the first SMS code check
should verify without consuming the code, so the second call (after
account selection) can re-verify. Default mark_used=True preserves
backward compatibility.
2026-05-29 11:31:40 +08:00
e95e3b8893 bugfix 2026-03-23 11:13:29 +08:00
ping
6b875adeb2 update 2026-03-21 11:51:05 +08:00
ping
a47f6df2e3 update 2026-03-21 10:27:11 +08:00
ping
2d13b9696b update 2026-03-21 09:58:35 +08:00
ping
2558c55dbb update 2026-03-21 09:33:13 +08:00

View File

@ -9,6 +9,7 @@ Features:
- 配置从文件读取
"""
import os
from functools import partial
import datetime
import random
import string
@ -21,6 +22,8 @@ from sqlor.dbpools import DBPools
from appPublic.uniqueID import getID as uuid
from ahserver.serverenv import ServerEnv
from sqlor.dbpools import get_sor_context
from appPublic.worker import awaitify
from appPublic.log import debug, exception, error
SMS_TEMPLATE_TABLE = {
"summary": [{"name": "sms_template", "primary": "id"}],
@ -96,12 +99,11 @@ class SMSEngine:
async def send(self, stype, template_id, phone, params) -> dict:
try:
resp = self.sms_client.send_message(
signature_id=self.signature_id,
f = partial(self.sms_client.send_message, signature_id=self.signature_id,
template_id=template_id,
mobile=phone,
content_var_dict=params
)
content_var_dict=params)
resp = await awaitify(f)()
return await self.__validation(stype, template_id, params, phone, resp)
except ex.BceHttpClientError as e:
if isinstance(e.last_error, ex.BceServerError):
@ -113,8 +115,9 @@ class SMSEngine:
async def send_vcode(self, phone: str, stype: str, vcode) -> dict:
env = ServerEnv()
async with get_sor_context(env, 'kboss') as sor:
async with get_sor_context(env, 'smssend') as sor:
template_info = await sor.R('sms_template', {'name': stype, 'del_flg': '0'})
debug(f'{phone=},{stype=}, {vcode=}, {template_info=}')
if template_info:
template_id = template_info[0]['code']
else:
@ -129,11 +132,11 @@ class SMSEngine:
}
await sor.C('sms_record', log)
return {'status': False, 'msg': '模板未配置请检查sms_template表'}
return await self.send(stype, template_id, phone, vcode)
return await self.send(stype, template_id, phone, vcode)
async def __validation(self, stype, template_id, params, phone, resp) -> dict:
env = ServerEnv()
async with get_sor_context(env, 'kboss') as sor:
async with get_sor_context(env, 'smssend') as sor:
send_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
log = {
'id': uuid(),
@ -155,12 +158,14 @@ class SMSEngine:
await sor.C('sms_record', log)
return {'status': False, 'msg': msg}
async def generate_sms_code(self, length: int = 6, expire_minutes: int = 5) -> str:
async def generate_sms_code(self, phone, length=None, expire_minutes=None):
length = int(length) if length is not None else 6
expire_minutes = int(expire_minutes) if expire_minutes is not None else 5
code = ''.join(random.choices(string.digits, k=length))
code_id = uuid()
expire_time = datetime.datetime.now() + datetime.timedelta(minutes=expire_minutes)
env = ServerEnv()
async with get_sor_context(env, 'kboss') as sor:
async with get_sor_context(env, 'smssend') as sor:
await sor.C('validatecode', {
'id': code_id,
'vcode': code,
@ -168,17 +173,26 @@ class SMSEngine:
'del_flg': '0',
'create_at': datetime.datetime.now()
})
return code_id, code
vcode = {'SMSvCode': code}
# d = await self.send_vcode(phone, "用户注册登录验证", vcode)
d = await self.send_vcode(phone, "用户注册登录验证", vcode)
debug(f'{d=}, {code=}, {phone=}')
if d['status']:
return code_id, code
else:
return None
async def check_sms_code(self, code_id: str, vcode: str) -> bool:
env = ServerEnv()
async with get_sor_context(env, 'kboss') as sor:
code_info = await sor.R('validatecode', {'id': code_id, 'del_flg': '0'})
async with get_sor_context(env, 'smssend') as sor:
code_info = await sor.R('validatecode', {'id': code_id})
if not code_info:
debug(f'check_sms_code():{code_id=} validatecode not found')
return False
code_info = code_info[0]
if code_info['vcode'] != vcode:
debug(f'check_sms_code():{vcode=} , {code_info["vcode"]} not match')
return False
now = datetime.datetime.now()
@ -187,12 +201,15 @@ class SMSEngine:
code_info['expire_time'] = datetime.datetime.fromisoformat(code_info['expire_time'])
if now > code_info['expire_time']:
debug(f'check_sms_code():timeout ')
return False
await sor.U('validatecode', {'id': code_id, 'del_flg': '1'})
debug(f'check_sms_code(): return True ')
return True
async def send_sms(self, phone: str, stype: str) -> dict:
async def send_sms(self, phone: str, stype: str, code: str) -> dict:
"""
code_id, code = await self.generate_sms_code()
if code_id is None:
return {
@ -201,7 +218,7 @@ class SMSEngine:
'message': '生成的手机号出错'
}
}
"""
vcode = {'SMSvCode': code}
result = await self.send_vcode(phone, stype, vcode)