예제 #1
0
def user_reg():
    params = register_schema(request.json or '')
    phone = params.get('phone')
    password1 = params.get('password1')
    password2 = params.get('password2')
    sms_code = params.get('sms_code')

    if password1 != password2:
        return common_response(SysStatus.FAIL, None, '密码不一致')

    real_sms_code = redis_store.get('{}-sms'.format(phone))
    print("sms-code for {}: {}".format(phone, real_sms_code))
    if sms_code != real_sms_code:
        return common_response(SysStatus.FAIL, None, '短信校验码错误')

    password = gen_md5(password1)

    user = User.query.filter(User.phone == phone).first()
    if user:
        return common_response(SysStatus.FAIL, user.phone, '该手机号已注册')
    else:
        user = User(phone=phone, password=password)
        db.session.add(user)
        db.session.commit()
        redis_store.delete('{}-sms'.format(phone))
        return common_response(SysStatus.FAIL, None, '注册成功')
예제 #2
0
def user_login():
    params = login_schema(request.json or '')
    phone = params.get('phone')
    password = params.get('password')
    # password = gen_md5(password) # TODO:正式环境加密密码

    user = User.query.filter(User.phone == phone, User.password == password).first()
    if user:
        session['user_id'] = user.id
        return common_response(SysStatus.SUCCESS, user.name, '登录成功')
    else:
        return common_response(SysStatus.FAIL, None, '账号号或密码错误')
예제 #3
0
def query_user_plants():
    """
       @api {GET} /api/v0/plants/user 获取用户植物信息
       @apiName query_user_plants
       @apiGroup farm
       @apiSuccess {number} sys_status 状态码
       @apiSuccess {string} message  返回的信息
       @apiSuccess {dict} data 返回的数据
       @apiSuccess {int} id 编号
       @apiSuccess {int} plant_id 植物ID
       @apiSuccess {bool} active_flag 是否激活
       @apiSuccess {int} water 水
       @apiSuccess {int} fertilizer 肥料
       @apiSuccess {int} pesticide 药物
       @apiSuccess {string} price 价格
       @apiSuccess {string} harvest_at 预计收获时间
       @apiSuccess {dict} status 状态
       @apiSuccess {string} image 预览图
       @apiSuccess {string} created_at 创建时间
       @apiSuccess {string} name 名称
       @apiSuccess {string} category 分类
       @apiSuccessExample Success-Response:
      {"sys_status": "SUCCESS", "data": {"pages": 1, "items": [{"plant_id": 1, "active_flag": true, "water": 80, "fertilizer": 54, "pesticide": 0, "price": "1.02", "harvest_at": "2019-05-31", "status": {"HEALTHY": "健康"}, "image": "http://image.antns.com/uploads/20171220/12/1513742641-BghOtrbPHu.jpg", "created_at": "2019-05-14 10:27:51", "name": "白菜", "category": "蔬菜"}, {"plant_id": 2, "active_flag": true, "water": 50, "fertilizer": 4, "pesticide": 0, "price": "2.00", "harvest_at": "2019-06-14", "status": {"HEALTHY": "健康"}, "image": null, "created_at": "2019-05-14 15:50:44", "name": "萝卜", "category": "蔬菜"}]}, "message": "成功"}
       @apiErrorExample Error-Response:
        {"data": null,"message": "失败","sys_status": 1}

    """
    params = request.args
    page_index, page_size = paginate_schema(params)
    user_plants = UserPlant.query.join(Plant).filter(
        UserPlant.user_id == 1).with_entities(*user_plant_basic_ser).order_by(
            UserPlant.id).paginate(page=page_index, per_page=page_size)
    data = {'pages': user_plants.pages, 'items': user_plants.items}
    return common_response(SysStatus.SUCCESS, data, None)
예제 #4
0
def query_all_plants():
    """
       @api {GET} /api/v0/plants/all 获取所有植物
       @apiName get_wechat_payinfo
       @apiGroup wechatpay
       @apiSuccess {number} sys_status 状态码
       @apiSuccess {string} message  返回的信息
       @apiSuccess {dict} data 返回的数据
       @apiSuccess {string} appId 小程序ID
       @apiSuccess {string} timeStamp 时间戳
       @apiSuccess {string} nonceStr 随机串
       @apiSuccess {string} signType 签名方式
       @apiSuccess {string} package 数据包
       @apiSuccess {bool} paid 是否已支付,True 是, False 否
       @apiSuccessExample Success-Response:
      {"sys_status": "SUCCESS", "data": {"appId": "wx2993c8765d5647a9", "timeStamp": "1536301397", "nonceStr": "9azOH5IFQP1Yd7yVAqXMtgUirNLwElSs", "signType": "MD5", "package": "prepay_id=wx20180907142317490531", "paySign": "6D57EA634634A84DC352472576D47586", "paid":false}, "message": "\u6210\u529f"}
       @apiErrorExample Error-Response:
        {"data": null,"message": "失败","sys_status": 1}

    """
    params = request.args
    page_index, page_size = paginate_schema(params)
    plants = Plant.query.with_entities(*plant_basic_ser).order_by(
        Plant.id).paginate(page=page_index, per_page=page_size)
    data = {'pages': plants.pages, 'items': plants.items}
    return common_response(SysStatus.SUCCESS, data, None)
예제 #5
0
    def wrapper(*args, **kwargs):

        # 需求:
        # 获取session里面uers_id
        user = None

        user_id = session.get("user_id")
        if not user_id:
            user_id = 1  # TODO:方便测试,未登陆用户id为1,登陆状态为是True
            login = True
        # 根据user_id查询用户数据
        try:
            # 延迟导入 解决循环导入问题
            from api.models.user import User
            if user_id:
                user = User.query.get(user_id)
                if not user:
                    return common_response(SysStatus.FAIL, None, '请先登陆')
        except Exception as e:
            current_app.logger.error(e)
        # 3.保存数据后在view_func函数中能够获取到用户数据(*******)
        # g.user = user
        # 在进入实现函数里面由于是处于同一个request请求,里面就能够获取g对象中的临时变量
        result = view_func(user, **kwargs)
        return result
예제 #6
0
def feed_plant(user):
    """
       @api {GET} /api/v0/plants/feed 浇水/施肥/播种
       @apiName buy
       @apiGroup farm
       @apiParam {number} item_id 物品id
       @apiParam {number} plant_id 植物id
       @apiParam {string} type 物品类型(FERTILIZER, PESTICIDE, SEED)
       @apiParam {number} quantity 数量
       @apiSuccess {number} sys_status 状态码
       @apiSuccess {string} message  返回的信息
       @apiSuccessExample Success-Response:
      {"sys_status": "SUCCESS", "data": null, "message": "成功"}
       @apiErrorExample Error-Response:
        {"data": null,"message": "失败","sys_status": 1}

    """
    params = request.args
    params = buy_schema(params)
    item_id = params.get('item_id')
    type = params.get('type')
    quantity = params.get('quantity')

    if type == 'SEED':
        item = Seed
    elif type == 'FERTILIZER':
        item = Seed  # TODO:肥料
    elif type == 'PESTICIDE':
        item = Seed  # TODO:药物
    else:
        item = Seed
    exist = db.session.query(
        item.query.filter(item.id == item_id).exists()).scalar()
    if not exist:
        return common_response(SysStatus.FAIL, None, "物品不存在")
    total_price = item.query.filter(item.id == item_id).with_entities(
        item.price).first()

    user_coin = UserPlantCoin.query.filter(
        UserPlantCoin.user_id == user.id).first()
    if total_price.price * quantity > user_coin:
        return common_response(SysStatus.FAIL, None, "金币不足")

    return common_response(SysStatus.SUCCESS, None, "购买成功")
예제 #7
0
def sms():
    params = sms_schema(request.json or '')
    phone = params.get('phone')

    sms_code = random.randint(0, 999999)
    sms_code = "%06d" % sms_code

    redis_store.set('{}-sms'.format(phone), sms_code, 60 * 5)
    print(sms_code)
    return common_response(SysStatus.SUCCESS, None, '发送成功')
예제 #8
0
def user_info_get(user):
    user_id = user.id
    user = User.query.filter(User.id == user_id).with_entities(User.id,
                                                               User.avatar,
                                                               User.name,
                                                               User.phone,
                                                               User.birthday,
                                                               User.gender,
                                                               User.email,
                                                               ).first()

    return common_response(SysStatus.SUCCESS, user, None)
예제 #9
0
def lost_pass():
    params = register_schema(request.json or '')
    phone = params.get('phone')
    password_old = params.get('password_old')
    password = params.get('password')
    sms_code = params.get('sms_code')

    real_sms_code = redis_store.get('{}-sms'.format(phone))
    print("sms-code for {}: {}".format(phone, real_sms_code))
    if sms_code != real_sms_code:
        return common_response(SysStatus.FAIL, None, '短信校验码错误')

    # password = gen_md5(password1) TODO: 正式环境不要明文保存密码
    # password_old = gen_md5(password_old) TODO: 正式环境不要明文保存密码

    user = User.query.filter(User.phone == phone, User.password == password_old).first()
    if user:
        user.password = password
        user.save()
        redis_store.delete('{}-sms'.format(phone))
    else:
        return common_response(SysStatus.FAIL, None, '原密码错误')
예제 #10
0
def query_pictures():
    """
       @api {GET} /api/v0/index/pictures 首页轮播图
       @apiName query_pictures
       @apiGroup index
       @apiSuccess {number} sys_status 状态码
       @apiSuccess {string} message  返回的信息
       @apiSuccess {dict} data 返回的数据
       @apiSuccess {string} appId 小程序ID
       @apiSuccess {string} timeStamp 时间戳
       @apiSuccess {string} nonceStr 随机串
       @apiSuccess {string} signType 签名方式
       @apiSuccess {string} package 数据包
       @apiSuccess {bool} paid 是否已支付,True 是, False 否
       @apiSuccessExample Success-Response:
      {"sys_status": "SUCCESS", "data": {"appId": "wx2993c8765d5647a9", "timeStamp": "1536301397", "nonceStr": "9azOH5IFQP1Yd7yVAqXMtgUirNLwElSs", "signType": "MD5", "package": "prepay_id=wx20180907142317490531", "paySign": "6D57EA634634A84DC352472576D47586", "paid":false}, "message": "\u6210\u529f"}
       @apiErrorExample Error-Response:
        {"data": null,"message": "失败","sys_status": 1}

    """
    demo_data = [{
        "id": 1,
        "name": "meinv10",
        "img_num": 10,
        "img_src": "https://picsum.photos/id/521/414/165"
    }, {
        "id": 2,
        "name": "meinv05",
        "img_num": 10,
        "img_src": "https://picsum.photos/id/431/414/165"
    }, {
        "id": 3,
        "name": "fengjing04",
        "img_num": 10,
        "img_src": "https://picsum.photos/id/65/414/165"
    }]

    return common_response(SysStatus.SUCCESS, demo_data, None)
예제 #11
0
def user_info_put(user):
    params = request.json or ''
    params = user_info_schema(params)
    user.update(params)
    user.save()
    return common_response(SysStatus.SUCCESS, user, None)
예제 #12
0
def article_recommend():
    # article = Article.query.filter(Article.id == 1).all().paginate(page=1, per_page=5)
    data = {'user_id': 1, 'name': 'Gemingyu'}
    return common_response(SysStatus.SUCCESS, data, None)
예제 #13
0
def handle_exception(error):
    return common_response(SysStatus.PARAMETER_CHECK_ERROR, None, str(error))