Compare commits

..

No commits in common. "0db205d0ef2a4ec2e90a83c1eb6022d5ca9a7fcd" and "5e6476f47a50d455a9dc08ae904fc25263ccd8c2" have entirely different histories.

View File

@ -9,7 +9,6 @@ Features:
- 配置从文件读取
"""
import os
from functools import partial
import datetime
import random
import string
@ -22,8 +21,6 @@ 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"}],
@ -99,11 +96,12 @@ class SMSEngine:
async def send(self, stype, template_id, phone, params) -> dict:
try:
f = partial(self.sms_client.send_message, signature_id=self.signature_id,
resp = self.sms_client.send_message(
signature_id=self.signature_id,
template_id=template_id,
mobile=phone,
content_var_dict=params)
resp = await awaitify(f)()
content_var_dict=params
)
return await self.__validation(stype, template_id, params, phone, resp)
except ex.BceHttpClientError as e:
if isinstance(e.last_error, ex.BceServerError):
@ -115,9 +113,8 @@ class SMSEngine:
async def send_vcode(self, phone: str, stype: str, vcode) -> dict:
env = ServerEnv()
async with get_sor_context(env, 'smssend') as sor:
async with get_sor_context(env, 'kboss') 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:
@ -132,11 +129,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, 'smssend') as sor:
async with get_sor_context(env, 'kboss') as sor:
send_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
log = {
'id': uuid(),
@ -158,14 +155,12 @@ class SMSEngine:
await sor.C('sms_record', log)
return {'status': False, 'msg': msg}
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
async def generate_sms_code(self, length: int = 6, expire_minutes: int = 5) -> str:
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, 'smssend') as sor:
async with get_sor_context(env, 'kboss') as sor:
await sor.C('validatecode', {
'id': code_id,
'vcode': code,
@ -173,26 +168,17 @@ class SMSEngine:
'del_flg': '0',
'create_at': datetime.datetime.now()
})
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
return code_id, code
async def check_sms_code(self, code_id: str, vcode: str) -> bool:
env = ServerEnv()
async with get_sor_context(env, 'smssend') as sor:
code_info = await sor.R('validatecode', {'id': code_id})
async with get_sor_context(env, 'kboss') as sor:
code_info = await sor.R('validatecode', {'id': code_id, 'del_flg': '0'})
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()
@ -201,15 +187,12 @@ 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, code: str) -> dict:
"""
async def send_sms(self, phone: str, stype: str) -> dict:
code_id, code = await self.generate_sms_code()
if code_id is None:
return {
@ -218,7 +201,7 @@ class SMSEngine:
'message': '生成的手机号出错'
}
}
"""
vcode = {'SMSvCode': code}
result = await self.send_vcode(phone, stype, vcode)