Goods.php 6.9 KB
<?php


namespace app\api\controller;


use app\api\model\GoodsComment;
use app\api\model\GoodsSpec;
use app\api\model\GoodsSpecRel;
use app\common\controller\Api;
use think\Db;

/**
 * 商品页面
 */
class Goods extends Api
{
    protected $noNeedRight = ['*'];
    protected $noNeedLogin = ['*'];


    /**
     * @ApiTitle    (商品详情)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name=goods_id, type=integer, required=true, description="商品id")
     * @ApiReturn   ({
    'code':'1',
    'msg':'商品详情'
    'data':{
    "goods_id": 22,商品id
    "goods_name": "Mate 20 华为 HUAWEI ",
    "spec_type": "20", 10=单规格20=多规格
    "brand": null, 品牌
    "makefor": null, 进口国产
    "packing": null, 包装方式
    "keep": null, 保存条件
    "number": null, 编号
    "price_description": null, 价格说明
    "sales_actual": 64, 销量
    "price": "4499.00", 价格
    "line_price": "0.00", 划线价
    "images_text": [
        轮播图
    ],
    "down_image_text": "底部图",
    "four_image_text": [
        四宫格图
    ]
    }
    })
     */
    public function goodsDetail()
    {
        $goods_id = $this->request->post('goods_id');
        $goodsmodel = new \app\api\model\Goods();
        if (!is_numeric($goods_id)){
            $this->error('商品id不合法');
        }
        $goods = $goodsmodel::get($goods_id);
        $goods_spec = Db::name('litestore_goods_spec')->where('goods_id',$goods['goods_id'])->find();
        $goods['price'] = $goods_spec['goods_price'];
        $goods['line_price'] = $goods_spec['line_price'];
        $this->success('商品详情',$goods);
    }


    /**
     * @ApiTitle    (商品规格)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name=goods_id, type=integer, required=true, description="商品id")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    'data':{
    // 规格组合完毕的列表
    "list": [
    {
    "goods_spec_id": 103,
    "goods_id": 22,
    "goods_no": "SNHW001",
    "goods_price": "4499.00",
    "line_price": "0.00",
    "stock_num": 941,
    "goods_sales": 58,
    "goods_weight": 500,
    "spec_sku_id": "44_46", // 搜索字段 组合sku里面的id搜索 从小到大排序
    "spec_image": "",
    "create_time": 1542784591,
    "update_time": 1543242861
    }
    ],
    // 规格展示的列表
    "sku": [
    {
    "name": "颜色",
    "second": [
    {
    "id": 44,
    "name": "亮黑色"
    }
    ]
    },
    {
    "name": "内存",
    "second": [
    {
    "id": 46,
    "name": "6GB+64GB"
    }
    ]
    }
    ]
    }
    })
     */
    public function goodsSku()
    {
        $goods_id = $this->request->post('goods_id');
        $goodsspecrelmodel = new GoodsSpecRel();
        $list = $goodsspecrelmodel
            ->where('goods_id',$goods_id)
            ->select();
        $array = [];
        foreach ($list as $key => $value){
            if (!isset($array[$value['spec_id']])){
                $array[$value['spec_id']]['name'] = Db::name('litestore_spec')
                    ->where('id',$value['spec_id'])
                    ->value('spec_name');
            }
            $spec_value =Db::name('litestore_spec_value')
                ->where('id',$value['spec_value_id'])
                ->value('spec_value');
            $array[$value['spec_id']]['second'][] = [
                'id' => $value['spec_value_id'],
                'name' => $spec_value
            ];
        }
        $array = array_values($array);
        $goods_spec = GoodsSpec::all(['goods_id'=>$goods_id]);
        $this->success('商品规格',['list'=>$goods_spec,'sku'=>$array]);
    }



    /**
     * @ApiTitle    (商品详情页评价)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name=goods_id, type=integer, required=true, description="商品id")
     * @ApiReturn   ({
    'code':'1',
    'msg':'商品详情页评价'
    'data':{
    "comment_number": 1, 评价数量
    "comment": {
        "id": 1,
        "user_id": null,
        "goods_id": 22,
        "comment": "12121212", 评论内容
        "images": "1asdasd",
        "score": null,
        "createtime": null,
        "status": "normal",
        "images_text": [
            评价图片数组
            "http://temporaryfood.com1asdasd"
        ]
    }
    "user": {
        "id": 1,
        "nickname": "admin",
        "avatar_text": "用户头像",
    }
    }
    })
     */
    public function goodsDetailComment()
    {
        $goods_id = $this->request->post('goods_id');
        if (!is_numeric($goods_id)){
            $this->error('商品id不合法');
        }
        $model = new GoodsComment();
        $goods = [];
        $goods['comment_number'] = $model->where('status','normal')->count();
        $goods['comment'] = $model
            ->with(['user'])
            ->where('goods_id',$goods_id)
            ->where('status','normal')
            ->order('id','desc')
            ->find();
        $goods['comment']->getRelation('user')->visible(['id','nickname']);
        $this->success('商品详情页评价',$goods);
    }


    /**
     * @ApiTitle    (商品评价列表)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name=goods_id, type=integer, required=true, description="商品id")
     * @ApiParams   (name=page, type=integer, required=false, description="页数")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    'data':{
        "total": 1,
        "per_page": 10,
        "current_page": 1,
        "last_page": 1,
        "data": [
        {
            "id": 1,
            "user_id": 1,
            "goods_id": 22,
            "comment": "12121212",
            "images": "1asdasd",
            "score": null,
            "createtime": null,
            "status": "normal",
            "user": {
                "id": 1,
                "nickname": "admin",
                "avatar_text": ""
            },
            "images_text": [
                "http://temporaryfood.com1asdasd"
            ],
            "createtime_text": ""
        }
        ]
    }
    })
     */
    public function goodsComment()
    {
        $goods_id = $this->request->post('goods_id');
        $page = $this->request->post('page');
        $model = new \app\api\model\GoodsComment();
        if (!is_numeric($goods_id)){
            $this->error('商品id不合法');
        }
        $lists = $model
            ->with(['user'])
            ->where('goods_id',$goods_id)
            ->order('id','desc')
            ->paginate(10,false,['page'=>$page])
            ->each(function ($item,$key){
                $item->getRelation('user')->visible(['id','nickname']);
            });
        $this->success('商品规格',$lists);
    }
}