Files

53 lines
1.6 KiB
PHP

<?php
// +----------------------------------------------------------------------
// | YwxApp [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: ywxapp<admin@ywxapp.cn>
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace addon\docs\controller;
use addon\docs\model\DocsProject;
use addon\docs\service\DocSearchService;
/**
* 文档搜索
*
* @author ywxapp <admin@ywxapp.cn>
*/
class Search extends DocsFrontend
{
/**
* 搜索结果页
*/
public function index()
{
$keyword = trim((string) $this->request->get('kw', ''));
$projectId = (int) $this->request->get('project_id', 0);
$limit = (int) ($this->docsConfig['search_limit'] ?? 30);
$limit = $limit > 0 && $limit <= 100 ? $limit : 30;
$list = [];
if ($keyword !== '') {
// 限制关键词长度,避免超长串拖慢 LIKE 查询
$keyword = mb_substr($keyword, 0, 50, 'UTF-8');
$list = DocSearchService::search($keyword, $projectId, $limit);
}
$this->assign([
'keyword' => $keyword,
'projectId' => $projectId,
'list' => $list,
'total' => count($list),
'projects' => DocsProject::enabledList(),
'pageTitle' => $keyword !== '' ? '搜索:' . $keyword : '文档搜索',
]);
return $this->fetch('search/index');
}
}