Exemplo n.º 1
0
    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'})
Exemplo n.º 2
0
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)
Exemplo n.º 3
0
    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'})
Exemplo n.º 4
0
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)
Exemplo n.º 5
0
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)
Exemplo n.º 6
0
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)
Exemplo n.º 7
0
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("发送短信失败啦!!!"))
Exemplo n.º 8
0
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)
Exemplo n.º 9
0
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("发送短信失败!"))
Exemplo n.º 10
0
def send_sms_code(mobile, sms_code):
    ccp = CCP()
    ccp.send_template_sms(mobile, [sms_code, '5'], 1)
Exemplo n.º 11
0
def send_sms_code(mobile, code):
    ccp = CCP()
    ccp.send_template_sms(mobile, code, constants.SMS_CODE_TEMPLATE_ID)
Exemplo n.º 12
0
def sms_task(mobile, sms_code, temp_id):
    ccp = CCP()
    ccp.send_template_sms(mobile, sms_code, temp_id)

    return "发送短信--任务执行完毕"