审查视图

application/api/controller/Goods.php 7.1 KB
李忠强 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?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 = ['*'];
李忠强 authored
19
    protected $noNeedLogin = ['*'];
李忠强 authored
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62


    /**
     * @ApiTitle    (商品详情)
     * @ApiMethod   (POST)
     * @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'];
何书鹏 authored
63 64 65
        // 产地
        $makerfor_list = ['1' => '国产','2' => '进口'];
        $goods['makefor'] = isset($makerfor_list[$goods['makefor']]) ? $makerfor_list[$goods['makefor']] : '未知';
李忠强 authored
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
        $this->success('商品详情',$goods);
    }


    /**
     * @ApiTitle    (商品规格)
     * @ApiMethod   (POST)
     * @ApiParams   (name=goods_id, type=integer, required=true, description="商品id")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    'data':{
    // 规格组合完毕的列表
    "list": [
    {
李忠强 authored
81
    "goods_spec_id": 103,  // 规格列表id
李忠强 authored
82 83 84 85
    "goods_id": 22,
    "goods_no": "SNHW001",
    "goods_price": "4499.00",
    "line_price": "0.00",
李忠强 authored
86
    "stock_num": 941,   // 库存
李忠强 authored
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
    "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 = [];
李忠强 authored
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
        $goods = \app\api\model\Goods::get($goods_id);
        if ($goods['spec_type'] == 20){
            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
                ];
李忠强 authored
142
            }
李忠强 authored
143
            $array = array_values($array);
李忠强 authored
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
        }
        $goods_spec = GoodsSpec::all(['goods_id'=>$goods_id]);
        $this->success('商品规格',['list'=>$goods_spec,'sku'=>$array]);
    }



    /**
     * @ApiTitle    (商品详情页评价)
     * @ApiMethod   (POST)
     * @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 = [];
李忠强 authored
190
        $goods['comment_number'] = $model->where('goods_id',$goods_id)->where('status','normal')->count();
何书鹏 authored
191
        $table_name = $model->getTable();
李忠强 authored
192 193
        $goods['comment'] = $model
            ->with(['user'])
何书鹏 authored
194 195 196
            ->where($table_name.'.goods_id',$goods_id)
            ->where($table_name.'.status','normal')
            ->order($table_name.'.id','desc')
李忠强 authored
197
            ->find()??[];
李忠强 authored
198
        if ($goods['comment'])$goods['comment']->getRelation('user')->visible(['id','nickname']);
李忠强 authored
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
        $this->success('商品详情页评价',$goods);
    }


    /**
     * @ApiTitle    (商品评价列表)
     * @ApiMethod   (POST)
     * @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);
    }
}