コード例 #1
0
ファイル: car.py プロジェクト: 316685881/cab
def get_areas():
    # 首先从redis中拿数据
    try:
        resp_json = redis_connect1.get('areas_info')
    except Exception as e:
        current_app.logger.error(e)
    else:
        if resp_json is not None:
            # print('area缓存数据')
            return resp_json, 200, {"Content-Type": "application/json"}

    # 查询数据库
    try:
        areas = Area.query.all()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='数据库异常')

    data_list = []
    for area in areas:
        data_list.append(area.area_to_dict())

    # 设置缓存
    # 把响应结果都存入redis
    resp_dict = dict(errno=RET.OK, errmsg='查询地区数据ok', data=data_list)
    resp_json = json.dumps(resp_dict)

    try:
        redis_connect1.setex('areas_info', AREA_INFO_EXPIRES, resp_json)
    except Exception as e:
        current_app.logger.error(e)

    return resp_json, 200, {"Content-Type": "application/json"}
コード例 #2
0
ファイル: car.py プロジェクト: 316685881/cab
def get_facilities():
    # 首先从redis中拿数据
    try:
        resp_json = redis_connect1.get('facilities')
    except Exception as e:
        current_app.logger.error(e)
    else:
        if resp_json is not None:
            # print('area缓存数据')
            return resp_json, 200, {"Content-Type": "application/json"}

    # 查询数据库
    try:
        facilities = Facility.query.all()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='数据库异常')

    data_list = []
    for facility in facilities:
        data_list.append(facility.to_dict())

    # 设置缓存
    # 把响应结果都存入redis
    resp_dict = dict(errno=RET.OK, errmsg='查询配置数据ok', data=data_list)
    resp_json = json.dumps(resp_dict)

    try:
        redis_connect1.setex('facilities', FACILITY_INFO_EXPIRES, resp_json)
    except Exception as e:
        current_app.logger.error(e)

    return resp_json, 200, {"Content-Type": "application/json"}
コード例 #3
0
ファイル: car.py プロジェクト: 316685881/cab
def get_car_detail(car_id):
    # print(car_id)
    # 获取当前用户id
    user_id = session.get('user_id', '-1')

    # 先从redis获取数据
    try:
        car_json = redis_connect1.get('car_info_%s' % car_id)
    except Exception as e:
        current_app.logger.error(e)
        car_json = None

    if car_json:
        # print('detail--redis数据')
        return jsonify(errno=RET.OK,
                       errmsg='车辆详情查询成功',
                       data={
                           'car': json.loads(car_json),
                           'user_id': user_id
                       })

    else:
        # 查询数据库
        try:
            car = Car.query.filter_by(id=car_id).first()
        except Exception as e:
            current_app.logger.error(e)
            return jsonify(errno=RET.DBERR, errmsg='数据库错误,获取数据失败')

        if car is None:
            return jsonify(errno=RET.NODATA, errmsg='无车辆信息')

        car_json = json.dumps(car.to_full_dict())

        # 缓存到redis中
        try:
            redis_connect1.setex('car_info_%s' % car_id,
                                 HOUER_DETAIL_INFO_EXPIRES, car_json)
        except Exception as e:
            current_app.logger.error(e)

        return jsonify(errno=RET.OK,
                       errmsg='车辆详情查询成功',
                       data={
                           'car': car.to_full_dict(),
                           'user_id': user_id
                       })
コード例 #4
0
ファイル: car.py プロジェクト: 316685881/cab
def get_index_cars():
    # 首先从redis缓存中拿数据
    try:
        cars_json = redis_connect1.get('index_cars')
    except Exception as e:
        current_app.logger.error(e)
        cars_json = None
    # 如果redis缓存有数据
    if cars_json:
        # print('redis数据')
        return jsonify(errno=RET.OK, errmsg='查询成功', data=json.loads(cars_json))

    else:
        # redis缓存中无数据,查询数据库,查询有默认图片的5个车辆,按照订单数量降序
        try:
            cars = Car.query.order_by(Car.order_count.desc()).filter(Car.index_image_url.isnot(None))\
                .limit(INDEX_PAGE_CAR_MAX_COUNT).all()
            # cars = Car.query.all()

        except Exception as e:
            current_app.logger.error(e)
            return jsonify(errno=RET.DBERR, errmsg='数据库异常,查询失败')

        # 如果没有车辆信息
        if not cars:
            return jsonify(errno=RET.NODATA, errmsg='无数据')

        # 车辆对象转换成字典
        car_list = []
        for car in cars:
            # print(car)
            car_list.append(car.to_base_dict())

        # 字典列表转换成json数据
        cars_json = json.dumps(car_list)

        # 车辆json数据存入redis中
        try:
            redis_connect1.setex('index_cars', INDEX_PAGE_CAR_EXPIRES,
                                 cars_json)
        except Exception as e:
            current_app.logger.error(e)
            return jsonify(errno=RET.DBERR, errmsg='数据库异常,保存失败')

        # return '{"errno": 0, "errmsg": "查询成功", "data":%s}'%cars_json, 200, {"Content-Type": "application/json"}  #这种方法不行
        return jsonify(errno=RET.OK, errmsg='查询成功', data=car_list)
コード例 #5
0
ファイル: send_sms.py プロジェクト: 316685881/cab
def send_sms(phone):
    # 验证图片验证码
    # 获取前段传来的验证码及id
    image_code = request.args.get('image_code')
    image_code_id = request.args.get('image_code_id')
    print('get: ' + image_code)

    # 验证参数完整性
    # 参数不完整
    if not all([image_code, image_code_id]):
        return jsonify(errno=response_code.RET.PARAMERR, errmsg='验证码参数不完整')

    # 参数完整,验证图片验证码
    # 从数据库中获取验证码
    try:
        real_image_code = redis_connect1.get('image_code_%s'%image_code_id)
        # print(real_image_code)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=response_code.RET.DBERR, errmsg='验证码已过期')

    # 如果验证码为空
    if real_image_code is None:
        return jsonify(errno=response_code.RET.NODATA, errmsg='验证码已过期')

    # 删除验证码,防止一个验证码用两次
    try:
        redis_connect1.delete('image_code_%s' % image_code_id)
    except Exception as e:
        current_app.logger.error(e)

    print('redis: ' + real_image_code)
    if image_code.upper() != real_image_code.upper():
        # print(image_code+'--'+real_image_code)
        return jsonify(errno=response_code.RET.PARAMERR, errmsg='图片验证码错误')

    else:
        print('图片验证码ok')

#   此时验证码验证通过,发送短信验证码
#     判断60s内是否发过短信
    try:
        send_flag = redis_connect1.get('send_phone_%s' % phone)
    except Exception as e:
        current_app.logger.error(e)
    else:
        if send_flag is not None:
            return jsonify(errno=response_code.RET.REQERR, errmsg='操作频繁,请稍后再试')

#
#   判断手机号是否注册过
    try:
        user = User.query.filter_by(mobile=phone).first()
    except Exception as e:
        current_app.logger.error(e)
    else:
        if user is not None:
            return jsonify(errno=response_code.RET.DATAEXIST, errmsg='此手机号已经注册')


    # 发送短信
    # 构造参数
    # 随机生产一个整数,不满6位则在前面补0
    sms_code = '%06d'%random.randint(0, 999999)
    # 保存短信验证码信息到redis中
    try:
        redis_connect1.setex('sms_code_%s' % phone, SMS_CODE_EXPIRES, sms_code)
        redis_connect1.setex('send_phone_%s' % phone, SEND_SMS_EXPIRES, 1)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=response_code.RET.DBERR, errmsg='保存短信验证码失败')

    data = [sms_code, int(SMS_CODE_EXPIRES/60)]

    # 原始发送短信代码****************************
    ccp = CCP()
    try:
        result = ccp.send_template_sms(phone, data, 1)
        # print(type(result))
        print(result)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=response_code.RET.THIRDERR, errmsg='短信发送异常')
    else:

        if result.get('statusCode') == '000000':
            return jsonify(errno=response_code.RET.OK, errmsg='短信已发送')
        else:
            return jsonify(errno=response_code.RET.THIRDERR, errmsg='短信发送失败')


# #   使用celery发送短信******************************
#     celery_tasks.send_sms.delay(phone, data, 1)

    return jsonify(errno=response_code.RET.OK, errmsg='短信已发送')