perf: 支持自定义短信认证

This commit is contained in:
jiangweidong
2023-05-24 17:32:57 +08:00
committed by Jiangjie.Bai
parent 5e7d474bb7
commit 20cc4ea320
9 changed files with 99 additions and 6 deletions

View File

@@ -0,0 +1,48 @@
import requests
from collections import OrderedDict
from django.conf import settings
from common.utils import get_logger
from common.exceptions import JMSException
from .base import BaseSMSClient
logger = get_logger(__file__)
class CustomSMS(BaseSMSClient):
@classmethod
def new_from_settings(cls):
return cls()
@staticmethod
def need_pre_check():
return False
def send_sms(self, phone_numbers: list, template_param: OrderedDict, **kwargs):
phone_numbers_str = ','.join(phone_numbers)
params = {}
for k, v in settings.CUSTOM_SMS_API_PARAMS.items():
params[k] = v.format(
code=template_param.get('code'), phone_numbers=phone_numbers_str
)
logger.info(f'Custom sms send: phone_numbers={phone_numbers}param={params}')
if settings.CUSTOM_SMS_REQUEST_METHOD == 'post':
action = requests.post
kwargs = {'json': params}
else:
action = requests.get
kwargs = {'params': params}
try:
response = action(url=settings.CUSTOM_SMS_URL, **kwargs)
if response.reason != 'OK':
raise JMSException(detail=response.text, code=response.status_code)
except Exception as exc:
logger.error('Custom sms error: {}'.format(exc))
client = CustomSMS

View File

@@ -17,6 +17,7 @@ class BACKENDS(TextChoices):
TENCENT = 'tencent', _('Tencent cloud')
HUAWEI = 'huawei', _('Huawei Cloud')
CMPP2 = 'cmpp2', _('CMPP v2.0')
Custom = 'custom', _('Custom type')
class SMS:
@@ -42,8 +43,9 @@ class SMS:
)
def send_verify_code(self, phone_number, code):
sign_name = getattr(settings, f'{self.client.SIGN_AND_TMPL_SETTING_FIELD_PREFIX}_VERIFY_SIGN_NAME')
template_code = getattr(settings, f'{self.client.SIGN_AND_TMPL_SETTING_FIELD_PREFIX}_VERIFY_TEMPLATE_CODE')
prefix = getattr(self.client, 'SIGN_AND_TMPL_SETTING_FIELD_PREFIX', '')
sign_name = getattr(settings, f'{prefix}_VERIFY_SIGN_NAME', None)
template_code = getattr(settings, f'{prefix}_VERIFY_TEMPLATE_CODE', None)
if self.client.need_pre_check() and not (sign_name and template_code):
raise JMSException(