def get(self, request, mobile): redis_conn = get_redis_connection("verify_code") send_flag = redis_conn.get("send_flag_%s" % mobile) if send_flag: return JsonResponse({"code": 400, "message": "获取验证码频繁"}) image_code = request.GET.get("image_code") uuid = request.GET.get("image_code_id") if not all([image_code, uuid]): return JsonResponse({"code": 400, "message": "缺少必传参数"}) image_code_redis = redis_conn.get("img_%s" % uuid) if image_code_redis is None: return JsonResponse({"code": 400, "message": "图形验证码失效"}) try: redis_conn.delete("img_%s" % uuid) except Exception as e: logger.error(e) if image_code.lower() != image_code_redis.lower(): return JsonResponse({"code": 400, "message": "验证码输入错误"}) sms_code = "%06d" % random.randint(0, 999999) logger.info("短信验证码为: %s" % sms_code) # redis_conn.set("sms_%s" % mobile, sms_code, 300) # redis_conn.set("send_flag_%s" % mobile, 1, 60) pl = redis_conn.pipeline() pl.set("sms_%s" % mobile, sms_code, 300) pl.set("send_flag_%s" % mobile, 1, 60) pl.execute() # from celery_tasks.sms.tasks import send_sms_code # send_sms_code.delay(mobile, sms_code) CCP().send_template_sms(mobile, [sms_code, 5], 1) return JsonResponse({"code": 0, "message": "发送短信成功"})
def ccp_send_sms_code(self, mobile, sms_code): """ 发送短信异步任务 :param mobile: 手机号 :param sms_code: 短信验证码 :return: 成功0 或 失败-1 """ try: # 调用 CCP() 发送短信, 并传递相关参数: result = CCP().send_template_sms(mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES // 60], constants.SEND_SMS_TEMPLATE_ID) except Exception as e: # 如果发送过程出错, 打印错误日志 logger.error(e) # 有异常自动重试三次 raise self.retry(exc=e, max_retries=3) # 如果发送成功, rend_ret 为 0: if result != 0: # 有异常自动重试三次 raise self.retry(exc=Exception('发送短信失败'), max_retries=3) return result
def ccp_send_sms_code(self, mobile, sms_code): try: result = CCP().send_template_sms( mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES // 60], constants.SEND_SMS_TEMPLATE_ID) pass except: pass
def get(self, reqeust, mobile): # 1. 接收参数 image_code_client = reqeust.GET.get('image_code') uuid = reqeust.GET.get('image_code_id') # 2. 校验参数 if not all([image_code_client, uuid]): return JsonResponse({'code': 400, 'errmsg': '缺少必传参数'}) # 3. 创建连接到redis的对象 redis_conn = get_redis_connection('verify_code') # 4. 提取图形验证码 image_code_server = redis_conn.get('img_%s' % uuid) if image_code_server is None: # 图形验证码过期或者不存在 return JsonResponse({'code': 400, 'errmsg': '图形验证码失效'}) # 5. 删除图形验证码,避免恶意测试图形验证码 try: redis_conn.delete('img_%s' % uuid) except Exception as e: logger.error(e) # 6. 对比图形验证码 # bytes 转字符串 image_code_server = image_code_server.decode() # 转小写后比较 if image_code_client.lower() != image_code_server.lower(): return JsonResponse({'code': 400, 'errmsg': '输入图形验证码有误'}) # 7. 生成短信验证码:生成6位数验证码 sms_code = '%06d' % random.randint(0, 999999) logger.info(sms_code) # 8. 保存短信验证码 # 短信验证码有效期,单位:300秒 redis_conn.setex('sms_%s' % mobile, 300, sms_code) # 9. 发送短信验证码 # 短信模板 CCP().send_template_sms(mobile,[sms_code, 5], 1) # 10. 响应结果 return JsonResponse({'code': 0, 'errmsg': '发送短信成功'})
def get(self, request, mobile): redis_conn = get_redis_connection('verify_code') send_flag = redis_conn.get('send_flag_%s' % mobile) if send_flag: return JsonResponse({"code": 400, "errmsg": "该手机号发送短信过于频繁"}) uuid = request.GET.get('image_code_id') image_code_client = request.GET.get('image_code') image_code_server = redis_conn.get('img_%s' % uuid) if image_code_server is None: return JsonResponse({"code": 400, "errmsg": "验证码过期"}) try: redis_conn.delete('img_%s' % uuid) except Exception as e: logger.error(e) image_code_server = image_code_server.decode() if image_code_client.lower() != image_code_server.lower( ): # redis里面取出来的是bytes类型,要解码,因为redis是把数据存放在内存中的 return JsonResponse({"code": 400, "errmsg": "验证码有误"}) sms_code = '%06d' % random.randint(0, 999999) logger.info(sms_code) CCP().send_template_sms(mobile, [sms_code, 5], 1) #, 这里要用异步方案celery替换 # 注意,这里的函数调用需要加上delay # ccp_send_sms_code.delay(mobile,sms_code) # 创建Redis管道 pl = redis_conn.pipeline() pl.setex('sms_%s' % mobile, 300, sms_code) pl.setex('send_flag_%s' % mobile, 60, 1) pl.execute() return JsonResponse({ "code": 0, "errmsg": 'ok', })
from meiduo_mall.libs.yuntongxun.ccp_sms import CCP if __name__ == '__main__': CCP().send_template_sms('13896584883', ['测试验证码', 5], 1)
def get(self, request, mobile): '''接受参数,检验,发送短信验证码''' #0. 从redis中获取60s保存的信息 redis_conn = get_redis_connection('verify_code') send_flag = redis_conn.get('send_flag_%s' % mobile) #0.1 判断该信息是否存在,如果不存在, 进行下面的步骤,如果存在,返回 if send_flag: return JsonResponse({'code': 400, 'errmsg': '发送短信过于频繁'}) #1.接收查询字符串 image_code_client = request.GET.get('image_code') uuid = request.GET.get('image_code_id') #2.总体检验(查看参数是否为空) if not all([image_code_client, uuid]): return JsonResponse({'code': 400, 'errmsg': '必传参数不能为空'}) #3. 链接redis,获取redis的连接对象 # 挪到上面去了 #4.从redis中取出图形验证码 image_code_server = redis_conn.get('img_%s' % uuid) #5. 判断服务器的图形验证码是否过期,如果过期,return if image_code_server is None: return JsonResponse({'code': 400, 'errmsg': '图形验证码过期'}) #6. 删除redis中图形验证码 try: redis_conn.delete('img_%s' % uuid) print('=================') except Exception as e: logger.info(e) #7. 对比前后端的图形验证码 if image_code_client.lower() != image_code_server.decode().lower(): return JsonResponse({'code': 400, 'errmsg': '输入的图形验证码出错'}) #8. 随机生成6位的短信验证码 sms_code = '%06d' % random.randint(0, 999999) #9. 打印短信验证码 print('==============') logger.info(sms_code) print('----------------') #创建redis管道: p1 = redis_conn.pipeline() #10. 把短信验证码保存到redis p1.setex('sms_%s' % mobile, 300, sms_code) p1.setex('send_flag_%s' % mobile, 60, 1) #执行管道 p1.excute() # 11. 调用容联云,发送短信验证码 CCP().send_template_sms('13682540633', [sms_code, 5], 1) #添加一个提示celery抛出任务的提醒 send_sms_verify_code.delay(mobile, sms_code) #12. 返回json return JsonResponse({'code': 0, 'errmsg': 'ok'})
def get(self,request,mobile): # 3.创建连接到redis的对象 redis_conn = get_redis_connection('verify_code') get = redis_conn.get('sms_flag_%s' % mobile) if get: return http.JsonResponse({'code':RETCODE.IMAGECODEERR,'errmsg':'发送短信过于频繁'}) """ :param request: 请求对象 :param mobile: 手机号 :return: JSON """ # 1.接收参数 image_code_client = request.GET.get('image_code') uuid = request.GET.get('image_code_id') # 2.校验参数 if not all([image_code_client,uuid]): return http.JsonResponse({'code':RETCODE.NECESSARYPARAMERR,'errmsg':'缺少必要参数'}) # 4.提取图形验证码 image_code_server = redis_conn.get('img_%s' % uuid) # 判断图形验证码是否存在 if image_code_server is None: return http.JsonResponse({'code':RETCODE.IMAGECODEERR,'errmsg':'图形验证码失效'}) # 删除图形验证码 try: redis_conn.delete('img_%s' % uuid) except Exception as e: logging.error(e) # 对比图形验证码 image_code_server=image_code_server.decode() if image_code_server.lower() != image_code_client.lower(): return http.JsonResponse({'code':RETCODE.IMAGECODEERR,'errmsg':'输入验证码错误'}) # 生成短信验证码,生成6位数验证码 sms_code = '%06d' % random.randint(0,999999) logger.info(sms_code) # 创建管道 pl = redis_conn.pipeline() # 保存短信验证码 pl.setex('sms_%s' % mobile, constants.IMAGE_CODE_REDIS_EXPIRES,sms_code) pl.setex('sms_flag_%s' % mobile, constants.IMAGE_CODE_REDIS_EXPIRES,sms_code) pl.execute() #发送短信验证码 CCP().send_template_sms(mobile,[sms_code,constants.SMS_CODE_REDIS_EXPIRES],1) from celery_tasks.sms.tasks import send_sms_code send_sms_code.delay(mobile,sms_code) # 响应结果 return http.JsonResponse({'code':RETCODE.OK,'errmsg':'短信发送成功'})