<?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); $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['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']; //获取筛选标签,国家,品牌,等级,部位,品类 $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); $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']; $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'].'%']; //获取筛选标签,国家,品牌,等级,部位,品类 $data['extend'] = $this->categoryModel->getExtend($where,$this->lang); //获取列表数据 $data['list'] = $this->goodsModel->selectPageData($goodsWhere,$page,$limit,$this->lang); $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(); } $this->success('请求成功', $data); } }