Exemplo n.º 1
0
def generate_image_code(image_code_id):
    """
        生成图片验证码:
        1/调用captcha扩展包,生成图片验证码,name,text,image
        2/保存图片验证码的内容,使用redis数据库
        3/返回前端图片
        4/需要设置响应的类型
        :param image_code_id:
        :return:
        """
    # 调用扩展生成图片验证码
    name, text, image = captcha.generate_captcha()
    # with open('1.jpg', 'a') as obj:
    #     obj.write(image)

    try:
        # 保存图片验证码到redis数据库,保存格式为:imagecode_image_code_id, 设置有效期
        redis_store.setex('imagecode_' + image_code_id,
                          constants.IMAGE_CODE_REDIS_EXPIRES, text)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errcode=RET.DBERR, errmsg='保存图片验证码失败')
    else:
        response = make_response(image)
        response.headers['Content-Type'] = 'image/jpg'
        # 返回结果
        return response
Exemplo n.º 2
0
def get_image_code(image_code_id):
    """
    获取图片验证码
    :param image_code_id: 图片验证码编码
    :return: 正常:验证码图片  异常:返回json数据
    """
    # 生成验证码图片
    # 名字 真实文本 图片数据
    name, text, image_data = captcha.generate_captcha()

    # 将验证码真实值与编号保存到redis中,设置有效期
    # 使用这个setex可以直接将数据和有效期一起存储
    #                      记录名字                          过期时间         记录值

    try:
        redis_store.setex('image_code_%s' % image_code_id, IAMGE_CODE_ID_TIME,
                          text)
    except Exception as e:
        # 返回错误信息,对应的状态信息
        return jsonify(errno=RET.DBERR, errmsg='保存验证码图片失败')

    # 返回图片,设置Content-Type,表示为一个图片
    resp = make_response(image_data)
    resp.headers['Content-Type'] = 'image/jsp'
    return resp
Exemplo n.º 3
0
def get_image_code(image_code_id):
    """提供图片验证码"""
    # 业务处理
    # 生成验证码图片 名字 验证码真实值 图片二进制内容
    name, text, image_data = captcha.generate_captcha()
    try:
        # 保存验证码的真实值和编号
        # redis_store.set("image_code_%s"%image_code_id,text)
        # 保存验证码的有效期
        # redis_store.expires("image_code_%s"%image_code_id,constants.IMAGE_CODE_REDIS_EXPIRES)

        # 保存验证码真实值和编号,有效期 setex()
        redis_store.setex("image_code_%s" % image_code_id, constants.IMAGE_CODE_REDIS_EXPIRES, text)
    except Exception as e:
        # 出现异常则在日志中记录异常
        current_app.logger.error(e)
        resp = {
            "errno": RET.DBERR,
            "errmsg": "保存验证码失败"
            # "errmsg":"Failed to save the verification code"
        }
        return jsonify(resp)
    # 返回验证码图片
    # 返回的Content-Type 为 image/jpg
    resp = make_response(image_data)
    resp.headers["Content-Type"] = "image/jpg"
    return resp
Exemplo n.º 4
0
def get_area_info():
    """获取城区信息"""
    # 先从redis中尝试获取城区信息
    try:
        areas_json = redis_store.get("area_info")
    except Exception as e:
        current_app.logger.error(e)
        areas_json = None

    if areas_json is None:
        # 查询数据库获取城区信息
        try:
            areas_list = Area.query.all()
        except Exception as e:
            current_app.logger.error(e)
            return jsonify(error_code=RET.DBERR, errmsg="查询城区信息异常")

        # 遍历列表,处理每一个对象,转换一下对象的属性名
        areas = []
        for area in areas_list:
            areas.append(area.to_dict())

        # 将数据转换为json
        areas_json = json.dumps(areas)
        # 将数据在redis中保存一份缓存
        try:
            redis_store.setex("area_info", constants.AREA_INFO_REDIS_EXPIRES, areas_json)
        except Exception as e:
            current_app.logger.error(e)

    return '{"error_code": 0, "errmsg": "查询城区信息成功", "data": {"areas": %s}}' % areas_json, 200, \
           {"Content-Type": "application/json"}
Exemplo n.º 5
0
def get_areas_info():
    try:
        areas_info = redis_store.get('areas_info')
    except Exception as e:
        current_app.logger.error(e)
    else:
        if areas_info:
            current_app.logger.info('hit redis areas_info')
            return areas_info, 200, {"Content-Type": "application/json"}
    try:
        areas = Area.query.all()
    except Exception as e:
        return jsonify(errno=RET.DBERR, errmsg='数据库异常')
    area_list = []
    for area in areas:
        area_list.append({
            'aid': area.id,
            'aname': area.name,
        })
    resp_dict = dict(errno=RET.OK, errmsg="OK", data=area_list)
    resp_json = json.dumps(resp_dict)
    try:
        redis_store.setex("areas_info", constants.AREA_INFO_REDIS_EXPIRES,
                          resp_json)
    except Exception as e:
        current_app.logger.error(e)

    return resp_json, 200, {"Content-Type": "application/json"}
Exemplo n.º 6
0
def get_area_info():
    """获取城区信息"""
    # 从redis中读取数据
    try:
        resp_json = redis_store.get('area_info')
    except Exception as e:
        pass
    else:
        if resp_json is not None:
            return resp_json, 200, {
                'Content-Type': 'application/json;charset=UTF-8'
            }

    # 查询数据库,读取城区信息
    try:
        # 获取所有的城区信息
        area_li = Area.query.all()
    except Exception as e:
        return jsonify(errno=RET.DBERR, errmsg='数据库异常')
    # 因为要传入前端以json格式,只有dict能转换成json,所以将对象转换成字典
    area_dict_li = []
    for area in area_li:
        d = {"aid": area.id, "aname": area.name}
        area_dict_li.append(d)

    # 将数据转换成json格式
    resp_dict = dict(errno=RET.OK, data=area_dict_li)
    resp_json = json.dumps(resp_dict)
    redis_store.setex('area_info', AREA_TIME, resp_json)

    return resp_json, 200, {'Content-Type': 'application/json;charset=UTF-8'}
Exemplo n.º 7
0
def get_image_code(image_code_id):
    u'''
    获取图片验证码
    :param image_code_id: 前端生成的图片编码
    :return: 正常:image, 异常:json
    '''
    # 业务逻辑处理
    # 生成图片
    name, text, image = captcha.generate_captcha()

    # 存入redis: 图片真实文本和编号,采用键值对的方式存储
    # redis_store.set('image_%s' % image_code_id, text)
    # redis_store.expire('image_%s' % image_code_id, 180)
    try:
        redis_store.setex('image_code_%s' % image_code_id,
                          constants.IMAGE_CODE_REDIS_EXPIRE, text)
    except Exception as e:
        # 记录日志
        current_app.logger.error(e)
        # 返回错误信息
        data = {
            'errno': RET.DBERR,
            'errmsg': 'cannot save image code in redis'
        }
        return jsonify(**data)

    # 返回图片
    resp = make_response(image)
    resp.headers['Content-Type'] = 'image/jpg'

    return resp
Exemplo n.º 8
0
def get_image_code(image_code_id):
    """
    获取图片验证码
    :params image_code_id: 图片验证码编号
    :return:
    """
    #业务逻辑处理
    #生成验证码图片
    #将验证码真实值与编号保存到redis中
    #返回图片
    #名字 真实文本 图片数据
    name, text, image_data = captcha.generate_captcha()
    # redis_store.set("image_code_%s" % image_code_id, text)
    # redis_store.expire("image_code_%s" %image_code_id, constants.IMAGE_CODE_REDIS_EXPIRES)
    #redis值:记录名、有效期、记录值
    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(error=RET.DBERR, errmsg="save image code failed.")

    resp = make_response(image_data)
    resp.headers["Content-Type"] = 'image/jpg'
    return resp
Exemplo n.º 9
0
def get_area_info():
	"""获取城区信息"""
	# 尝试从redis中读取数据
	try:
		resp_json = redis_store.get("area_info")
	except Exception as e:
		current_app.logger.error(e)
	else:
		if resp_json is not None:
			# redis有缓存数据
			current_app.logger.info("hit redis area_info")
			return resp_json, 200, {"Content-Type": "application/json"}
	# 查询数据库,读取城区信息
	try:
		area_li = Area.query.all()
	except Exception as e:
		current_app.logger.error(e)
		return jsonify(errno=RET.DBERR, errmsg="数据库异常")

	area_dict_li = []
	# 将对象转换为字典
	for area in area_li:
		area_dict_li.append(area.to_dict())

	# 将数据转换为json字符串
	resp_dict = dict(errno=RET.OK, errmsg="OK", data=area_dict_li)
	resp_json = json.dumps(resp_dict)

	# 将数据保存到redis中
	try:
		redis_store.setex("area_info", constants.AREA_INFO_REDIS_CACHE_EXPIRES, resp_json)
	except Exception as e:
		current_app.logger.error(e)

	return resp_json, 200, {"Content-Type": "application/json"}
Exemplo n.º 10
0
def get_image_code(image_code_id):
    """获取图片验证码, 返回验证码图片"""
    # 1. 获取参数  url中已经获取

    # 2. 校验参数   参数存在,无其他校验

    # 3. 业务逻辑
    # 3.1 生成验证码图片
    # 名字  真实文本  图片数据
    name, text, image_data = captcha.generate_captcha()
    # 3.2 将图片和编号存储到redis
    # redis_store.set("image_code_%s" % image_code_id, text)
    # redis_store.expire("image_code_%s" % image_code_id, IMAGE_CODE_REDIS_EXPIRES)
    try:  # 防止redis连接中断
        redis_store.setex("image_code_%s" % image_code_id,
                          IMAGE_CODE_REDIS_EXPIRES, text)
    except Exception as e:
        # 记录日志
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="save image_code _id failed")

    # 4. 返回图片
    resp = make_response(image_data)
    resp.headers["Content-Type"] = "image/jpg"
    return resp
Exemplo n.º 11
0
def get_image_code(image_code_id):
    """提供图片验证码"""
    # 业务处理
    # 生成验证码图片
    # 名字, 验证码真实值,图片的二进制内容
    name, text, image_data = captcha.generate_captcha()

    try:
        # 保存验证码的真实值与编号
        # redis_store.set("image_code_%s" % image_code_id, text)
        # redis_store.expires("image_code_%s" % image_code_id, constants.IMAGE_CODE_REDIS_EXPIRES)
        # 设置redis的数据与有效期
        redis_store.setex("image_code_%s" % image_code_id,
                          constants.IMAGE_CODE_REDIS_EXPIRES, text)
    except Exception as e:
        # 在日志中记录异常
        current_app.logger.error(e)
        resp = {
            "errno": RET.DBERR,
            # "errmsg": "save image code failed"
            "errmsg": "保存验证码失败h"
        }
        return jsonify(resp)

    # 返回验证码图片
    resp = make_response(image_data)
    resp.headers["Content-Type"] = "image/jpg"
    return resp
Exemplo n.º 12
0
def get_area_info():
    """获取区域信息"""

    # 从redis中获取数据
    try:
        resp_json = redis_store.get('area_info')
    except Exception as e:
        current_app.logger.error(e)
    else:
        if resp_json is not None:
            current_app.logger.info('hide redis area_info')
            return resp_json, 200, {"Contant-Type": 'application/json'}
    # 查新数据库
    try:
        area_li = Area.query.all()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='数据库异常')
    area_dict_li = []
    for area in area_li:
        area_dict_li.append(area.to_dict())

    # 将数据转换为json格式
    resp_dict = dict(errno=RET.OK, errmsg='OK', data=area_dict_li)
    resp_json = json.dumps(resp_dict)

    # 将数据保存到redis中
    try:
        redis_store.setex('area_info', constants.AREA_INFO_REDIS_CACHE_EXPIRE,
                          resp_json)
    except Exception as e:
        current_app.logger.error(e)

    return resp_json, 200, {"Contant-Type": 'application/json'}
Exemplo n.º 13
0
def get_image_code(image_code_id):
    """
    获取图片验证码
    :params image_code_id: 图片验证码编号
    :return: 正常情况下返回验证码图片, 异常返回json
    """

    # 生成图片验证码
    # 名字 真是文本,图片数据
    name, text, image_data = captcha.generate_captcha()
    # 将验证码真实值与编号保存到redis中, 设置有效期
    # redis: 字符串 列表 哈希 set
    # 使用哈希维护有效期的时候只能整体设置
    # 单条维护记录使用字符串
    # "image_code_编号1": "真实值"
    # "image_code_编号2": "真实值"
    # redis保存数据
    # redis_store.set("image_code_%s" % image_code_id, text)
    # 设置有效期
    # redis_store.expire("image_code_%s" % image_code_id, constants.IMAGE_CODE_REDIS_EXPIRES)
    # redis中一步保存数据并设置有效期:  记录名字, 有效期, 记录值
    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.DBERR, errmsg="save image code id failed!")

    # 返回图片
    resp = make_response(image_data)
    resp.headers["Content-Type"] = "image/jpg"
    return resp
Exemplo n.º 14
0
def get_area_info():
    """城区信息"""
    # 查询数据库,读取城区信息
    try:
        resp_json_str = redis_store.get("area_info")
    except Exception as e:
        current_app.logger.error(e)
    else:
        if resp_json_str is not None:
            current_app.logger.info("hit redis area")
            return resp_json_str, 200, {"Content-Type": "application/json"}

    try:
        area_li = Area.query.all()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="获取地区信息异常")

    area_dict_li = []
    # 将对象庄为[]
    for area in area_li:
        area_dict_li.append(area.to_dict())

    resp_dict = dict(errno=RET.OK, errmsg="OK", data=area_dict_li)
    resp_json_str = json.dumps(resp_dict)
    try:
        redis_store.setex("area_info", constants.AREA_INFO_REDIS_CACHE_EXPIRES, resp_json_str)
    except Exception as e:
        current_app.logger.error(e)

    return resp_json_str, 200, {"Content-Type": "application/json"}
Exemplo n.º 15
0
def get_image_code():
    """
    获取图片验证码
    :param image_code_id: 图片验证码编号
    :return: 正常:验证码图片  异常:返回json
    """
    # 获取参数
    # 检验参数
    # 业务逻辑处理,生成验证码操作
    # 名字 真实文本 图片数据

    image_code_id = request.args.get("image_code_id")
    name, text, image_data = captcha.generate_captcha()
    # 将验证码真实值与编号存到redis中
    # 单条维护记录,选用字符串
    # image_codes_编号1: "真实值"
    # image_codes_编号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.DBERR, errmsg="保存图片验证码信息失败")

    # 返回图片
    resp = make_response(image_data)

    resp.headers["Content-Type"] = "image/jpg"
    return resp
Exemplo n.º 16
0
def get_image_code(image_code_id):
    """获取图片验证码
    : params image_code_id: 图片验证码编号
    :return :验证码图片
    """
    # 业务逻辑处理
    # 生成验证码图片
    # 名字,真实文本,图片数据
    name, text, image_data = captcha.generate_captcha()

    # 将验证码真实值与编导保存到redis中,设置有效期
    # redis的数据类型: 字符串 列表 哈希 set(集合)
    # 使用哈希维护有效期的时候只能整体设置
    # "image_codes": {"id1":"abc","id2":"123"} hset{"image_codes","id1","abc"}#设置值 hget("id1")#取值

    # 单条维护记录,选用字符串
    # "image_code_id1":"真实值"
    # 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.DBERR, errmsg="保存图片验证码失败")
        return jsonify(errno=RET.DBERR, errmsg="save image code id failed")

    # 返回图片
    resp = make_response(image_data)
    resp.headers["Content-Type"] = "image/jpg"
    return resp
Exemplo n.º 17
0
def get_area_info():
    """获取城区信息"""
    # 先从redis中取数据,如果没有,再去mysql中取数据

    try:
        resp_json = redis_store.get("area_info")
    except Exception as e:
        current_app.logger.error(e)
    else:
        if resp_json:
            current_app.logger.info("hit redis area_info")
            return resp_json, 200, {"Content-Type": "application/json"}

    try:
        area_li = Area.query.all()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="mysql数据库错误")
    area_dict = []
    for area in area_li:
        area_dict.append(area.to_dict())

    resp_dict = dict(errno=RET.OK, errmsg="OK", data=area_dict)
    resp_json = json.dumps(resp_dict)

    # 将城区信息存入到redis中
    try:
        redis_store.setex("area_info", constants.AREA_INFO_REDIS_CACHE_EXPIRES,
                          resp_json)
    except Exception as e:
        current_app.logger.error(e)

    return resp_json, 200, {"Content-Type": "application/json"}
Exemplo n.º 18
0
def get_area_info():
    '''获取城区信息'''

    # 尝试从redis中获取缓存
    try:
        resp_json = redis_store.get('area_info').decode()
    except Exception as e:
        current_app.logger.error(e)
    else:
        if resp_json:
            print('hit redis area info')
            return resp_json, 200, {"Content-Type": "application/json"}

    # 查询数据库,读取城区信息
    try:
        area_li = Area.query.all()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="数据库异常")

    # 组织数据
    area_dict_li = list()
    for area in area_li:
        area_dict_li.append(area.to_dict())

    # 将数据保存到redis中,作为缓存使用,将响应结果直接转换为json字符串
    resp_dict = dict(errno=RET.OK, errmsg="获取成功", data=area_dict_li)
    resp_json = json.dumps(resp_dict)
    try:
        redis_store.setex('area_info', constants.AREA_INFO_REDIS_EXPIRE,
                          resp_json)
    except Exception as e:
        current_app.logger.error(e)

    return resp_json, 200, {"Content-Type": "application/json"}
Exemplo n.º 19
0
def get_image_code(image_code_id):
    """提供图片验证码"""
    # 业务处理
    # 生成验证码图片
    # 验证码名字, 真实值, 二进制数据
    name, text, image_data = captcha.generate_captcha()

    try:
        # 保存验证码的真实值与编号
        # redis_store.set("image_code_%s" % image_code_id, text)
        # redis_store.expires("image_code_%s" % image_code_id, constants.IMAGE_CODE_REDIS_EXPIRES)
        redis_store.setex("image_code_%s" % image_code_id, constants.IMAGE_CODE_REDIS_EXPIRES, text)
    except Exception as e:
        # 在日志中记录异常
        current_app.logger.error(e)
        resp = {
            "error_code": RET.DBERR,
            "errmsg": "保存验证码失败"
        }
        return jsonify(resp)

    # 返回验证码图片
    resp = make_response(image_data)
    resp.headers["Content-Type"] = "image/jpg"
    return resp
Exemplo n.º 20
0
def get_image_code(image_code_id):
    """提供图片验证码"""

    # 提取参数  image_code_id(不用提取用和校验)
    # 生成验证码图片
    # 名字,真实验证码 验证码图片数据
    name, text, image_data = captcha.generate_captcha()

    # 保存验证码的真实值,和这个验证码的编号,redis中,有效期
    # redis数据类型:字符串,列表,hash,set...
    # key:val 采用字符串
    # "image_code_编号1":"真实值"

    # redis_store.set(key,value)
    # 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(errcode=RET.DBERR, errmsg="数据库异常")
    # 返回验证码图片
    return image_data, 200, {"Content-Type": "image/jpg"}
Exemplo n.º 21
0
def generate_image_code(image_code_id):
    """
    生成图片验证码
    1/调用captcha扩展包,生成图片验证码,name,text,image
    2/把图片验证码存入redis缓存中,设置过期时间
    3/返回图片验证码,需要设置响应类型
    :param image_code_id:
    :return:
    """
    # 调用captcha扩展包,生成图片验证码
    name, text, image = captcha.generate_captcha()
    # 把图片验证码存入redis数据库中
    try:
        redis_store.setex('ImageCode_' + image_code_id,
                          constants.IMAGE_CODE_REDIS_EXPIRES, text)
    except Exception as e:
        # 记录错误日志信息
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='保存图片验证码失败')
    else:
        # 返回前端图片
        response = make_response(image)
        # 需要设置响应类型
        response.headers['Content-Type'] = 'image/jpg'
        # 返回结果
        return response
Exemplo n.º 22
0
def get_image_code(image_code_id):
    """
    获取图片验证码
    :param: image_code_id:  图片验证编号
    :return:    正常:验证码图片   异常: 返回json
    """
    # 业务逻辑处理
    # 生成验证码图片
    # 名字,真实文本,图片数据
    name, text, image_data = captcha.generate_captcha()

    # 将验证码真实值与编号保存到redis中,设置有效期 3分钟
    # redis:  字符串 列表 哈希(键值对) set
    # "key": xxx  "image_codes": {"编号1": "文本1", "编号2": "文本2"}
    # 哈希 hset("image_codes", "id1", "abc")  hget("image_codes", "id1")
    # 使用哈希维护有效期的时候只能整体设置 有效期结束 就整条记录删除 pass
    # 单条维护记录 选用字符串      "image_code_编号": "真实值"
    # 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.DBERR, errmsg="save image code id failed")
        return jsonify(errno=RET.DBERR, errmsg="保存图片验证码失败")

    # 返回图片
    resp = make_response(image_data)
    resp.headers["Content-Type"] = "image/jpg"
    return resp
Exemplo n.º 23
0
def get_image_code(image_code_id):
    '''
    获取图片验证码
    :param image_code_id: 图片验证码编号
    :return: 正常:验证图片    异常:返回json
    '''
    # 业务逻辑处理
    # 生成验证码图片
    # 名字  真实文本  图片数据
    name, text, image_data = captcha.generate_captcha()
    # 将验证码真实值与编号保存到redis中,使用set集合
    image_code_id_number = 'image_code_%s' % image_code_id
    # redis_store.set(image_code_id_number, text)
    # redis_store.expire(image_code_id_number, constants.IMAGE_CODE_REDIS_EXPIRE)
    # 以上两行代码可以整合成以下一行,setex方法中既可以设置对应的键值对,还可以设置有效期
    try:
        #                       记录名字                有效期                         记录值
        redis_store.setex(image_code_id_number, constants.IMAGE_CODE_REDIS_EXPIRE, text)
    except Exception as e:
        # 记录日志
        current_app.logger.error(e)
        # return jsonify(errorno=RET.DBERR, errmsg='save image code id failed')
        return jsonify(errorno=RET.DBERR, errmsg='保存图片验证码失败')
    # 返回图片
    resp = make_response(image_data)
    resp.headers['Content-Type'] = 'image/jpg'
    return resp
Exemplo n.º 24
0
def get_houses_index():
    """
    项目首页幻灯片
    缓存----磁盘----缓存
    1/尝试查询redis数据库,获取项目首页信息
    2/判断查询结果
    3/如果有数据,留下访问的记录,直接返回
    4/查询mysql数据库
    5/默认采取房屋成交次数最高的五套房屋
    houses = House.query.order_by(House.order_count.desc()).limit(5)
    6/判断查询结果
    7/定义容器存储查询结果
    8/遍历查询结果,判断房屋是否有主图片,如未设置默认不添加
    9/调用模型类中方法house.to_basic_dict()
    10/把房屋数据转成json字符串
    11/存入redis缓存中
    12/返回结果
    :return:
    """
    # 尝试从redis中获取后人幻灯片信息
    try:
        ret = redis_store.get("home_page_data")
    except Exception as e:
        current_app.logger.error(e)
        ret = None
    # 判断获取结果
    if ret:
        # 留下访问redis数据库的记录
        current_app.logger.info("hit redis houses index info")
        return '{"errno":0,"errmsg":"OK","data":%s}' % ret

    # 未获取,查询mysql数据库
    try:
        # 默认按房屋成交次数排序,使用limit分页5条房屋数据
        houses = House.query.order_by(House.order_count.desc()).limit(
            constants.HOME_PAGE_MAX_HOUSES)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="查询房屋数据失败")
    # 判断查询结果
    if not houses:
        return jsonify(erno=RET.NODATA, errmsg="无房屋数据")
    # 定义容器
    houses_list = []
    for house in houses:
        # 如果房屋为设置主图片,默认不添加
        if not house.index_image_url:
            continue
        houses_list.append(house.to_basic_dict())
    # 把房屋数据转换成json
    houses_json = json.dumps(houses_list)
    # 存入redis中
    try:
        redis_store.setex("home_page_data",
                          constants.HOME_PAGE_DATA_REDIS_EXPIRES, houses_json)
    except Exception as e:
        current_app.logger.error(e)
    # 构造响应报文,返回结果
    resp = '{"errno":0,"errmsg":"OK","data":%s}' % houses_json
    return resp
Exemplo n.º 25
0
def get_house_index():
    """获取首页房屋信息"""
    # 从缓存中获取首页房屋信息
    try:
        json_dict = redis_store.get("index_house")
    except Exception as e:
        current_app.logger.error(e)

    if json_dict:
        return json_dict, 200, {"Content-Type": "application/json"}

    try:
        houses = House.query.order_by(House.order_count.desc()).limit(
            constants.INDEX_MAX_AMOUNT)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="数据库异常")

    house_list = []
    for house in houses:
        if not house.index_image_url:
            continue
        house_list.append(house.to_basic_dict())

    house_dict = dict(errno=RET.OK, errmsg="获取数据成功", data=house_list)
    json_dict = json.dumps(house_dict)

    # 将首页信息保存到缓存中
    try:
        redis_store.setex("index_house", constants.INDEX_HOUSE_SAVE_TIME,
                          json_dict)
    except Exception as e:
        current_app.looger.error(e)

    return json_dict, 200, {"Content-Type": "application/json"}
Exemplo n.º 26
0
def get_image_code(image_code_id):
    """
    获取图片验证码
    :param image_code_id: 图片验证码编号
    :return: 正常:验证码图片   异常:json数据
    """
    # 业务逻辑处理
    # 生成图片验证码
    # 名字,真实文本,图片数据
    name, text, image_data = captcha.generate_captcha()
    # 将验证码真实值与编号保存到Redis中,设置有效期
    # 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.DBERR, errmsg="保存图片验证码信息失败")

    # 返回图片
    resp = make_response(image_data)
    resp.headers["Content-Type"] = "image/jpg"
    return resp
Exemplo n.º 27
0
def get_area_info():
    #从redis信息
    try:
        resp_json=redis_store.get("area_info")
    except Exception as e:
        current_app.logger.error(e)
    else:
        if resp_json is not None:
            current_app.logger.info("从redis中读取成功")
            return resp_json,200,{"Content-Type": "application/json"}
    #从数据库查询城区信息
    all_area_list=[]
    try:
        all_area=Area.query.all()
    except Exception as  e:
        current_app.logger.error(e)
        return  jsonify(errno=RET.DBERR,errmsg="数据库查询异常")
    for area in all_area:
        #将对象转换为字典
        all_area_list.append(area.to_dict())
    #将数据转换为json
    resp_dict=dict(errno=RET.OK,errmsg="OK",data=all_area_list)
    resp_json=json.dumps(resp_dict)
    #将数据保存到redis当中
    try:
        redis_store.setex("area_info",Const.AREA_INFO_REDIS_CACHE_EXPIRES,resp_json)
    except Exception as  e:
        current_app.logger.error(e)
        return  jsonify(errno=RET.DBERR,errmsg="城区信息缓存失败")
    return resp_json,200,{"Content-Type":"application/json"}
Exemplo n.º 28
0
def get_image_code(image_code_id):
    """
    获取验证码图片
    :param image_code_id: 图片验证码编号
    :return: 如果出现异常,返回异常信息,否则,返回验证码图片
    """
    # 生成验证码图片
    # 名字,真是文本,图片数据
    name, text, image_code = captcha.generate_captcha()

    # 将编号以及验证码的真实值保存到redis(选择字符串)中,并设置有效期(自定义有效期为180秒,设置成了常量,在constants中)
    # redis_store.set('iamge_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)
        # 出现异常,返回json格式的提示
        # return jsonify(errno=RET.DBERR, errmsg="save image code failed")
        return jsonify(errno=RET.DBERR, errmsg="保存图片验证码失败")

    # 没有异常 返回验证码图片,并指定一下Content-Type(默认为test/html)类型为image,不改不认识图片
    resp = make_response(image_code)
    resp.headers['Content-Type'] = 'image/jpg'
    return resp
Exemplo n.º 29
0
def generate_image_code(image_code_id):
    """
    生成图片验证码
    1/导入使用captcha扩展包,生成图片验证码,name/text/image
    2/在服务器保存图片验证码,保存到redis中,
    3/如果保存失败,返回错误信息,记录日志current_app
    4/返回图片,使用make_response对象
    :param image_code_id:
    :return:
    """
    # 生成图片验证码
    name,text,image = captcha.generate_captcha()
    # 保存图片验证码到redis中
    try:
        redis_store.setex('ImageCode_' + image_code_id,constants.IMAGE_CODE_REDIS_EXPIRES,text)
    except Exception as e:
        # 记录错误日志信息
        current_app.logger.error(e)
        # 返回错误信息
        return jsonify(errno=RET.DBERR,errmsg='保存图片验证码失败')
    # 如果未发生异常,执行else
    else:
        # 使用响应对象返回图片
        response = make_response(image)
        # 设置响应的数据类型
        response.headers['Content-Type'] = 'image/jpg'
        # 返回结果
        return response
Exemplo n.º 30
0
def get_image_code(image_code_id):
    """
	获取图片验证码
	:return: 正常情况:验证码图片 异常:返回json
	"""
    # 业务逻辑处理
    # 生成验证码图片
    name, text, image_data = captcha.generate_captcha()
    # 将验证码图片真实值和编号保存在redis中, 设置有效期
    # redis: string list hash set
    # "key": xxx
    # 使用hash操作,只能整体操作,不符合要求
    # image_codes: {"":"", "":"", "":"",}  hash数据类型 hset("image_codes", "id1", "abc")

    # 单条维护记录
    # 使用set和expire方法进行组合使用,配置存储及过去时间
    # redis_store.set("image_code_%s" % image_code_id, text)
    # # 设置有效期
    # redis_store.expire("image_code_%s" % image_code_id, constants.IMAGE_CODE_REDIS_EXPIRES)

    # 直接使用setex方法对文本和有效期进行设置
    # redis_store.setex(mage_code_id, 有效期, text)
    try:
        redis_store.setex("image_code_%s" % image_code_id,
                          constants.IMAGE_CODE_REDIS_EXPIRES, text)
    except Exception as e:
        current_app.logger.error("redis存储图像识别码错误:%s" % e)
        return jsonify(errno=RET.DATAERR, errmsg="save image code id failed")
    # 返回图片
    resp = make_response(image_data)
    resp.headers["Content-Type"] = "image/jpg"
    # print("resp:", resp, type(resp))
    return resp
Exemplo n.º 31
0
def get_image_code(image_code_id):
    """
    获取图片验证码
    :params image_code_id: 图片验证码
    :return: 验证码图片
    """

    # 业务逻辑处理
    # 生成验证码图片

    name, text, image_data = captcha.generate_captcha()

    try:
        # 将验证码真实值与编号保存到redis中,设置有效期
        redis_store.setex(f"image_code_{image_code_id}",
                          constants.IMAGE_CODE_REDIS_EXPIRES, text)
        print("image_code_id", image_code_id)
        print("text", text)
    except Exception as e:
        # 记录日志
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="保存图片验证码失败")

    # 返回图片
    resp = make_response(image_data)
    resp.headers["Content-Type"] = "image/jpg"
    return resp
Exemplo n.º 32
0
def get_areas():
    """
        获取城区的接口,这个接口的数据会经常请求,并且变化不大,可以缓存起来
    """

    try:
        resp_json = redis_store.get('area_info')
    except Exception as e:
        current_app.logger.error(e)
    else:
        if resp_json is not None:
            current_app.logger.info('缓存中获取')
            return resp_json, 200, {'Content-Type': 'application/json'}

    try:
        areas = Area.query.all()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="数据库操作失败")

    a_dict_lis = []
    for area in areas:
        a_dict_lis.append(area.to_dict())

    # 将返回的数据转成json字符串, dict() 构建一个字典
    resp_dict = dict(errno=RET.OK, errmsg="获取城区成功", data=a_dict_lis)
    resp_json = json.dumps(resp_dict)
    # 将数据保存到 内存缓存的数据库 redis 中
    try:
        redis_store.setex("area_info", constants.AREA_INFO_REDIS_CACHE_EXPIRES,
                          resp_json)
    except Exception as e:
        current_app.logger.error(e)
    print('从数据库中获取')
    return resp_json, 200, {'Content-Type': 'application/json'}
Exemplo n.º 33
0
def get_house_detail(house_id):
    """获取房屋详情"""
    # 前端在房屋详情页面展示时,如果浏览页面的用户不是该屋的房东,则展示预定按钮,否则不展示
    # 所以需要后端返回登录用户的id
    # 尝试获取用户登录的信息,若登录,则返回给前端登录用户的id,否则返回user_id = -1
    user_id = session.get("user_id", "-1")

    # 校验参数
    if not house_id:
        return jsonify(error_code=RET.PARAMERR, errmsg="缺少参数")

    # 先从redis缓存中获取信息
    try:
        ret = redis_store.get("house_info_%s" % house_id)
    except Exception as e:
        current_app.logger.error(e)
        ret = None
    if ret:
        current_app.logger.info("hit house info redis")
        return '{"error_code": 0, "errmsg":"ok", "data":{"user_id":%s, "house":%s}}' % (user_id, ret), \
               200, {"Content-Type": "application/json"}

    # 查询数据库
    try:
        house = House.query.get(house_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(error_code=RET.DBERR, errmsg="查询数据失败")

    if not house:
        return jsonify(error_code=RET.NODATA, errmsg="房屋不存在")

    # 将房屋对象数据转换为字典
    try:
        house_data = house.to_full_dict()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(error_code=RET.DATAERR, errmsg="数据出错")

    # 存入到redis中
    json_house = json.dumps(house_data)
    try:
        redis_store.setex("house_info_%s" % house_id, constants.HOUSE_DETAIL_REDIS_EXPIRE_SECOND, json_house)
    except Exception as e:
        current_app.logger.error(e)

    resp = '{"error_code":0, "errmsg":"ok", "data":{"user_id": %s, "house": %s}}' % (user_id, json_house), \
           200, {"Content-Type": "application/json"}

    return resp
Exemplo n.º 34
0
def get_house_index():
    """获取主页幻灯片展示的房屋基本信息"""
    # 尝试从缓存中获取
    try:
        ret = redis_store.get("home_page_data")
    except Exception as e:
        current_app.logger.error(e)
        ret = None

    if ret:
        current_app.logger.info("hit house index info redis")
        # 因为redis中保存的是json字符串,所以值直接进行字符串拼接返回
        return '{"error_code": 0, "errmsg": "ok", "data": %s}' % ret, 200, {"Content-Type": "application/json"}
    else:
        try:
            # 查询数据库,返回房屋订单数目最多的5条数据
            houses = House.query.order_by(House.order_count.desc()).limit(constants.HOME_PAGE_MAX_HOUSES)
        except Exception as e:
            current_app.logger.error(e)
            return jsonify(error_code=RET.DBERR, errmsg="查询数据失败")

        if not houses:
            return jsonify(error_code=RET.NODATA, errmsg="查询无数据")

        houses_list = []
        for house in houses:
            # 如果房屋未设置主图片,则跳过
            if not house.index_image_url:
                continue
            houses_list.append(house.to_basic_dict())

        # 将数据转换为json,并保存到redis缓存
        json_houses = json.dumps(houses_list)
        try:
            redis_store.setex("home_page_data", constants.HOME_PAGE_DATA_REDIS_EXPIRES, json_houses)
        except Exception as e:
            current_app.logger.error(e)

        return '{"error_code": 0, "errmsg": "ok", "data": %s}' % json_houses, 200, {"Content-Type": "application/json"}
Exemplo n.º 35
0
def get_image_codes(image_code_id):
    # 前端传递uuid -->服务器生成验证码(文本和图片)--> 存储到redis中:image_code_id:text--> 返回图片数据
    # 生成图像验证码
    name, text, image_data = captcha.generate_captcha()
    # 存储到Redis中  设置过期时间180s setex
    # 参数 key expiretime value
    #     image_code_image_code_id 180 text
    try:
        redis_store.setex('image_code_%s' % image_code_id, constants.IMAGE_CODE_REDIS_EXPIRE, text)
    except Exception as e:
        # 记录错误日志
        logging.error(e)
        resp_dict = {
            'errno': RET.DBERR,
            'errmsg': '设置redis错误'
        }
        return jsonify(resp_dict)

    # 返回图片
    resp = make_response(image_data)
    resp.headers['Content-Type'] = 'image/jpg'
    return resp
Exemplo n.º 36
0
def generate_image_code(image_code_id):
    """
    生成图片验证码
    1.调用captcha扩展,生成图片验证码, name,text,image
    2.存储图片验证码,保存在redis中
    3.返回前端图片验证码,设置响应数据类型
    :param image_code_id:
    :return:
    """
    # 调用扩展,生成图片验证码
    name, text, image = captcha.generate_captcha()
    # 调用redis数据库,存储图片验证码内容
    try:
        redis_store.setex('ImageCode_' + image_code_id,constants.IMAGE_CODE_REDIS_EXPIRES, text)
    except Exception as e:
        # 记录错误的日志
        current_app.logger.error(e)
        # 返回前端错误信息
        return jsonify(errno=RET.DBERR, errmsg='保存图片验证码失败')
    else:
        # 返回图片
        response = make_response(image)
        response.headers['Content-Type'] = 'image/jpg'
        return response
Exemplo n.º 37
0
def get_sms_codes(mobile):
    # 获取参数
    code = request.args.get('code')
    codeId = request.args.get('codeId')

    # 参数校验
    if not all([code, codeId]):
        resp = {
            'errno': RET.DATAERR,
            'errmsg': '数据不完整'
        }
        return jsonify(resp)
    # 业务处理

    # 2 获取图片验证码
    image_code_id = 'image_code_' + codeId
    try:
        new_code = redis_store.get(image_code_id)
    except Exception as e:
        logging.error(e)
        resp = {
            'errno': RET.DBERR,
            'errmsg': '获取图片验证码失败'
        }
        return jsonify(resp)
    # 图片验证码是否有效
    if new_code is None:
        resp_dict = {
            'errno': RET.NODATA,
            'errmsg': '图片验证码过期/失效'
        }
        return jsonify(resp_dict)

    # 删除原来的验证码
    try:
        redis_store.delete(image_code_id)
    except Exception as e:
        logging.error(e)
        resp = {
            'errno': RET.DBERR,
            'errmsg': '删除图片验证码失败'
        }
        return jsonify(resp)

    # 3 对比是否一致
    if new_code.lower() != code.lower():
        # 不一致
        resp = {
            'errno': RET.PARAMERR,
            'errmsg': '图片二维码填写错误,请点击刷新后重新输入'
        }
        return jsonify(resp)

    # 1 用户名是否存在
    try:
        user = User.query.filter_by(mobile=mobile).first()
    except Exception as e:
        logging.error(e)
        resp = {
            'errno': RET.DBERR,
            'errmsg': '查找用户信息失败'
        }
        return jsonify(resp)
    if user:
        # 用户存在
        resp = {
            'errno': RET.DATAEXIST,
            'errmsg': '该用户已存在'
        }
        return jsonify(resp)
    # 创建6位验证码
    sms_code = '%06d' % random.randint(0, 999999)
    # 将短信验证码存储到Redis中  点击注册时进行对比
    try:
        redis_store.setex('sms_code_%s' % mobile, constants.SMS_CODE_REDIS_EXPIRE, sms_code)
    except Exception as e:
        logging.error(e)
        resp = {
            'errno': RET.DBERR,
            'errmsg': '保存短信验证码失败'
        }
        return jsonify(resp)
    # 发送验证码
    ccp = CCP()
    status_code = ccp.send_template_sms('18204681825', [sms_code, constants.SMS_CODE_TIME_EXPIRE], 1)
    # 返回数据
    if status_code == '000000':
        resp = {
            'errno': RET.OK,
            'errmsg': '发送短信验证码成功'
        }
        return jsonify(resp)
    else:
        resp = {
            'errno': RET.THIRDERR,
            'errmsg': '发送短信验证码失败'
        }
        return jsonify(resp)
Exemplo n.º 38
0
def send_sms_code(mobile):
    """
    1/获取参数,mobile,text,id,request.args.get('text')
    2/校验参数的完整性
    3/校验手机号,正则表达式校验手机号格式
    4/校验图片验证码,操作redis数据库,获取本地存储的真实图片验证码
    5/校验获取结果,判断真实的图片验证码是否存在
    6/删除redis中的图片验证码
    7/比较图片验证码是否一致
    8/生成短信验证码,'%06d' % random.randint(1,999999)生成随机数
    9/在本地存储短信验证码,存redis中
    10/判断手机号是否已注册
    11调用云通讯接口,发送短信,send_template_sms(mobile,sms_coce,time,tempID)
    12/保存发送结果,判断发送是否成功
    13/返回结果
    :param mobile:
    :return:
    """
    # 获取参数
    image_code = request.args.get('text')
    image_code_id = request.args.get('id')
    # 校验参数的完整性
    if not all([mobile, image_code_id, image_code]):
        return jsonify(errno=RET.PARAMERR, errmsg='参数不完整')
    # 校验手机号码的格式
    if not re.match(r'1[3456789]\d{9}', mobile):
        return jsonify(errno=RET.PARAMERR, errmsg='手机号码格式不正确')
    # 校验图片验证码,从redis中获取
    try:
        real_image_code = redis_store.get('ImageCode_' + image_code_id)
    except Exception as e:
        # 写入日志文件
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='获取图片验证码失败')
    # 校验获取结果
    if not real_image_code:
        return jsonify(errno=RET.NODATA, errmsg='图片验证码失效')
    # 删除图片验证码
    try:
        redis_store.delete('ImageCode_' + image_code_id)
    except Exception as e:
        # 写入日志文件
        current_app.logger.error(e)

    # 比较图片验证码是否一致,并忽略大小写
    if real_image_code.lower() != image_code.lower():
        return jsonify(errno=RET.PARAMERR, errmsg='图片验证码错误')

    # 开始准备发送短信验证码
    sms_code = '%06d'% random.randint(1, 999999)
    # 把验证码保存到redis中
    try:
        redis_store.setex('SMSCode_' + mobile,constants.SMS_CODE_REDIS_EXPIRES,sms_code)

    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='保存短信验证码失败')

    # 判断手机号是否已经注册
    try:
        user = User.query.filter_by(mobile=mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='获取用户的信息失败')
    else:
        # 判断用户是否已经存在
        if user is not None:
            return jsonify(errno=RET.DATAEXIS,errmsg='用户已经存在')

    # 调用云通讯接口发送短信
    try:
        cpp = sms.CCP()
        # 发送短信的模板方法会有返回值,0 表示发送成功
        result = cpp.send_template_sms(mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES/60], 1)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.THIRDERR, errmsg='发送短信异常')

    # 判断发送结果
    if 0 == result:
        return jsonify(errno=RET.OK, errmsg='发送成功')
    else:
        return jsonify(errno=RET.THIRDERR, errmsg='发送失败')
Exemplo n.º 39
0
def send_sms_code(mobile):
    """发送短信验证码"""
    # 获取参数
    image_code_id = request.args.get("image_code_id")
    image_code = request.args.get("image_code")

    # 校验参数
    if not all([image_code_id, image_code]):
        resp = {
            "error_code": RET.PARAMERR,
            "errmsg": "参数不完整"
        }
        return jsonify(resp)

    # 业务处理
    # 取出真实的图片验证码
    try:
        real_image_code = redis_store.get("image_code_%s" % image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        resp = {
            "error_code": RET.DBERR,
            "errmsg": "获取图片验证码失败"
        }
        return jsonify(resp)

    # 判断验证码的有效期
    if real_image_code is None:
        resp = {
            "error_code": RET.NODATA,
            "errmsg": "图片验证码过期"
        }
        return jsonify(resp)

    # 删除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():
        # 表示用户填写错误
        resp = {
            "error_code": RET.DATAERR,
            "errmsg": "图片验证码有误"
        }
        return jsonify(resp)

    # 判断用户手机号是否注册过
    try:
        user = User.query.filter_by(mobile=mobile).first()
    except Exception as e:
        current_app.logger.error(e)
    else:
        if user is not None:
            resp = {
                "error_code": RET.DATAEXIST,
                "errmsg": "手机号已注册过"
            }
            return jsonify(resp)

    # 创建短信验证码
    sms_code = "%06d" % random.randint(0, 999999)

    # 保存短信验证码
    try:
        redis_store.setex("sms_code_%s" % mobile, constants.SMS_CODE_REDIS_EXPIRES, sms_code)
    except Exception as e:
        current_app.logger.error(e)
        resp = {
            "error_code": RET.DBERR,
            "errmsg": "保存短信验证码异常"
        }
        return jsonify(resp)

    # 发送验证码短信
    # 通过celery发送
    # task_sms.send_template_sms.delay(mobile, [sms_code, str(constants.SMS_CODE_REDIS_EXPIRES/60)], 1)
    # result返回异步结果对象, 通过对象能够获取最终执行结果
    result = tasks.send_template_sms.delay(mobile, [sms_code, str(constants.SMS_CODE_REDIS_EXPIRES/60)], 1)
    # 通过get方法能返回执行结果
    # get()默认是阻塞的, 会等到worker执行完成有了结果的时候才返回
    # get()通过timeout超时时间,可以在超过超时时间后立即返回
    ret = result.get()
    print ret

    return jsonify(error_code=RET.OK, errmsg="ok")