审查视图

application/api/controller/Goods.php 8.0 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


    /**
     * @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": [
SHW\戥岁。。 authored
43
    轮播图
李忠强 authored
44 45 46
    ],
    "down_image_text": "底部图",
    "four_image_text": [
SHW\戥岁。。 authored
47
    四宫格图
李忠强 authored
48 49 50 51 52 53
    ]
    }
    })
     */
    public function goodsDetail()
    {
SHW\戥岁。。 authored
54
        $goods_id   = $this->request->post('goods_id');
李忠强 authored
55
        $goodsmodel = new \app\api\model\Goods();
SHW\戥岁。。 authored
56
        if (!is_numeric($goods_id)) {
李忠强 authored
57 58
            $this->error('商品id不合法');
        }
SHW\戥岁。。 authored
59 60
        $goods               = $goodsmodel::get($goods_id);
        $goods_spec          = Db::name('litestore_goods_spec')->where('goods_id', $goods['goods_id'])->find();
SHW\戥岁。。 authored
61 62
        $goods['price']      = $goods_spec['goods_price'];
        $goods['line_price'] = $goods_spec['line_price'];
SHW\戥岁。。 authored
63 64
        //判断是否打折
        $goods['is_discount'] = 'is';
SHW\戥岁。。 authored
65
        if (empty($goods_spec['discount'])) {
SHW\戥岁。。 authored
66 67
            $goods['is_discount'] = 'no';
        }
68
        $goods['discount'] = $goods_spec['discount'].'折';
何书鹏 authored
69
        // 产地
SHW\戥岁。。 authored
70
        $makerfor_list    = ['1' => '国产', '2' => '进口'];
何书鹏 authored
71
        $goods['makefor'] = isset($makerfor_list[$goods['makefor']]) ? $makerfor_list[$goods['makefor']] : '未知';
SHW\戥岁。。 authored
72
        $this->success('商品详情', $goods);
李忠强 authored
73 74 75 76 77 78 79 80 81 82 83 84 85 86
    }


    /**
     * @ApiTitle    (商品规格)
     * @ApiMethod   (POST)
     * @ApiParams   (name=goods_id, type=integer, required=true, description="商品id")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    'data':{
    // 规格组合完毕的列表
    "list": [
    {
李忠强 authored
87
    "goods_spec_id": 103,  // 规格列表id
李忠强 authored
88 89 90 91
    "goods_id": 22,
    "goods_no": "SNHW001",
    "goods_price": "4499.00",
    "line_price": "0.00",
李忠强 authored
92
    "stock_num": 941,   // 库存
李忠强 authored
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()
    {
SHW\戥岁。。 authored
127
        $goods_id          = $this->request->param('goods_id');
李忠强 authored
128
        $goodsspecrelmodel = new GoodsSpecRel();
SHW\戥岁。。 authored
129 130
        $list              = $goodsspecrelmodel
            ->where('goods_id', $goods_id)
李忠强 authored
131
            ->select();
SHW\戥岁。。 authored
132 133 134 135 136
        $array             = [];
        $goods             = \app\api\model\Goods::get($goods_id);
        if ($goods['spec_type'] == 20) {
            foreach ($list as $key => $value) {
                if (!isset($array[$value['spec_id']])) {
李忠强 authored
137
                    $array[$value['spec_id']]['name'] = Db::name('litestore_spec')
SHW\戥岁。。 authored
138
                        ->where('id', $value['spec_id'])
李忠强 authored
139 140
                        ->value('spec_name');
                }
SHW\戥岁。。 authored
141 142
                $spec_value                           = Db::name('litestore_spec_value')
                    ->where('id', $value['spec_value_id'])
李忠强 authored
143 144
                    ->value('spec_value');
                $array[$value['spec_id']]['second'][] = [
SHW\戥岁。。 authored
145 146
                    'id'   => $value['spec_value_id'],
                    'name' => $spec_value,
李忠强 authored
147
                ];
李忠强 authored
148
            }
李忠强 authored
149
            $array = array_values($array);
李忠强 authored
150
        }
SHW\戥岁。。 authored
151 152
        $goods_spec = GoodsSpec::all(['goods_id' => $goods_id]);
        foreach ($goods_spec as &$value) {
153 154 155 156 157 158 159 160 161
            //规格下的购物车数量
            if ($this->auth->isLogin()) {
                $number               = Db::name('cart')
                    ->where(['goods_id' => $value['goods_id'], 'sku_id' => $value['goods_spec_id'], 'user_id' => $this->auth->id])
                    ->value('number');
                $value['cart_number'] = $number ?? 0;
            } else {
                $value['cart_number'] = 0;
            }
SHW\戥岁。。 authored
162 163
            //判断是否打折
            $value['is_discount'] = 'is';
SHW\戥岁。。 authored
164
            if (empty($value['discount'])) {
SHW\戥岁。。 authored
165 166
                $value['is_discount'] = 'no';
            }
167
            $value['discount'] = $value['discount'].'折';
SHW\戥岁。。 authored
168 169
        }
        $this->success('商品规格', ['list' => $goods_spec, 'sku' => $array]);
李忠强 authored
170 171 172 173 174 175 176 177 178 179 180 181 182
    }


    /**
     * @ApiTitle    (商品详情页评价)
     * @ApiMethod   (POST)
     * @ApiParams   (name=goods_id, type=integer, required=true, description="商品id")
     * @ApiReturn   ({
    'code':'1',
    'msg':'商品详情页评价'
    'data':{
    "comment_number": 1, 评价数量
    "comment": {
SHW\戥岁。。 authored
183 184 185 186 187 188 189 190 191 192 193 194
    "id": 1,
    "user_id": null,
    "goods_id": 22,
    "comment": "12121212", 评论内容
    "images": "1asdasd",
    "score": null,
    "createtime": null,
    "status": "normal",
    "images_text": [
    评价图片数组
    "http://temporaryfood.com1asdasd"
    ]
李忠强 authored
195 196
    }
    "user": {
SHW\戥岁。。 authored
197 198 199
    "id": 1,
    "nickname": "admin",
    "avatar_text": "用户头像",
李忠强 authored
200 201 202 203 204 205 206
    }
    }
    })
     */
    public function goodsDetailComment()
    {
        $goods_id = $this->request->post('goods_id');
SHW\戥岁。。 authored
207
        if (!is_numeric($goods_id)) {
李忠强 authored
208 209
            $this->error('商品id不合法');
        }
SHW\戥岁。。 authored
210 211 212 213 214 215 216 217 218 219 220 221
        $model                   = new GoodsComment();
        $goods                   = [];
        $goods['comment_number'] = $model->where('goods_id', $goods_id)->where('status', 'normal')->count();
        $table_name              = $model->getTable();
        $goods['comment']        = $model
                ->with(['user'])
                ->where($table_name . '.goods_id', $goods_id)
                ->where($table_name . '.status', 'normal')
                ->order($table_name . '.id', 'desc')
                ->find() ?? [];
        if ($goods['comment']) $goods['comment']->getRelation('user')->visible(['id', 'nickname']);
        $this->success('商品详情页评价', $goods);
李忠强 authored
222 223 224 225 226 227 228 229 230 231 232 233
    }


    /**
     * @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':{
SHW\戥岁。。 authored
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
    "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": ""
    }
    ]
李忠强 authored
259 260 261 262 263 264
    }
    })
     */
    public function goodsComment()
    {
        $goods_id = $this->request->post('goods_id');
SHW\戥岁。。 authored
265 266 267
        $page     = $this->request->post('page');
        $model    = new \app\api\model\GoodsComment();
        if (!is_numeric($goods_id)) {
李忠强 authored
268 269 270 271
            $this->error('商品id不合法');
        }
        $lists = $model
            ->with(['user'])
SHW\戥岁。。 authored
272 273 274 275 276
            ->where('goods_id', $goods_id)
            ->order('id', 'desc')
            ->paginate(10, false, ['page' => $page])
            ->each(function ($item, $key) {
                $item->getRelation('user')->visible(['id', 'nickname']);
李忠强 authored
277
            });
SHW\戥岁。。 authored
278
        $this->success('商品规格', $lists);
李忠强 authored
279 280
    }
}