From 3519525af821b8cdafa70fc349521660f9e898d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=BF=97=E5=BC=BA?= <357099073@qq.com> Date: Fri, 26 Dec 2025 17:03:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=90=9C=E7=B4=A2=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/index/controller/ArticlesController.php | 12 +- app/index/controller/SearchController.php | 127 ++++++++++++++++++++ route/app.php | 3 + 3 files changed, 137 insertions(+), 5 deletions(-) diff --git a/app/index/controller/ArticlesController.php b/app/index/controller/ArticlesController.php index b40b997..8c31493 100644 --- a/app/index/controller/ArticlesController.php +++ b/app/index/controller/ArticlesController.php @@ -375,12 +375,14 @@ class ArticlesController extends BaseController // 查询相关文章(同分类,排除当前文章,按发布时间倒序取5个) $relatedArticles = Articles::where([ - ['id', '<>', $id], - ['cate', '=', $currentCateId], - ['delete_time', '=', null], - ['status', '=', 2] + ['a.id', '<>', $id], + ['a.cate', '=', $currentCateId], + ['a.delete_time', '=', null], + ['a.status', '=', 2] ]) - ->field('id,title,image,views,likes,publishdate') + ->field('a.id,a.title,IF(a.image IS NULL OR a.image = \'\', ac.image, a.image) as image,a.views,a.likes,a.publishdate') + ->alias('a') + ->join('articles_category ac', 'a.cate = ac.id', 'LEFT') ->order('publishdate', 'desc') ->limit(5) ->select(); diff --git a/app/index/controller/SearchController.php b/app/index/controller/SearchController.php index 82d45d6..8b2b1ff 100644 --- a/app/index/controller/SearchController.php +++ b/app/index/controller/SearchController.php @@ -125,4 +125,131 @@ class SearchController extends BaseController return view('index', $viewData); } + + // JSON API搜索接口 + public function apiSearch() + { + try { + $keyword = input('keyword', ''); + $type = input('type', 'articles'); // 搜索类型:articles-文章,resources-资源 + $page = input('page/d', 1); + $limit = input('limit/d', 10); + + if (empty($keyword)) { + return json([ + 'code' => 1, + 'msg' => '请输入搜索关键词' + ]); + } + + if (empty($type)) { + return json([ + 'code' => 1, + 'msg' => '请选择搜索类型' + ]); + } + + // 根据类型选择对应的表和分类表 + if ($type == 'articles') { + $model = new Articles(); + $categoryModel = new ArticlesCategory(); + $status = 2; // 文章状态为2 + $fields = 'id, title, cate, image, author, FROM_UNIXTIME(create_time, "%Y-%m-%d") as publishdate'; + } else if ($type == 'resources') { + $model = new Resources(); + $categoryModel = new ResourcesCategory(); + $status = 1; // 资源状态为1 + $fields = 'id, title, cate, icon, uploader, FROM_UNIXTIME(create_time, "%Y-%m-%d") as publishdate'; + } else { + return json([ + 'code' => 1, + 'msg' => '无效的搜索类型' + ]); + } + + // 搜索内容 + $items = $model->where('title', 'like', "%{$keyword}%") + ->where('status', $status) + ->field($fields) + ->order('create_time desc') + ->page($page, $limit) + ->select(); + + // 获取总数 + $count = $model->where('title', 'like', "%{$keyword}%") + ->where('status', $status) + ->count(); + + // 获取分类名称和图片 + $resultItems = []; + foreach ($items as $item) { + $itemData = $item->toArray(); + + if ($type == 'articles') { + $category = $categoryModel->where('id', $itemData['cate']) + ->field('id, name, image') + ->find(); + + $itemData['category'] = $category ? $category->toArray() : ['id' => 0, 'name' => '未分类', 'image' => '']; + + // 如果文章的图片为空,使用分类的图片 + if (empty($itemData['image'])) { + $itemData['image'] = $itemData['category']['image']; + } + if (empty($itemData['image'])) { + $itemData['image'] = '/static/images/default.jpg'; + } + + $itemData['detail_url'] = url('articles/detail', ['id' => $itemData['id']], true, true); + } else { + $category = $categoryModel->where('id', $itemData['cate']) + ->field('id, name, icon, cid') + ->find(); + + $itemData['category'] = $category ? $category->toArray() : ['id' => 0, 'name' => '未分类', 'icon' => '', 'cid' => 0]; + + // 如果资源的图片为空,使用分类的图片 + if (empty($itemData['icon'])) { + $itemData['icon'] = $itemData['category']['icon']; + } + if (empty($itemData['icon'])) { + $itemData['icon'] = '/static/images/default.jpg'; + } + + // 根据分类cid判断资源类型 + if ($itemData['category']['cid'] == 8) { + $itemData['detail_url'] = url('game/detail', ['id' => $itemData['id']], true, true); + } else { + $itemData['detail_url'] = url('program/detail', ['id' => $itemData['id']], true, true); + } + } + + $resultItems[] = $itemData; + } + + // 计算分页信息 + $totalPages = ceil($count / $limit); + + return json([ + 'code' => 0, + 'msg' => '搜索成功', + 'data' => [ + 'keyword' => $keyword, + 'type' => $type, + 'items' => $resultItems, + 'total' => $count, + 'page' => $page, + 'limit' => $limit, + 'total_pages' => $totalPages, + 'has_more' => $page < $totalPages + ] + ]); + + } catch (\Exception $e) { + return json([ + 'code' => 1, + 'msg' => '搜索失败:' . $e->getMessage() + ]); + } + } } diff --git a/route/app.php b/route/app.php index 96c2b0e..d4b7b06 100644 --- a/route/app.php +++ b/route/app.php @@ -47,4 +47,7 @@ Route::group('api', function () { Route::post('admin/logout', 'api/Admin/logout'); Route::post('admin/change-password', 'api/Admin/changePassword'); Route::get('admin/menus', 'api/Admin/menus'); + + // 搜索接口 + Route::get('search', 'index/Search/apiSearch'); })->middleware(\app\middleware\Cors::class);