Goods.php 16.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 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 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 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 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 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 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
<?php

namespace app\api\controller;

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

/**
 * 商品接口
 */
class Goods extends Api
{
    protected $goodsModel;
    protected $categoryModel;
    protected $keywordLogModel;

    public function _initialize()
    {
        parent::_initialize();
        $this->goodsModel = new \app\api\model\Goods;
        $this->categoryModel = new \app\api\model\Category;
        $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 = [];
        $data = $this->goodsModel->selectHotData($where, $limit,$this->lang);
        $this->success('请求成功', $data);
    }

    /**
     * @ApiTitle    (推荐商品列表)
     * @ApiSummary  (推荐商品列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/goods/getRecommendGoods)
     * @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", 缩略图
    "goods_price": "0.00", 售价
    "country_ch_name": "意大利",  原产地中文
    "country_en_name": "Ltaly",  原产地英文
    "is_vip_price": "2", 会员特价:1=开启,2=关闭
    "stock_num": 55  库存
    }
    ]
    })
     */
    public function getRecommendGoods()
    {
        $limit = $this->request->param('limit')?$this->request->param('limit'):10;
        $where['g.is_recommend'] = 1;

        $list = $this->goodsModel->selectData($where, $limit,$this->lang);
        $data['image'] = cdnurl(config('site.index_recommend_image'));
        $data['list'] = $list;
        $this->success('请求成功', $data);
    }


    /**
     * @ApiTitle    (商品列表)
     * @ApiSummary  (商品列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/goods/getGoodsList)
     * @ApiParams  (name=type, type=string, required=false, description="类型:1=普通商品,2=推荐商品 默认全部")
     * @ApiParams  (name=page, type=string, required=false, description="页数")
     * @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  库存
    }
    ]
    })
     */
    public function getGoodsList()
    {
        $page = $this->request->param('page');
        $limit = Config::get('paginate.index_rows');
        $where = [];

        $type = $this->request->param('type');
        $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()]);
        }

        if(!empty($type) && $type == 2){
            $where['g.is_recommend'] = 1;
        }elseif(!empty($type) && $type == 1){
            $where['g.is_recommend'] = 2;
        }
        $data = $this->goodsModel->selectPageData($where,$page,$limit,$this->lang);
        foreach ($data['list'] as $k => $v){
            $v['user_type'] = $this->user['type'];
            $data['list'][$k]['price'] = get_price_not_group($v);
        }
        $this->success('请求成功', $data);
    }

    /**
     * @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 = [];
        if (!empty($params['category_two_id'])) $where['category_two_id'] = ['like','%,'.$params['category_two_id'].',%'];
        if (!empty($params['category_three_id'])) $where['category_three_id'] = ['like','%,'.$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'];

        if (!empty($params['category_two_id'])){
            //获取筛选标签,国家,品牌,等级,部位,品类
            $data['extend'] = $this->categoryModel->getExtend($where,$this->lang);
            //获取三级分类
            $data['category_three'] = $this->categoryModel->selectData(['pid'=>$params['category_two_id']],null,$this->lang);
            //获取列表数据
            $data['list'] = $this->goodsModel->selectPageData($where,$page,$limit,$this->lang);
            foreach ($data['list']['list'] as $k => $v){
                $v['user_type'] = $this->user['type'];
                $data['list']['list'][$k]['price'] = get_price_not_group($v);
            }
        }else{
            $data = $this->goodsModel->selectPageData($where,$page,$limit,$this->lang);
        }


        $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!');
        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()]);
            }
        }
        $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'];

//        $where['g.ch_name|g.en_name|g.ch_content|g.en_content|cate1.name|cate2.name|cate3.name'] = ['like','%'.$params['keyword'].'%'];
        $cateWhere['name'] = $params['keyword'];
        $cateWhere['en_name'] = $params['keyword'];
        $cateIds = $this->categoryModel->whereOr($cateWhere)->column('id');
        $cateIds = implode(',',$cateIds);
        $whereOr['g.category_one_id|g.category_two_id|g.category_three_id'] = ['like','%,'.$cateIds.',%'];
        $where['g.ch_name|g.en_name'] = ['like','%'.$params['keyword'].'%'];
        //获取筛选标签,国家,品牌,等级,部位,品类
        $data['extend'] = $this->categoryModel->getExtend($where,$this->lang,$whereOr);
        //获取列表数据
        $data['list'] = $this->goodsModel->selectPageData($where,$page,$limit,$this->lang,$whereOr);
        foreach ($data['list']['list'] as $k => $v){
            $v['user_type'] = $this->user['type'];
            $data['list']['list'][$k]['price'] = get_price_not_group($v);
        }
        $this->success('请求成功', $data);
    }

    /**
     * @ApiTitle    (商品详情)
     * @ApiSummary  (商品详情)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/goods/getGoodsInfo)
     * @ApiParams  (name=goods_id, type=string, required=true, description="商品id")
     * @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"
    "is_car": 0  购物车:1=加入,2=未加入
    }
    }
    ]
    })
     */
    public function getGoodsInfo()
    {
        $goods_id = $this->request->param('goods_id');
        if (!$goods_id) $this->error('缺少参数 goods_id!');
        $where = ['g.id'=>$goods_id];
        $data = $this->goodsModel->getInfo($where,$this->lang);

        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();
        }
        $data['user_type'] = $this->user['type'];
        $data['price'] = get_price($data);
        $this->success('请求成功', $data);
    }
}