Exemplo n.º 1
0
def get_areas():
    """
    1. 查询出所有的城区
    2. 返回
    :return:
    """

    # 先从redis中查询
    try:
        areas_dict = sr.get("areas")
    except Exception as e:
        current_app.logger.error(e)

    if areas_dict:
        return jsonify(errno=RET.OK, errmsg="ok", data=eval(areas_dict))

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

    # 因为不能直接返回对象数组,所以定义一个列表,去中保存每一个模型所对应的字典信息
    areas_dict = []
    # 遍历进行保存
    for area in areas:
        areas_dict.append(area.to_dict())

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

    return jsonify(errno=RET.OK, errmsg="ok", data=areas_dict)
Exemplo n.º 2
0
def house_index():
    """
    获取首页房屋列表
    :return:
    """

    # 先从redis中取
    try:
        houses_dict = sr.get("home_page_house_info")
    except Exception as e:
        houses_dict = None
        current_app.logger.error(e)
    if houses_dict:
        return jsonify(errno=RET.OK, errmsg="OK", data=eval(houses_dict))

    # 查询房屋信息
    try:
        houses = House.query.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="查询数据错误")

    houses_dict = []
    for house in houses:
        houses_dict.append(house.to_basic_dict())

    # 将数据缓存到redis中
    try:
        sr.set("home_page_house_info", houses_dict,
               constants.HOME_PAGE_DATA_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)

    return jsonify(errno=RET.OK, errmsg="OK", data=houses_dict)
Exemplo n.º 3
0
def send_sms():
    # 1. 接收参数并判断是否有值
    receiver = request.json
    mobile = receiver.get('mobile')
    image_code = receiver.get('image_code')
    image_code_id = receiver.get('image_code_id')

    if not all([mobile, image_code, image_code_id]):
        current_app.logger.error("参数不足")
        # 错误信息
        err_dict = {"errno": RET.PARAMERR, "errmsg": "参数不足"}
        return jsonify(err_dict)
    # 2. 校验手机号是正确
    if not re.match(r"1[2345678][0-9]{9}", mobile):
        return jsonify(errno=RET.PARAMERR, errmsg="手机号码格式错误")
    # 3. 通过传入的图片编码去redis中查询真实的图片验证码内容
    try:
        real_image_code = sr.get("Iamge_Code_%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.PARAMERR, errmsg="图片失效")
    # 4. 进行验证码内容的比对
    if image_code.lower() != real_image_code.lower():
        return jsonify(errno=RET.PARAMERR, errmsg="图片错误")

    # 5. 生成发送短信的内容并发送短信
    # TODO: 判断手机号码是否已经注册 【提高用户体验】
    try:
        user = User.query.filter(User.mobile == mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="查询用户数据异常")

    if user:
        return jsonify(errno=RET.DATAEXIST, errmsg="手机号码已经注册")
    real_sms_code = random.randint(0, 999999)
    # 6位的密码
    real_sms_code = "%06d" % real_sms_code
    # 发送短信验证码
    try:
        result = CCP().send_template_sms(mobile, {real_sms_code, 5}, 1)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.THIRDERR, errmsg="云通信发送短信验证码异常")
    # 发送短信验证码失败
    if result == -1:
        return jsonify(errno=RET.THIRDERR, errmsg="云通信发送短信验证码异常")
    # 6. redis中保存短信验证码内容
    sr.setex("SMS_CODE_%s" % mobile, constants.SMS_CODE_REDIS_EXPIRES,
             real_sms_code)
    # 7. 返回发送成功的响应
    return jsonify(errno=RET.OK, errmsg="发送短信验证码成功")
Exemplo n.º 4
0
def get_sms_code():
    # 获取参数
    image_code_id = request.json.get("image_code_id")
    image_code = request.json.get("image_code")
    mobile = request.json.get("mobile")
    # 校验参数
    print(image_code_id, image_code, mobile)
    if not all([image_code_id, image_code, mobile]):
        return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR])

    # 根据图片key取出验证码文字
    try:
        real_img_code = sr.get("image_code_id" + image_code_id)
    except BaseException as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR])

    print("实际验证码:", real_img_code)
    print("获取到的验证码", image_code)
    # 校验图片验证码(文字)
    if real_img_code != image_code.upper():
        return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR])

    # 获取短信验证码 细节处理
    # 用户存在则不需要重新注册
    # 判断用户是否存在
    try:
        user = User.query.filter_by(mobile=mobile).first()
    except BaseException as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR])

    if user:
        return jsonify(errno=RET.DATAEXIST, errmsg=error_map[RET.DATAEXIST])

    # 生成随机短信验证码
    rand_num = "%04d" % random.randint(0, 9999)  # 4位随机数

    # # 发送短信
    # response_code = CCP().send_template_sms(mobile, [rand_num, 5], 1)
    # if response_code != 0:  # 发送失败
    #     return jsonify(RET.THIRDERR, errmsg=error_map[RET.THIRDERR])

    # 保存短信
    try:
        sr.set("sms_code_id" + mobile, rand_num, ex=SMS_CODE_REDIS_EXPIRES)
    except BaseException as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR])

    # 控制台打印短信验证码
    current_app.logger.info("短信验证码位:%s" % rand_num)

    # json 返回发送结果
    return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])
Exemplo n.º 5
0
def user():
    # 获取参数
    phonecode = request.json.get("phonecode")
    mobile = request.json.get("mobile")
    password = request.json.get("password")
    # 校验参数
    # print(sms_code,mobile,password)
    if not all([phonecode, mobile, password]):
        return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR])
    # 手机号校验
    if not re.match(r"1[345678]\d{9}$", mobile):
        # print("手机校验失败")
        return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR])
    # 判断用户是否一存在
    try:
        user = User.query.filter(User.mobile == mobile).first()
    except BaseException as  e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR])

    if user:
        return jsonify(errno=RET.DATAEXIST, errmsg=error_map[RET.DATAEXIST])
    # 校验短信验证码,更具手机号取出短信验证码
    # print("手机校验通过了")
    try:
        real_phonecode = sr.get("sms_code_id" + mobile)
    except BaseException as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR])
    # 如获取到了验证码
    if real_phonecode != phonecode:
        # print("验证码错误")
        return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR])

    # 记录用户数据
    user = User()
    user.mobile = mobile
    user.name = mobile
    # user.password_hash=password   #  直接存储密码为明文密码  不安全

    # 使用计算属性封装密码
    user.password = password
    db.session.add(user)

    try:
        db.session.commit()
    except BaseException as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR])

    # 使用session 记录用户登录状态,记录主键就可以查询出其他的数据
    session["user_id"] = user.id

    # json 返回结果
    return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])
Exemplo n.º 6
0
def register():
    """
    1. 获取参数和判断是否有值
    2. 从redis中获取指定手机号对应的短信验证码的
    3. 校验验证码
    4. 初始化 user 模型,并设置数据并添加到数据库
    5. 保存当前用户的状态
    6. 返回注册的结果
    :return:
    """

    # 1. 获取参数和判断是否有值
    data_dict = request.json
    mobile = data_dict.get("mobile")
    phonecode = data_dict.get("phonecode")
    password = data_dict.get("password")

    if not all([mobile, phonecode, password]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数不全")

    # 2. 从redis中获取指定手机号对应的短信验证码的
    try:
        sms_code = sr.get("SMS_" + mobile)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="获取本地验证码失败")

    if not sms_code:
        return jsonify(errno=RET.NODATA, errmsg="短信验证码过期")

    # 3. 校验验证码
    if phonecode != sms_code:
        return jsonify(errno=RET.DATAERR, errmsg="短信验证码错误")

    # 4. 初始化 user 模型,并设置数据并添加到数据库
    user = User()
    user.name = mobile
    user.mobile = mobile
    # 对密码进行处理
    user.password = password

    try:
        db.session.add(user)
        db.session.commit()
    except Exception as e:
        db.session.rollback()
        current_app.logger.error(e)
        return jsonify(errno=RET.DATAERR, errmsg="数据保存错误")

    session["user_id"] = user.id
    session["name"] = user.name
    session["mobile"] = user.mobile

    return jsonify(errno=RET.OK, errmsg="注册成功")
Exemplo n.º 7
0
def login_sms():
    # 1. 获取参数和判断是否有值

    mobile = request.json.get("mobile")
    phonecode = request.json.get("phonecode")

    if not all([mobile, phonecode]):
        # 参数不全
        return jsonify(errno=RET.PARAMERR, errmsg="参数不全")

    # 2. 校验手机号是正确
    if not re.match(r"1[2345678][0-9]{9}", mobile):
        return jsonify(errno=RET.PARAMERR, errmsg="手机号码格式错误")

    # 3. 从数据库查询出指定的用户
    try:
        user = User.query.filter(User.mobile == mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="查询用户对象异常")

    if not user:
        return jsonify(errno=RET.USERERR, errmsg="当前手机号码未注册")

    try:
        real_sms_code = sr.get("SMS_CODE_%s" % mobile)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="查询redis中短信验证码异常")

    # 4. 校验验证码
    if phonecode != real_sms_code:
        #  3.3 不相等:短信验证码填写错误
        return jsonify(errno=RET.DATAERR, errmsg="短信验证码填写错误")

    # 5. 保存用户登录状态

    session["user_id"] = user.id
    session["mobile"] = user.mobile

    # 记录用户最后一次登录时间
    user.last_login = datetime.now()

    try:
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        db.session.rollback()
        return jsonify(errno=RET.DBERR, errmsg='数据库保存用户信息异常')

    # 5. 登录成功
    return jsonify(errno=RET.OK, errmsg="OK")
Exemplo n.º 8
0
def get_house_detail(house_id):
    """
    分析:①在房屋详情页面,角色分为房东以及客户,当客户进入时对于前端页面来说需显示预定功能按钮,如是房东角色进入就不展示此功能按钮;
    ②对于角色来说,那么就需要用到user_id了;
    ③尝试从session中去获取用户id,如果存在,说明用户为登录状态,那么将用户id返回给前端,不存在返回user_id = -1

    """
    user_id = session.get("user_id", "-1")
    if not house_id:
        return jsonify(errno=RET.PARAMERR, errmsg="参数不足")

    house_list = None  # type:House
    try:
        house_list = House.query.get(house_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DATAERR, errmsg="查询房屋信息失败")
    if not house_list:
        return jsonify(errno=RET.NODATA, errmsg="房屋不存在")
    # 将房屋列表转字典对象
    house_dict_list = house_list.to_full_dict()

    # 将房屋详情数据转换成json格式的数据,并存到redis数据库中
    try:
        json_houses = json.dumps(house_dict_list)
        sr.setex("house_info_%s" % house_id,
                 constants.HOUSE_DETAIL_REDIS_EXPIRE_SECOND, json_houses)
    except Exception as e:
        current_app.logger.error(e)
    # 尝试从redis数据库中获取房屋详情信息, 出现异常则使ret为None,所以需要在进入函数后,那么需要从去数据库中获取房屋详情信息
    try:
        ret = sr.get("house_info_%s" % house_id)
    except Exception as e:
        current_app.logger.error(e)
        ret = None
    # 对ret进行判断, 存在不为None 则直接返回正确响应数据即可
    if ret:
        current_app.logger.info("house info from redis")
        return '{"errno":"0", "errmsg":"OK", "data":{"user_id":%s, "house":%s}}' % (
            user_id, ret), 200, {
                "Content-Type": "application/json"
            }

    resp = '{"errno":"0", "errmsg":"OK", "data":{"user_id":%s, "house":%s}}' % (
        user_id, json_houses), 200, {
            "Content-Type": "application/json"
        }
    return resp
Exemplo n.º 9
0
def get_house_detail(house_id):
    """
    1. 通过房屋id查询出房屋模型
    :param house_id:
    :return:
    """
    user_id = session.get("user_id", -1)

    # 先从 redis 中查询
    try:
        house_dict = sr.get("house_info_%d" % house_id)
        if house_dict:
            return jsonify(errno=RET.OK,
                           errmsg="OK",
                           data={
                               "user_id": user_id,
                               "house": eval(house_dict)
                           })
    except Exception as e:
        current_app.logger.error(e)

    # 如果redis中没有查询到
    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="房屋信息不存在")

    # 将数据缓存到redis中
    house_dict = house.to_full_dict()
    try:
        sr.set(("house_info_%d" % house_id), house_dict,
               constants.HOUSE_DETAIL_REDIS_EXPIRE_SECOND)
    except Exception as e:
        current_app.logger.error(e)
    return jsonify(errno=RET.OK,
                   errmsg="OK",
                   data={
                       "user_id": user_id,
                       "house": house_dict
                   })
Exemplo n.º 10
0
def register():
    # 1. 获取参数和判断是否有值
    Post_register = request.json
    mobile = Post_register.get('mobile')
    ses_code = Post_register.get('phonecode')
    password = Post_register.get('password')
    if not all([mobile, ses_code, password]):
        current_app.logger.error("参数不足")
        return jsonify(errno=RET.PARAMERR, errsmg="参数不足")
    # 判断手机号码
    if not re.match(r"1[2345678][0-9]{9}", mobile):
        return jsonify(errno=RET.PARAMERR, errmsg="手机号码格式错误")
    # 2. 从redis中获取指定手机号对应的短信验证码的
    try:
        real_sms_code = sr.get("SMS_CODE_%s" % mobile)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="查询redis中短信验证码异常")

    # 3. 校验验证码
    if ses_code != real_sms_code:
        #  3.3 不相等:短信验证码填写错误
        return jsonify(errno=RET.DATAERR, errmsg="短信验证码填写错误")

    # 4. 初始化 user 模型,并设置数据并添加到数据库
    user = User()
    user.name = mobile
    user.mobile = mobile
    user.password = password
    # 5. 保存当前用户的状态
    try:
        db.session.add(user)
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="保存对象异常")
    # 6. 返回注册的结果
    return jsonify(errno=RET.OK, errmsg="注册成功")
Exemplo n.º 11
0
def send_sms():
    """
    1. 接收参数并判断是否有值
    2. 校验手机号是正确
    3. 通过传入的图片编码去redis中查询真实的图片验证码内容
    4. 进行验证码内容的比对
    5. 生成发送短信的内容并发送短信
    6. redis中保存短信验证码内容
    7. 返回发送成功的响应
    :return:
    """
    # 1. 接收参数并判断是否有值
    # 取到请求值中的内容
    data = request.data
    data_dict = json.loads(data)
    mobile = data_dict.get("mobile")
    image_code = data_dict.get("image_code")
    image_code_id = data_dict.get("image_code_id")

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

    # 2. 校验手机号是正确
    if not re.match("^1[3578][0-9]{9}$", mobile):
        return jsonify(errno=RET.PARAMERR, errmsg="手机号格式不正确")

    # 3. 通过传入的图片编码去redis中查询真实的图片验证码内容
    try:
        real_image_code = sr.get("ImageCode_" + image_code_id)
        # 如果能够取出来值,删除redis中缓存的内容
        if real_image_code:
            sr.delete("ImageCode_" + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="获取图片验证码失败")
    # 3.1 判断验证码是否存在,已过期
    if not real_image_code:
        return jsonify(errno=RET.NODATA, errmsg="验证码已过期")

    # 4. 进行验证码内容的比对
    if image_code.lower() != real_image_code.lower():
        return jsonify(errno=RET.DATAERR, errmsg="验证码输入错误")

    # 4.1 校验该手机是否已经注册
    try:
        user = User.query.filter_by(mobile=mobile).first()
    except Exception as e:
        user = None  # 如果查询时出现错误,也需要给user初始化,如果不初始化,会报未定义的异常
        current_app.logger.error(e)
    if user:
        return jsonify(errno=RET.DATAEXIST, errmsg="该手机已被注册")

    # 5. 生成发送短信的内容并发送短信
    result = random.randint(0, 999999)
    sms_code = "%06d" % result
    current_app.logger.debug("短信验证码的内容:%s" % 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="发送短信失败")

    # 6. redis中保存短信验证码内容
    try:
        sr.set("SMS_" + mobile, sms_code, constants.SMS_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="保存短信验证码失败")

    # 7. 返回发送成功的响应
    return jsonify(errno=RET.OK, errmsg="发送成功")