request->isGet()) { $goodsId = $this->request->get('goods_id', '', 'trim'); $validate = Validate::rule('goods_id', 'require|integer|gt:0'); if(!$validate->check(['goods_id'=>$goodsId])) { return JsonServer::error($validate->getError()); } $goodsDetail = GoodsLogic::getGoodsDetail($goodsId, $this->user_id); if(false === $goodsDetail) { $error = GoodsLogic::getError() ?? '获取商品详情失败'; return JsonServer::error($error); } return JsonServer::success('获取商品详情成功', $goodsDetail); }else{ return JsonServer::error('请求方式错误'); } } /** * 热销榜单 */ public function getHotList() { return $this->getGoodsListTemplate($this->request, 'getHotList'); } /** * 商品列表 */ public function getGoodsList(){ return $this->getGoodsListTemplate($this->request, 'getGoodsList'); } /** * 商品列表模板 * 作用:代码复用 */ public function getGoodsListTemplate($request, $methodName) { if($request->isGet()) { $get = $this->request->get(); $get['user_id'] = $this->user_id; $get['page_no'] = $this->page_no; $get['page_size'] = $this->page_size; $data = GoodsLogic::$methodName($get); // 可变方法 return JsonServer::success('获取成功', $data); }else{ return JsonServer::error('请求方式错误'); } } /** * 根据商品栏目获取商品列表 */ public function getGoodsListByColumnId() { if($this->request->isGet()) { $columnId = $this->request->get('column_id', '', 'trim'); $validate = Validate::rule('column_id', 'require|integer|gt:0'); if(!$validate->check(['column_id'=>$columnId])) { return JsonServer::error($validate->getError()); } $data = GoodsLogic::getGoodsListByColumnId($columnId,$this->page_no, $this->page_size); return JsonServer::success('获取成功', $data); }else{ return JsonServer::error('请求方式错误'); } } // 获取商品的推荐商品 public function getRecommendGoods() { if($this->request->isGet()) { $goodsId = input('goods_id/d',0); if (empty($goodsId)) { return JsonServer::error('参数错误'); } $data = GoodsLogic::getRecommendGoodsById($goodsId); return JsonServer::success('获取成功', $data); }else{ return JsonServer::error('请求方式错误'); } } public function getBrandListByCateId() { if($this->request->isGet()) { $cateId = input('cate_id/d',0); if (empty($cateId)) { return JsonServer::error('参数错误'); } $data = GoodsBrandLogic::getByCateId($cateId); return JsonServer::success('获取成功', $data); }else{ return JsonServer::error('请求方式错误'); } } }