def get_img_code(): """ 获取图片验证码 :return: response """ # 获取参数 img_code_id = request.args.get("img_code_id") # 校验参数 if not img_code_id: return abort(403) # 生成图片验证码 img_name, img_text, img_bytes = captcha.generate_captcha() # 保存验证码文字和图片key redis 设置过期时间 try: sr.set("img_code_id_" + img_code_id, img_text, ex=300) except BaseException as e: current_app.logger.error(e) # 记录错误信息 return abort(500) # 自定义响应对象 设置响应头的content-type为image/jpeg response = make_response(img_bytes) response.content_type = "image/jpeg" return response
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 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 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 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 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_img_code(): # 获取参数 img_code_id = request.args.get('img_code_id') # 校验参数 if not img_code_id: return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 生成图片验证码 img_name, img_text, img_bytes = captcha.generate_captcha() # 保存图片key和验证码文本 try: sr.set("img_code_id" + img_code_id, img_text, ex=180) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) # 设置自定义响应头 response = make_response(img_bytes) # type:Response response.content_type = 'image/jpedg' # 返回验证码图片 return response
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 get_img_code(): """获取图片验证码""" # 获取参数 img_code_id = request.args.get('img_code_id') # 校验参数 if not img_code_id: return abort(403) # 生成图片验证码 img_code_name, img_code_text, img_code = captcha.generate_captcha() # 将图片key和验证码文字存入redis try: sr.set("img_code_id_" + img_code_id, img_code_text, ex=180) except BaseException as e: current_app.logger.error(e) return abort(500) # 返回验证码图片 response = make_response(img_code) response.content_type = 'image/jpeg' return response
def get_img_code(): # 获取参数 img_code_id = request.args.get('img_code_id') # 校验参数 if not img_code_id: return abort(403) # 生成图片验证码 img_name, img_code_text, img_code_bytes = captcha.generate_captcha() # 将图片key 和图片文字保存到数据中 try: sr.set('img_code_id_' + img_code_id, img_code_text, ex=180) except BaseException as e: current_app.logger.error(e) return abort(500) # 返回验证码图片 # 创建响应头 设置响应头 response = make_response(img_code_bytes) # 设置响应头 response.content_type = 'image/jpeg' return response
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 get_img_code(): # 获取图片验证码 # 获取参数 img_code_id = request.args.get("img_code_id") # 校验参数 if not img_code_id: return abort(404) # 生成图片验证码 img_name, img_code, img_bytes = captcha.generate_captcha() # 保存验证码和图片key , 设置过期时间,键值形式要符合要求 try: sr.set("img_code_id" + img_code_id, img_code, ex=IMAGE_CODE_REDIS_EXPIRES) except BaseException as e: current_app.logger.error(e) return abort(500) response = make_response(img_bytes) # tpye:Response response.content_type = "image/jpeg" return response
def get_img_code(): # 获取参数 img_code_id = request.args.get("img_code_id") # 校验参数 if not img_code_id: return abort(403) # 生成图片验证码 img_name, img_text, img_bytes = captcha.generate_captcha() # 保存图片key和验证码文字 redis 方便设置过期时间 try: sr.set("img_code_id" + img_code_id, img_text, ex=180) except BaseException as e: current_app.logger.error(e) return abort(500) # 设置自定义的响应头, 必须手动创建响应对象 response = make_response(img_bytes) # type: Response response.content_type = "image/jpeg" # 返回验证码图片 return response
def get_img_code(): # 获取参数 img_code_id = request.args.get("img_code_id") # 拿到图片的key # 校验参数 if not img_code_id: return abort(403) # 如果参数错误,那么直接返回403 # 生成图片验证码 img_name, img_code_text, img_code_bytes = captcha.generate_captcha() # 将图片key和验证码文字保存到数据库中 try: sr.set("img_code_id" + img_code_id, img_code_text, ex=180) except BaseException as e: # 将错误信息保存到日志中 current_app.logger.error(e) return abort(500) # 返回验证码图片 response = make_response(img_code_bytes) # type:Response response.content_type = "image/jpeg" return response
def get_img_code(): # 获取参数 img_code_id = request.args.get("img_code_id") # 校验参数 if not img_code_id: return abort(403) # 生成图片验证码 img_name, img_code, img_bytes = captcha.generate_captcha() # 保存图图片验证码 try: sr.set("img_code_id_" + img_code_id, img_code, ex=IMAGE_CODE_REDIS_EXPIRES) except BaseException as e: current_app.logger.error(e) return abort(500) # 返回图片 自定义响应对象 response = make_response(img_bytes) # type:Response response.content_type = "image/jpeg" # 返回数据 return response
def get_img_code(): # 获取参数-图片key img_code_id = request.args.get('img_code_id') # 校验参数 if not img_code_id: return abort(403) # 生成图片验证码 name, img_code_text, img_code_bytes = captcha.generate_captcha() # 将图片验证码文字和图片key保存导数据库 try: sr.set('img_code_id_' + img_code_id, img_code_text, ex=300) except BaseException as e: current_app.logger.error(e) return abort(500) # 创建自定义响应对象(包含图片) response = make_response(img_code_bytes) # type:Response response.content_type = 'image/jpeg' # 返回对象 return response
def get_img_code(): """获取图片验证码""" # 获取参数---验证码图片id img_code_id = request.args.get("img_code_id") # 校验参数 if not img_code_id: return abort(403) # 生成图片验证码 name, img_code_text, img_code_bytes = captcha.generate_captcha() # 将图片key和验证码文字保存都数据库中 try: sr.set("img_code_id" + img_code_id, img_code_text, ex=300) except BaseException as e: current_app.logger.error(e) return abort(500) # 创建自定义相应对象 response = make_response(img_code_bytes) # type:Response response.content_type = "image/jpeg" # 返回图片 return response
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])
def index(): # sr.set("name", "zs") # session["name"] = "ls" sr.set("name", "zs") return 'index11111'