def post(self): # 获取参数 mobile = self.json_args.get("mobile") piccode = self.json_args.get("piccode") piccode_id = self.json_args.get("piccode_id") # 参数校验 # if mobile and piccode and piccode_id if not all((mobile, piccode, piccode_id)): return self.write(dict(errcode=RET.PARAMERR, errmsg="参数缺失")) if not re.match(r"^1\d{10}$", mobile): return self.write(dict(errcode=RET.PARAMERR, errmsg="手机号格式错误")) # 获取图片验证码真实值 try: real_piccode = self.redis.get("pic_code_%s" % piccode_id) except Exception as e: logging.error(e) return self.write(dict(errcode=RET.DBERR, errmsg="查询验证码错误")) if not real_piccode: return self.write(dict(errcode=RET.NODATA, errmsg="验证码过期")) # 删除图片验证码 try: self.redis.delete("pic_code_%s" % piccode_id) except Exception as e: logging.error(e) if real_piccode.lower() != piccode.lower(): return self.write(dict(errcode=RET.DATAERR, errmsg="验证码错误")) # 手机号是否存在检查 sql = "select count(*) counts from ih_user_profile where up_mobile=%s" try: ret = self.db.get(sql, mobile) except Exception as e: logging.error(e) else: if 0 != ret["counts"]: return self.write(dict(errcode=RET.DATAEXIST, errmsg="手机号已注册")) # 产生随机短信验证码 sms_code = "%04d" % random.randint(0, 9999) try: self.redis.setex("sms_code_%s" % mobile, SMS_CODE_EXPIRES_SECONDS, sms_code) except Exception as e: logging.error(e) return self.write(dict(errcode=RET.DBERR, errmsg="数据库出错")) # 发送短信验证码 try: ccp.sendTemplateSMS(mobile, [sms_code, SMS_CODE_EXPIRES_SECONDS/60], 1) except Exception as e: logging.error(e) return self.write(dict(errcode=RET.THIRDERR, errmsg="发送短信失败")) self.write(dict(errcode=RET.OK, errmsg="发送成功")) print("sms_code:", sms_code)
def post(self): # 接收参数 mobile = self.json_args.get("mobile") pic_code = self.json_args.get("pic_code") pic_code_id = self.json.args.get("pic_code_id") if not all((mobile, pic_code, pic_code_id)): return self.write(dict(errcode=RET.PARAMERR, errmsg="参数缺失")) # 检查手机号 if not re.match(r"^1\d{10}$", mobile): return self.write(dict(errcode=RET.PARAMERR, errmsg="手机号格式错误")) sql = "select count(*) counts from ih_user_profile where up_mobile=%s" try: res = self.db.get(sql, mobile) except Exception as e: logging.error(e) else: if 0 != res["counts"]: return self.write(dict(errcode=RET.DATAEXIST, errmsg="手机号已注册")) # 检验验证码 try: real_pic_code = self.redis.get("pic_code_%s" % pic_code_id) except Exception as e: logging.error(e) return self.write(dict(errcode=RET.DBERR, errmsg="查询验证码错误")) if not real_pic_code: return self.write(dict(errcode=RET.NODATA, errmsg="验证码过期")) # 删除redis 中保存的验证码 try: self.redis.delete("pic_code_%s" % pic_code_id) except Exception as e: logging.error(e) if real_pic_code.lower() != pic_code.lower(): return self.write(dict(errcode=RET.DATAERR, errmsg="验证码错误")) # 产生随机短信验证码 sms_code = "%06d" % random.randint(1, 1000000) try: self.redis.setex("sms_code_%s" % mobile, SMS_CODE_EXPIRES_SECONDS, sms_code) except Exception as e: logging.error(e) return self.write(dict(errcode=RET.DBERR, errmsg="数据库出错")) # 发送短信验证码 try: result = ccp.sendTemplateSMS( mobile, [sms_code, SMS_CODE_EXPIRES_SECONDS / 60], 1) except Exception as e: logging.error(e) return self.write(dict(errcode=RET.THIRDERR, errmsg="发送短信失败")) if result: self.write(dict(errcode=RET.OK, errmsg="发送成功")) else: self.write(dict(errcode=RET.UNKOWNERR, errmsg="发送失败"))
def post(self): # 获取参数 mobile = self.json_args.get("mobile") piccode = self.json_args.get("piccode") piccode_id = self.json_args.get("piccode_id") # 参数校验 # if mobile and piccode and piccode_id if not all((mobile, piccode, piccode_id)): return self.write(dict(errcode=RET.PARAMERR, errmsg="参数缺失")) if not re.match(r"^1\d{10}$", mobile): return self.write(dict(errcode=RET.PARAMERR, errmsg="手机号格式错误")) # 获取图片验证码真实值 try: real_piccode = self.redis.get("pic_code_%s" % piccode_id) except Exception as e: logging.error(e) return self.write(dict(errcode=RET.DBERR, errmsg="查询验证码错误")) if not real_piccode: return self.write(dict(errcode=RET.NODATA, errmsg="验证码过期")) # 删除图片验证码 try: self.redis.delete("pic_code_%s" % piccode_id) except Exception as e: logging.error(e) if real_piccode.lower() != piccode.lower(): return self.write(dict(errcode=RET.DATAERR, errmsg="验证码错误")) # 手机号是否存在检查 sql = "select count(*) counts from ih_user_profile where up_mobile=%s" try: ret = self.db.get(sql, mobile) except Exception as e: logging.error(e) else: if 0 != ret["counts"]: return self.write(dict(errcode=RET.DATAEXIST, errmsg="手机号已注册")) # 产生随机短信验证码 sms_code = "%06d" % random.randint(1, 1000000) try: self.redis.setex("sms_code_%s" % mobile, SMS_CODE_EXPIRES_SECONDS, sms_code) except Exception as e: logging.error(e) return self.write(dict(errcode=RET.DBERR, errmsg="数据库出错")) # 发送短信验证码 try: result = ccp.sendTemplateSMS(mobile, [sms_code, SMS_CODE_EXPIRES_SECONDS/60], 1) except Exception as e: logging.error(e) return self.write(dict(errcode=RET.THIRDERR, errmsg="发送短信失败")) if result: self.write(dict(errcode=RET.OK, errmsg="发送成功")) else: self.write(dict(errcode=RET.UNKOWNERR, errmsg="发送失败"))
def post(self): """获取参数""" mobile = self.json_args.get("mobile") piccode = self.json_args.get("piccode") piccode_id = self.json_args.get("piccode_id") print "*" * 30 print mobile print piccode print piccode_id print "*" * 30 """获取图片验证码真是值""" try: real_piccode = self.redis.get("pic_code_%s" % piccode_id) print real_piccode except Exception as e: logging.error(e) return self.write(dict(errcode=RET.DBERR, errmsg="查询验证码错误")) if not real_piccode: return self.write(dict(errcode=RET.NODATA, errmsg="验证码过期")) """删除图片验证码""" try: self.redis.delete("pic_code_%s" % piccode_id) except Exception as e: logging.error(e) if real_piccode.lower() != piccode.lower(): return self.write(dict(errcode=RET.DATAERR, errmsg="验证码错误")) # 验证手机号码是否存在 sql = "select count(*) counts from ih_user_profile where up_mobile = %s" try: ret = self.db.get(sql, mobile) except Exception as e: logging.error(e) else: if 0 != ret["counts"]: return self.write(dict(errcode=RET.DATAEXIST, errmsg="该手机号已注册")) # 产生随机验证码 sms_code = "%06d" % random.randint(0, 1000000) try: self.redis.setex("sms_code_%s" % mobile, SMS_CODE_EXPIRES_SECONDS, sms_code) except Exception as e: logging.error(e) return self.write(dict(errcode=RET.DBERR, errmsg="数据库出错")) # 发送短信验证码 try: result = ccp.sendTemplateSMS(mobile, [sms_code, 1], 1) except Exception as e: logging.error(e) return self.write(dict(errcode=RET.THIRDERR, errmsg="发送短信失败")) if result: self.write(dict(errcode=RET.OK, errmsg="发送成功")) else: self.write(dict(errcode=RET.UNKOWNERR, errmsg="发送失败"))
def post(self, *args, **kwargs): # 接收前段传过来的json数据(mobile, image_code_text, image_code_id) mobile = self.json_args.get('mobile') image_code_text = self.json_args.get('image_code_text') image_code_id = self.json_args.get('image_code_id') # print mobile, image_code_id, image_code_text # 判断这三个数据是否都存在,如果有其中一个数据为空,则返回相关json数据{error_num:RET.NODATA, error_msg:'不能为空'} if not all((mobile, image_code_text, image_code_id)): return self.write(dict(errorcode=RET.NODATA, errormsg='不能为空')) # 判断用户输入的手机号码是否符合以1开头的11位号码,不符合返回json数据{error_num:RET.DATAERR, error_msg:'请输入11位数的手机号码'} if not re.match(r"^1\d{10}$", mobile): return self.write( dict(errorcode=RET.NODATA, errormsg='请输入11位数的手机号码')) # 获取图片验证码真实值 try: real_image_code_text = self.redis.get("pic_code_%s" % image_code_id) except Exception as e: logging.error(e) return self.write(dict(errorcode=RET.DBERR, errormsg='查询验证码错误')) print real_image_code_text # 如果数据为空,则说明验证码过期 if not real_image_code_text: return self.write(dict(errorcode=RET.NODATA, errormsg='验证码过期')) # 删除保存在Redis中的图片验证码 try: self.redis.delete("pic_code_%s" % image_code_id) except Exception as e: logging.error(e) # 判断用户传递过来的图片验证码文本是否和存在Redis数据库中的相同,不相同返回json数据{error_num:RET.DATAERR, error_msg:'验证码输入错误'} if image_code_text.lower() != real_image_code_text.lower(): return self.write(dict(errorcode=RET.DATAERR, errormsg='验证码输入错误')) # 随机生成四位验证码并保存在Redis数据库中,如保存不成功,返回json数据{error_num:RET.DBERR, error_msg:'验证码存入数据库出错'} sms_code = "%04d" % random.randint(1, 9999) try: self.redis.setex('sms_code_%s' % mobile, constants.SMS_CODE_EXPIRES_SECONDS, sms_code) except Exception as e: logging.error(e) return self.write(dict(errorcode=RET.DBERR, errormsg='验证码存入数据库出错')) # 向mobile发送随机生成的验证码,发送不成功,返回json数据{error_num:RET.THIRDERR, error_msg:'第三方系统错误'} try: result = ccp.sendTemplateSMS( mobile, [sms_code, constants.SMS_CODE_EXPIRES_SECONDS / 60], 1) except Exception as e: logging.error(e) return self.write(dict(errorcode=RET.THIRDERR, errormsg='发送短信失败')) if result: # 成功返回json数据{error_num:RET.OK, error_msg:'成功发送短信验证码'} self.write(dict(errorcode=RET.OK, errormsg='成功发送短信验证码')) else: self.write(dict(errorcode=RET.UNKOWNERR, errormsg='发送短信失败'))