From 989542e814b12d141eedccc274d3f8a8b74e2ba4 Mon Sep 17 00:00:00 2001 From: yin5th <541304803@qq.com> Date: Tue, 26 Sep 2023 10:38:35 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E5=85=B3=E9=94=AE?= =?UTF-8?q?=E8=AF=8D=E6=9F=A5=E8=AF=A2=E4=B8=89=E7=BA=A7=E5=88=86=E7=B1=BB?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/app/api/controller/GoodsCategory.php | 18 ++++++++++++- server/app/api/logic/GoodsCategoryLogic.php | 30 +++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) 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