Exemplo n.º 1
0
def get_image_code(image_codes_id):
    """
    获取图片验证码
    : params 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": "真实值"
    # 以列表格式存储,获取get key
    # 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:
        print(image_codes_id)
        print(type(image_codes_id))
        redis_store.setex("image_code_%s" % image_codes_id,
                          constants.IMAGE_CODE_REDIS_OUTTIME, text)
    except Exception as e:
        # 记录日志
        current_app.logger.error(e)
        return jsonify(error=response_code.RET.DBERR, errmsg="保存图片验证码失败")
    # 返回图片
    resp = make_response(image_data)
    resp.headers["Content-Type"] = "image/jpg"
    return resp
Exemplo n.º 2
0
def get_houses_area():
    '''获取城区信息'''
    try:
        resp = redis_store.get("area_info").decode()
        print(type(resp))
    except Exception as e:
        current_app.logger.error(e)
    else:
        if resp is not None:
            current_app.logger.info("hit redis area_info")
            return resp, 200, {"Content-Type": "application/json"}

    try:
        area_li = Area.query.all()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(error=RET.DATAERR, errmsg="数据库异常")
    area_dict_li = []

    for area in area_li:
        area_dict_li.append(area.to_dict())

    # 将数据转化为json
    resp_dict = dict(error=RET.OK, errmsg="OK", data=area_dict_li)
    resp_json = json.dumps(resp_dict)
    # 将数据保存到redis中
    try:
        redis_store.setex("area_info", AREA_REDIS_CACHE_TIME, resp_json)
    except Exception as e:
        current_app.logger.error(e)

    return resp_json, 200, {"Content-Type": "application/json"}
Exemplo n.º 3
0
def get_houses_index():
    '''
    获取主页幻灯片展示的房屋基本信息
    参数:房屋图片路由,从数据库获取,访问七牛云
    redis
    '''
    # 从缓存中尝试获取数据
    try:
        resp = redis_store.get("home_page_data")
    except Exception as e:
        current_app.logger.error(e)
        resp = None

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

        if not houses:
            return jsonify(error=RET.NODATA, errmsg="查询无数据")
        print("-----------1===-----------")
        print("-----------%s_______------" % houses)
        print("-----------2===-----------")
        houses_li = []

        for house in houses:
            # 如果房屋未设置主图片,则跳过
            if not house.index_image_url:
                continue
            houses_li.append(house.to_basic_dict())
        print("-----------1===-----------")
        print("-----------%s_______------" % houses_li)
        print("-----------2===-----------")
        # 将数据转换为json,并保存到redis缓存
        house_json = json.dumps(houses_li)
        try:
            redis_store.setex("home_page_data", HOUSES_REDIS_CACHE_TIME,
                              house_json)
        except Exception as e:
            current_app.logger.error(e)
        print("-----------1===-----------")
        print("-----%s_______------" % house_json)
        print("-----------2===-----------")

    return '{"error":0, "errmsg":"OK", "data":%s}' % house_json, 200, {
        "Content-Type": "application/json"
    }
Exemplo n.º 4
0
def get_houses_detail(house_id):
    """获取房屋详情"""
    # 前端在房屋详情页面展示时,如果浏览页面的用户不是该房屋的房东,则展示预定按钮,否则不展示,
    # 所以需要后端返回登录用户的user_id
    # 尝试获取用户登录的信息,若登录,则返回给前端登录用户的user_id,否则返回user_id=-1
    user_id = session.get("user_id", "-1")
    # 校验参数
    if not house_id:
        return jsonify(error=RET.PARAMERR, errmsg="参数确实")

    # 先从redis缓存中获取信息
    try:
        ret = redis_store.get("house_info_%s" % house_id).decode()
    except Exception as e:
        current_app.logger.error(e)
        ret = None
    if ret:
        current_app.logger.info("hit house info redis")
        return '{"error":"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=RET.DBERR, errmsg="查询数据失败")

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

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

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

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

    return resp
Exemplo n.º 5
0
def get_sms_code(moblie):
    """获取短信验证码"""
    # 获取参数
    image_code_id = request.args.get("image_code_id")
    image_codes = request.args.get("image_codes")
    # 校验参数
    if not all([image_codes, image_code_id]):
        # 表示参数不完整
        return jsonify(error=response_code.RET.PWDERR, 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(error=response_code.RET.DBERR, errmsg="redis数据库异常")
    # 判断图片验证码是否过期
    if real_image_code is None:
        # 表示图片验证码没有或者过期
        return jsonify(error=response_code.RET.NODATA, errmsg="图片验证码失效")

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

    # 与用户填写的值进行对比
    '''
    print("----------------------1---------------------------")
    print("---%s----%s" % (real_image_code.lower(),image_codes.lower()))
    print("----------------------2---------------------------")

    ----------------------1---------------------------
    ---b'zsyj'----zsyj      从url获取,比特类型 real_image_code.decode()
    ----------------------2---------------------------
    '''
    if real_image_code.decode().lower() != image_codes.lower():
        # 表示用户填写错误
        return jsonify(error=response_code.RET.DATAERR, errmsg="图片验证码错误")

    # 判断对于这个手机号的操作,在60秒内有没有之前的记录,如果有,则认为用户操作频繁,不接受处理
    try:
        send_flag = redis_store.get("send_sms_code_%s" % moblie)
    except Exception as e:
        current_app.logger.error(e)
    else:
        if send_flag is not None:
            # 表示在60秒内发送过记录
            return jsonify(error=response_code.RET.REQERR, errmsg="请求过于频繁")

    try:
        user = User.query.filter_by(moblie=moblie).first()
    except Exception as e:
        current_app.logger.error(e)
    else:
        if user is not None:
            # 表示手机号已存在
            return jsonify(error=response_code.RET.DATAEXIST, errmsg="手机号已存在")

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

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

    datas = sms_code, constants.SMS_CODE_REDIS_OUTTIME
    '''
    发送成功返回的值
    {"statusCode":"000000","templateSMS":{"smsMessageSid":"b388c3051dc042
    ...: ec9e286ab30543db8e","dateCreated":"20210126143409"}}
    '''
    Send = {
        "statusCode": "000000",
        "templateSMS": {
            "smsMessageSid": "a22692e1ea804c61b4830db8085d6186",
            "dateCreated": "20210126154918"
        }
    }
    Send = json.dumps(Send)
    try:
        # Send=send_msg(moblie,datas)
        Send = json.loads(Send)  #返回的值为json,需要将json格式转化为字典
        print("----------------------1---------------------------")
        print(Send)
        print("----------------------2---------------------------")
    except Exception as e:
        current_app.logger.error(e)

    if Send["statusCode"] == "000000":
        return jsonify(error=response_code.RET.OK, errmsg="发送成功")
    else:
        return jsonify(error=response_code.RET.THIRDERR, errmsg="发送失败")