Beispiel #1
0
def get_areas_info():
    """
    获取所有城区信息:
    1. 获取所有城区信息
    2. 组织数据,返回应答
    """
    try:
        # 先尝试从redis缓存中获取缓存的城区信息
        areas_str = redis_store.get("areas")
        # 如果获取到,直接返回
        if areas_str:
            return jsonify(errno=RET.OK, errmsg="OK", data=json.loads(areas_str))
    except Exception as e:
        current_app.logger.error(e)

    # 如果获取不到,查询数据库
    # 1. 获取所有城区信息
    try:
        areas = Area.query.all() # list
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="获取城区信息失败")

    # 2. 组织数据,返回应答
    areas_dict_li = []
    for area in areas:
        areas_dict_li.append(area.to_dict())

    # 在redis中缓存城区的信息
    try:
        redis_store.set("areas", json.dumps(areas_dict_li), constants.AREA_INFO_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)

    return jsonify(errno=RET.OK, errmsg="OK", data=areas_dict_li)
Beispiel #2
0
def get_image_code():
    # 获取图片验证码  get请求

    # 1.获取uuid作为存储验证码的key  "image:uuid" : image_code
    uuid = request.args.get('uuid')
    last_uuid = request.args.get('last_uuid')
    if not uuid:
        return jsonify(errno=RET.PARAMERR, errmsg=u'缺少参数')

    # 2.生成图片验证码
    name,text,image = captcha.generate_captcha()

    # 3.redis存储图片验证码
    # 拼接redis存储的key

    redis_key = 'image:%s' %uuid
    try:
        if last_uuid: # 如果存在上一个的uuid
            redis_store.delete(last_uuid)  # 删除多余的uuid
                        # key      lavue     过期时间
        redis_store.set(redis_key,text,constants.IMAGE_CODE_REDIS_EXPIRES)  # 设置redis缓存
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR , errmsg=u'验证码保存失败')


    # 4.返回图片验证码
    response = make_response(image)
    response.headers['Content-Type'] = 'image/jpg'
    return response
Beispiel #3
0
def get_house_detail(house_id):

    # 通过house_id查询出房屋
    # 将房屋模型的数据封装到字典中,
    # 直接返回
    # 将房屋数据缓存到redis

    user_id = session.get("user_id")

    # 先从redis缓存中获取数据
    try:
        house_dict = redis_store.get("house_detail_%d"%house_id)
        if house_dict:
            return jsonify(erron=RET.OK,errmsg="OK",data={"user_id":user_id,"house":eval(house_dict)})
    except Exception as e:
        current_app.logger.error(e)

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

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

    # 生成返回数据
    house_dict = house.to_full_dict()

    # 将生成的数据存到redis缓存中
    redis_store.set("house_detail_%d"%house_id,house_dict,HOUSE_DETAIL_REDIS_EXPIRE_SECOND)

    return jsonify(erron=RET.OK,errmsg="OK",data={"user_id":user_id,"house":house_dict})
Beispiel #4
0
def get_areas():

    # 先从redis中获取城区信息
    try:
        areas_array = redis_store.get("areas")
        if areas_array:
            return jsonify(erron=RET.OK,errmsg="OK",data={"areas":eval(areas_array)})
    except Exception as e:
        current_app.logger.error(e)


    # 获取所有的城区信息
    areas = Area.query.all()

    # 因为areas是一个对象的列表,不能直接返回,需要将其转成字典的列表
    areas_array = []
    for area in areas:
        areas_array.append(area.to_dict())

    # 将数据缓存到redis中
    try:
        redis_store.set("areas",areas_array,AREA_INFO_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)

    # 数据返回
    return jsonify(erron=RET.OK,errmsg="OK",data={"areas":areas_array})
Beispiel #5
0
def get_houses_index():
    # 首页房屋推荐图最多5个,以定单数来排序(倒序)
    # 从缓存中获取
    try:
        house_dict = redis_store.get("home_house_index")
        if house_dict:
            return jsonify(erron=RET.OK,errmsg="OK",data=eval(house_dict))
    except Exception as e:
        current_app.logger.error(e)

    try:
        houses = House.query.order_by(House.order_count.desc()).limit(HOME_PAGE_MAX_HOUSES).all()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(erron=RET.DBERR,errmsg="数据查询失败")

    house_dict=[]
    if houses:
        for house in houses:
            house_dict.append(house.to_basic_dict())
        # 保存到缓存中
        try:
            redis_store.set("home_house_index",house_dict,HOME_PAGE_DATA_REDIS_EXPIRES)
        except Exception as e:
            current_app.logger.error(e)

        return jsonify(erron=RET.OK,errmsg="OK",data=house_dict)
    return jsonify(erron=RET.OK,errmsg="OK",data=house_dict)
Beispiel #6
0
def get_verify_code():
    # 获取UUID
    uuid = request.args.get('uuid')

    if not uuid:
        abort(403)

    # 获取验证码信息
    name, text, image = captcha.generate_captcha()

    try:
        if old_uuid:
            # 删除之前的验证信息
            redis_store.delete('Img_code:%s' % old_uuid)
        redis_store.set('Img_code:%s' % uuid, text, constants.IMAGE_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify({'errcode': RET.DATAERR, 'errmsg': error_map[RET.DATAERR]})
    global old_uuid
    old_uuid = uuid

    response = make_response(image)

    response.headers['Content-Type'] = 'image/jpg'
    current_app.logger.debug(text)

    return response
Beispiel #7
0
def get_image_code():
    """
    产生图片验证码:
    1.接收参数(图片验证码标识)并进行校验
    2.生成图片验证码
    3.在redis中保存图片验证码
    4.返回验证码图片
    """
    # 1.接收参数(图片验证码标识)并进行校验
    image_code_id = request.args.get("cur_id")

    if not image_code_id:
        return jsonify(errno=RET.PARAMERR, errmsg="缺少参数")

    # 2.生成图片验证码
    # 文件名称 验证码文本 验证码图片内容
    name, text, content = captcha.generate_captcha()
    # 测试输出图片验证码
    current_app.logger.info('图片验证码是:%s' % text)

    # 3.在redis中保存图片验证码
    # redis_store.set('key', 'value', 'expires')
    try:
        redis_store.set('imagecode:%s' % image_code_id, text,
                        constants.IMAGE_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.PARAMERR, errmsg="保存图片验证失败")
    # 4.返回验证码图片
    response = make_response(content)
    # 指定返回内容的类型
    response.headers["Content-Type"] = "image/jpg"
    return response
Beispiel #8
0
def get_areas():

    # 将城区显示存储为redis缓存
    try:
        areas_list = redis_store.get('Areas')
        if areas_list:
            return jsonify(errno=RET.OK,
                           errmsg=u'城区数据查询成功',
                           data=eval({'areas_list': areas_list}))
    except Exception as e:
        current_app.logger.error(e)

    # 1.查找出所以城区信息
    try:  # 获取所有相关的城区数据对象
        areas = Area.query.all()  # 这是 一个模型对象集合列表
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg=u'城区数据查询失败')

    # 2.封装响应数据
    areas_list = []  # 这个列表就是作为响应数据的装有城区数据对象的列表数据
    for area in areas:  #在所有城区数据信息对象遍历出每个城区的数据对象,再调用封装好的显示信息函数
        areas_list.append(area.to_dict())

    try:  # 将数据缓存在redis
        redis_store.set('Areas', areas_list, constants.AREA_INFO_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)

    # 3.响应
    return jsonify(errno=RET.OK,
                   errmsg=u'城区数据查询成功',
                   data={'areas_list': areas_list})
Beispiel #9
0
def get_areas():
    """提供城区信息
    1.查询所有的城区信息
    2.构造响应数据
    3.响应结果
    """

    # 查询缓存数据,如果有缓存数据,就使用缓存数据,反之,就查询,并缓存新查询的数据
    try:
        area_dict_list = redis_store.get('Areas')
        if area_dict_list:
            return jsonify(errno=RET.OK, errmsg='OK', data=eval(area_dict_list))
    except Exception as e:
        current_app.logger.error(e)

    # 1.查询所有的城区信息 areas == [Area,Area,Area,...]
    try:
        areas = Area.query.all()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询城区信息失败')

    # 2.构造响应数据
    area_dict_list = []
    for area in areas:
        area_dict_list.append(area.to_dict())

    # 缓存城区信息到redis : 没有缓存成功也没有影响,因为前爱你会判断和查询
    try:
        redis_store.set('Areas', area_dict_list, constants.AREA_INFO_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)

    # 3.响应结果
    return jsonify(errno=RET.OK, errmsg='OK', data=area_dict_list)
Beispiel #10
0
def get_house_image():
    try:
        index_houses = redis_store.get('Index_images_info')
        if index_houses:
            return jsonify(errno=RET.OK,
                           errmsg='OK',
                           index_houses=eval(index_houses))
    except Exception as e:
        current_app.logger.error(e)

    try:
        houses = House.query.filter(House.index_image_url != '').order_by(
            House.order_count.desc()).limit(
                constants.HOME_PAGE_MAX_HOUSES).all()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='数据查询失败')
    if not houses:
        return jsonify(errno=RET.DBERR, errmsg='数据错误')
    index_houses = []
    for house in houses:
        index_houses.append(house.to_basic_dict())
    try:
        redis_store.set('Index_images_info', index_houses,
                        constants.HOME_PAGE_DATA_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)

    return jsonify(errno=RET.OK, errmsg='OK', index_houses=index_houses)
Beispiel #11
0
def get_image_code():
    """
    产生图片验证码
    1.接收参数(图片验证码标识)并进行校验
    2.生成图片验证码
    3.再redis中保存图片验证码
    4.返回验证码图片
    """

    # 1.接收参数(图片验证码标识)并进行校验
    image_code_id = request.args.get("cur_id")
    if not image_code_id:
        return jsonify(errno=RET.PARAMERR, errmsg='缺少参数')
    # 2.生成图片验证码
    # 产生图片验证码
    # 文本名称 验证码文本 验证码图片内容
    name, text, content = captcha.captcha.generate_captcha()
    current_app.logger.info("图片验证码是:%s" % text)

    # 3.再redis中保存图片验证码
    # redis_store.set("key", "value", "expires")
    try:
        redis_store.set("imagecode:%s" % image_code_id, text,
                        IMAGE_CODE_REDIS_EXPIRES)
    except Exception as e:
        # print e
        current_app.logger.error(e)
        return jsonify(error=RET.DBERR, errmsg="保存图片验证码失败")
    # 4.返回验证码图片
    response = make_response(content)
    # 指定返回内容的类型
    response.headers['Content-Type'] = 'image/jpg'

    return response
Beispiel #12
0
def get_image_code():
    """
    产生图片验证码
    1、接收参数(图片验证码标识)并进行校验
    2、生成图片验证码
    3、在redis中保存图片验证码
    4、返回验证码图片
    """
    # 1、接收参数(图片验证码标识)并进行校验
    image_code_id = request.args.get("cur_id")

    if not image_code_id:
        return jsonify(errno=RET.PARAMERR, errmsg="缺少参数")

    # 2、生成图片验证码
    # 文件名称 验证码文本 验证码图片内容
    name, text, content = captcha.generate_captcha()
    current_app.logger.info("图片验证码是:%s" % text)

    # 3、在redis中保存图片验证码
    # redis_store.set("key", "value", "expires")
    try:
        redis_store.set("imagecode:%s" % image_code_id, text,
                        constants.IMAGE_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="保存图片验证码失败")

    # 4、返回验证码图片
    response = make_response(content)
    # 指定返回内容的类型
    response.headers["Content-Type"] = "image/jpg"
    return response
Beispiel #13
0
def get_house_detail(house_id):
    try:
        redis_house_detail = redis_store.get("house_detail:%s" % house_id)
    except:
        current_app.logger.error(e)
        # return jsonify(errno=RET.DBERR,errmsg="获取缓存异常")

        if redis_house_detail:
            return jsonify(errno=RET.OK,
                           errmsg="获取成功",
                           data={"house": json.loads(redis_house_detail)})
    try:
        house = House.query.get(house_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="查询异常")
    if not house:
        return jsonify(errno=RET.NODATA, errmsg="该房屋不存在")

    try:
        redis_store.set(
            "house_detail:%s" % house_id,
            json.dump(house.to_full_dict(),
                      constants.HOUSE_DETAIL_REDIS_EXPIRE_SECOND))

    except Exception as e:
        current_app.logger.error(e)
        # return jsonify(errno=RET.DBERR,errmsg="缓存异常")
    return jsonify(errno=RET.OK,
                   errmsg="获取成功",
                   data={"house": house.to_full_dict()})
Beispiel #14
0
def generate_image_code():
    # 获取到之前的id和当前的id
    pre_image_id = request.args.get('pre_id')
    cur_image_id = request.args.get('cur_id')
    # 生成验证码
    name, text, image = captcha.generate_captcha()
    current_app.logger.debug(text)
    try:
        # 删除之前的
        redis_store.delete('ImageCode_' + pre_image_id)
        # print "删除之前的验证码"
        # 保存当前的
        redis_store.set('ImageCode_' + cur_image_id, text,
                        constants.IMAGE_CODE_REDIS_EXPIRES)
        # print "保存当前的验证码"
    except Exception as e:
        current_app.logger.debug(e)
        # 返回响应内容
        # print '保存图片验证码失败'  # for test
        return make_response(jsonify(errno=RET.DATAERR, errmsg='保存图片验证码失败'))
    else:
        resp = make_response(image)
        # 设置内容类型
        resp.headers['Content-Type'] = 'image/jpg'
        return resp
Beispiel #15
0
def get_sms_code():
    json_data = request.data
    dict_data = json.loads(json_data)
    mobile = dict_data.get("mobile")
    image_code = dict_data.get("image_code")
    image_code_id = dict_data.get("image_code_id")

    if not all([mobile, image_code, image_code_id]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数不完整")

    if not re.match(r"1[34578]\d{9}", mobile):
        return jsonify(errno=RET.DATAERR, errmsg="手机号格式不正确")

    try:
        redis_image_code = redis_store.get("image_code:%s" % image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DATAERR, errmsg="图片验证码获取失败")
    try:
        redis_store.delete("imge_code:%s" % image_code_id)
    except Exception as e:
        current_app.logger.error(e)
    if image_code != redis_image_code:
        return jsonify(errno=RET.DATAERR, errmsg="图片验证码填写错误")
    sms_code = "%06d" % random.randint(0, 999999)

    current_app.logger.debug("短信验证码是:%s" % sms_code)

    try:
        redis_store.set("sms_code:%s" % mobile, sms_code,
                        constants.SMS_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="短信验证码保存失败")
    return jsonify(errno=RET.OK, errmsg="发送成功")
Beispiel #16
0
def get_image_code():
    '''产生图片验证码'''
    # 1.接收参数(图片验证码标识)并进行校验
    image_code_id = request.args.get('cur_id')
    if not image_code_id:
        return jsonify(errno=RET.PARAMERR, errmsg='缺少参数')

    # 2. 产生图片验证码,  1.文件名称,2.验证码文本,3.验证码图片内容
    name, text, content = captcha.generate_captcha()
    current_app.logger.info('图片验证码:%s' % text)

    # 3.在redis中保存图片验证码
    # redis_store.set('key','value','expires')
    try:
        redis_store.set('imagecode:%s' % image_code_id, text,
                        constants.IMAGE_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.loger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='保存图片验证码失败')

    # 4.返回验证码图片
    response = make_response(content)

    # 指定返回内容的类型
    response.headers['Content-Type'] = 'image/jpg'
    return response
Beispiel #17
0
def get_image_code():
    """
    1. 取到图片编码
    2. 生成图片验证码
    3. 存储到redis中(key是图片编码,值是验证码的文字内容)
    4. 返回图片
    """
    # 1. 取到图片编码
    args = request.args
    cur = args.get("cur")
    pre = args.get("pre")
    # 如果用户没有传图片id的话,直接抛错
    if not cur:
        abort(403)

    # 2. 生成图片验证码
    _, text, image = captcha.generate_captcha()
    # 为了测试输入到控制台中
    current_app.logger.debug(text)

    # 3. 存储到redis中(key是图片编码,值是验证码的文字内容)
    try:
        if pre:
            redis_store.delete("ImageCode_" + pre)
        redis_store.set("ImageCode_" + cur, text,
                        constants.IMAGE_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="保存图片验证码失败")

    # 4. 返回验证码图片
    respnose = make_response(image)
    # 设置contentType
    respnose.headers["Content-type"] = "image/jpg"
    return respnose
Beispiel #18
0
def send_sms_code():
    """
    发送短信验证码:
    1.接收参数(手机号,图片验证码,图片验证码标始)并进行参数校验
    2.从redis获取图片验证,如果取不到,说明图片验证码已经过期
    3.对比图片验证码,如果一致
    4.使用云通讯发送短信验证码
    5.返回应答,发送短信成功
    :return:
    """
    # 1.接收参数(手机号,图片验证码,图片验证码标始)并进行参数校验
    # req_data = request.data
    # req_dict = json.load(req_data)
    # req_dict = request.get_json()
    req_dict = request.json
    mobile = req_dict.get("mobile")
    image_code = req_dict.get("image_code")
    image_code_id = req_dict.get("image_code_id")
    if not all([mobile, image_code, image_code_id]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数不完整")
    if not re.match(r"^1[356789]\d{9}", mobile):
        return jsonify(errno=RET.PARAMERR, errmsg="手机号格式不正确")
    # 2.从redis获取图片验证,如果取不到,说明图片验证码已经过期
    try:
        real_image_code = redis_store.get("imagecode:%s" % 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="图片验证码已过期")
    # 3.对比图片验证码,如果一致
    if real_image_code != image_code:
        return jsonify(errno=RET.DATAERR, errmsg="图片验证码错误")
    # 4.使用云通讯发送短信验证码
    # 4.1随机生成一个6位短信验证码
    sms_code = "%06d" % random.randint(0, 999999)
    current_app.logger.info("短信验证码是:%s" % sms_code)
    # try:
    #     res = CCP().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 res != 1:
    #     # 发送短信验证码失败
    #     return jsonify(errno=RET.THIRDERR, errmsg="发送短信验证码失败")
    # 4.3 在redis中保存短信验证码内容
    try:
        redis_store.set("smscode:%s" % mobile, sms_code,
                        constants.SMS_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="保存短信验证码失败")
    # 5.返回应答,发送短信成功
    return jsonify(errno=RET.OK, errmsg="发送短信验证码成功")
Beispiel #19
0
def send_sms_code():
    '''发送短信验证码'''
    # 1、接收参数(1.手机号,图片验证码,图片验证码标识)并进行校验
    req_dict = request.json
    mobile = req_dict.get('mobile')
    image_code = req_dict.get('image_code').upper()
    image_code_id = req_dict.get('image_code_id')

    if not all([mobile, image_code, image_code_id]):
        return jsonify(errno=RET.PARAMERR, errmsg='参数不完整')

    if not re.match(r"^1[356789]\d{9}", mobile):
        return jsonify(errno=RET.PARAMERR, errmsg='手机号码格式错误')

    # 2、从redis中获取图片验证码(如果获取不到,说明图片验证码过期)
    try:
        real_image_code = redis_store.get('imagecode:%s' % image_code_id)
        print real_image_code
    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='图片验证码已过期')

    # 3、对比图片验证码
    if real_image_code != image_code:
        print image_code
        return jsonify(errno=RET.DATAERR, errmsg='图片验证码错误')
    # 4、使用云通讯发送短信验证码
    # 4.1、随机生成一个6位的短信验证码 (%06s,格式化输出,字符串s有六位,不足六位补0)
    sms_code = "%06d" % random.randint(0, 999999)
    current_app.logger.info('短信验证码:%s' % sms_code)
    # 4.2、发送短信验证码
    # try:
    #     res = CCP().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 res != 1:
    #     # 发送短信验证码失败
    #     return jsonify(errno=RET.THIRDERR, errmsg='发送短信验证码失败')
    # 4.3、保存短信验证码在redis中
    try:
        redis_store.set('smscode:%s' % mobile, sms_code,
                        constants.SMS_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.PARAMERR, errmsg='保存短信验证码失败')

    # 5、返回应答发送成功
    return jsonify(errno=RET.OK, errmsg='手机验证码发送成功')
Beispiel #20
0
def get_image_code():
    cur_id = request.args.get("cur_id")
    pre_id = request.args.get("pre_id")

    name, text, image_data = captcha.generate_captcha()
    try:
        redis_store.set("image_code:%s" % cur_id, text,
                        constants.IMAGE_CODE_REDIS_EXPIRES)
        if pre_id:
            redis_store.delete("image_code" % pre_id)

    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="验证码保存失败")
Beispiel #21
0
def get_image_codes(image_code_id):
    """
        获取图片验证码
        :params image_code_id :图片验证码编号
        :return :成功返回 验证码图片; 失败 返回json字符串
    """
    # 1.获取参数
    # 2.验证参数
    if not image_code_id:
        return jsonify(error=RET.DBERR, errmsg='参数为空')
    else:
        # 3.业务逻辑处理
        # 3.1生成验证码图片
        # 图片名称  文本  图片二进制码
        name, text, image_data = captcha.generate_captcha()

        # 3.2将验证码图片存在redis中,还要设计有效期
        # redis : 字符串  列表  哈希  set
        # 'image_codes':'xxxx'   字符串
        # 'image_codes':['', '', '']   列表
        # 'image_codes':{'编号1':'值1', '':'', '':''}   哈希。使用案例,hset('image_codes','编号1','值1') ;hget('image_codes','编号1')

        # 最后选择字符串类型,可以很好的设计有效期
        # 'image_codes_编号1':'值1'
        # 'image_codes_编号2':'值2'

        print(image_code_id)

        try:
            image_codes_key = 'image_codes_%s' % image_code_id
            redis_store.set(image_codes_key, text)
            redis_store.expire(image_codes_key, 180)  # 这个key存在的时间是180s
            # 简写:              记录名称         有效期  文本
            # redis_store.setex(image_codes_key, 180, text)
        # except Exception , e:
        except Exception as e:
            # 记录日志
            current_app.logger.error(e)
            return jsonify(error=RET.DBERR, errmsg='保存图片数据到redis失败')

        # 4.返回值
        # return image_data
        resp = make_response(image_data)
        resp.headers['Content-Type'] = 'image/jpg'
        return resp
Beispiel #22
0
def get_image_code():
    """获取uuid,redis保存uuid和验证码,返回图片"""
    uuid = request.args.get("cur_id")
    if not uuid:
        return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR])

    name, text, content = captcha.generate_captcha()
    current_app.logger.info("图片验证码是:%s" % text)
    try:
        redis_store.set("uuid:%s" % uuid, text,
                        constants.IMAGE_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR])

    response = make_response(content)
    response.headers["Content-Type"] = "image/jpg"
    return response
Beispiel #23
0
def get_areas():
    """
	1.查询数据库的城区信息
	2.将城区信息装成字典
	3.响应给前端页面
	:return:
	"""
    # 0.先到redis中查询是否已经有城区信息了
    try:
        areas = redis_store.get("areas")
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="缓冲中没有区域信息")

    # 如果有内容,转成字典返回
    if areas:
        area_list = json.loads(areas)
        return jsonify(errno=RET.OK, errmsg="缓冲中获取地域信息成功", data=area_list)

    # 1.查询数据库的城区信息
    try:
        areas = Area.query.all()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="数据库查询城区信息失败")

    if not areas:
        return jsonify(errno=RET.DATAERR, errmsg="数据库中城区信息不存在")

    # 2.将城区信息装成字典
    # 因为查询出来全是对象实例,不能直接进行 JSON 转化,所以定义一个数组保存所有对象的字典信息
    area_list = []
    for area in areas:
        area_list.append(area.to_dict())

    # 2.1 纯属数据到redis中
    try:
        redis_store.set("areas", json.dumps(area_list))
    except Exception as e:
        current_app.logger.error(e)

    # 3.响应给前端页面
    return jsonify(errno=RET.OK, errmsg="获取城区信息成功", data=area_list)
Beispiel #24
0
def index():
    # 测试redis
    redis_store.set("name", "itcast")

    # 测试session存储
    # session["name"] = "itheima"

    # 测试日志功能
    # logging.fatal("Fatal Message")
    # logging.error("Error Message")
    # logging.warning("Warning Message")
    # logging.info("Info Message")
    # logging.debug("Debug Message")

    # current_app.logger.fatal("Fatal Message")
    # current_app.logger.error("Error Message")
    # current_app.logger.warning("Warning Message")
    # current_app.logger.info("Info Message")
    # current_app.logger.debug("Debug Message")

    return "index"
Beispiel #25
0
def get_areas():
    """获取城区信息"""
    # 查询所有地区
    try:  # 从redis中获取
        areas_list = redis_store.get('Areas')
        return jsonify(errno=RET.OK, errmsg='OK', data=eval(areas_list))
    except Exception as e:
        current_app.logger.error(e)
    try:  # 从数据库中获取
        areas = Area.query.all()
    except Exception as e:
        return jsonify(errno=RET.DBERR, errmsg='获取参数失败')

    areas_list = [area.to_dict() for area in areas]

    try:  # 保存到redis中
        redis_store.set('Areas', areas_list, constants.AREA_INFO_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)

    return jsonify(errno=RET.OK, errmsg='OK', data=areas_list)
Beispiel #26
0
def send_sms_code():
    """发送短信验证码"""
    json_str = request.data
    json_dict = json.loads(json_str)

    mobile = json_dict.get('mobile')
    imageCode_client = json_dict.get('imagecode')
    uuid = json_dict.get('uuid')

    if not all([mobile, imageCode_client, uuid]):
        return jsonify(errno=RET.PARAMERR, errmsg="缺少参数")
    if not re.match(r'^1[345678][0-9]{9}$', mobile):
        return jsonify(error=RET.PARAMERR, errmsg="手机号码格式错误")

    try:
        imageCode_server = redis_store.get('ImageCode:' + uuid)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.PARAMERR, errmsg="验证嘛失败")

    if not imageCode_server:
        return jsonify(error=RET.PARAMERR, errmsg="验证码不存在")

    if imageCode_server.lower() != imageCode_client.lower():
        return jsonify(errno=RET.DATAERR, errmsg='验证码输入有误')

    sms_code = '%06d' % random.randint(0, 999999)
    current_app.logger.debug('短信的验证码为:' + sms_code)

    # result=CCP().send_template_sms(mobile,[sms_code,constants.SMS_CODE_REDIS_EXPIRES/60],'1')
    # if result !=1:
    #     return jsonify(errno=RET.THIRDERR,errmsg='发送短信验证码失败')
    try:
        redis_store.set('Mobile:' + mobile, sms_code,
                        constants.SMS_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='存储短信验证码失败')

    return jsonify(errno=RET.OK, errmsg='发送短信验证码成功')
Beispiel #27
0
def get_areas():
    """
    1.获取城区信息
    2.将城区信息转成字典列表
    3.返回
    :return:
    """

    # 0.先从redis中获取
    try:
        redis_json_areas = redis_store.get("areas")
    except Exception as e:
        current_app.logger.error(e)
        # return jsonify(errno=RET.DBERR,errmsg="获取失败")

    if redis_json_areas:
        return jsonify(errno=RET.OK,
                       errmsg="获取成功",
                       data=json.loads(redis_json_areas))

    try:
        areas = Area.query.all()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="查询失败")

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

    try:
        redis_store.set("areas", json.dumps(areas_list),
                        constants.AREA_INFO_REDIS_EXPIRES)

    except Exception as e:
        current_app.logger.error(e)
        # return jsonify(errno=RET.DBERR,errmsg="存储失败")

        # 4.返回
    return jsonify(errno=RET.OK, errmsg="获取成功", data=areas_list)
Beispiel #28
0
def send_sms_code():
    # 接受参数
    request_str = request.data
    request_dict = json.loads(request_str)
    mobile = request_dict.get('mobile')
    img_code_client = request_dict.get('img_code')
    uuid = request_dict.get('uuid')

    # 验证参数合法性
    if not all([mobile, img_code_client, uuid]):
        return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR])

    if not re.match(r'^1[3-8]\d{9}$', mobile):
        return jsonify(errno=RET.PARAMERR, errmsg='手机号码不合法')
    try:
        img_code_server = redis_store.get('Img_code:%s' % uuid)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR])
    if not img_code_server:
        return jsonify(errno=RET.DATAERR, errmsg='验证码不存在')
    if img_code_client.upper() != img_code_server.upper():
        return jsonify(errno=RET.PARAMERR, errmsg='验证码错误')
    # 发送短信验证码
    sms_code = '%06d' % random.randint(0, 999999)

    result = CCP().sendTemplateSMS(mobile, [sms_code], '1')
    if result:
        return jsonify(errno=RET.THIRDERR, errmsg=error_map[RET.THIRDERR])
    # 保存到redis中
    try:
        redis_store.set('sms_code:%s' % mobile, sms_code, constants.SMS_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR])
    current_app.logger.debug(sms_code)
    return jsonify(errno=RET.OK, errmsg='发送验证码成功')
Beispiel #29
0
def get_image_code():
    """
    产生图片的验证码
    1. 接受参数(图片验证码表示)并进行校验
    2. 生成图片验证码
    3. 在redis中保存图片的验证码
    4. 返回验证码图片
    :return:
    """

    # 1.接受参数(图片验证码表示)并进行校验
    image_code_id = request.args.get('cur_id')
    print image_code_id

    if not image_code_id:
        return jsonify(errno=RET.PARAMERR, errmsg='缺少参数')

    # 2.生成图片验证码
    # 文件名称, 验证码文本, 验证码图片内容
    name, text, content = captcha.generate_captcha()
    current_app.logger.info("图片验证码是:%s" % text)

    response = make_response(content)
    # 指定返回内容的类型
    response.headers['Content-Type'] = 'image/jpg'

    # 3.在redis中保存图片的验证码
    try:
        # redis_store.set('key', 'value', 'expiry')
        redis_store.set('imagecode: %s' % image_code_id, text,
                        constants.IMAGE_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='保存图片验证码失败')
    # 4.返回验证码图片
    return response
Beispiel #30
0
def get_image_code():

    uuid = request.args.get('uuid')
    last_uuid = request.args.get('last_uuid')
    if not uuid:
        abort(403)
    #生成验证码:text是验证码的文职信息
    name, text, image = captcha.generate_captcha()
    # logging.debug('验证码信息:'+text)
    current_app.logger.debug('验证码信息:' + text)
    try:
        if last_uuid:
            redis_store.delete('ImageCode:' + last_uuid)
        redis_store.set('ImageCode:' + uuid, text,
                        constants.IMAGE_CODE_REDIS_EXPIRES)

    except Exception as e:
        print e
        logging.error(e)
        current_app.logger.error(e)
        return jsonify(error=RET.DBERR, errmsg=u'保存验证码失败')
    response = make_response(image)
    response.headers['Content-Type'] = 'image/jpg'
    return response