def get(self, request, mobile): # 1、获取手机号 路由中进行正则匹配 # 判断请求间隔,是否在60s内 conn = get_redis_connection('sms_code') flag = conn.get('sms_code_flag_%s' % mobile) if flag: return Response({'error': '请求过于频发'}, status=400) # 2、生成短信验证 sms_code = '%06d' % randint(0, 999999) print(sms_code) # 3、保存短信到redis缓存 pl = conn.pipeline() # 生成管道对象 pl.setex('sms_code_%s' % mobile, 300, sms_code) pl.setex('sms_code_flag_%s' % mobile, 60, 'a') pl.execute() # 4、发送短信 ccp = CCP() ccp.send_template_sms(mobile, [sms_code, '5'], 1) # 异步发送短信 # t=Thread(target='send_sms_code',kwargs={'mobile':mobile,'sms_code':sms_code}) # t.start() # send_sms_code.delay(mobile, sms_code) # 5、结果返回 return Response({'message': 'ok'})
def send_sms_code(mobile, sms_code): """ 发送短信验证码异步任务 :return: """ ccp = CCP() ccp.send_template_sms( mobile, [sms_code, str(constants.SMS_CODE_REDIS_EXPIRES / 60)], constants.SMS_CODE_TEMP_ID)
def send_msg_code(self, mobile, sms_code): #1,发送短信 try: ccp = CCP() result = ccp.send_template_sms(mobile, [sms_code, 5], 1) except Exception as e: result = -1 #2,判断短信是否发送成功 if result == -1: #exc: 发送失败之后的异常, countdown:间隔的时间 max_retries:重试的次数 self.retry(exc=Exception("短信发送失败啦!"), countdown=5, max_retries=3)
def send_sms_code(mobile, code, expires): try: ccp = CCP() result = ccp.send_template_sms(mobile, [code, expires], SMS_CODE_TEMP_ID) except Exception as e: logger.error("发送验证码短信[异常][ mobile: %s, message: %s ]" % (mobile, e)) else: if result == 0: logger.info("发送验证码短信[正常][ mobile: %s ]" % mobile) else: logger.warning("发送验证码短信[失败][ mobile: %s ]" % mobile)
def send_sms_code(self, mobile, sms_code, time): #1,发送短信 try: ccp = CCP() result = ccp.send_template_sms(mobile, [sms_code, time], 1) except Exception as e: result = -1 #2,判断短信是否发送成功 if result == -1: print("发送失败!") #参数1: 重试的次数到了之后报错信息, 参数2: 隔几秒发一次, 参数3: 重新发送几次 self.retry(exc=Exception("最终没有发送成功"), countdown=5, max_retries=3)
def send_sms_code(self, mobile, sms_code, time): # import time # time.sleep(10) # 1,发送短信 try: ccp = CCP() result = ccp.send_template_sms(mobile, [sms_code, time], 1) except Exception as e: result = -1 # 2,判断结果 if result == -1: print("重试中....") self.retry(countdown=5, max_retries=3, exc=Exception("发送短信失败啦!!!"))
def send_sms_code(mobile, code, expires): ''' 发送短信验证码 :param mobile:手机号 :param code: 验证码 :param expires: 有效期 :return: None ''' logging.info('-----------------------------------') logging.info(code) logging.info('-----------------------------------') try: ccp = CCP() # result = ccp.send_template_sms(mobile,[code,expires],SMS_CODE_TEMP_ID) result = 0 except Exception as e: logger.error("发送短信验证码【异常】:%s,message:%s ]" % (mobile, e)) else: if result == 0: logger.info("发送短信【正常】【mobile:%s]" % mobile) else: logging.info('-----------------------------------') logging.info(code) logging.info('-----------------------------------') logger.warning('result%s' % result) logger.warning("发送短信验证码【失败】【mobile:%s ]" % mobile)
def send_sms_code(mobile, sms_code): """ 发短信的异步任务 :param mobile: 要收短信的手机号 :param sms_code: 验证码 """ CCP().send_template_sms(mobile, [sms_code, 5], 1)
def get(self,request,mobile): # 创建链接到redis的对象 redis_conn = get_redis_connection('verify_codes') # 再次点击时读取redis中存储的标记 send_flag = redis_conn.get('send_flag%s' % mobile) if send_flag: return Response({'message':'频繁发送短信验证码'},status=status.HTTP_400_BAD_REQUEST) # 生成短信验证码 sms_code = '%06d' % random.randint(0,999999) logger.info(sms_code) # 保存短信验证码导Redis # redis_conn.setex('key','expire_time','value') # redis_conn.setex('sms_%s' % mobile,300, sms_code) redis_conn.setex('sms_%s' % mobile, constants.SMS_CODE_REDIS_EXPIRES, sms_code) # 向redis中添加一个标记,数据。该标记的有效期是60秒 redis_conn.setex('send_flag_%s' % mobile,constants.SEND_SMS_CODE_INTERVAL,1) # 集成容联云通讯发送短信验证码 CCP().send_template_sms(mobile,[sms_code,constants.SMS_CODE_REDIS_EXPIRES // 60],1) # 响应结果 return Response({'message':'OK'})
def send_sms_code(mobile, sms_code): """ 发短信的任务 :param mobile: 要收短信的手机号 :param sms_code: 验证码 """ # CCP().send_template_sms(接收短信的手机号, [短信验证码, 提示用户的过期时间单位:分钟], 模板id) CCP().send_template_sms(mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES // 60], 1)
def send_sms_code(mobile, sms_code, expires): """ 发送短信异步任务 :param mobile: ⼿手机号 :param sms_code: 短信验证码 :return: None """ try: ccp = CCP() result = ccp.send_template_sms(mobile,[sms_code],SMS_CODE_TEMP_ID) except Exception as e: logger.error("发送验证短信[异常][ mobile: %s, message: %s ]" % (mobile, e)) else: if result ==0: logger.info("发送验证码短信[正常][ mobile: %s ]" % mobile) else: logger.warning("发送验证码短信[失败][ mobile: %s ]" % mobile)
def get(self, request, mobile): # 接收 uuid = request.GET.get('image_code_id') image_code = request.GET.get('image_code') # 验证 redis_cli_sms = get_redis_connection('sms_code') # 验证是否在60秒内 if redis_cli_sms.get(mobile + '_flag') is not None: return JsonResponse({'code': RETCODE.SMSCODERR, 'errmsg': '发送短信太频繁,请稍后再试!'}) # 非空 if not all([uuid, image_code]): return JsonResponse({'code': RETCODE.PARAMERR, 'errmsg': '参数不完整'}) # 验证图形验证码是否正确 # 从redis中读取之前保存的图像验证码文本 redis_cli = get_redis_connection('image_code') image_code_redis = redis_cli.get(uuid) # bytes类型 # 如果redis中的数据过期 if image_code_redis is None: return JsonResponse({'code': RETCODE.IMAGECODEERR, 'errmsg': '图像验证码已过期,点击图片换一个'}) # 立即删除redis中图形验证码,表示这个值不能使用第二次 redis_cli.delete(uuid) # 对比图形验证码 if image_code_redis.decode().lower() != image_code.lower(): # 全部转换为小写,不区分大小分,根据业务需求来定 return JsonResponse({'code': RETCODE.IMAGECODEERR, 'errmsg': '图像验证码错误'}) # 处理 # 1、生成随机6位数 sms_code = random.randint(100000, 999999) # 2、存入redis # redis_cli_sms.setex(mobile, constans.SMS_CODE_EXPIRES, sms_code) # 键是mobile,值是sms_code # 存入发送标记,时间 # redis_cli_sms.setex(mobile + '_flag', constans.SMS_CODE_FLAG, 1) # 优化:使用管道,减少和redis服务器的交互次数,提升服务器性能 redis_pl = redis_cli_sms.pipeline() redis_pl.setex(mobile, constans.SMS_CODE_EXPIRES, sms_code) redis_pl.setex(mobile + '_flag', constans.SMS_CODE_FLAG, 1) redis_pl.execute() # 发送短信 ccp = CCP() ccp.send_template_sms(mobile, [sms_code, constans.SMS_CODE_EXPIRES / 60], 1) # print(sms_code) # 响应 return JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK'})
def send_sms_code(mobile, sms_code): try: result = CCP().send_template_sms(mobile, [sms_code, '5'], 1) except Exception as e: logger.error("发送验证码短信[异常][ mobile: %s, message: %s ]" % (mobile, e)) else: if result == 0: logger.info("发送验证码短信[正常][ mobile: %s ]" % mobile) else: logger.warning("发送验证码短信[失败][ mobile: %s ]" % mobile)
def get(self,request,mobile): # 1.生成短信验证码 sms_code = '%06d'%randint(0,999999) #2.创建redis连接对象 redis_conn = get_redis_connection("verify_codes") #3.将短信验证码储存 #redis_conn.setex(key,过期时间,value) redis_conn.setex('sms_%s'%mobile,300,sms_code) #4.利用容联运发短信 CCP().send_template_sms(mobile,[sms_code,5],1) #响应对象 return Response({"massage":"OK"})
def send_sms_code(self, mobile, sms_code, time): #1,发送短信 try: ccp = CCP() result = ccp.send_template_sms(mobile, [sms_code, time], 1) print(result) except Exception as e: # raise self.retry(exc=e,countdown=5,max_retries=3) result = -1 # #2,判断是否发送成功 # if result != 0: # print("重试..%s"%result) # #exec: 异常信息, countdown:发送短信间隔时间, max_retries: 重试次数 # raise self.retry(exc=Exception("发送短信失败啦!!"), countdown=5, max_retries=3) #2 判断结果 if result == -1: print("正在重新发送") self.retry(countdown=5, max_retries=3, exc=Exception("发送短信失败!"))
def get(self, request): # 手机号,access_token access_token = request.GET.get('access_token') serializer = Serializer(settings.SECRET_KEY, expires_in=60 * 5) access_token = serializer.loads(access_token) sms_codes = "%06d" % randint(0, 999999) # 生成验证码(容联云通信) mobile = access_token['mobile'] CCP().send_template_sms(mobile, [sms_codes, 5], 1) print(sms_codes) # 存进redis redis_conn = get_redis_connection('verify_codes') redis_conn.setex('sms_code%s' % mobile, 300, sms_codes) return http.JsonResponse({'message': 'ok'})
def send_message(self, mobile, sms_code): # 测试耗时 # import time # time.sleep(10) # 1, 发送短信 try: result = CCP().send_template_sms(mobile, [sms_code, 5], 1) # 5是5分钟 except Exception as e: result = -1 # 2,判断是否发送成功 if result == -1: # 重试, exc: 发送短信失败报的异常, countdown: 间隔几秒重试, max_retries:重试的次数 self.retry(exc=Exception("%s发送短信失败" % mobile), countdown=5, max_retries=3)
def send_sms_code(mobile, sms_code): ccp = CCP() ccp.send_template_sms(mobile, [sms_code, '5'], 1)
def ccp_send_sms_code(): print(111) # 发送短信验证码 # CCP().send_template_sms(mobile, [sms_code, 5*60], 1) CCP()
def ccp_send_sms_code(mobile, sms_code): CCP().send_template_sms(mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES // 60], 1)
def send_sms_code(mobile, sms_code): CCP().send_template_sms(to=mobile, datas=[sms_code, 10], temp_id=1)
def send_sms_code(mobile, sms_code): CCP().send_template_sms(mobile, [sms_code, 5], 1)
def sms_task(mobile, sms_code, temp_id): ccp = CCP() ccp.send_template_sms(mobile, sms_code, temp_id) return "发送短信--任务执行完毕"
def send_sms_code(mobile, code): ccp = CCP() ccp.send_template_sms(mobile, code, constants.SMS_CODE_TEMPLATE_ID)