Example #1
0
def get_area_info():
    """获取城区信息"""
    try:
        # 先查询redis缓存,如果没有再从数据库查询
        area_li = redis_store.get("area_info")
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="数据库查询出错")

    if area_li:
        # 用日志记录一下命中redis缓存
        current_app.logger.error("成功击中redis缓存")
        return area_li, 200, {"Content-Type": "application/json"}

    # 查询数据
    try:
        area_list = Area.query.all()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="数据库错误")

    # 把数据集对象转化为单个的字典对象
    area_dict_list = []
    for area in area_list:
        area_dict_list.append(area.to_dict())

    response_dict = dict(errno=RET.OK, errmsg="OK", data=area_dict_list)
    response_json = json.dumps(response_dict)
    # 把数据保存到redis缓存中
    try:
        redis_store.setex("area_info", constants.AREA_INFO_TIME, response_json)
    except Exception as e:
        current_app.logger.error(e)

    return response_json, 200, {"Content-Type": "application/json"}
Example #2
0
def get_house_detail(house_id):
    """获取房屋详情"""
    # 前端在房屋详情页面展示时,如果浏览页面的用户不是该房屋的房东,则展示预定按钮,否则不展示,
    # 所以需要后端返回登录用户的user_id
    # 尝试获取用户登录的信息,若登录,则返回给前端登录用户的user_id,否则返回user_id=-1
    user_id = session.get("user_id", "-1")

    # 校验参数
    if not house_id:
        return jsonify(errno=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("击中缓存")
        return '{"errno":"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(errno=RET.DBERR, errmsg="查询数据失败")

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

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

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

    resp = '{"errno":"0", "errmsg":"OK", "data":{"user_id":%s, "house":%s}}' % (user_id, json_house), \
           200, {"Content-Type": "application/json"}
    return resp
Example #3
0
def get_home_index():
    """
    获取首页图片展示
    参数:index_image_url,   
    """
    # 从缓存中获取数据
    try:
        image_list = redis_store.get("home_page_image")
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="数据获取错误")

    if image_list:
        return '{"errno": 0, "errmsg": "OK", "data": %s}' % image_list, 200, {
            "Content-Type": "application/json"
        }

    # 缓存理没有数据,从数据库里获取
    try:
        # 以订单量最多房屋图片来作为首页图片推荐
        home_list = House.query.order_by(House.order_count.desc()).limit(
            constants.home_page_max)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="数据库查询错误")

    if home_list is None:
        return jsonify(errno=RET.NODATA, errmsg="无数据")

    # 把图片对象转换成一个个字典数据
    home_json_list = []
    for home in home_list:
        # 如果该房子没有设置过主页图片则跳过
        if home.index_image_url is None:
            continue
        home_json_list.append(home.to_dict())

    # 把字典数据转换成json格式
    home_json_data = json.dumps(home_json_list)

    # 把数据保存到redis缓存中
    try:
        redis_store.setex("home_page_image", constants.HOME_PAGE_CACHE_TIME,
                          home_json_data)
    except Exception as e:
        current_app.logger.error(e)

    return '{"errno:"0", "errmsg":"ok", "data": %s}' % home_json_data, 200, \
           {"Content-Type": "application/json"}
Example #4
0
def get_image_code(image_code_id):
    """

    :param image_code_id: 图形验证码的唯一编号
    :return:
    """
    # 生成验证码
    name, text, image_code_data = captcha.generate_captcha()
    # 设置image_code到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="保存图片验证码失败")

    # 返回图片
    response = make_response(image_code_data)
    response.headers["Content-Type"] = "image/jpg"
    return response
Example #5
0
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="参数不完整")

    # 业务逻辑处理
    # 从redis中取出真实的图片验证码
    try:
        real_image_code = redis_store.get("image_code_%s" % image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="redis数据库异常")

    # 判断图片验证码是否过期
    if real_image_code is None:
        # 表示图片验证码没有或者过期
        return jsonify(errno=RET.NODATA, errmsg="图片验证码失效")

    # 删除redis中的图片验证码,防止用户使用同一个图片验证码验证多次
    try:
        redis_store.delete("image_code_%s" % image_code_id)
    except Exception as e:
        current_app.logger.error(e)

    # 与用户填写的值进行对比
    print(image_code)
    print(real_image_code.decode())
    if real_image_code.decode().lower() != image_code.lower():
        # 表示用户填写错误
        return jsonify(errno=RET.DATAERR, errmsg="尼玛炸裂")

    # 判断对于这个手机号的操作,在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="请求过于频繁,请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="手机号已存在")

    # 如果手机号不存在,则生成短信验证码
    sms_code = "%06d" % random.randint(0, 999999)

    # 保存真实的短信验证码
    try:
        redis_store.setex("sms_code_%s" % mobile, constants.SMS_CODE_REDIS_EXPIRES, sms_code)
        # 保存发送给这个手机号的记录,防止用户在60s内再次出发发送短信的操作
        redis_store.setex("send_sms_code_%s" % mobile, constants.SEND_SMS_CODE_INTERVAL, 1)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="保存短信验证码异常")

    # 使用celery异步发送短信
    res = send_sms.delay(mobile, [sms_code, 5], 1)
    print(res)

    # 发送成功
    return jsonify(errno=RET.OK, errmsg="发送成功")