def register(): """注册功能""" # 获取参数 mobile password sms_code mobile = request.json.get('mobile') password = request.json.get('password') sms_code = request.json.get('sms_code') # 校验参数 if not all([mobile, password, sms_code]): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 校验手机格式 if not re.match('^(13\d|14[5|7]|15\d|166|17[3|6|7]|18\d)\d{8}$', mobile): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 检查验证码是否过期 real_sms_code = sr.get("sms_code_id_" + mobile) if not real_sms_code: return jsonify(errno=RET.PARAMERR, errmsg='验证码已过期') # 检验sms_code正确 if sms_code != real_sms_code: return jsonify(errno=RET.PARAMERR, errmsg='验证码错误') # 保存数据到数据库 user = User() user.mobile = mobile user.password = password user.nick_name = mobile user.last_login = datetime.now() try: db.session.add(user) db.session.commit() except BaseException as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) # 返回结果 return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])
def get_sms_code(): # 获取参数 img_code_id = request.json.get("img_code_id") mobile = request.json.get("mobile") img_code = request.json.get("img_code") # 校验参数 if not all([img_code_id, mobile, img_code]): # 返回参数错误 return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) if not re.match(r"1[35678]\d{9}$", mobile): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 根据手机号从数据库取出用户 try: user = User.query.filter_by(mobile=mobile).first() except BaseException as e: current_app.logger.error(e) # 返回数据库查询错误 return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) # 判断用户是否已经注册 if user: return jsonify(errno=RET.DATAEXIST, errmsg="用户已存在") # 根据图片key取出对应的验证码文字 try: real_img_text = sr.get("img_code_id" + img_code_id) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="key" + error_map[RET.DBERR]) print(real_img_text) print(img_code) # 校验图片验证码是否过期 if not real_img_text: return jsonify(errno=RET.PARAMERR, errmsg="验证码已经过期") # 验证图片验证码是否正确 if real_img_text.decode("utf-8") != img_code.upper(): return jsonify(errno=RET.PARAMERR, errmsg="验证码错误") # 如果正确就发短信 # 生成短信验证码 rand_num = "%04d" % random.randint(0, 9999) current_app.logger.info("短信验证码为:%s" % rand_num) # result = CCP().send_template_sms(mobile, [rand_num, 1], 1) # if result == -1: # return jsonify(errno=RET.THIRDERR, errmsg=error_map[RET.THIRDERR]) # 将短信验证码保存到数据库中 try: sr.set("sms_code_" + mobile, rand_num, ex=60) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])
def register(): # 注册 # 校验数据 mobile = request.json.get("mobile") sms_code = request.json.get("sms_code") password = request.json.get("password") if not all([mobile, sms_code, password]): return jsonify(errno=RET.PARAMERR, errmgs=error_map[RET.PARAMERR]) try: real_sms_code = sr.get("sms_code_id" + mobile) except BaseException as e: current_app.logger.info(e) return jsonify(errno=RET.DBERR, errmgs=error_map[RET.DBERR]) if real_sms_code != sms_code: return jsonify(errno=RET.PARAMERR, errmgs=error_map[RET.PARAMERR]) # 保存数据 try: user = User() user.mobile = mobile user.nick_name = mobile # user.password = generate_password_hash(password)# 这样为了加密密码,但是通常不用这种方法 user.password = password # 定义用户模型的时候对password属性使用计算型属性 db.session.add(user) db.session.commit() except BaseException as e: db.session.rollback() current_app.logger.info(e) return jsonify(errno=RET.DBERR, errmgs=error_map[RET.DBERR]) # 注册成功对用户的信息进行保存 session["user_id"] = user.id return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])
def get_sms_code(): # 获取参数 mobile = request.json.get("mobile") # 手机号 img_code = request.json.get("img_code") # 图片验证码 img_code_id = request.json.get("img_code_id") # 图片key # 校验参数 if not all([mobile, img_code, img_code_id]): # 返回自定义的错误信息 return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 校验手机号格式 if not re.match(r"1[35678]\d{9}$", mobile): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 根据图片key取出验证码文字 try: real_img_code = sr.get("img_code_id" + img_code_id) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) # 验证是否过期 if not real_img_code: return jsonify(errno=RET.PARAMERR, errmsg="验证码已过期") # 校验验证码 if real_img_code != img_code.upper(): # 不一致 return jsonify(errno=RET.PARAMERR, errmsg="验证码错误") # 判断该用户是否存在(数据库中是否有该手机号对应的用户) try: user = User.query.filter_by(mobile=mobile).first() except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) if user: return jsonify(errno=RET.DATAEXIST, errmsg=error_map[RET.DATAEXIST]) # 生成随机短信验证码 sms_code = "%04d" % random.randint(0, 9999) current_app.logger.info("短信验证码为:%s" % sms_code) # 发送短信 # response_code = CCP().send_template_sms(mobile, [sms_code, 5], 1) # if response_code != 0: # return jsonify(errno=RET.THIRDERR, errmsg=error_map[RET.THIRDERR]) # 保存短信验证码 key是手机号 value是验证码 try: sr.set("sms_code_id" + mobile, sms_code, ex=60) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) # json返回发送结果 return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])
def get_sms_code(): # 获取参数-手机号/用户输入的图片验证码/图片验证码key img_code_id = request.json.get("img_code_id") mobile = request.json.get("mobile") img_code = request.json.get("img_code") # 校验参数 if not all([img_code_id, mobile, img_code]): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) if not re.match(r"1[35678]\d{9}$", mobile): # 手机号格式是否合格 return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 判断用户是否存在 try: user = User.query.filter_by(mobile=mobile).first() except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) if user: return jsonify(errno=RET.DATAEXIST, errmsg=error_map[RET.DATAEXIST]) # 根据图片key取出数据库中的图片验证码文字 try: real_img_text = sr.get("img_code_id_" + img_code_id) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) # 校验验证码是否过期/正确 if not real_img_text: return jsonify(errno=RET.PARAMERR, errmsg='图片验证码已过期') if real_img_text != img_code.upper(): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 如果正确,生成短信验证码并发送 # 生成短信验证码 rand_num = '%04d' % random.randint(0, 9999) current_app.logger.info('短信验证码为:%s' % rand_num) # result = CCP().send_template_sms(mobile, [rand_num, 5], 1) # if result == -1: # return jsonify(errno=RET.PARAMERR, errmsg="短信发送失败") # 短信发送成功,将短信验证码保存到数据库中 try: sr.set('sms_code_' + mobile, rand_num, ex=60) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])
def get_sms_code(): # 获取参数 request.json可以获取到application/json格式传过来的json数据 img_code_id = request.json.get("img_code_id") img_code = request.json.get("img_code") mobile = request.json.get("mobile") # 校验参数 if not all([img_code_id, img_code, mobile]): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 校验手机号格式 if not re.match(r"1[35678]\d{9}$", mobile): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 根据图片key取出验证码文字 try: real_img_code = sr.get("img_code_id_" + img_code_id) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) # 校验图片验证码 if not real_img_code: # 校验是否已过期 return jsonify(errno=RET.PARAMERR, errmsg="验证码已过期") if img_code.upper() != real_img_code: # 校验验证码是否正确 return jsonify(errno=RET.PARAMERR, errmsg="验证码错误") # 根据手机号从数据库中取出对应的记录 try: user = User.query.filter_by(mobile=mobile).first() except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) # 判断该用户是否存在 if user: # 提示用户已存在 return jsonify(errno=RET.DATAEXIST, errmsg=error_map[RET.DATAEXIST]) # 如果校验成功, 发送短信 # 生成4位随机数字 sms_code = "%04d" % random.randint(0, 9999) current_app.logger.info("短信验证码为: %s" % sms_code) # res_code = CCP().send_template_sms(mobile, [sms_code, 5], 1) # if res_code == -1: # 短信发送失败 # return jsonify(errno=RET.THIRDERR, errmsg=error_map[RET.THIRDERR]) # 将短信验证码保存到redis try: sr.set("sms_code_id_" + mobile, sms_code, ex=60) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) # 将短信发送结果使用json返回 return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])
def get_sms_code(): # 获取参数 img_code_id = request.json.get("img_code_id") img_code = request.json.get("img_code") mobile = request.json.get("mobile") # 校验参数 if not all([img_code, img_code_id, mobile]): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 校验手机号格式 if not re.match(r"^1[345678]\d{9}$", mobile): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 判断用户是否存在 从数据库查找数据 try: user = User.query.filter_by(mobile=mobile).first() except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DATAEXIST, errmsg=error_map[RET.DATAEXIST]) # 用户已经注册过 if user: return jsonify(errno=RET.DATAEXIST, errmsg=error_map[RET.DATAEXIST]) # 从redis中取出数据校验参数 try: real_img_code = sr.get("img_code_id_" + img_code_id) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) # 校验图片验证码 if real_img_code != img_code.upper(): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 生成随机四位数字 rand_num = "%04d" % random.randint(0, 9999) # 生成短信验证码 # response_code = CCP().send_template_sms(mobile, [rand_num, 5], 1) # if response_code != 0: # return jsonify(errno=RET.THIRDERR, errmsg=error_map[RET.THIRDERR]) # 保存短信验证码 try: sr.set("sms_code_id_" + mobile, rand_num, ex=SMS_CODE_REDIS_EXPIRES) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) # 打印短信验证码 current_app.logger.info("sms_code:%s" % rand_num) # 返回数据 return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])
def register(): # 获取参数 mobile = request.json.get("mobile") password = request.json.get("password") sms_code = request.json.get("sms_code") # 校验参数 if not all([mobile, password, sms_code]): # 返回自定义的错误信息 return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 校验手机号格式 if not re.match(r"1[35678]\d{9}$", mobile): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 根据手机号取出短信验证码 try: real_sms_code = sr.get("sms_code_id" + mobile) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) # 校验短信验证码 过期/是否一致 if not real_sms_code: return jsonify(errno=RET.PARAMERR, errmsg="验证码已过期") if real_sms_code != sms_code: return jsonify(errno=RET.PARAMERR, errmsg="验证码错误") # 保存数据 user = User() user.mobile = mobile # 封装加密过程 user.password = password user.nick_name = mobile # 记录最后登录时间(保存到是一个日期对象) user.last_login = datetime.now() try: db.session.add(user) db.session.commit() except BaseException as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) # 状态保持(免密码登录) 保存用户的主键, 就可以取出用户的所有信息 session["user_id"] = user.id # json返回结果 return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])
def register(): # 获取参数 mobile = request.json.get("mobile") sms_code = request.json.get("sms_code") password = request.json.get("password") # 校验参数 if not all([mobile, sms_code, password]): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) if not re.match(r"1[35678]\d{9}$", mobile): # 手机号格式是否合格 return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 从数据库中取出短信验证码 try: real_sms_code = sr.get("sms_code_" + mobile) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="1" + error_map[RET.DBERR]) # 校验验证码 if not real_sms_code: return jsonify(errno=RET.PARAMERR, errmsg="短信验证码已过期") if real_sms_code.decode("utf-8") != sms_code: return jsonify(errno=RET.PARAMERR, errmsg="短信验证码错误") user = User() user.mobile = mobile # 生成计算属性password,封装加密过程 user.password = password user.nick_name = mobile # 记录用户最后登录时间 user.last_login = datetime.now() # 如果正确则存入数据库 try: db.session.add(user) db.session.commit() except BaseException as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg="2" + error_map[RET.DBERR]) # 状态保持,免密登录 session['user_id'] = user.id # 返回响应 return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])
def get_sms_code(): # 获取参数 手机号码 用户输入的验证码 图片key mobile = request.json.get("mobile") img_code = request.json.get("img_code") img_code_id = request.json.get("img_code_id") # 校验参数 if not all([mobile, img_code_id, img_code]): return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) # 校验手机号码的格式 if not re.match(r"1[35678]\d{9}$", mobile): return jsonify(errno=RET.DATAERR, errmsg=error_map[RET.DATAERR]) # 校验图片验证码是否过期 try: real_img_code = sr.get("img_code_id" + img_code_id) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) if not real_img_code: return jsonify(errno=RET.PARAMERR, errmsg="验证码已过期!") # 判断用户输入的验证码是否正确 if real_img_code != img_code.upper(): return jsonify(errno=RET.PARAMERR, errmsg="验证码输入错误!") # 判断用户输入的手机号码是否注册过 user = User.query.filter_by(mobile=mobile).first() if user: return jsonify(errno=RET.DATAEXIST, errmsg=error_map[RET.DATAEXIST]) # 获取4位随机数字 sms_code = "%04d" % random.randint(0, 9999) current_app.logger.error("短信验证码为:%s" % sms_code) # 发送短信 res_code = CCP().send_template_sms(mobile, [sms_code, 5], 1) # 判断短信是否发送成功 if res_code == -1: return jsonify(errno=RET.THIRDERR, errmsg=error_map[RET.THIRDERR]) # 将短信验证码保存到redis,并设置有效期为一分钟 try: sr.set("sms_code_id" + mobile, sms_code, ex=60) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])
def register(): """ 用户注册 :return: """ # 获取参数 mobile = request.json.get("mobile") password = request.json.get("password") sms_code = request.json.get("sms_code") # 校验参数 if not all([mobile, password, sms_code]): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 校验手机格式 if not re.match(r"1[345678]\d{9}$", mobile): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 校验短信验证码, 先根据手机号取出短信验证码 try: real_sms_code = sr.get("sms_code_id_" + mobile) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) if sms_code != real_sms_code: return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 保存用户信息 try: user = User() user.mobile = mobile user.nick_name = mobile # 使用@ property 属性来封装加密过程 user.password = password user.last_login = datetime.now() db.session.add(user) db.session.commit() except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) # 状态保持(免密支付) 只需要存储用户uid 即可 session['user_id'] = user.id # 返回json结果 return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])
def get_sms_code(): # 获取参数 mobile = request.json.get('mobile') img_code = request.json.get('img_code') img_code_id = request.json.get('img_code_id') # 校验参数 if not all([mobile, img_code, img_code_id]): # 返回自定义错误信息 return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 校验电话号码是否合法 if not re.match(r'1[35678]\d{9}$', mobile): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 根据图片key取出验证码文本 try: real_img_code = sr.get('img_code_id' + img_code_id) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) # 验证是否过期 if not real_img_code: return jsonify(errno=RET.PARAMERR, errmsg='验证码已过期') # 校验图片验证码文本 if real_img_code != img_code.upper(): return jsonify(errno=RET.PARAMERR, errmsg='验证码错误') # 判断用户是否存在 try: user = User.query.filter_by(mobile=mobile).first() except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) if user: return jsonify(errno=RET.DATAEXIST, errmsg=error_map[RET.DATAEXIST]) # 生成随机短信验证码 sms_code = '%04d' % random.randint(0, 9999) current_app.logger.info("短信验证码为: %s" % sms_code) # # 发送短信 # response_code = CCP().send_template_sms(mobile, [sms_code, 5], 1) # if response_code != 0: # return jsonify(errno=RET.THIRDERR, errmsg=error_map[RET.THIRDERR]) # 保存短信验证码 try: sr.set('sms_code_id' + mobile, sms_code, ex=60) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) # 返回发送结果 return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])
def register(): # 获取参数 request.json可以获取到application/json格式传过来的json数据 mobile = request.json.get("mobile") password = request.json.get("password") sms_code = request.json.get("sms_code") # 校验参数 if not all([mobile, password, sms_code]): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 校验手机号格式 if not re.match(r"1[35678]\d{9}$", mobile): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 根据手机号取出短信验证码文字 try: real_sms_code = sr.get("sms_code_id_" + mobile) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) # 校验图片验证码 if not real_sms_code: # 校验是否已过期 return jsonify(errno=RET.PARAMERR, errmsg="验证码已过期") if sms_code != real_sms_code: # 校验验证码是否正确 return jsonify(errno=RET.PARAMERR, errmsg="验证码错误") # 将用户数据保存到数据库 user = User() user.mobile = mobile # 使用计算性属性password对密码加密过程进行封装 user.password = password user.nick_name = mobile # 记录用户最后的登录时间 user.last_login = datetime.now() try: db.session.add(user) db.session.commit() except BaseException as e: current_app.logger.error(e) db.session.rollback() # 设置回滚 return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) # 状态保持 免密码登录 session["user_id"] = user.id return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])
def register(): # 获取参数 手机号 密码 短信验证码 mobile = request.json.get("mobile") password = request.json.get("password") sms_code = request.json.get("sms_code") # 参数校验 if not all([mobile, password, sms_code]): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 校验手机号码的格式是否正确 if not re.match(r"1[35678]\d{9}$", mobile): return jsonify(errno=RET.DATAERR, errmsg=error_map[RET.DATAERR]) # 判断短信验证码是否过期 try: real_sms_code = sr.get("sms_code_id" + mobile) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) if not real_sms_code: return jsonify(errno=RET.PARAMERR, errmsg="验证码已过期!") # 判断用户输入的验证码是否正确 if real_sms_code != sms_code: return jsonify(errno=RET.PARAMERR, errmsg="验证码输入错误!") # 将手机号、密码保存到mysql数据库 user = User() user.mobile = mobile user.password = password user.nick_name = mobile user.last_login = datetime.now() try: db.session.add(user) db.session.commit() except BaseException as e: db.session.rollback() current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) # 状态保持,免密码登陆 session["user_id"] = user.id return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])
def register(): # 获取参数-手机号/密码/输入的短信验证码 mobile = request.json.get('mobile') password = request.json.get('password') sms_code = request.json.get('sms_code') # 校验参数 if not all([mobile, password, sms_code]): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) if not re.match(r'1[35678]\d{9}', mobile): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 从数据库取出短信验证码进行校验 try: real_sms_code = sr.get('sms_code_' + mobile) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) if not real_sms_code: return jsonify(errno=RET.PARAMERR, errmsg='短信验证码已过期') if real_sms_code != sms_code: return jsonify(errno=RET.PARAMERR, errmsg='验证码输入错误') # 如果正确保存用户数据到数据库,注册成功 user = User() user.mobile = mobile user.nick_name = mobile user.password = password # 进行密码加密操作 try: db.session.add(user) db.session.commit() except BaseException as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) # 设置状态保持 session["user_id"] = user.id # 记录最后登陆时间 user.last_login = datetime.now() return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])
def get_sms_code(): """ 获取短信验证码 :return: json结果 """ # 获取参数 img_code_id = request.json.get("img_code_id") img_code = request.json.get("img_code") mobile = request.json.get("mobile") # 校验参数 if not all([img_code_id, img_code, mobile]): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 校验手机格式 if not re.match(r"1[34567]\d{9}$", mobile): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 校验图片验证码 根据图片key取出真实的验证码文字 try: real_img_code = sr.get("img_code_id_" + img_code_id) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DATAERR, errmsg=error_map[RET.DBERR]) # 生成随机短信验证码 rand_num = "%04d" % random.randint(0, 9999) # 打印验证码 current_app.logger.info(("短信验证码为:%s" % rand_num)) # 发送短信(需要注册账户 配置信息) # response_code = CCP().send_template_sms(mobile, [rand_num, 5], 1) # if response_code != 0: # return jsonify(errno = RET.THIRDERR, errmsg = error_map[RET.THIRDERR]) # 保存短信验证码 设置过期时间 try: sr.set("sms_code_id_" + mobile, rand_num, ex=60) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) # 返回json结果 return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])
def get_sms_code(): # 获取短息验证码 # 获取参数 img_code_id = request.json.get("img_code_id") img_code = request.json.get("img_code") mobile = request.json.get("mobile") # 校验参数 if not all([img_code, img_code_id, mobile]): return jsonify(errno=RET.OK, errmsg=error_map[RET.PARAMERR]) if re.match(r"1[345678]\d{}$", mobile): return jsonify(errno=RET.OK, errmsg=error_map[RET.PARAMERR]) try: real_img_code = sr.get("img_code_id" + img_code_id) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map(RET.DBERR)) if real_img_code != img_code.upper(): return jsonify(erorno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 发短信之前可以判断该用户是否存在 try: user = User.query.filter_by(mobile=mobile).first() except BaseException as e: current_app.logger.info(e) return jsonify(errno=RET.DBERR, errmsg=error_map(RET.DBERR)) if user: return jsonify(errno=RET.DATAEXIST, errmgs=error_map[RET.DATAEXIST]) # 发送短信 rand_num = "%04d" % random.randint(0, 9999) # response_code = CCP().send_template_sms('18516952650', ['1234', 5], 1) # if response_code != 0: # return jsonify(errno=RET.THIRDERR,errmsg=error_map[RET.THIRDERR]) #保存短信验证码 try: sr.set("sms_code_id" + mobile, rand_num, ex=SMS_CODE_REDIS_EXPIRES) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmgs=error_map[RET.DBERR]) current_app.logger.info("短信验证码:%s" % rand_num) # json返回 return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])
def register(): # 获取参数 sms_code = request.json.get("sms_code") mobile = request.json.get("mobile") password = request.json.get("password") # 校验参数 if not all([sms_code, mobile, password]): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 校验手机号格式 if not re.match(r"^1[345678]\d{9}$", mobile): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 校验短信验证码 try: real_sms_code = sr.get("sms_code_id_" + mobile) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) if real_sms_code != sms_code: return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 保存用户信息 try: user = User() user.mobile = mobile user.nick_name = mobile # 封装加密过程 user.password = password # 记录最后登陆时间 user.last_login = datetime.now() db.session.add(user) db.session.commit() except BaseException as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) # 状态保持 session["user_id"] = user.id # 返回结果 return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])
def get_sms_code(): """获取短信验证码""" # 获取参数 img_code_id = request.json.get('img_code_id') mobile = request.json.get('mobile') img_code = request.json.get('img_code') # 校验参数 if not all([img_code_id, mobile, img_code]): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 校验手机号格式 if not re.match('^(13\d|14[5|7]|15\d|166|17[3|6|7]|18\d)\d{8}$', mobile): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 根据img_code_id取出redis里的img_code_text try: real_img_code = sr.get("img_code_id_"+img_code_id) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) # 判断验证码是否过期 if not real_img_code: return jsonify(errno=RET.PARAMERR, errmsg="验证码过期") # 判断验证码是否一致 if img_code.upper() != real_img_code: return jsonify(errno=RET.PARAMERR, errmsg="验证码错误") # 检验 手机号是否已存在 user = User.query.filter_by(mobile=mobile).first() if user: return jsonify(errno=RET.DATAEXIST, errmsg=error_map[RET.DATAEXIST]) # 根据结果发送短信 sms_code = "%04d" % random.randint(0, 9999) current_app.logger.info('短信验证码为:%s' % sms_code) # 注意: 测试的短信模板编号为1 # CCP().send_template_sms(mobile, [sms_code, 5], 1) # 将短信验证码存入redis try: sr.set('sms_code_id_' + mobile, sms_code, ex=60) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) # 返回结果给前端 return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])
def register(): # 获取参数 mobile = request.json.get('mobile') password = request.json.get('password') sms_code = request.json.get('sms_code') # 校验参数 if not all([mobile, password, sms_code]): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 校验手机号码格式 if not re.match(r'1[35678]\d{9}$', mobile): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 根据手机号取出短信验证码 try: real_sms_code = sr.get('sms_code_id' + mobile) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) # 校验短信验证码 if not real_sms_code: return jsonify(errno=RET.PARAMERR, errmsg='短信验证码已过期') if real_sms_code != sms_code: return jsonify(errno=RET.PARAMERR, errmsg='验证码错误') # 保存数据 user = User() user.mobile = mobile user.password = password user.nick_name = mobile # 记录最后登录时间 user.last_login = datetime.now() try: db.session.add(user) db.session.commit() except BaseException as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) # 状态保持(免密码登录) session["user_id"] = user.id # 返回结果 return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])
def register(): # 获取参数 mobile = request.json.get('mobile') password = request.json.get('password') sms_code = request.json.get('sms_code') # 校验参数 if not all([mobile, password, sms_code]): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 手机号校验 if not re.match(r"1[345678]\d{9}$", mobile): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 根据手机号取出短信验证码 try: real_sms_code = sr.get('sms_code_id_' + mobile) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) # 检验短信验证码 if not real_sms_code: return jsonify(errno=RET.PARAMERR, errmsg='验证码已过期') if sms_code != real_sms_code: return jsonify(errno=RET.PARAMERR, errmsg="验证码错误") # 将用户数据保存到数据库 user = User() user.mobile = mobile user.password = password user.nick_name = mobile user.last_login = datetime.now() try: db.session.add(user) db.session.commit() except BaseException as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) # 状态保持 免密码登录 session['user_id'] = user.id return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])
def get_sms_code(): # 获取参数 (手机号 图片key 图片验证码) mobile = request.json.get('mobile') img_code_id = request.json.get('img_code_id') img_code = request.json.get('img_code') # 校验参数 if not all([mobile, img_code_id, img_code]): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) if not re.match(r"1[345678]\d{9}$", mobile): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 根据图片key 取出验证码文字 try: real_img_code = sr.get('img_code_id_' + img_code_id) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) # 检验验证码是否过期 是否正确 if not real_img_code: return jsonify(errno=RET.PARAMERR, errmsg='验证码已过期') if img_code.upper() != real_img_code: return jsonify(errno=RET.PARAMERR, errmsg="验证码错误") # 校验成功 发送短信 sms_code = "%04d" % random.randint(0, 9999) current_app.logger.info('短信验证码为%s' % sms_code) res_code = CCP().send_template_sms(18715221675, [sms_code, 5], 1) print(res_code) if res_code == -1: return jsonify(errno=RET.THIRDERR, errmsg=error_map[REI.RET.THIRDERR]) # 将短信验证码保存到数据库中 try: sr.set('sms_code_id_' + mobile, sms_code, ex=60) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) # 根据短信结果使用json 返回 return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])