def send_sms(request): """发送短信""" tpl = request.GET.get('tpl') template_id = settings.TENCENT_SMS_TEMPLATE.get(tpl) if not template_id: return HttpResponse('模板不存在') code = random.randrange(100000, 999999) send_sms_single('1527182', 645945, [ code, ]) return HttpResponse('成功')
def clean_mobile_phone(self): """验证钩子""" mobile_phone = self.cleaned_data.get('mobile_phone') # 为了判断使用的是哪个模板,需要前端将tpl传过来 tpl = self.request.GET.get('tpl') template_id = settings.TENCENT_SMS_APP_TEMPLATE.get(tpl) if not template_id: raise ValidationError('短信模板错误') exists = models.UserInfo.objects.filter( mobile_phone=mobile_phone).exists() # 判断是注册还是登陆操作 if tpl == 'login': if not exists: raise ValidationError('手机号不存在,请先注册') else: if exists: raise ValidationError('手机号已存在') # 发短信 code = random.randrange(1000, 9999) print(code) sms = send_sms_single(mobile_phone, template_id, [ code, ]) if sms['result'] != 0: raise ValidationError('短信发送失败,{}'.format(sms['errmsg'])) # 验证码写入redis,设置超时时间为60s conn = get_redis_connection() conn.set(mobile_phone, code, ex=60) return mobile_phone
def clean_mobile_phone(self): """手机号校验钩子""" mobile_phone = self.cleaned_data['mobile_phone'] # 判断短信模板是否有问题 tpl = self.request.GET.get('tpl') template_id = settings.TENCENT_SMS_TEMPLATE.get(tpl) # print("获取不到吗"+self.data.get('tpl')) if not template_id: return ValidationError('短信模板错误') # 校验数据库中是否有手机号 exists = models.UserInfo.objects.filter(mobile_phone=mobile_phone).exists() if tpl == 'login': if not exists: raise ValidationError('手机号不存在') else: if exists: raise ValidationError('手机号已存在') # 发短信 写Redis code = random.randrange(1000, 99999) sms = send_sms_single(mobile_phone, template_id, [code, ]) if sms['result'] != 0: raise ValidationError("短信发送失败,{}".format(sms['errmsg'])) # 验证码写入Redis conn = get_redis_connection() conn.set(mobile_phone, code, ex=60) return mobile_phone
def clean_phone_num(self): ''' 手机号校验的钩子 通过反射执行 判断是否已clean开头 if hasattr(self, 'clean_%s' % name) ''' phone_num = self.cleaned_data['phone_num'] # 判断短信模板是否正确 tpl = self.request.GET.get('tpl', None) tpl_id = settings.TENCENT_SMS_TEMPLATE.get(tpl) if not tpl_id: raise ValidationError('短信模板错误') # 校验数据库中是否已有手机号 if tpl == 'register': exists = models.User.objects.filter(phone_num=phone_num).exists() if exists: raise ValidationError('手机号已存在') # 校验都通过 发短信 code = random.randrange(1000, 9999) sms = send_sms_single(phone_num, tpl_id, [ code, ]) if sms['result'] != 0: raise ValidationError('短信发送失败,{}'.format(sms['errmsg'])) # 将验证码写入redis conn = get_redis_connection('default') # 获取连接 conn.set(phone_num, code, ex=1000) return phone_num
def clean_phone_num(self): """手机号校验钩子 """ phone_num = self.cleaned_data['phone_num'] # 判断短信模板有没有问题 tpl = self.request.GET.get('tpl') template_id = settings.TENCENT_SMS_TEMPLATE.get(tpl) if not template_id: raise ValidationError('模板错误') # 校验数据库已有手机号 exists = models.UserModel.objects.filter(phone_num=phone_num).exists() if tpl == 'login': if not exists: raise ValidationError('手机号不存在') else: if exists: raise ValidationError('手机号存在') # 发短信 code = random.randrange(1000, 9999) sms = send_sms_single(phone_num, template_id, [code, ]) if sms['result'] != 0: raise ValidationError('短信发送失败, {}'.format(sms['errmsg'])) # 写入redis return phone_num
def clean_mobile_phone(self): mobile_phone = self.cleaned_data['mobile_phone'] # 校验模板格式 tpl = self.request.GET.get('tpl') tpl_id = settings.TENCENT_SMS_TEMPLATE[tpl] if not tpl_id: return ValidationError("短信模板不存在") exists = models.UserInfo.objects.filter(mobile_phone=mobile_phone).exists() if exists: if tpl == 'login': if not exists: raise ValidationError('手机号不存在') else: # 校验数据库中是否已有手机号 if exists: raise ValidationError('手机号已存在') code = random.randrange(1000, 9999) sms = send_sms_single(mobile_phone, tpl_id, [code, ]) if sms['result'] != 0: raise ValidationError('短信发送失败, {}'.format(sms['errmsg'])) conn = get_redis_connection() conn.set(mobile_phone, code, ex=60) return mobile_phone
def send_sms(request): """发送验证码""" tpl = request.GET.get('tpl') mobile = request.GET.get('mobile_phpne') qy = UserInfo.objects.filter(mobile_phpne=mobile) if tpl == 'register': if qy: return JsonResponse({ 'filed': 'mobile_phpne', 'result': '2', 'errmsg': '该手机号已注册' }) elif tpl == 'login': if not qy: return JsonResponse({ 'filed': 'mobile_phpne', 'result': '2', 'errmsg': '该手机号未注册' }) try: mobile = int(mobile) res = send_sms_single(tpl, mobile) res['errmsg'] = '发送成功' res['filed'] = 'code' return JsonResponse(res) except TypeError: return JsonResponse({'filed': 'code', 'result': '2', 'errmsg': '发送失败'})
def clean_mobile_phone(self): mobile_phone = self.cleaned_data['mobile_phone'] tpl = self.request.GET.get('tpl') template_id = settings.TENCENT_SMS_TEMPLATE.get(tpl) if not template_id: raise ValidationError("短信模板错误") # 校验数据库中是否存在该手机号 exists = UserInfo.objects.filter(mobile_phone=mobile_phone).exists() if tpl == 'login': if not exists: raise ValidationError("手机号未注册") else: if exists: raise ValidationError("该号码已注册") # 发短信 code = random.randrange(1000, 9999) ret = send_sms_single(mobile_phone, template_id, [ code, ]) if ret['result'] != 0: raise ValidationError("短信发送失败,{}".format(ret['result'])) # 写入redis(django-redis) conn = get_redis_connection() conn.set(mobile_phone, code, ex=60) return mobile_phone
def clean_mobile_phone(self): """ Check Phone number """ mobile_phone = self.cleaned_data['mobile_phone'] # Check if the template of phone number is correct tpl = self.request.GET.get('tpl') template_id = settings.TENCENT_SMS_TEMPLATE.get(tpl) if not template_id: raise ValidationError('Wrong Template.') exists = models.UserInfo.objects.filter( mobile_phone=mobile_phone).exists() if tpl == 'login': if not exists: raise ValidationError('The number is not existed.') else: # Check if the phone is already existed if exists: raise ValidationError('The number is existed.') code = random.randrange(1000, 9999) # Send sms sms = send_sms_single(mobile_phone, template_id, [ code, ]) if sms['result'] != 0: raise ValidationError("Send failed,{}".format(sms['errmsg'])) # Write Verify Code into redis(django-redis) conn = get_redis_connection() conn.set(mobile_phone, code, ex=60) return mobile_phone
def clean_mobile_phone(self): """手机号校验的钩子""" mobile_phone = self.cleaned_data['mobile_phone'] print(">>", mobile_phone) # 判断短信模板是否有问题 tpl = self.request.GET.get('tpl') template_id = settings.TENCENT_SMS_TEMPLATE.get(tpl) if not template_id: raise ValidationError('短信模板错误') # 手机号格式校验 ret = re.match(r"^1[35678]\d{9}$", mobile_phone) if not ret: raise ValidationError('手机号格式不正确') exists = models.UserInfo.objects.filter( mobile_phone=mobile_phone).exists() if tpl == 'login': # 若tpl == 'login'则为短信验证登录 if not exists: raise ValidationError('手机号不存在,请注册') else: # 当tpl==register,校验数据库中是否存在此电话号码 if exists: raise ValidationError('手机号已存在') code = random.randrange(1000, 9999) # 发送短信 sms = send_sms_single(mobile_phone, template_id, [ code, ]) if sms['result'] != 0: raise ValidationError("短信发送失败,{}".format(sms['errmsg'])) # 验证码写入redis(Django Redis) conn = get_redis_connection() conn.set(mobile_phone, code, ex=60) return mobile_phone
def clean_mobile_phone(self): """ 手机号校验的钩子 """ mobile_phone = self.cleaned_data['mobile_phone'] # 判断短信模板是否有问题 tpl = self.request.GET.get('tpl') template_id = settings.TENCENT_SMS_TEMPLATE.get(tpl) if not template_id: # self.add_error('mobile_phone','短信模板错误') raise ValidationError('短信模板错误') exists = models.UserInfo.objects.filter( mobile_phone=mobile_phone).exists() if tpl == 'login': if not exists: raise ValidationError('手机号不存在') else: # 校验数据库中是否已有手机号 if exists: raise ValidationError('手机号已存在') code = random.randrange(1000, 9999) # 发送短信 sms = send_sms_single(mobile_phone, template_id, [ code, ]) if sms['result'] != 0: raise ValidationError("短信发送失败,{}".format(sms['errmsg'])) # 验证码写入redis(django-redis) conn = get_redis_connection() conn.set(mobile_phone, code, ex=60) # ex表示验证码过期时间 return mobile_phone
def clean_mobile_phpne(self): mobile_phpne = self.cleaned_data['mobile_phpne'] tpl = self.request.GET.get('tpl') if tpl == 'register': # 查看手机号是否被注册 qy = UserInfo.objects.filter(mobile_phpne=mobile_phpne) if qy: raise ValidationError('该手机号已注册') elif tpl == 'login': # 查看手机号是否被注册 qy = UserInfo.objects.filter(mobile_phpne=mobile_phpne) if not qy: raise ValidationError('该手机号未注册--') # 查看短信模板是否正确 if not SMS_TEMPLATE.get(tpl): raise ValidationError('请求格式错误') # 发送短信验证码 res = send_sms_single(tpl, mobile_phpne) if res['result'] != 0: raise ValidationError('短信发送失败:{}'.format(res['errmsg'])) return mobile_phpne
def clean_mobile_phone(self): # 手机号校验的钩子 mobile_phone = self.cleaned_data['mobile_phone'] #判断短信模板是否有问题 tpl = self.request.GET.get('tpl') template_id = settings.TENCENT_SMS_TEMPLATE.get(tpl) if not template_id: raise ValidationError('短信模板错误') #校验数据库中是否已有手机号 exists = models.UserInfo.objects.filter( mobile_phone=mobile_phone).exists() if exists: raise ValidationError('手机号已存在') #发短信 code = random.randrange(1000, 9999) sms = send_sms_single(mobile_phone, template_id, [ code, ]) if sms['result'] != 0: raise ValidationError('短信发送失败, {}'.format(sms['errmsg'])) #验证码写入redis(django_redis) conn = get_redis_connection() conn.set(mobile_phone, code, ex=60) return mobile_phone
def send_sms(request): #发送短信 code = random.randrange(1000, 9999) res = send_sms_single('18846826282', 666366, [ code, ]) print(res) return HttpResponse('成功')
def send_sms(request): """ 发送短信 ?tpl=login -> 614333 ?tpl=register -> 614334 ?tp;=reset_password'->614332 """ # 用户请求过来判断是哪种短信服务 tpl = request.GET.get('tpl') # 根据短信服务类型从设置中获得服务ID template_id = settings.TENCENT_SMS_TEMPLATE.get(tpl) if not template_id: return HttpResponse('模板不存在') # 产生随机验证码 code = random.randrange(1000, 9999) # 根据服务类型发送短信 res = send_sms_single('13875769471', template_id, [ code, ]) # 如果res的返回值中的result等于0,表示短信发送成功,否则失败 if res['result'] == 0: return HttpResponse('成功') else: return HttpResponse(res['errmsg'])
def clean_mobile_phone(self): """钩子 校验手机号""" mobile_phone = self.cleaned_data['mobile_phone'] # 校验tpl短信模板是否正确 tpl = self.request.GET.get('tpl') template_id = settings.TENCENT_SMS_TEMPLATE.get(tpl) if not template_id: raise ValidationError('短信模板错误') # 校验数据库是否已经存在该手机号 is_exists = models.UserInfo.objects.filter( mobile_phone=mobile_phone).exists() if tpl == 'register': if is_exists: raise ValidationError('该手机号已经注册!') elif tpl == 'login': if not is_exists: raise ValidationError('该手机号尚未注册!') # 发送短信,验证码写入redis,做完之后最后才返回 code = str(random.randint(0, 9)) + str(random.randint(0, 9)) + str( random.randint(0, 9)) + str(random.randint(0, 9)) # 发送短信 sms = send_sms_single(mobile_phone, template_id, [ code, ]) if sms['result'] != 0: raise ValidationError('短信发送失败,{}'.format(sms['errmsg'])) # 验证码写入redis conn = get_redis_connection() conn.set(mobile_phone, code, ex=60) # 手机号作为键,验证码作为值 return mobile_phone
def clean_mobile_phone(self): """重写局部钩子手机校验""" mobile_phone=self.cleaned_data['mobile_phone'] #判断短信模板是否有问题 tpl=self.request.GET['tpl'] template_id = settings.TENCENT_SMS_TEMPLATE.get(tpl) if not template_id: # self.add_error('mobile_phone','短信模板错误') raise ValidationError('短信模板错误') exists = models.UserInfo.objects.filter(mobile_phone=mobile_phone).exists() if tpl == 'login': # exists = models.UserInfo.objects.filter(mobile_phone=mobile_phone).exists() if not exists: raise ValidationError('该手机未注册') else: #检测手机号是否已被注册 # exists = models.UserInfo.objects.filter(mobile_phone=mobile_phone).exists() if exists: raise ValidationError('手机号已存在') #生成随机四位数 code=random.randrange(1000,9999) #发送短信, sms=send_sms_single(mobile_phone,template_id,[code,]) if sms['result'] != 0: raise ValidationError('短信发送失败,{}'.format(sms['errmsg'])) #验证码写入redis(django-redis) conn = get_redis_connection() conn.set(mobile_phone,code,ex=60) return mobile_phone
def send_sms(request): '''发送短信''' code = random.randrange(1000, 9999) res = send_sms_single('17691394303', 845994, [ code, ]) print(res) return HttpResponse('发送成功')
def send(request): """发送短信""" tpl = request.GET.get('tpl') template_id = settings.TENCENT_SMS_APP_TEMPLATE.get(tpl) code = random.randrange(1000, 9999) res = send_sms_single('1309893511', template_id, [code, ]) if res['result'] == 0: return HttpResponse('成功') else: return HttpResponse(res['errmsg'])
def send_sms(request): tpl = request.GET.get('tpl') mobile = request.GET.get('mobile') print(tpl, mobile) res = send_sms_single(tpl, mobile) # res = HttpResponse('hello word') # return JsonResponse(res) return HttpResponse(res)
def send_sms(request): """发送短信""" tpl = request.GET.get('tpl') template_id = settings.TENCENT_SMS_TEMPLATE.get(tpl) if not template_id: return HttpResponse('模板不存在') code = random.randrange(1000, 9999) res = send_sms_single('18830292815', 559464, [ code, ]) if res['result'] == 0: return HttpResponse('发送成功') else: return HttpResponse('errmsg')
def clean_mobile_phone(self): """ 点击发送短信,手机号校验的钩子 """ mobile_phone = self.cleaned_data['mobile_phone'] # 判断短信模板是否有问题 tpl = self.request.GET.get('tpl') template_id = settings.TENCENT_SMS_TEMPLATE.get(tpl) if not template_id: # self.add_error('mobile_phone','短信模板错误') raise ValidationError('短信模板错误') exists = models.UserInfo.objects.filter( mobile_phone=mobile_phone).exists() if tpl == 'login': if not exists: raise ValidationError('手机号未注册,请先注册') else: # 校验数据库是否已有手机号 if exists: raise ValidationError('手机号已存在') # 发短信 & 写redis # 生成随机验证码 code = random.randrange(1000, 9999) # 发送短信 sms = result = send_sms_single(mobile_phone, template_id, [ code, ]) if sms['result'] != 0: raise ValidationError('短信发送失败,{}'.format(sms['errmsg'])) # 验证码写入redis # 方法一:直接连接redis # conn = redis.Redis(host='127.0.0.1', port=6379, encoding='utf-8') # 方法二:创建redis连接池 pool = redis.ConnectionPool(host='127.0.0.1', port=6379, encoding='utf-8', max_connections=1000) # 去连接池中获取一个连接 conn = redis.Redis(connection_pool=pool) # 方法三:django-redis # conn = get_redis_connection() conn.set(mobile_phone, code, ex=60) return mobile_phone
def send_single(request): tpl = request.GET.get('tpl') tpl_id = settings.TENCENT_SMS_TEMPLATE.get(tpl) print(tpl, tpl_id) if not tpl_id: return HttpResponse('模板不存在') code = randrange(1000, 9999) res = send_sms_single('18231346616', 549888, [ code, ]) if res['result'] == 0: return HttpResponse('成功') else: return HttpResponse(res['errmsg'])
def send_sms(request): tpl = request.GET.get('tpl') template_id = settings.TENCENT_SMS_TEMPLATE.get(tpl) if not template_id: return HttpResponse('模板不存在') code = random.randrange(1000, 9999) res = send_sms_single('13151003856', template_id, [ code, ]) if res['result'] == 0: return HttpResponse('成功') else: return HttpResponse(res['errmsg'])
def send_sms(request): """ 发送短信 ?tpl=login -> 548762 ?tpl=register -> 548760 """ tpl = request.GET.get('tpl') template_id = settings.TENCENT_SMS_TEMPLATE.get(tpl) if not template_id: return HttpResponse('模板不存在') code = random.randrange(1000, 9999) res = send_sms_single('15131289', template_id, [code, ]) if res['result'] == 0: return HttpResponse('成功') else: return HttpResponse(res['errmsg'])
def sms(request): """发送短信 ?tpl=login -> 873640 ?tpl=register -> 873641 """ tpl = request.GET.get('tpl') template_id = settings.TENCENT_SMS_TEMPLATE.get(tpl) if not template_id: return HttpResponse('模板不存在') code = random.randrange(1000, 9999) res = send_sms_single(18067066338, 873641, [ code, ]) if res['result'] == 0: return HttpResponse("发送成功") else: return HttpResponse(res['errmsg'])
def send_sms(request): """发送短信 ?tpl=login -> 590050 ?tpl=register ->590049 """ tpl = request.GET.get('tpl') template_id = settings.TENCENT_SMS_TEMPLATE.get(tpl) if not template_id: return HttpResponse('模板不存在') code = random.randrange(1000, 9999) res = send_sms_single('17190199811', template_id, [ code, ]) if res['result'] == 0: return HttpResponse("success") else: return HttpResponse(res['errmsg'])
def send_sms(requests): """ 发送短信 :param requests: :return: """ tpl = requests.GET.get('tpl') template_id = settings.TENCENT_SMS_TEMPLATE.get(tpl) if not template_id: return HttpResponse('模板不存在') code = random.randrange(1000, 9999) res = send_sms_single("18108817986", template_id, [ code, ]) if res['result'] == 0: return HttpResponse('成功') else: return HttpResponse(res['errmsg'])
def send_sms(request): """发送短信 ?tpl=login ?tpl=register """ tpl = request.GET.get('tpl') template_id = settings.TENCENT_SMS_TEMPLATE(tpl) if not template_id: return HttpResponse('不存在') code = random.randrange(1000, 9999) res = send_sms_single('18800253559', template_id, [ code, ]) if res['result'] == 0: return HttpResponse('ok') else: return HttpResponse(res['errmsg'])
def send_sms(request): """发送短信 ?tpl=login => 947221 ?tpl=register => 947220 """ tpl = request.GET.get('tpl') template_id = settings.TENCENT_SMS_TEMPLATE.get(tpl) if not template_id: return HttpResponse('模板不存在') code = random.randrange(1000, 9999) res = sms.send_sms_single('135336404', 947220, [code, ]) print(res) if res['result'] == 0: return HttpResponse('发送成功') else: return HttpResponse(res['errmsg'])