Beispiel #1
0
def login():
    """
    登录
    :param:mobile 和密码
    :return: json
    """
    # 获取参数
    request_dict = request.get_json()
    mobile = request_dict.get("mobile")
    password = request_dict.get("password")
    # 校验参数
    if not all([mobile, password]):
        return jsonify(errno=RET.PARAMERR, errmsg='参数不完整')
    # 判断手机格式
    if not re.match(r'1[358]\d{9}', mobile):
        return jsonify(errno=RET.PARAMERR, errmsg="手机号格式错误")

    # 业务逻辑处理
    # 判断错误次数是否超过限制,如果超过限制直接返回
    # redis 用户的ip地址:次数
    user_ip = request.remote_addr
    try:
        access_nums = redis_store.get("user_ip_%s" % user_ip)
    except Exception as e:
        logging.error(e)
    else:
        if access_nums is not None and int(
                access_nums) >= contants.LOGIN_ERROR_MAX_TIME:
            return jsonify(errno=RET.REQERR, errmsg="错误次数太多,请稍后重试")

    # 从数据库中查询手机是否存在
    try:
        user = User.query.filter_by(mobile=mobile).first()
    except Exception as e:
        logging.error(e)
        # 查询数据库错误
        return jsonify(errno=RET.DBERR, errmsg="获取用户信息失败")
    # 验证密码
    if user is None or not user.check_pwd_hash(password):
        try:
            redis_store.incr("user_ip_%s" % user_ip)
            redis_store.expire("user_ip_%s" % user_ip,
                               contants.LOGIN_ERROR_FORBID_TIMES)
        except Exception as e:
            logging.error(e)
        return jsonify(errno=RET.DATAERR, errmsg="账号密码不匹配")

    # 保存登录状态
    session['name'] = user.name
    session['mobile'] = user.mobile
    session['user_id'] = user.id
    # 返回
    return jsonify(errno=RET.OK, errmsg='登录成功')
Beispiel #2
0
def login():
    """
    用户登录
    :param:手机号,密码
    :return: json
    """
    #接收参数
    request_dict = request.get_json()
    mobile = request_dict.get("mobile")
    password = request_dict.get("password")
    # 校验参数
    if not all([mobile, password]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数不完整")
    #判断手机号格式
    if not re.match(r"1[345678]\d{9}", mobile):
        return jsonify(errno=RET.PARAMERR, errmsg="手机号格式错误")
    #业务逻辑处理
    #判断错误次数是否超过限制,如果超过限制直接返回
    #redis 用户IP地址;次数
    user_ip = request.remote_addr
    try:
        #这是byte
        access_nums = redis_store.get("access_nums_%s" % user_ip)
    except Exception as e:
        logging.error(e)
    else:
        if access_nums is not None and int(
                access_nums) >= constants.LOGIN_ERROR_MAIN_TIEMS:
            return jsonify(errno=RET.REQERR, errmsg="错误次数太多稍后重试")

    #从数据库中查询手机号是否存在
    try:
        user = User.query.filter_by(mobile=mobile).first()
    except Exception as e:
        logging.error(e)
        #查询数据库错误
        return jsonify(errno=RET.DBERR, errmsg="获取用户信息失败")
    #验证密码
    if user is None or not user.check_pwd_hash(password):
        try:
            redis_store.incr("access_nums_%s" % user_ip)
            redis_store.expire("access_nums_%s" % user_ip,
                               constants.LOGIN_ERROR_MAIN_TIEMS)  #设置过期时间
        except Exception as e:
            logging.error(e)
        return jsonify(errno=RET.DATAERR, errmsg="账号密码不匹配")
    #保存登录状态 使用session()
    session["name"] = user.name
    session["mobile"] = user.mobile
    session["id"] = user.id
    #返回
    return jsonify(errno=RET.OK, errmsg="登录成功")
def get_house_index():
    """
    获取首页房屋信息
    :return: 排序后房屋信息
    """
    # 先查询缓存
    try:
        # print("wrwerwerwer")
        result = redis_store.get("home_page_data")
    except Exception as e:
        logging.error(e)
        result = None

    if result:
        return result.decode(), 200, {"Content-Type": "pplication/json"}
    else:
        # 查询数据库,房屋订单最多的5条
        try:
            houses = House.query.order_by(House.order_count.desc()).limit(
                contants.HOME_PAGE_MAX_NUMX).all()
        except Exception as e:
            logging.error(e)
            return jsonify(errno=RET.DBERR, errmsg='查询数据库失败')

        if not houses:
            return jsonify(errno=RET.NODATA, errmsg='没有数据')

        # print(houses)
        # print(type(houses))

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

        house_dict = dict(errno=RET.OK, errmsg="ok", data=houses_list)
        json_houses = json.dumps(house_dict)
        try:
            redis_store.setex("home_page_data",
                              contants.HOME_PAGE_DATE_REDIS_EXPIRES,
                              json_houses)
        except Exception as e:
            logging.error(e)

        return json_houses, 200, {"Content-Type": "pplication/json"}
Beispiel #4
0
def get_house_datail(house_id):
    """
    获取房屋详情   house_id  房屋信息
    :return:
    """
    #当前用户    #g对象中
    user_id = session.get("user_id", "-1")  #取不到值显示-1 (未登录)

    #校验参数
    if not house_id:
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")

    #先从缓存中查询数据
    try:
        result = redis_store.get("house_info_%s" % house_id)
    except Exception as e:
        logging.error(e)
        result = None
    if result:
        return '{"errrno":%s, "errmsg":"ok", data:{"house": %s, "user_id": %s}}'%(RET.OK, result.decode(), user_id), 200, {"Content-Type":"application/json"}


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

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

    house_data = house.to_full_dict()
    #存入redis
    json_house = json.dumps(house_data)
    try:
        redis_store.setex("house_info_%s" % house_id, constants.HOUSE_DETAIL_REDIS_EXPIRES, json_house)
    except Exception as e:
        logging.error(e)


    return jsonify(errno=RET.OK, errmsg="okk", data={"house":house_data, "user_id":user_id})
Beispiel #5
0
def get_area_info():

    #用redis中读取数据
    try:
        response_json = redis_store.get("area_info")
    except Exception as e:
        logging.error(e)
    else:
        # redis有缓存数据
        if response_json is not None:
            response_json = json.loads(response_json)
            # print(response_json)
            logging.info('redis cache')
            #return response_json, 200, {"Content-Type": "application/json"}
            return jsonify(errno=RET.OK, errmsg='OK', data=response_json['data'])


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

    area_dict_li = []
    #
    for area in area_li:
        area_dict_li.append(area.to_dict())

    #将数据转成json字符串
    #response_dict = dict(errno=RET.OK, errmsg="ok", data=area_dict_li)
    response_dict = dict(data=area_dict_li)
    response_json = json.dumps(response_dict)

    try:
        redis_store.setex("area_info", constants.AREA_INFO_REDIS_CACHE_EXPIRES, response_json)
    except Exception as e:
        logging.error(e)

    #return response_json, 200, {"Content-Type": "application/json"}
    return jsonify(errno=RET.OK, errmsg="ok", data=area_dict_li)
def get_area_info():
    """
    获取城区信息
    :return:
    """
    # 从redis里读取数据
    try:
        response_json = redis_store.get("area_info")
    except Exception as e:
        logging.error(e)
    else:
        # redis 有缓存数据
        if response_json is not None:
            response_json = json.loads(response_json)
            # print(response_json)
            # print("--",type(response_json['data']))
            return jsonify(errno=RET.OK,
                           errmsg='OK',
                           data=response_json['data'])

    # 查询数据库 获取城区信息
    try:
        area_li = Area.query.all()
    except Exception as e:
        logging.error(e)
        return jsonify(errno=RET.DBERR, errmsg='数据库异常')
    area_dict_li = []
    for area in area_li:
        area_dict_li.append(area.to_dict())
    # 将数据转成json字符串
    response_dict = dict(data=area_dict_li)
    # response_dict = dict(area_dict_li)
    response_json = json.dumps(response_dict)
    try:
        redis_store.setex("area_info", contants.AREA_INFO_REDIS_CACHE_EXPIRES,
                          response_json)
    except Exception as e:
        logging.error(e)
    # print("--",type(area_dict_li))
    return jsonify(errno=RET.OK, errmsg='ok', data=area_dict_li)
Beispiel #7
0
def get_sms_code(mobile):
    """
    获取短信验证码
    :param mobile:
    :return:
    """
    #获取参数
    #图片验证码
    image_code = request.args.get('image_code')
    #uuid
    image_code_id = request.args.get('image_code_id')
    #校验参数
    if not all([image_code, image_code_id]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数不完整")

    #业务逻辑
    #从redis中取出参数
    try:
        real_image_code = redis_store.get('image_code_%s' % image_code_id)
    except Exception as e:
        logging.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:
        logging.error(e)

    #与用户填写的图片验证码对比
    real_image_code = real_image_code.decode()
    if real_image_code.lower() != image_code.lower():
        return jsonify(errno=RET.DATAERR, errmsg='图片验证码错误了!')

    #判断手机号的操作
    try:
        send_flag = redis_store.get('send_sms_code_%s' % mobile)
    except Exception as e:
        logging.error(e)
    else:
        if send_flag is not None:
            return jsonify(errno=RET.REQERR, errmasg='请求过于频繁')

    #判断手机号存在吗
    try:
        user = User.query.filter_by(mobile=mobile).first()
    except Exception as e:
        logging.error(e)
    else:
        if user is not None:
            #表示手机号被注册过
            return jsonify(error=RET.DATAEXIST, errmsg="手机号已经存在")
    #生成短信验证码
    sms_code = "%06d" % random.randint(0, 999999)
    # 保存正确的短信验证码到redis
    try:
        #redis管道
        pl = redis_store.pipeline()
        pl.setex("sms_code_%s" % mobile, constants.SMS_CODE_REDIS_EXPIRES,
                 sms_code)
        #保存发给这个手机号的记录
        pl.setex("send_cms_code_%s" % mobile, constants.SEND_SMS_CODE_EXPIRES,
                 1)
        pl.execute()
    except Exception as e:
        logging.error(e)
        return jsonify(errno=RET.DBERR, errmsg='短信验证码验证异常')

    # #发短信
    # try:
    #     ccp = CCP()
    #     result = ccp.send_message(mobile, (sms_code, int(constants.SMS_CODE_REDIS_EXPIRES/60)) ,1)
    # except Exception as e:
    #     logging.error(e)
    #     return  jsonify(errno=RET.THIRDERR, errmsg='发送异常')

    #from lghome.tasks.task_sms import send_sms
    #from lghome.tasks.sms.tasks import send_sms
    send_sms.delay(mobile,
                   (sms_code, int(constants.IMAGE_CODE_REDISE_EXPIRE / 60)), 1)
    return jsonify(errno=RET.OK, errmsg='发送成功')
def get_sms_code(mobile):
    """
    获取短信验证码
    :param mobile:
    :return:
    """
    # 获取参数
    # 图片验证
    image_code = request.args.get('image_code')
    # uuid
    image_code_id = request.args.get('image_code_id')
    # 校验参数
    if not all([image_code, image_code_id]):
        return jsonify(errno=RET.PARAMERR, errmsg='参数不完整')

    # 从redis中取出验证码
    try:
        real_image_code = redis_store.get('image_code_%s' % image_code_id)
    except Exception as e:
        logging.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:
        logging.error(e)

    # print(real_image_code)
    # 与用户填写的图片验证码对比
    print(real_image_code)
    print(image_code)
    real_image_code = real_image_code.decode()
    if real_image_code.lower() != image_code.lower():
        return jsonify(errno=RET.DATAERR, errmsg='图片验证码错误')

    # 判断手机号的操作
    try:
        send_flag = redis_store.get("send_sms_code_%s" % mobile)
    except Exception as e:
        logging.error(e)
    else:
        if send_flag is not None:
            return jsonify(errno=RET.REQERR, errmsg='请求过于频繁')


    # 判断手机号是否存在
    try:
        user = User.query.filter_by(mobile=mobile).first()
    except Exception as e:
        logging.error(e)
    else:
        if user is not None:
            # 手机号存在
            return jsonify(errno=RET.DATAEXIST, errmsg = '手机号存在')

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


    # 保存真实的短信验证码到redis
    try:
        # redis的管道
        pl = redis_store.pipeline()
        pl.setex("sms_code_%s" % mobile,contants.SMS_CODE_REDIS_EXPIRES, sms_code)
        # 保存发送这个手机号的记录
        pl.setex("send_sms_code_%s" % mobile,contants.SEND_SMS_CODE_EXPIRES, 1)
        pl.execute()

    except Exception as e:
        logging.error(e)
        return jsonify(errno=RET.DBERR, errmsg = '保存短信验证码异常')

    # 发送短信
    # try:
    #     ccp = CCP()
    #     result = ccp.send_message(mobile,(sms_code,
    #                                   int(contants.SMS_CODE_REDIS_EXPIRES/60)), 1)
    # except Exception as e:
    #     logging.error(e)
    #     return jsonify(errno=RET.THIRDERR, errmsg='发送异常')

    # from lghome.tasks.task_sms import send_sms
    from lghome.tasks.sms.tasks import send_sms
    send_sms.delay(mobile, (sms_code,int(contants.SMS_CODE_REDIS_EXPIRES/60)), 1)

    #

    # 返回值
    # if result == 0:
    return jsonify(errno=RET.OK, errmsg='发送成功')
def get_house_detail(house_id):
    """
    获取房屋的详情
    :param house_id: 房屋id
    :return:  房屋的详细信息
    """
    # 当前用户
    # g对象
    user_id = session.get("user_id", "-1")

    # 校验参数
    if not house_id:
        return jsonify(errno=RET.PARAMERR, errmsg='参数错误')

    # 查询缓存
    try:
        result = redis_store.get("house_info_%s" % house_id)
    except Exception as e:
        logging.error(e)
        # return jsonify(errno=RET.PARAMERR, errmsg='参数错误')
        result = None
    if result:
        # print("result---2:", result)
        # print("result---2:", type(result))
        # print("result---2:", result)
        print("result---3:", result.decode())
        return '{"errno":%s, "errmsg":"OK", "data":{"house": %s, "user_id": %s }}' % (
            RET.OK, result.decode(), user_id), 200, {
                "Content-Type": "pplication/json"
            }

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

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

    # print(house)

    house_data = house.to_full_dict()

    # 保存redis
    json_data = json.dumps(house_data)
    print("json:--", json_data)
    try:
        redis_store.setex("house_info_%s" % house_id,
                          contants.HOUSE_DETAIL_REDIS_EXPIRES, json_data)
    except Exception as e:
        logging.error(e)
    # print("house_data---1:", house_data)
    # print("house_data---1:", type(house_data))
    print(house)
    return jsonify(errno=RET.OK,
                   errmsg='OK',
                   data={
                       "house": house_data,
                       "user_id": user_id
                   })
Beispiel #10
0
def register():
    """
    注册
    :param: 手机号, 短信验证码  密码确认密码
    :return: json
    """
    # 接收参数
    request_dict = request.get_json()
    mobile = request_dict.get("mobile")
    sms_code = request_dict.get("sms_code")
    password = request_dict.get("password")
    password2 = request_dict.get("password2")

    # 验证
    if not all([mobile, sms_code, password, password2]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数不完整")

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

    if password != password2:
        return jsonify(errno=RET.PARAMERR, errmsg="两次密码不一致")

    # 业务逻辑处理
    # 从redis里取短信验证码
    try:
        real_sms_code = redis_store.get("sms_code_%s" % mobile)
    except Exception as e:
        logging.error(e)
        return jsonify(errno=RET.PARAMERR, errmsg="读取短信验证码异常")
    # 判断短信验证码是否过期
    if real_sms_code is None:
        return jsonify(errno=RET.NODATA, errmsg="读取短信验证码失效")

    # 删除redis中的短信验证码
    try:
        redis_store.delete("sms_code_%s" % mobile)
    except Exception as e:
        logging.error(e)

    # 判断用户填写的验证码的正确性
    real_sms_code = real_sms_code.decode()
    if real_sms_code != sms_code:
        return jsonify(errno=RET.DATAERR, errmsg="读取短信验证码错误")
    # 手机号是否被注册过
    # try:
    #     user = User.query.filter_by(mobile=mobile).first()
    # except Exception as e:
    #     logging.error(e)
    # else:
    #     if user is not None:
    #         # 手机号存在
    #         return jsonify(errno=RET.DATAEXIST, errmsg = '手机号存在')
    # 保存数据

    user = User(name=mobile, mobile=mobile)

    user.password = password

    # user.pwd_hash(password)
    try:
        db.session.add(user)
        db.session.commit()
    except IntegrityError as e:
        db.session.rollback()
        logging.error(e)
        return jsonify(errno=RET.DATAEXIST, errmsg='手机号已经存在')
    except Exception as e:
        # 回滚
        db.session.rollback()
        logging.error(e)
        return jsonify(errno=RET.DBERR, errmsg='插入数据库异常')

    # user.password_hash()
    # 加密密码
    # md5 加密

    # 保存登录状态到session中
    session['name'] = mobile
    session['mobile'] = mobile
    session['user_id'] = user.id
    # 返回结果
    return jsonify(errno=RET.OK, errmsg='注册成功')
Beispiel #11
0
def register():
    """
    :param  手机号 短信验证码 密码 确认密码
    :return:  json
    """
    #接收参数
    request_dict = request.get_json()
    mobile = request_dict.get("mobile")
    sms_code = request_dict.get("sms_code")
    password = request_dict.get("password")
    password2 = request_dict.get("password2")

    #验证
    if not all([mobile, sms_code, password, password2]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数不完整")

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

    if password != password2:
        return jsonify(errno=RET.PARAMERR, errmsg="两次密码不一致")

    #业务逻辑
    #从redis中取短信验证码
    try:
        real_sms_code = redis_store.get("sms_code_%s" % mobile)
    except Exception as e:
        logging.error(e)
        return jsonify(errno=RET.DBERR, errmsg="读取短信验证码异常")

    #判断短信验证码是否过期
    if real_sms_code is None:
        return jsonify(errno=RET.NODATA, errmsg="短信验证码失效")
    # 删除redis中的短信验证码
    try:
        redis_store.delete("sms_code_%s" % mobile)
    except Exception as e:
        logging.error(e)
    # 验证码的正确性
    real_sms_code = real_sms_code.decode()  #转码一下
    if real_sms_code != sms_code:
        return jsonify(errno=RET.DATAERR, errmsg="短信验证码错误")
    # 手机号是否注册过
    # try:
    #     user = User.query.filter_by(mobile=mobile).first()
    # except Exception as e:
    #     logging.error(e)
    # else:
    #     if user is not None:
    #         #表示手机号被注册过
    #         return jsonify(error=RET.DATAEXIST, errmsg="手机号已经存在")
    # 保存数据
    user = User(name=mobile, mobile=mobile)
    user.password = password
    #user.generate_pwd_hash(password)
    #User(name=mobile, mobile=mobile, password_hash="加密后的密码")
    try:
        db.session(user)
        db.session.commit()

    except IntegrityError as e:
        db.session.rollback()
        logging.error(e)
        return jsonify(errno=RET.DATAEXIST, errmsg="手机号已经存在")
    except Exception as e:
        #回滚
        db.session.rollback()
        logging.error(e)  #记录异常
        return jsonify(errno=RET.DBERR, errmsg="插入数据库异常")
    #, password_hash=password
    #保存登录状态到session中
    session["name"] = mobile
    session["mobile"] = mobile
    session["user_id"] = user.id
    #返回结果
    return jsonify(errno=RET.OK, errmsg="注册成功")