Beispiel #1
0
def get_house_index():
    try:
        ret = redis_store.get('home_page_data')
    except Exception as e:
        current_app.logger.error(e)
        ret = None
    if ret:
        return '{"errno":0, "errmsg":"OK", "data":%s}' % ret, 200, {
            "Content-Type": "application/json"
        }

    try:
        houses = House.query.order_by(House.order_count.desc()).limit(
            constant.HOME_INDEX_PAGE)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询数据库异常')
    if not houses:
        return jsonify(errno=RET.NODATA, errmsg='查询无数据')
    #若房屋没有主图,则不展示该图片
    houses_list = []
    for house in houses:
        if not house.index_image_url:
            continue
        houses_list.append(house.to_myhouse_info())
    json_houses = json.dumps(houses_list)
    try:
        redis_store.setex('home_page_data',
                          constant.HOME_PAGE_DATA_REDIS_EXPIRES, json_houses)
    except Exception as e:
        current_app.logger.error(e)
    return '{"errno":0, "errmsg":"OK", "data":%s}' % json_houses, 200, {
        "Content-Type": "application/json"
    }
Beispiel #2
0
def login():
    '''
    用户登录
    手机号,密码
    :return:
    '''
    #手机号格式,错误次数限制
    req_dict=request.get_json()
    mobile =req_dict.get('mobile')
    password =req_dict.get('password')
    if not all([mobile,password]):
        return jsonify(errno=RET.PARAMERR, errmsg='参数不完整')

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

    #错误次数限制ip,redis记录 ’access_nums_请求id地址‘:次数
    ip =request.remote_addr
    try:
        access_nums = redis_store.get('access_nums_%s'%ip)

    except Exception as e:
        current_app.logger.error(e)
    else:
        if access_nums is not None and int(access_nums) >=constant.LOGIN_ERROR_MAX_TIMES:
            return jsonify(errno=RET.REQERR, errmsg='错误次数过多,请稍后重试')

    #从数据库中根据手机号查询用户的数据对象
    try:
        user =User.query.filter_by(mobile=mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='获取用户信息失败')

    if user is None or  not user.check_password(password):
        #验证失败,记录错误次数
        try:
            redis_store.incr('access_nums_%s'%ip)
            redis_store.expire('access_nums_%s'%ip,constant.LOGIN_ERROR_FORBID_TIME)
        except Exception as e:
            current_app.logger.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 #3
0
def get_house_detail(house_id):
    '''获取房屋详情'''
    user_id = session.get('user_id')

    if not house_id:
        return jsonify(errno=RET.PARAMERR, errmsg='参数缺失')
    # 尝试从redis中读取数据
    try:
        ret = redis_store.get('house_inf_%s' % house_id)
    except Exception as e:
        current_app.logger.error(e)
        ret = None
    if ret:
        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_house_detail()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='数据出错')
    #存入到redis中
    json_house = json.dumps(house_data)
    try:
        redis_store.setex('house_info_%s' % house_id,
                          constant.HOUSE_DETAIL_REDIS_EXPIRE_SECOND,
                          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
Beispiel #4
0
def get_area_info():
    '''获取城区信息'''
    #查询数据库,读取城区信息
    #尝试从redis中读取数据
    try:
        resp_json = redis_store.get('area_info')
    except Exception as e:
        current_app.logger.error(e)
    else:
        if resp_json is not None:
            #有缓存数据

            return resp_json, 200, {'Content-Type': 'application/json'}
    try:
        area_li = Area.query.all()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='数据库异常')
    area_dict_li = []
    #将对象的属性转换成字典值,存储在列表中
    for area in area_li:
        area_dict_li.append(area.to_dict())

    #将数据转换为json字符串
    resp_dict = dict(errno=RET.OK, errmsg='OK', data=area_dict_li)
    resp_json = json.dumps(resp_dict)
    #将数据保存到redis中

    try:
        redis_store.setex('area_info', constant.AREA_INFO_REDIS_CACHE_EXPIRES,
                          resp_json)
    except Exception as e:
        current_app.logger.error(e)
    return '{"errno":0, "errmsg":"OK", "data":%s}' % resp_json, 200, {
        "Content-Type": "application/json"
    }
Beispiel #5
0
def register():
    '''
    注册逻辑,请求参数,手机号,短信验证码,密码
    参数格式:json
    :return:
    '''
    #获取请求的json数据,返回字典
    req_dict=request.get_json()
    mobile =req_dict.get('mobile')
    sms_code =req_dict.get('sms_code')
    password =req_dict.get('password')
    password2 = req_dict.get('password2')
    #校验参数
    if not all([mobile,sms_code,password]):
        return jsonify(errno=RET.PARAMERR,errmsg='参数不完整')

    #判断手机号格式
    if not re.match(r'1[34578]\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:
        current_app.logger.error(e)
        return jsonify(RET.DBERR,errmsg='去读redis短信验证码异常')
    #删除redis中的短信验证码,防止重复使用校验
    try:
        redis_store.delete('sms_code_%s'%mobile)
    except Exception as e:
        current_app.logger.error(e)


    if real_sms_code is None:
        return jsonify(errno=RET.NODATA,errmsg='短信验证码失效')
    #判断手机号是否注册过
    try:
        user = User.query.filter_by(mobile=mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR,errmsg ='数据库异常')
    else:
        if user is not None:
            return jsonify(errno=RET.DATAEXIST, errmsg='手机号已存在')
    #保存数据
    user =User(name =mobile,mobile=mobile)
    user.password_hash=password #设置密码
    try:
        db.session.add(user)
        db.session.commit()
    #保存登录状态
    except IntegrityError as e:
        #表示手机号出现重复值,即手机号已注册过
        db.session.rollback()
        current_app.logger.error(e)
        return jsonify(errno=RET.DATAEXIST, errmsg='手机号已存在')
    except Exception as e:
        db.session.rollback()
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询数据库异常')
    #保存登录状态到session中
    session['name']=mobile
    session['mobile']=mobile
    session['user_id']=user.id
    return jsonify(errno=RET.OK, errmsg='注册成功')
Beispiel #6
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:
        print('code:', 'image_code_%s' % image_code_id)
        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='图片验证码失效')

    #删除图片验证码,防止用户使用同一个图片验证码验证多次防止撞库等行为的攻击,
    try:
        redis_store.delete('image_code_%s' % image_code_id)
    except Exception as e:
        current_app.logger.error(e)
    #与用户填写的信息进行对比
    if real_image_code.lower() != image_code.lower():
        return jsonify(errno=RET.DATAERR, errmsg='图片验证码错误')

    #判断这个手机号是否在60s内发送给短信
    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:
            return jsonify(errno=RET.REQERR, errmsg='请求过于频繁,请60s后再试')

    #判断手机号是否存在
    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,
                          constant.SMS_CODE_REDIS_EXPIRES, sms_code)
        #保存发送给这个手机号的记录,防止用户在60s内再次发出短信的操作
        redis_store.setex('send_sms_code_%s' % mobile,
                          constant.SEND_SMS_CODE_INTERVAL, 1)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='保存短信验证码异常')

    #发送短信
    #使用celery异步发送短信,delay函数调用后立即返回
    result = send_sms.delay(
        mobile, [sms_code, int(constant.SMS_CODE_REDIS_EXPIRES / 60)], 1)
    #返回异步执行结果对象
    # print (result.id)
    #通过get方法能获取异步执行的结果
    #get方法默认是阻塞的行为,会等到有执行结果之后才返回
    #get方法页接受参数timeout,超时时间,超过超时时间还拿不到结果则返回
    # ret =result.get()
    # print (ret)
    #发送成功
    return jsonify(errno=RET.OK, errmsg='发送成功')