async def post(self, *args, **kwargs): param = self.request.body.decode('utf8') re_data = {} param = json.loads(param) # 注意这里 sms_form = SmsCodeForm.from_json(param) if sms_form.validate(): code = self.generate_code() mobile = sms_form.mobile.data print(mobile) yun_pian = AsyncYunPian("fbdaa372a39ac1c459c27cf4cebf139f") print(yun_pian, "*" * 100) print(code) re_json = await yun_pian.send_single_sms(code, mobile) if re_json["code"] != 0: self.set_status(400) re_data["mobile"] = re_json["msg"] else: print(self.redis_conn) # 将验证码写入redis中 self.redis_conn.set("{}_{}".format(mobile, code), 1, 10 * 60) else: self.set_status(400) for field in sms_form.errors: re_data[field] = sms_form.errors[field][0] self.finish(re_data)
async def post(self): re_data = {} param = self.request.body.decode("utf-8") param = json.loads(param) sms_form = SmsCodeForm.from_json(param) if sms_form.validate(): mobile = sms_form.mobile.data code = self.generate_code() yun_pain = AsyncYunPian("asdfsafq2123") re_json = await yun_pain.send_single_sms(code, mobile) # re_json = await partial(yun_pain.send_single_sms, code, mobile) if re_json["code"] != 0: self.set_status(400) re_data["mobile"] = re_json["msg"] else: # 成功,将验证码写入到redis中,并设置一个有效时间 # self.redis_conn.set("{}_{}".format(mobile, code), 1, 10*60) print("存入redis") re_data["msg"] = "ok" re_data["code"] = code else: self.set_status(400) for field in sms_form.errors: re_data[field] = sms_form.errors[field][0] self.finish(re_data)
async def post(self, *args, **kwargs): re_data = {} params = self.request.body.decode('utf-8') params = json.loads(params) # 使用 wtforms_json 打猴子补丁 解决参数格式化错误问题 # 错误原因是因为现在传入的是一个字符串类型 不再是列表类型了 sms_form = SmsCodeForm.from_json(params) if sms_form.validate(): code = self.get_code() mobile = sms_form.mobile.data yun_pian = AsyncYunPian("7b14af96096b7f9f12ed467d21102fab") res_json = await yun_pian.send_single_sms(code, mobile) if res_json["code"] != 0: self.set_status(400) re_data["mobile"] = res_json["msg"] else: # 将验证码和手机号作为键存储到 redis 设置过期时间十分钟 self.redis_conn.set(f"{mobile}_{code}", 1, 10 * 60) else: self.set_status(400) for field in sms_form.errors: re_data[field] = sms_form.errors[field][0] self.finish(re_data)
async def post(self, *args, **kwargs): re_data = {} param = self.request.body.decode("utf-8") param = json.loads(param) sms_form = SmsCodeForm.from_json(param) if sms_form.validate(): mobile = sms_form.mobile.data # code = self.generate_code() code = '0720' yun_pian = AsyncYunPian("57477467cb72b6bac9abc9c327986752") # re_json = await yun_pian.send_single_sms(code, mobile) re_json = dict() re_json["code"] = 0 if re_json["code"] != 0: self.set_status(400) re_data["mobile"] = re_json["msg"] else: #将验证码写入redis self.redis_conn.set('{}_{}'.format(mobile, code), 1, 10*60) else: self.set_status(400) for field in sms_form.errors: re_data[field]=sms_form.errors[field][0] self.finish(re_data)
async def post(self, *args, **kwargs): re_data = {} param = self.request.body.decode("utf-8") param = json.loads(param) sms_form = SmsCodeForm.from_json(param) if sms_form.validate(): mobile = sms_form.mobile.data code = self.generate_code() yun_pian = AsyncYunPian("d6c4ddbf50ab36611d2f52041a0b949e") re_json = await yun_pian.send_single_sms(code, mobile) if re_json["code"] != 0: self.set_status(400) re_data["mobile"] = re_json["msg"] else: #将验证码写入到redis中 self.redis_conn.set("{}_{}".format(mobile,code), 1, 10*60) else: self.set_status(400) for field in sms_form.errors: re_data[field] = sms_form.errors[field][0] self.finish(re_data)