def get(self, request, mobile): #创建redis连接对象 redis_conn = get_redis_connection('verify_codes') # 指定数据库名称连接到数据库 # 从redis中获取标记 send_flag = redis_conn.get('send_flag_%s' % mobile) # 取不到也不会报错 是none if send_flag: # 如果取到了标记说明此手机号频繁发送 return Response({'message': '手机频繁发送短信'}, status=status.HTTP_400_BAD_REQUEST) # 生成验证码 sms_code = '%06d' % randint(0, 999999) logger.info(sms_code) # 创建redis管道(把多次redis操作放入管道中将一次性去执行,减少redis连接操作) pl = redis_conn.pipeline() #.把验证码存储到redis数据库 # redis_conn.setex('sms_%s' % mobile, constants.SMS_CODE_REDIS_EXPIRES, sms_code) # 键,存储时间秒,值 pl.setex('sms_%s' % mobile, constants.SMS_CODE_REDIS_EXPIRES, sms_code) # 键,存储时间秒,值 # 存储一个标记表示此手机号已经发送过短信 标记有效期为60s # redis_conn.setex('send_flag_%s' % mobile, constants.SEND_SMS_CODE_INTERVAL, 1) # 键,存储时间秒,值 pl.setex('send_flag_%s' % mobile, constants.SEND_SMS_CODE_INTERVAL, 1) # 键,存储时间秒,值 # 执行管道 pl.execute() # import time # time.sleep(5) #现在是单线程吗 就已经是多任务了吧 服务器自己解决的runserver或者你部署nginx等 #利用容联云通讯发送短信验证码 # CCP().send_template_sms(to, datas, temp_id) to是手机号 datas是列表[验证码,5] 5是分钟表示过期时间 还有短信内容的模板 # CCP().send_template_sms(mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES // 60], 1) # 触发异步任务,将异步任务添加到celery任务队列 send_sms_code(mobile, sms_code) # 这么做是错的 调用普通函数而已 send_sms_code.delay(mobile, sms_code) # 触发异步任务 #响应 return Response({'message': 'ok'})
def get(self, request, mobile): # 0. 创建redis连接对象 redis_conn = get_redis_connection('verify_codes') # 先尝试获取redis中此手机号60s内是否发送短信标识 send_flag = redis_conn.get('send_flag_%s' % mobile) # 判断是否有标识,如果有提前响应 if send_flag: return http.JsonResponse({ 'code': RETCODE.THROTTLINGERR, 'errmsg': '频繁发送短信' }) # 1. 接收 query_dict = request.GET image_code_client = query_dict.get('image_code') uuid = query_dict.get('uuid') # 2. 校验 if all([image_code_client, uuid]) is False: return http.HttpResponseForbidden('缺少必传参数') # 2.2 获取redis中图形验证码字符 image_code_server_bytes = redis_conn.get(uuid) pl = redis_conn.pipeline() # 2.2.1 将redis中的图形验证码删除,让它是一次性 pl.delete(uuid) # 3.2 判断redis中的图形验证码是否已过期 if image_code_server_bytes is None: return http.JsonResponse({ 'code': RETCODE.IMAGECODEERR, 'errmsg': '图形验证码已过期' }) # 3.3 将bytes类型转换成str (从redis中获取出来的数据都是bytes str, dict, list, set) image_code_server = image_code_server_bytes.decode() # 判断用户填写验证码和redis取的是否一致 if image_code_client.lower() != image_code_server.lower(): return http.JsonResponse({ 'code': RETCODE.IMAGECODEERR, 'errmsg': '图形验证码填写错误' }) # 3. 生成6位随机数字做为短信验证码 sms_code = '%06d' % randint(0, 999999) logger.info(sms_code) # CCP().send_template_sms('接收收短信手机号', ['验证码', '提示用户的过期时间:单秒分钟'], 1) # 3.1 发送短信 # CCP().send_template_sms(mobile, [sms_code, 5], 1) # send_sms_code.delay(mobile, sms_code) # 触发异步任务 send_sms_code(mobile, sms_code) # 3.2 存储短信验证码到redis,以备注册时验证短信验证码 pl.setex('sms_%s' % mobile, constants.SMS_CODE_EXPIRE, sms_code) # 3.3 向redis存储一个此手机号60s内发过短信标识 pl.setex('send_flag_%s' % mobile, 60, 1) pl.execute() # 4. 响应 return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK'})
def get(self,request): data = request.GET token = data.get('access_token') access_token = check_access_token_token(token) mobile = access_token['mobile'] redis_conn = get_redis_connection('code') if redis_conn.get('send_findflag_%s'%mobile): return HttpResponseBadRequest('短信发送太频繁') sms_find_code = '%06d' % randint(0, 999999) redis_conn.setex('find_sms_%s'%mobile,300,sms_find_code) redis_conn.setex('send_findflag_%s'%mobile,60,1) from celery_tasks.sms.tasks import send_sms_code send_sms_code(mobile,sms_find_code) return JsonResponse({'msg': 'ok', 'code': '0'})
def get(self, request, mobile): ser = self.get_serializer(data=request.query_params) ser.is_valid(raise_exception=True) sms_code = random.randint(0, 999999) sms_code = '%06d' % sms_code print(sms_code) redis_conn = get_redis_connection('verify_codes') redis_conn.setex('sms_code_%s' % mobile, constants.SMS_CODE_REDIS_EXPIRES, sms_code) # redis_conn_ = redis_conn.get('sms_code_%s' % mobile) # print(redis_conn_) sms_code_expires = str(constants.SMS_CODE_REDIS_EXPIRES // 60) # ccp = CCP() # ccp.send_template_sms(mobile, [sms_code,sms_code_expires], constants.SMS_CODE_TEMP_ID) sms_tasks.send_sms_code(mobile, sms_code, sms_code_expires) return Response({'message': 'ok'})
def get(self, request): token = request.GET.get('access_token') data = get_account_token(token) mobile = data['mobile'] # 0. 创建redis连接对象 redis_conn = get_redis_connection('verify_codes') # 先尝试获取redis中此手机号60s内是否发送短信标识 send_flag = redis_conn.get('send_flag_%s' % mobile) # 判断是否有标识,如果有提前响应 if send_flag: return http.JsonResponse({'code': RETCODE.THROTTLINGERR, 'errmsg': '频繁发送短信'}) sms_code = '%06d' % randint(0, 999999) logger.info(sms_code) send_sms_code(mobile, sms_code) redis_conn.setex('sms_%s' % mobile, constants.SMS_CODE_EXPIRE, sms_code) redis_conn.setex('send_flag_%s' % mobile, 60, 1) return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK'})
def get(self, request): access_token = request.GET.get('access_token') serializer = Serializer(secret_key=settings.SECRET_KEY, expires_in=3600 * 24) try: data = serializer.loads(access_token) except BadData: return BadData else: mobile = data.get('access_token') mobile = int(mobile) sms_code = '%06d' % random.randint(0, 999999) send_sms_code(mobile, sms_code) redis_conn = get_redis_connection('verify_code') redis_conn.setex('sms_%s' % mobile, 300, sms_code)
def get(self, request, mobile): # 校验参数 由序列化器完成 serializer = self.get_serializer(data=request.query_params) # s.get_serializer serializer.is_valid(raise_exception=True) # 生成短信验证码 sms_code = "%06d" % random.randint(0, 999999) # 保存短信验证码 redis管道 redis_conn = get_redis_connection('verify_codes') pl = redis_conn.pipeline() pl.setex("sms_%s" % mobile, constants.SMS_CODE_REDIS_EXPIRES, sms_code) pl.setex("send_flag_%s" % mobile, constants.SEND_SMS_CODE_INTERVAL, 1) pl.execute() # 发送短信 # try: # ccp = CCP() # expires = constants.SMS_CODE_REDIS_EXPIRES // 60 # result = ccp.send_template_sms(mobile, [sms_code, expires], constants.SMS_CODE_TEMP_ID) # except Exception as e: # logger.error("发送验证码短信[异常][ mobile: %s, message: %s ]" % (mobile, e)) # return Response({'message': 'failed'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) # else: # if result == 0: # logger.info("发送验证码短信[正常][ mobile: %s ]" % mobile) # else: # logger.warning("发送验证码短信[失败][ mobile: %s ]" % mobile, status=status.HTTP_500_INTERNAL_SERVER_ERROR) # 发送短信验证码 用celery sms_code_expires = str(constants.SMS_CODE_REDIS_EXPIRES // 60) sms_tasks.send_sms_code(mobile, sms_code, sms_code_expires, constants.SMS_CODE_TEMP_ID) return Response({"message": "OK"})
def get(self, request, mobile): # 1.从前端获取手机号 # 2.对手机号进行正则校验 # 3.生成短信验证码 sms_code = '%06d' % random.randint(0, 999999) # print(sms_code) # 4.保存短信信息到redis数据中 # 和redis数据库建立连接 con = get_redis_connection('smscodes') flag = con.get('smscode_flag_%s' % mobile) # print('----flag', flag) if flag: return Response({'error': '请求过于频繁'}) # 生成管道对象 p1 = con.pipeline() # 保存短信验证码到redis中 p1.setex('smscode_%s' % mobile, 300, sms_code) # 设置请求时效标志 p1.setex('smscode_flag_%s' % mobile, 60, 1) # 执行管道(连接缓存, 存入数据) p1.execute() # 使用celery异步发送短信 result_dic = send_sms_code(sms_code, mobile) return Response(result_dic)