This commit is contained in:
Developer
2026-06-13 09:07:07 +08:00
parent 82bb3e2f14
commit 906dcc9eb1
69 changed files with 6812 additions and 1254 deletions

View File

@@ -0,0 +1,109 @@
<?php
namespace app\admin\controller;
use app\common\controller\Backend;
/**
* 搜索热门配置
* @icon fa fa-search
* @time 2026-06-13
* @description 管理搜索热门关键词配置,支持开关/统计时长/置顶/自定义热词
*/
class SearchHotConfig extends Backend
{
protected $model = null;
protected $searchFields = 'id';
public function _initialize()
{
parent::_initialize();
$this->model = new \app\admin\model\SearchHotConfig;
}
/**
* 查看
*/
public function index()
{
$this->relationSearch = false;
$this->request->filter(['strip_tags', 'trim']);
if ($this->request->isAjax()) {
if ($this->request->request('keyField')) {
return $this->selectpage();
}
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$list = $this->model
->where($where)
->order($sort, $order)
->paginate($limit);
foreach ($list as $row) {
$row->visible(['id','enabled','hours','pinned_keywords','custom_keywords','updated_at']);
}
$result = array("total" => $list->total(), "rows" => $list->items());
return json($result);
}
return $this->view->fetch();
}
/**
* @name 安装/初始化数据表
* @desc 创建tool_search_hot_config表并插入默认配置
*/
public function install()
{
$sql = <<<SQL
CREATE TABLE IF NOT EXISTS `tool_search_hot_config` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`enabled` tinyint(1) NOT NULL DEFAULT 1 COMMENT '是否启用热门搜索 0否1是',
`hours` int(11) NOT NULL DEFAULT 24 COMMENT '统计时长(小时) 12/24/72/120',
`pinned_keywords` text DEFAULT NULL COMMENT '置顶关键词JSON数组',
`custom_keywords` text DEFAULT NULL COMMENT '自定义热词JSON数组',
`updated_at` int(11) unsigned NOT NULL DEFAULT 0 COMMENT '更新时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='搜索热门配置表';
SQL;
try {
\think\Db::execute($sql);
$count = \think\Db::name('search_hot_config')->count();
if ($count == 0) {
\think\Db::name('search_hot_config')->insert([
'enabled' => 1,
'hours' => 24,
'pinned_keywords' => json_encode([]),
'custom_keywords' => json_encode([]),
'updated_at' => time(),
]);
$this->success('安装成功,已插入默认配置');
} else {
$this->success('数据表已存在,已有 ' . $count . ' 条配置');
}
} catch (\Exception $e) {
$this->error('安装失败: ' . $e->getMessage());
}
}
/**
* @name 清空热门搜索记录
* @desc 删除RUNTIME_PATH/search_hot目录下所有txt文件
*/
public function clear_hot()
{
$dir = RUNTIME_PATH . 'search_hot';
$count = 0;
if (is_dir($dir)) {
$files = glob($dir . '/*.txt');
foreach ($files as $file) {
if (unlink($file)) {
$count++;
}
}
}
$this->success("已清空 {$count} 个热门搜索记录文件");
}
}