def create(self, request, *args, **kwargs): """ 使用腾讯云短信平台发送短信 """ serializer = self.get_serializer(data=request.data) # 验证合法 serializer.is_valid(raise_exception=True) mobile = serializer.validated_data["mobile"] # 使用腾讯云短信平台发送短信 tencent_sms = TencentSms(TENCENT_SECRET_ID, TENCENT_SECRET_KEY) # 生成验证码 code = self.generate_code() sms_status = tencent_sms.send_sms(code=code, mobile=mobile) if sms_status["SendStatusSet"][0]["Message"] != "send success": return Response( {"mobile": sms_status["SendStatusSet"][0]["Message"]}, status=status.HTTP_400_BAD_REQUEST) else: code_record = VerifyCode(code=code, mobile=mobile) code_record.save() return Response({"mobile": mobile}, status=status.HTTP_201_CREATED)
def create(self, request, *args, **kwargs): """ 重写CreateModelMixin下的create方法 """ print(request.data) serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) mobile = serializer.validated_data["mobile"] yun_pian = YunPian(APIKEY) code = self.generate_code() sms_status = yun_pian.send_sms(code=code, mobile=mobile) if sms_status["code"] != 0: return Response({ "mobile": sms_status["msg"] }, status=status.HTTP_400_BAD_REQUEST) else: code_record = VerifyCode(code=code, mobile=mobile) code_record.save() return Response({ "mobile": mobile }, status=status.HTTP_201_CREATED)
def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) mobile = serializer.validated_data["mobile"] yun_pian = YunPian(APIKEY) code = self.generate_code() sms_dic = yun_pian.send_sms(code=code, mobile=mobile) print(sms_dic) if sms_dic["code"] != 0: return Response( { "msg": sms_dic["msg"], "data": { "mobile": mobile }, "code": 400 }, status=status.HTTP_400_BAD_REQUEST) else: code_record = VerifyCode(code=code, mobile=mobile) code_record.save() return Response( { "msg": "验证码发送成功", "data": { "mobile": mobile }, "code": 200 }, status=status.HTTP_200_OK)
def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) # 发送验证码 code = self.__get_random_code() mobile = serializer.validated_data['mobile'] yun_pian = YunPian('b1d45af7f60a69881ec49ac396b93a7d') response = yun_pian.send_sms(code=code, mobile=mobile) if response.get('code', 'fail') == 0: # 发送成功 # 保存到数据库 verify_code = VerifyCode(code=code, mobile=mobile) verify_code.save() # api规范返回状态码 return Response( { 'mobile': mobile, 'msg': response.get('msg', '发送成功') }, status.HTTP_201_CREATED) else: # 发送失败, 返回错误信息 return Response( { 'mobile': mobile, 'msg': response.get('msg', '发送出错') }, status.HTTP_400_BAD_REQUEST)
def create(self, request, *args, **kwargs): """ 创建smsCode 时 使用 yunpian 1. :param request: :param args: :param kwargs: :return: """ print("on create new sms code resp ") serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) mobile = serializer.validated_data['mobile'] yun_pian = YunPian(APIKEY) code = self.generate_code() sms_status = yun_pian.send_sms(code=code, mobile=mobile) if sms_status['code'] != 0: print("bad request") return Response({'mobile': sms_status['msg']}, status=status.HTTP_400_BAD_REQUEST) else: code_record = VerifyCode(code=code, mobile=mobile) code_record.save() return Response({'mobile': mobile}, status=status.HTTP_201_CREATED)
def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) mobile = serializer.validated_data["mobile"] sms_code_ins = YunPian(APIKEY) code = self.generate_code() sms_req_data = sms_code_ins.send_sms_fake(code=code, mobile=mobile) if sms_req_data["code"] != 0: return Response({ "mobile": sms_req_data["msg"], }, status=status.HTTP_400_BAD_REQUEST) else: code_record = VerifyCode(code=code, mobile=mobile) code_record.save() return Response({"mobile": mobile}, status=status.HTTP_201_CREATED) self.perform_create(serializer) headers = self.get_success_headers(serializer.data) return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) # 获取手机号码 mobile = serializer.validated_data['mobile'] # 发送短信验证码 yun_pian = YunPian(APIKEY) # 创建实例 code = yun_pian.generate_code() sms_status = yun_pian.send_sms(code=code,mobile=mobile) # 云片 状态sms_status为0时发送成功 # 此处判定短信是否发送成功!,并给前端返回相应的状态码 if sms_status['code'] !=0: return Response({ 'mobile':sms_status['msg'] },status=status.HTTP_400_BAD_REQUEST) else: # 验证码发送成功后就保存VerifyCode对象到数据库,注意此处应该发送成功后保存 # 保存到的数据用于接下来的注册过程中对比认证 code_record = VerifyCode(code=code, mobile=mobile) code_record.save() # 给前端返回数据和状态码 return Response({ "mobile": mobile }, status=status.HTTP_201_CREATED)
def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) #生成验证码 params = {} mobile = serializer.validated_data['mobile'] code = self.generate_code() params['code'] = code params['product'] = 'mshop' params = json.dumps(params) #发送验证码 ret = send_sms(mobile, template_param=params) ret = str(ret, encoding='utf-8') ret = json.loads(ret) if ret['Code'] == 'OK': #向数据添加验证码 code_record = VerifyCode(code=code, mobile=mobile) code_record.save() #用户是否存在,不存在则创建 if not UserProfile.objects.filter(mobile=mobile).first(): user = UserProfile() user.mobile = mobile user.username = mobile user.name = mobile user.set_password('123456') user.save() return Response({'mobile': mobile}, status=status.HTTP_201_CREATED) else: return Response({'mobile': ret['Message']}, status=status.HTTP_400_BAD_REQUEST)
def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) mobile = serializer.validated_data['mobile'] code = self.generate_code() yun_pian = YunPian(api_key=API_KEY) sms_status= yun_pian.send_ms(code=code,mobile = mobile) if sms_status['code']!=0: return Response({ "mobile":sms_status['msg'] },status = status.HTTP_400_BAD_REQUEST) else: code_record = VerifyCode(code=code,mobile=mobile) code_record.save() return Response({ "mobile":mobile },status = status.HTTP_201_CREATED) self.perform_create(serializer) headers = self.get_success_headers(serializer.data) return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
def addver(self): verifycode_list = [] for verifycode in verifycodes_data: verfy = VerifyCode() verfy.code = verifycode["code"] verfy.mobile = verifycode["mobile"] verifycode_list.append(verfy) VerifyCode.objects.bulk_create(verifycode_list)
def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) # 获取验证通过的email email = serializer.validated_data["email"] code = self.generate_code() # 想注册邮箱发送邮件 send_status = send_email(email, DOMAIN_PREFIX, code) if send_status == 0: return Response({"email": "发送失败"}, status=status.HTTP_400_BAD_REQUEST) else: code_recode = VerifyCode(code=code, email=email) code_recode.save() return Response({"email": email}, status=status.HTTP_201_CREATED)
def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) mobile = serializer.validated_data["mobile"] yun_pian = YunPian(API_KEY) _code = self.generate_code() result = yun_pian.send_sms(_code, mobile) if result['code'] != 0: return Response({"mobile": result["msg"]}, status=status.HTTP_400_BAD_REQUEST) else: record_code = VerifyCode(code=_code, mobile=mobile) record_code.save() return Response({"mobile": mobile}, status=status.HTTP_201_CREATED)
def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) mobile = serializer.validated_data['mobile'] yun_pian = YunPian(APIKEY) sms_status = yun_pian.send_sms(code=self.generate_code(), mobile=mobile) if sms_status['code'] != 0: return Response({'mobile': sms_status['msg']}, status=status.HTTP_400_BAD_REQUEST) else: code_record = VerifyCode(code=self.generate_code(), mobile=mobile) code_record.save() return Response({'mobile': mobile}, status=status.HTTP_201_CREATED)
def send_register_email(email, send_type='register'): email_record = VerifyCode() # 生成一个16位长的随机字符串 code = random_str(16) email_record.code = code email_record.email = email email_record.send_type = send_type email_record.save() email_title = "" email_body = "" if send_type == 'register': email_title = '注册激活链接' email_body = '点击下面的链接激活你的帐号 :http://127.0.0.1:8000/user/active/{0}'.format(code) send_status = send_mail(email_title, email_body, EMAIL_FROM, [email]) if send_status: pass elif send_type == 'forget': email_title = '找回密码链接' email_body = '点击下面的链接修改的帐号密码 :http://127.0.0.1:8000/user/reset/{0}'.format(code) send_status = send_mail(email_title, email_body, EMAIL_FROM, [email]) if send_status: pass
def create(self, request, *args, **kwargs): # 检查电话号码验证是否成功,不成功raise_exception=true serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) # 从序列化实例中取出已经验证过的数据 mobile = serializer.validated_data['mobile'] # 检查成功后,随机生成四位短信验证码 code = self.get_valify_code() # 使用SmsYunPian发送短信给验证通过的mobile yun_pian = SmsYunPian(APIKEY) # 发送后返回的JSON字符串数据,当发回的code字段等于0,证明发送成功,msg字段也为发送成功,部位0时,msg字段会有相应提示 response_data = yun_pian.send_mess(code, mobile) if response_data["code"] != 0: return Response({"mobile": response_data["msg"]}, status=status.HTTP_400_BAD_REQUEST) else: ms = VerifyCode(mobile=mobile, code=code) ms.save() return Response({"mobile": mobile}, status=status.HTTP_201_CREATED)
def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) # 1、创建随机的验证码 code = self.generate_code() # 2、使用云片接口发送验证码到指定的电话号码 mobile = serializer.data['mobile'] yun_pian = Yunpian(API_KEY) sms_status = yun_pian.send_sms(code, mobile) if sms_status['code'] != 0: return Response({ "mobile": sms_status['msg'], }, status=status.HTTP_400_BAD_REQUEST) else: # 3、保存发送验证码记录数据到VerifyCode verify_code = VerifyCode(code=code, mobile=mobile) verify_code.save() return Response({"mobile": mobile}, status=status.HTTP_201_CREATED)
def create(self, request, *args, **kwargs): # 获取数据然后传进序列化器进行序列化 serializer = self.get_serializer(data=request.data) # 进行校验 serializer.is_valid(raise_exception=True) print(serializer.validated_data) # 拿到校验后的结果. mobile = serializer.validated_data["mobile"] # 生成验证码. code = self.generate_code() ''' 修改为自己的运营商验证 # send_sms(code=code, mobile=mobile) ''' # TODO by wzj 验证码用服务商接口发给用户. sms_status = {} sms_status["code"] = 'success' if sms_status["status"] != 'success': return Response({ "mobile": mobile, "detail": sms_status["msg"] }, status=status.HTTP_202_ACCEPTED) else: if mobile != request.data.get('mobile', ''): return Response({ "detail": mobile, "error_code": 1000 }, status=status.HTTP_202_ACCEPTED) code_record = VerifyCode(code=code, mobile=mobile) code_record.save() return Response({ "mobile": mobile, "error_code": 1, }, status=status.HTTP_201_CREATED)
def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) username = serializer.validated_data['mobile'] code = self.generate_code() registerType = "" print('验证码: ' + code) # 使用电话号码注册 if re.match(REGEX_EMAIL, username) is not None: # 发送短信验证码 registerType = 'mobile' pass # 使用邮件注册 if re.match(REGEX_EMAIL, username) is not None: # 发送永久验证码 registerType = 'email' # send_mail的参数分别是 邮件标题,邮件内容,发件箱(settings.py中设置过的那个),收件箱列表(可以发送给多个人),失败静默(若发送失败,报错提示我们) send_mail('Subject here', '欢迎注册ProjectSample,验证码 {0}'.format(code), '*****@*****.**', ['*****@*****.**'], fail_silently=False) code_record = VerifyCode(code=code, mobile=username) code_record.save() return Response( { "type": registerType, "username": username, "code": code }, status=status.HTTP_201_CREATED) self.perform_create(serializer) headers = self.get_success_headers(serializer.data) return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) # failed will return 400 mobile = serializer.validated_data["mobile"] receiver_num = "+86" + mobile print(receiver_num) # "13xxxxx" need add +86 code = self.generate_code() sms_content = "[FSPT] Your verify code is {}".format(code) tss = TwilioSmsSender(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN) sms_status = tss.send_sms(receiver_num, TWILIO_SENDER_NUM, sms_content) if sms_status["code"] != 1: # sms_status["code"] 1 success , 0 failed return Response({"mobile": sms_status["msg"]}, status=status.HTTP_400_BAD_REQUEST) else: # 短信发送成功后才保存到数据库 code_record = VerifyCode(code=code, mobile=mobile) code_record.save() return Response({"mobile": mobile}, status=status.HTTP_201_CREATED)
def create(self, request, *args, **kwargs): # 序列化验证 serializer = self.get_serializer(data=request.data) #验证合法 serializer.is_valid(raise_exception=True) # 获取号码 mobile = serializer.validated_data['mobile'] # 验证码 code = self.generate_code() # 发短信 yun_xun = message_validate(phone_number=mobile, validate_number=code) if not yun_xun[0]: return Response({ # 返回错误消息 'mobile': yun_xun[-1], }, status=status.HTTP_400_BAD_REQUEST) else: code_recode = VerifyCode(code=code, mobile=mobile) code_recode.save() return Response({ 'mobile': mobile, }, status=status.HTTP_201_CREATED)
def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) log.info('serializer: {}'.format(serializer)) serializer.is_valid(raise_exception=True) mobile = serializer.validated_data['mobile'] code = self.generate_code() log.info("mobile: {}, code: {}".format(mobile, code)) try: code_record = VerifyCode(code=code, mobile=mobile) code_record.save() return Response({ "mobile": mobile, 'msg': 'success' }, status=status.HTTP_201_CREATED) except Exception as e: log.exception(e) return Response({ "mobile": mobile, 'msg': 'failed' }, status=status.HTTP_400_BAD_REQUEST) """
def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) # 如果调用失败,抛异常(设置) mobile = serializer.validated_data["mobile"] yun_pian = YunPian(YUNPIAN_API_KEY) code = self.generate_code() sms_status = yun_pian.send_sms(code=code, mobile=mobile) if sms_status["code"] != 0: return Response({ "mobile": sms_status["msg"], }, status=status.HTTP_400_BAD_REQUEST) else: code_record = VerifyCode(code=code, mobile=mobile) # 短信发送成功才保存验证码 code_record.save() return Response({ "mobile": mobile, }, status=status.HTTP_201_CREATED)
def create(self, request, *args, **kwargs): """生成验证码,调用云片接口发送,并将验证码保存到数据库""" # 验证mobile serializer = self.get_serializer(data=request.data) serializer.is_valid( raise_exception=True) # 这里验证失败会raise一个ValidationError而不执行后面的 mobile = serializer.validated_data["mobile"] # 取出通过验证的mobile # 生成验证码 code = self.generate_code() """ # 云片网发送短信 yun_pian = YunPian(YUNPIAN_KEY) sms_status = yun_pian.send_sms(code=code, mobile=mobile) # 发送失败 if sms_status["code"] != 0: return Response({ "mobile": sms_status["msg"] # 这里将云片网发送短信接口的错误信息放到mobile字段了 }, status=status.HTTP_400_BAD_REQUEST) # 遵循REST规范,用HTTP状态码表征请求的结果如何 # 发送成功 else: """ code_record = VerifyCode(code=code, mobile=mobile) # 验证码,手机号对应 code_record.save() # 保存到数据库 return Response({"mobile": mobile}, status=status.HTTP_201_CREATED)
def send_email_code(email, code): email_ver = VerifyCode() email_ver.email = email email_ver.code = code email_ver.save() print('code', code) #第二步,准备参数发送邮件 send_title = '欢迎注册天天生鲜在线网站' send_body = f'你的验证码:{code}\n 是请点击以下链接进行激活:\n ' + EMAIL_REGISTER send_mail(send_title, send_body, EMAIL_FROM, [email])
def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) mobile = serializer.validated_data["mobile"] yunpian = YUNPIAN(APIKEY) code = self.generate_code() sms_status = YUNPIAN.send_sms(code=code, mobile=mobile) if sms_status["code"] != 0: return Response({"moile": sms_status["msg"]}, status=status.HTTP_400_BAD_REQUEST) else: code_record = VerifyCode(code=code, mobile=mobile) return Response({"mobile": mobile}, status=status.HTTP_201_CREATED)
def send_vcode(request, mobile, msg_template): ''' @param mobile: 手机 @param msg_template: 消息模板, 其中%(vcode)d 代表验证码 @return (success, reason) ''' ip = utils.get_client_ip(request) if not utils.is_mobile(mobile): return False, '无效手机号' now = datetime.datetime.now() # mobile restrict count = VerifyCode.objects.filter(mobile=mobile, cre_time__gt = now - MOBILE_RESTRICT[0]).count() if count >= MOBILE_RESTRICT[1]: return False, '验证码获取过于频繁, 请稍后再试' # ip restrict count = VerifyCode.objects.filter(ip=ip, cre_time__gt = now - IP_RESTRICT[0]).count() if count >= IP_RESTRICT[1]: return False, '验证码获取过于频繁, 请稍后再试' # send vcode = generate_vcode() msg = msg_template % {'vcode': vcode} smss.send(mobile, msg) verify_code = VerifyCode() verify_code.mobile = mobile verify_code.vcode = vcode verify_code.ip = ip verify_code.save() return True, '发送成功'