diff --git a/server/app/api/controller/GoodsCategory.php b/server/app/api/controller/GoodsCategory.php index 3c3e7bfd..89060557 100644 --- a/server/app/api/controller/GoodsCategory.php +++ b/server/app/api/controller/GoodsCategory.php @@ -8,7 +8,7 @@ use think\facade\Validate; class GoodsCategory extends Api { - public $like_not_need_login = ['getLevelOneList', 'getListByLevelOne']; + public $like_not_need_login = ['getLevelOneList', 'getListByLevelOne', 'getThirdListByKeyword']; /** * 获取一级分类列表 @@ -38,4 +38,20 @@ class GoodsCategory extends Api } return JsonServer::error('请求方式错误'); } + + /** + * 获取一级分类下的后代分类 + */ + public function getThirdListByKeyword() + { + if($this->request->isGet()) { + $keyword = input('keyword/s', ''); + if (empty($keyword)) { + return JsonServer::error('请输入关键词'); + } + $list = GoodsCategoryLogic::getThirdListByKeyword($keyword); + return JsonServer::success('获取成功', $list); + } + return JsonServer::error('请求方式错误'); + } } diff --git a/server/app/api/logic/GoodsCategoryLogic.php b/server/app/api/logic/GoodsCategoryLogic.php index 6db5c127..2ce11080 100644 --- a/server/app/api/logic/GoodsCategoryLogic.php +++ b/server/app/api/logic/GoodsCategoryLogic.php @@ -65,4 +65,34 @@ class GoodsCategoryLogic extends Logic return $list; } + + /** + * 根据关键词获取批量的三级分类列表 + * 注:完全匹配的词放到第一个 + */ + public static function getThirdListByKeyword($keyword) + { + $where = []; + $where[] = ['name', 'like', '%'.$keyword.'%']; + $where[] = ['level', '=', 3]; + $where[] = ['is_show', '=', 1]; + $list = GoodsCategory::field('id,name,image') + ->where($where) + ->order('sort', 'asc') + ->select() + ->toArray(); + + foreach($list as $key => $item) { + if ($keyword == $item['name']) { + // 关键词完全匹配的,提到数组最前面 也就是当前的key和0做交换 + if ($key > 0) { + $oldFirst = $list[0]; + $list[0] = $item; + $list[$key] = $oldFirst; + } + } + } + + return $list; + } } \ No newline at end of file