feat: 新增关键词查询三级分类接口

master
yin5th 2023-09-26 10:38:35 +08:00
parent 69e8b41587
commit 989542e814
2 changed files with 47 additions and 1 deletions

View File

@ -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('请求方式错误');
}
}

View File

@ -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;
}
}