审查视图

application/api/controller/Goods.php 14.9 KB
耿培杰 authored
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Config;

/**
 * 商品接口
 */
class Goods extends Api
{
    protected $goodsModel;
耿培杰 authored
14
    protected $categoryModel;
耿培杰 authored
15 16 17 18 19 20
    protected $keywordLogModel;

    public function _initialize()
    {
        parent::_initialize();
        $this->goodsModel = new \app\api\model\Goods;
耿培杰 authored
21
        $this->categoryModel = new \app\api\model\Category;
耿培杰 authored
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
        $this->keywordLogModel = new \app\api\model\KeywordLog;
    }

    /**
     * @ApiTitle    (本店热榜)
     * @ApiSummary  (本店热榜)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/goods/getHotGoods)
     * @ApiParams  (name=limit, type=string, required=false, description="显示条数 默认10条")
     * @ApiReturn({
    "code": 1,
    "msg": "请求成功",
    "time": "1587463117",
    "data": [
    {
    "goods_id": 1, 商品id
    "ch_name": "EMP精选 澳洲白肉油桃  500~540g  4只装", 中文名称
    "en_name": "EMP selected Australian white meat nectarines Australian  meat nectarines", 英文名称
    "image": "http://q7s0a1rb4.bkt.clouddn.com/uploads/20200420/26f5e51b8ac7fbd6f1c649cc45a18265.png", 缩略图
    "sales_actual": "100", 实际销量
    }
    ]
    })
     */
    public function getHotGoods()
    {
        $limit = $this->request->param('limit')?$this->request->param('limit'):10;
        $where = [];
50
        $data = $this->goodsModel->selectHotData($where, $limit,$this->lang);
耿培杰 authored
51 52 53 54
        $this->success('请求成功', $data);
    }

    /**
耿培杰 authored
55 56
     * @ApiTitle    (推荐商品列表)
     * @ApiSummary  (推荐商品列表)
耿培杰 authored
57
     * @ApiMethod   (POST)
耿培杰 authored
58
     * @ApiRoute    (/api/goods/getRecommendGoods)
耿培杰 authored
59
     * @ApiParams  (name=limit, type=string, required=false, description="显示条数 默认10条")
耿培杰 authored
60 61 62 63 64 65 66 67 68 69
     * @ApiReturn({
    "code": 1,
    "msg": "请求成功",
    "time": "1587463117",
    "data": [
    {
    "goods_id": 1, 商品id
    "ch_name": "EMP精选 澳洲白肉油桃  500~540g  4只装", 中文名称
    "en_name": "EMP selected Australian white meat nectarines Australian  meat nectarines", 英文名称
    "image": "http://q7s0a1rb4.bkt.clouddn.com/uploads/20200420/26f5e51b8ac7fbd6f1c649cc45a18265.png", 缩略图
耿培杰 authored
70
    "goods_price": "0.00", 售价
耿培杰 authored
71 72 73 74 75 76 77 78
    "country_ch_name": "意大利",  原产地中文
    "country_en_name": "Ltaly",  原产地英文
    "is_vip_price": "2", 会员特价:1=开启,2=关闭
    "stock_num": 55  库存
    }
    ]
    })
     */
耿培杰 authored
79
    public function getRecommendGoods()
耿培杰 authored
80
    {
耿培杰 authored
81
        $limit = $this->request->param('limit')?$this->request->param('limit'):10;
耿培杰 authored
82
        $where['g.is_recommend'] = 1;
耿培杰 authored
83
耿培杰 authored
84 85 86
        $list = $this->goodsModel->selectData($where, $limit,$this->lang);
        $data['image'] = cdnurl(config('site.index_recommend_image'));
        $data['list'] = $list;
耿培杰 authored
87 88 89
        $this->success('请求成功', $data);
    }
耿培杰 authored
90
耿培杰 authored
91
    /**
耿培杰 authored
92 93
     * @ApiTitle    (商品列表)
     * @ApiSummary  (商品列表)
耿培杰 authored
94
     * @ApiMethod   (POST)
耿培杰 authored
95
     * @ApiRoute    (/api/goods/getGoodsList)
耿培杰 authored
96
     * @ApiParams  (name=type, type=string, required=false, description="类型:1=普通商品,2=推荐商品 默认全部")
耿培杰 authored
97
     * @ApiParams  (name=page, type=string, required=false, description="页数")
耿培杰 authored
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
     * @ApiReturn({
    "code": 1,
    "msg": "请求成功",
    "time": "1587463117",
    "data": [
    {
    "goods_id": 1, 商品id
    "ch_name": "EMP精选 澳洲白肉油桃  500~540g  4只装", 中文名称
    "en_name": "EMP selected Australian white meat nectarines Australian  meat nectarines", 英文名称
    "image": "http://q7s0a1rb4.bkt.clouddn.com/uploads/20200420/26f5e51b8ac7fbd6f1c649cc45a18265.png", 缩略图
    "goods_price": "0.00", 普通售价
    "group_price": "0.00", 拼团售价 拼团售价优先级最高
    "vip_price": "0.00", 会员售价
    "country_ch_name": "意大利",  原产地中文
    "country_en_name": "Ltaly",  原产地英文
    "is_vip_price": "2", 会员特价:1=开启,2=关闭
    "stock_num": 55  库存
    }
    ]
    })
     */
耿培杰 authored
119
    public function getGoodsList()
耿培杰 authored
120
    {
耿培杰 authored
121 122 123
        $page = $this->request->param('page');
        $limit = Config::get('paginate.index_rows');
        $where = [];
耿培杰 authored
124
耿培杰 authored
125
        $type = $this->request->param('type');
耿培杰 authored
126 127 128 129 130 131
        $keyword = $this->request->param('keyword');
        if ($keyword){
            $where['g.ch_name|g.en_name|g.ch_content|g.en_content'] = ['like','%'.$keyword.'%'];
            //添加搜索记录
            if ($this->userId) $this->keywordLogModel->save(['user_id'=>$this->userId,'content'=>$keyword,'createtime'=>time()]);
        }
耿培杰 authored
132
耿培杰 authored
133 134 135 136 137
        if(!empty($type) && $type == 2){
            $where['g.is_recommend'] = 1;
        }elseif(!empty($type) && $type == 1){
            $where['g.is_recommend'] = 2;
        }
138
        $data = $this->goodsModel->selectPageData($where,$page,$limit,$this->lang);
耿培杰 authored
139 140 141
        $this->success('请求成功', $data);
    }
耿培杰 authored
142 143 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 190 191 192 193 194 195 196 197 198 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
    /**
     * @ApiTitle    (分类商品列表)
     * @ApiSummary  (分类商品列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/goods/getCategoryGoodsList)
     * @ApiParams  (name=category_two_id, type=string, required=true, description="二级分类id")
     * @ApiParams  (name=category_three_id, type=string, required=false, description="三级分类id")
     * @ApiParams  (name=category_group_id, type=string, required=false, description="分组id")
     * @ApiParams  (name=country_id, type=string, required=false, description="国家id")
     * @ApiParams  (name=brand_id, type=string, required=false, description="品牌id")
     * @ApiParams  (name=level_id, type=string, required=false, description="等级id")
     * @ApiParams  (name=part_id, type=string, required=false, description="部位id")
     * @ApiParams  (name=category2_id, type=string, required=false, description="品类id")
     * @ApiParams  (name=page, type=string, required=false, description="页数")
     * @ApiReturn({
    "code": 1,
    "msg": "请求成功",
    "time": "1587539236",
    "data": {
    "country": { 国家
    "1": {
    "id": 1,  国家id
    "ch_name": "意大利", 中文名称
    "en_name": "Ltaly"   英文名称
    }
    },
    "brand": {   品牌
    "1": {
    "id": 1,  品牌id
    "ch_name": "HAR",   中文名称
    "en_name": "HAR"    英文名称
    }
    },
    "level": {   等级
    "1": {
    "id": 1,  等级id
    "ch_name": "草饲",   中文名称
    "en_name": "Forage fed"   英文名称
    }
    },
    "part": {    部位
    "1": {  部位id
    "id": 1,
    "ch_name": "战斧",   中文名称
    "en_name": "Tomahawk"   英文名称
    }
    },
    "category2": [],  品类
    "list": {
    "total": 1, 总条数
    "list": [
    {
    "goods_id": 1,  商品id
    "ch_name": "EMP精选 澳洲白肉油桃  500~540g  4只装",   中文名称
    "en_name": "EMP selected Australian white meat nectarines Australian  meat nectarines",   英文名称
    "image": "http://q7s0a1rb4.bkt.clouddn.com/uploads/20200420/26f5e51b8ac7fbd6f1c649cc45a18265.png",  缩略图
    "goods_price": "123.00",  价格
    "ch_country_name": "意大利",   中文名称
    "en_country_name": "Ltaly",   英文名称
    "is_vip_price": "2",    会员特价:1=开启,2=关闭
    "is_group": "2",     团购:1=开启,2=关闭
    "group_price": null,  拼团售价 拼团售价优先级最高
    "vip_price": "0.00",   会员售价
    "stock_num": 55   库存
    }
    ]
    }
    }
    })
     */
    public function getCategoryGoodsList()
    {
        $params = $this->request->param();
        if (empty($params['category_two_id'])) $this->error('缺少参数 category_two_id!');
        $page = $params['page'];
        $limit = Config::get('paginate.index_rows');
        $where['category_two_id'] = $params['category_two_id'];
        if (!empty($params['category_three_id'])) $where['category_three_id'] = $params['category_three_id'];
        if (!empty($params['category_group_id'])) $where['category_group_ids'] = ['like','%,'.$params['category_group_id'].',%'];
        if (!empty($params['country_id'])) $where['country_id'] = $params['country_id'];
        if (!empty($params['brand_id'])) $where['brand_id'] = $params['brand_id'];
        if (!empty($params['level_id'])) $where['level_id'] = $params['level_id'];
        if (!empty($params['part_id'])) $where['part_id'] = $params['part_id'];
        if (!empty($params['category2_id'])) $where['category2_id'] = $params['category2_id'];

        //获取筛选标签,国家,品牌,等级,部位,品类
耿培杰 authored
228
        $data['extend'] = $this->categoryModel->getExtend($where,$this->lang);
耿培杰 authored
229
        //获取三级分类
230
        $data['category_three'] = $this->categoryModel->selectData(['pid'=>$params['category_two_id']],null,$this->lang);
耿培杰 authored
231
        //获取列表数据
232
        $data['list'] = $this->goodsModel->selectPageData($where,$page,$limit,$this->lang);
耿培杰 authored
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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
        $this->success('请求成功', $data);
    }

    /**
     * @ApiTitle    (搜索商品列表)
     * @ApiSummary  (搜索商品列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/goods/getSearchGoodsList)
     * @ApiParams  (name=keyword, type=string, required=false, description="关键字搜索")
     * @ApiParams  (name=country_id, type=string, required=false, description="国家id")
     * @ApiParams  (name=brand_id, type=string, required=false, description="品牌id")
     * @ApiParams  (name=level_id, type=string, required=false, description="等级id")
     * @ApiParams  (name=part_id, type=string, required=false, description="部位id")
     * @ApiParams  (name=category2_id, type=string, required=false, description="品类id")
     * @ApiParams  (name=page, type=string, required=false, description="页数")
     * @ApiReturn({
    "code": 1,
    "msg": "请求成功",
    "time": "1587539236",
    "data": {
    "country": { 国家
    "1": {
    "id": 1,  国家id
    "ch_name": "意大利", 中文名称
    "en_name": "Ltaly"   英文名称
    }
    },
    "brand": {   品牌
    "1": {
    "id": 1,  品牌id
    "ch_name": "HAR",   中文名称
    "en_name": "HAR"    英文名称
    }
    },
    "level": {   等级
    "1": {
    "id": 1,  等级id
    "ch_name": "草饲",   中文名称
    "en_name": "Forage fed"   英文名称
    }
    },
    "part": {    部位
    "1": {  部位id
    "id": 1,
    "ch_name": "战斧",   中文名称
    "en_name": "Tomahawk"   英文名称
    }
    },
    "category2": [],  品类
    "list": {
    "total": 1, 总条数
    "list": [
    {
    "goods_id": 1,  商品id
    "ch_name": "EMP精选 澳洲白肉油桃  500~540g  4只装",   中文名称
    "en_name": "EMP selected Australian white meat nectarines Australian  meat nectarines",   英文名称
    "image": "http://q7s0a1rb4.bkt.clouddn.com/uploads/20200420/26f5e51b8ac7fbd6f1c649cc45a18265.png",  缩略图
    "goods_price": "123.00",  价格
    "ch_country_name": "意大利",   中文名称
    "en_country_name": "Ltaly",   英文名称
    "is_vip_price": "2",    会员特价:1=开启,2=关闭
    "is_group": "2",     团购:1=开启,2=关闭
    "group_price": null,  拼团售价 拼团售价优先级最高
    "vip_price": "0.00",   会员售价
    "stock_num": 55   库存
    }
    ]
    }
    }
    })
     */
    public function getSearchGoodsList()
    {
        $params = $this->request->param();
        if (empty($params['keyword'])) $this->error('缺少参数 keyword!');
耿培杰 authored
308 309 310 311 312 313 314 315
        if ($this->userId){
            $last = $this->keywordLogModel->where(['user_id'=>$this->userId,'content'=>$params['keyword']])->value('id');
            if ($last){
                $this->keywordLogModel->where('id',$last)->update(['createtime'=>time()]);
            }else{
                $this->keywordLogModel->save(['user_id'=>$this->userId,'content'=>$params['keyword'],'createtime'=>time()]);
            }
        }
耿培杰 authored
316 317 318 319 320 321 322 323 324 325 326 327 328
        $page = $params['page'];
        $limit = Config::get('paginate.index_rows');
        $where = [];
        if (!empty($params['country_id'])) $where['country_id'] = $params['country_id'];
        if (!empty($params['brand_id'])) $where['brand_id'] = $params['brand_id'];
        if (!empty($params['level_id'])) $where['level_id'] = $params['level_id'];
        if (!empty($params['part_id'])) $where['part_id'] = $params['part_id'];
        if (!empty($params['category2_id'])) $where['category2_id'] = $params['category2_id'];
        $goodsWhere = $where;
        $where['ch_name|en_name|ch_content|en_content'] = ['like','%'.$params['keyword'].'%'];
        $goodsWhere['g.ch_name|g.en_name|g.ch_content|g.en_content'] = ['like','%'.$params['keyword'].'%'];

        //获取筛选标签,国家,品牌,等级,部位,品类
耿培杰 authored
329
        $data['extend'] = $this->categoryModel->getExtend($where,$this->lang);
耿培杰 authored
330
        //获取列表数据
331
        $data['list'] = $this->goodsModel->selectPageData($goodsWhere,$page,$limit,$this->lang);
耿培杰 authored
332 333 334

        $this->success('请求成功', $data);
    }
耿培杰 authored
335 336 337 338 339 340

    /**
     * @ApiTitle    (商品详情)
     * @ApiSummary  (商品详情)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/goods/getGoodsInfo)
耿培杰 authored
341
     * @ApiParams  (name=goods_id, type=string, required=true, description="商品id")
耿培杰 authored
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
     * @ApiReturn({
    "code": 1,
    "msg": "请求成功",
    "time": "1587463117",
    "data": [
    {
    "code": 1,
    "msg": "请求成功",
    "time": "1587471193",
    "data": {
    "goods_id": 1,  商品id
    "ch_name": "EMP精选 澳洲白肉油桃  500~540g  4只装",  中文名称
    "en_name": "EMP selected Australian white meat nectarines Australian  meat nectarines",  英文名称
    "image": "http://q7s0a1rb4.bkt.clouddn.com/uploads/20200420/26f5e51b8ac7fbd6f1c649cc45a18265.png",   缩略图
    "goods_price": "123.00",  普通价格
    "images": [
    "http://q7s0a1rb4.bkt.clouddn.com/uploads/20200420/26f5e51b8ac7fbd6f1c649cc45a18265.png"
    ],  轮播图
    "goods_no": "", 商品编码
    "ch_specification": "",  中文包装规格
    "en_specification": "",  英文包装规格
    "ch_save_where": "",  中文保存条件
    "en_save_where": "",  英文保存条件
    "ch_period": "",   中文有效期
    "en_period": "",   英文有效期
    "ch_content": "",   中文详情
    "en_content": "",   英文详情
    "stock_num": 55,    库存
    "sales_actual": 3,  实际销量
    "group_price": null,  拼团价格
    "vip_price": "0.00",   会员特价
    "is_vip_price": "2",   会员特价:1=开启,2=关闭
    "is_group": "2",    团购:1=开启,2=关闭
    "ch_country_name": "意大利",
    "en_country_name": "Ltaly"
耿培杰 authored
377
    "is_car": 0  购物车:1=加入,2=未加入
耿培杰 authored
378 379 380 381 382 383 384 385 386 387
    }
    }
    ]
    })
     */
    public function getGoodsInfo()
    {
        $goods_id = $this->request->param('goods_id');
        if (!$goods_id) $this->error('缺少参数 goods_id!');
        $where = ['g.id'=>$goods_id];
388
        $data = $this->goodsModel->getInfo($where,$this->lang);
耿培杰 authored
389 390 391 392 393 394 395 396

        if ($data['status'] == 2) $this->error('商品已下架');
        $userId = $this->userId;
        if ($userId){
            $carModel = new \app\api\model\Car;
            $data['is_car'] = $carModel->where(['user_id'=>$userId,'goods_id'=>$data['goods_id']])->count();
        }
耿培杰 authored
397 398 399
        $this->success('请求成功', $data);
    }
}