Gifts.php 4.2 KB
<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use think\Validate;
/**
 * 礼品接口**
 */
class Gifts extends Api
{
    protected  $noNeedLogin = [];
    protected $noNeedRight = '*';
    protected $user_id = '';//token存贮user_id
    protected $normal = '';//商品正常状态,1:下架
    public function _initialize()
    {
        parent::_initialize();
        $this->normal = config('site.goods_status');
        $this->user_id = $this->auth->getUserId();
    }

    /**
     * @ApiTitle    (礼品列表)
     * @ApiSummary  (礼品列表)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/gifts/getGiftsList)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="page", type="integer", required=true, description="分页页码")
     * @ApiReturn ({
            "code": 1,
            "msg": "成功",
            "time": "1553836021",
            "data": {
            "my_score": 99,
            "data": [
                {
                    "id": 1,
                    "title": "礼品标题1",//礼品名称
                    "score": 100,//礼品积分
                    "images": "/uploads/20190319/74cf9cbb633386b889f15a9797169b7d.jpg,/uploads/20190319/4e98e7e8bb1fa28db4f7a49df3159938.jpg"
                },
                {
                    "id": 2,
                    "title": "礼品标题2",
                    "score": 100,
                    "images": "/uploads/20190319/74cf9cbb633386b889f15a9797169b7d.jpg,/uploads/20190319/4e98e7e8bb1fa28db4f7a49df3159938.jpg"
                }
            ]
            }
            })
     */
    public function getGiftsList(){
        if($this->request->isGet()){
            $page = $this->request->get('page');//分页页码
            $limit = config('site.page_limit');//分页限制数量
            $rule = config('site.gift_pages');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['page'=>$page])) {
                $this->error($validate->getError());
            }
            $myScore = Db::table('gc_user')
                ->where(['id'=>$this->user_id,'status'=>'normal'])
                ->field('score')
                ->find();
            $data = Db::table('gc_gift')
                ->where(['status'=>$this->normal])
                ->page($page,$limit)
                ->field('id,title,score,images')
                ->select();
            $this->success('成功', ['my_score'=>$myScore['score'],'data'=>$data]);
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (礼品详情)
     * @ApiSummary  (礼品详情)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/gifts/giftsDetail)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="integer", required=true, description="礼品id")
     * @ApiReturn ({
            "code": 1,
            "msg": "成功",
            "time": "1553777266",
            "data":[
                {
                    "id": 1,
                    "title": "礼品标题1",//礼品名称
                    "stock": 1000,//礼品库存
                    "score": 100,//礼品所需积分
                    "description": " ★注:各种废旧五金、电子、塑胶等",//礼品详情
                    "images": "/uploads/20190319/74cf9cbb633386b889f15a9797169b7d.jpg,/uploads/20190319/4e98e7e8bb1fa28db4f7a49df3159938.jpg"
                }
            ]
            })
     */
    public function giftsDetail(){
        if($this->request->isGet()){
            $gifts_id = $this->request->get('id');//礼品id
            $rule = config('site.goods');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['id'=>$gifts_id])) {
                $this->error($validate->getError());
            }
            $data = Db::table('gc_gift')
                ->where(['id'=>$gifts_id,'status'=>$this->normal])
                ->field('id,title,stock,score,description,images')
                ->select();
            $this->success('成功', $data);
        }else{
            $this->error('请求方式错误');
        }
    }
}