Goods.php 4.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':{
    "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);
    }
}