我把生成的图片验证码按编号保存在redis中,然后按编号从redis中取值,但是返回一直为None,但是如果用redis_store.get("对应的id")就能取到值
#coding:utf8
from . import api
from ihome.utils.captcha.captcha import captcha
from ihome import redis_store,constants,db
from ihome.models import User
from ihome.utils.response_code import RET
from ihome.libs.yuntongxun.sms import CCP
from flask import current_app,jsonify,make_response,request
import random
@api.route("/image_codes/<image_code_id>")
def get_image_code(image_code_id):
"""
获取图片验证码
:param image_code_id: 图片验证码编号
:return: 正常:验证码图片 异常:返回json
"""
# 业务逻辑处理
# 生成验证码图片
# 名字,真实文本, 图片数据
name,text,image_data=captcha.generate_captcha()
# 将验证码真实值与编号保存到redis中, 设置有效期
# redis: 字符串 列表 哈希 set
# "key": xxx
# 使用哈希维护有效期的时候只能整体设置
# "image_codes": {"id1":"abc", "":"", "":""} 哈希 hset("image_codes", "id1", "abc") hget("image_codes", "id1")
# 单条维护记录,选用字符串
# "image_code_编号1": "真实值"
# "image_code_编号2": "真实值"
# redis_store.set("image_code_%s"%image_code_id,text)
# redis_store.expire("image_code_%s"%image_code_id,constants.IMAGE_CODE_REDIS_EXPIRES)
# 记录名字 有效期 记录值
try:
redis_store.setex("image_code_%s"%image_code_id,constants.IMAGE_CODE_REDIS_EXPIRES,text)
except Exception as e:
current_app.logger.error(e)
return jsonify(errno=RET.DATAERR,errmsg="save msg id error")
#返回图片
resp=make_response(image_data)
resp.headers["Content-Type"]="image/jpg"
return resp
@api.route("/sms_codes/<re(r'1[34578]\d{9}'):mobile>")
def get_sms_code(mobile):
"""获取短信验证码 """
#获取参数
image_code=request.args.get("image_code")
image_code_id=request.args.get("image_code_id")
#校验参数
if not all([image_code_id,image_code]):
#表示参数不完整
return jsonify(errno=RET.PARAMERR,errmsg="incomplete parameter")
#业务逻辑处理
#从redis中取出真实的图片验证码
try:
real_image_code = redis_store.get("image_code_%s"%image_code_id)
print real_image_code
except Exception as e:
current_app.logger.error(e)
return jsonify(errno=RET.DBERR,errmsg="database error")
#判断图片验证码是否过期
if real_image_code is None:
return jsonify(errno=RET.NODATA,errmsg="image not exit or overdue")
# 删除redis中的图片验证码,防止用户使用同一个图片验证码验证多次
try:
redis_store.delete("image_code_%s" % image_code_id)
except Exception as e:
current_app.logger.error(e)
#与用户填写的值进行对比
if real_image_code.lower() != image_code.lower():
return jsonify(errno=RET.DATAERR,errmsg="image error")
# 判断对于这个手机号的操作,在60秒内有没有之前的记录,如果有,则认为用户操作频繁,不接受处理
try:
send_flag = redis_store.get("send_sms_code_%s" % mobile)
except Exception as e:
current_app.logger.error(e)
else:
if send_flag is not None:
# 表示在60秒内之前有过发送的记录
return jsonify(errno=RET.REQERR, errmsg=u"请求过于频繁,请60秒后重试")
#判断手机号是否存在
try:
user=User.query.filter_by(mobile=mobile).first()
except Exception as e:
current_app.logger.error(e)
else:
if user is not None:
#表示手机号已经存在
return jsonify(errno=RET.DATAEXIST,errmsg="mobile is exist")
#如果手机号不存在则生成短信验证码
sms_code="%06d"%random.randint(0,999999)
#保存真实的短信验证码
try:
redis_store.setex("sms_code_%s"%mobile,constants.MSG_CODE_REDIS_EXPIRES,sms_code)
redis_store.setex("send_sms_code_%s"%mobile,constants.SEND_MSG_CODE_INTERNAL,1)
except Exception as e:
current_app.logger.error(e)
jsonify(errno=RET.DBERR,errmsg="save code error")
#发送短信
try:
ccp=CCP()
result=ccp.send_template_sms(mobile,[sms_code,int(constants.MSG_CODE_REDIS_EXPIRES/60)],1)
except Exception as e:
current_app.logger.error(e)
return jsonify(errno=RET.THIRDERR,errmsg="send abnormal")
#返回值
if result==0:
return jsonify(errno=RET.OK,errmsg="send success")
else:
return jsonify(errno=RET.SERVERERR,errmsg="send failed")