1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
from libs import tx_sms from django.core.cache import cache from django.conf import settings from .throttles import SMSRateThrottle
class SMSViewSet(ViewSet): throttle_classes = [SMSRateThrottle] def send(self, request, *args, **kwargs): # 1)接收前台手机号验证手机格式 mobile = request.query_params.get('mobile', None) if not mobile: return APIResponse(status=0, msg='没有输入手机号') if not re.match(r'^1[3-9][0-9]{9}$', mobile): return APIResponse(status=0, msg='手机号错误') # 2)后台产生短信验证码 code = tx_sms.get_code()
# 3)把验证码交给第三方,发送短信 # result = tx_sms.send_code(mobile, code) # 4)如果短信发送成功,服务器缓存验证码(内存数据库),方便下一次校验 if code: cache.set(mobile,code,180) # 5)响应前台短信是否发生成功 return APIResponse(status=1,msg=code)
|