From 009647f96fd740d79ea420d640669fa639cf8639 Mon Sep 17 00:00:00 2001 From: augushong Date: Sat, 25 Jul 2026 22:39:20 +0800 Subject: [PATCH] =?UTF-8?q?refactor(timer):=20TimerConfig=20=E6=8E=A7?= =?UTF-8?q?=E5=88=B6=E5=99=A8=E4=B8=8E=E8=A7=86=E5=9B=BE=E8=BF=81=E7=A7=BB?= =?UTF-8?q?=E5=88=B0=20Base=20=E5=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 控制器逻辑移入 extend/base/admin/controller/system/TimerConfigBase.php,app 层改为空壳继承。9 个视图文件迁移到 extend/base/admin/view/system/timer_config/。index.js 补 toolbar: ['refresh', 'export'] 声明(C 类页面,去掉默认的 add/delete)。 Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus --- app/admin/controller/system/TimerConfig.php | 174 +---------------- .../controller/system/TimerConfigBase.php | 179 ++++++++++++++++++ .../admin/view/system/timer_config/_common.js | 0 .../admin/view/system/timer_config/add.html | 0 .../admin/view/system/timer_config/add.js | 0 .../admin/view/system/timer_config/edit.html | 0 .../admin/view/system/timer_config/edit.js | 0 .../admin/view/system/timer_config/index.html | 0 .../admin/view/system/timer_config/index.js | 2 + .../admin/view/system/timer_config/read.html | 0 .../admin/view/system/timer_config/read.js | 0 .../admin/view/system/timer_log/_common.js | 0 .../admin/view/system/timer_log/index.html | 0 .../admin/view/system/timer_log/index.js | 0 .../admin/view/system/timer_log/read.html | 0 .../base}/admin/view/system/timer_log/read.js | 0 16 files changed, 183 insertions(+), 172 deletions(-) create mode 100644 extend/base/admin/controller/system/TimerConfigBase.php rename {app => extend/base}/admin/view/system/timer_config/_common.js (100%) rename {app => extend/base}/admin/view/system/timer_config/add.html (100%) rename {app => extend/base}/admin/view/system/timer_config/add.js (100%) rename {app => extend/base}/admin/view/system/timer_config/edit.html (100%) rename {app => extend/base}/admin/view/system/timer_config/edit.js (100%) rename {app => extend/base}/admin/view/system/timer_config/index.html (100%) rename {app => extend/base}/admin/view/system/timer_config/index.js (98%) rename {app => extend/base}/admin/view/system/timer_config/read.html (100%) rename {app => extend/base}/admin/view/system/timer_config/read.js (100%) rename {app => extend/base}/admin/view/system/timer_log/_common.js (100%) rename {app => extend/base}/admin/view/system/timer_log/index.html (100%) rename {app => extend/base}/admin/view/system/timer_log/index.js (100%) rename {app => extend/base}/admin/view/system/timer_log/read.html (100%) rename {app => extend/base}/admin/view/system/timer_log/read.js (100%) diff --git a/app/admin/controller/system/TimerConfig.php b/app/admin/controller/system/TimerConfig.php index b2de17a..4d3a694 100644 --- a/app/admin/controller/system/TimerConfig.php +++ b/app/admin/controller/system/TimerConfig.php @@ -2,178 +2,8 @@ namespace app\admin\controller\system; -use app\common\controller\AdminController; -use app\admin\service\annotation\ControllerAnnotation; -use app\admin\service\annotation\NodeAnotation; -use think\App; -use think\facade\Cache; +use base\admin\controller\system\TimerConfigBase; -/** - * @ControllerAnnotation(title="定时任务协调配置表") - */ -class TimerConfig extends AdminController +class TimerConfig extends TimerConfigBase { - use \app\admin\traits\Curd; - - public function __construct(App $app) - { - parent::__construct($app); - - $this->model = new \app\admin\model\SystemTimerConfig(); - - $this->assign('select_list_run_type', $this->model::SELECT_LIST_RUN_TYPE, true); - $this->assign('select_list_status', $this->model::SELECT_LIST_STATUS, true); - $this->assign('select_list_is_synced', $this->model::SELECT_LIST_IS_SYNCED, true); - $this->assign('select_list_manual_trigger', $this->model::SELECT_LIST_MANUAL_TRIGGER, true); - - // 允许通过行内修改的字段 - $this->allowModifyFields = [ - 'status', - 'run_type', - 'concurrency', - ]; - } - - /** - * @NodeAnotation(title="添加") - */ - public function add() - { - $this->error('定时任务配置由系统同步生成,不支持手动添加'); - } - - /** - * @NodeAnotation(title="删除") - */ - public function delete($id) - { - $this->error('定时任务配置由系统管理,不支持删除'); - } - - /** - * @NodeAnotation(title="编辑") - */ - public function edit($id) - { - $row = $this->model->find($id); - empty($row) && $this->error('数据不存在'); - if ($this->request->isPost()) { - $post = $this->request->post(); - // 只允许修改 run_type、status、concurrency - $post = array_intersect_key($post, array_flip(['run_type', 'status', 'concurrency'])); - if (array_key_exists('concurrency', $post)) { - // 空/null=继承代码默认写 NULL;正整数=覆盖;0或负数=拒绝(M5 防静默吞任务) - $post['concurrency'] = $this->normalizeConcurrency($post['concurrency']); - } - try { - $save = $row->save($post); - } catch (\Exception $e) { - $this->error('保存失败:' . $e->getMessage()); - } - if ($save) { - // D18:DB commit 后递增脏标记,触发 timer reload(edit 改 concurrency/run_type/status,与 trigger 一致) - Cache::inc('timer_config_version'); - $this->success('保存成功'); - } - $this->error('保存失败'); - } - $this->assign('row', $row); - - return $this->fetch(); - } - - /** - * @NodeAnotation(title="手动触发") - */ - public function trigger($id) - { - $this->checkPostRequest(); - $row = $this->model->find($id); - if (!$row) { - $this->error('数据不存在'); - } - $trigger_node_id = $this->request->post('trigger_node_id'); - if (empty($trigger_node_id)) { - $this->error('请选择节点'); - } - try { - // 手动触发跨 run_type、穿透 status(D19:调试工作流,关闭+手动触发+打开) - $row->save([ - 'manual_trigger' => 1, - 'trigger_node_id' => $trigger_node_id, - 'last_trigger_time' => time(), - ]); - // D18:DB commit 成功后才递增 version,防脏读(commit 前递增→节点 reload 读旧值→永不 reload) - Cache::inc('timer_config_version'); - } catch (\Exception $e) { - $this->error('触发失败:' . $e->getMessage()); - } - $this->success('触发成功,等待定时器执行'); - } - - /** - * @NodeAnotation(title="节点列表") - */ - public function hostList() - { - $list = \app\admin\model\SystemHost::where('status', 1) - ->field('node_id,is_master,ip_address,last_heartbeat_at') - ->order('is_master desc, node_id asc') - ->select(); - $this->success('', $list); - } - - /** - * @NodeAnotation(title="属性修改") - */ - public function modify() - { - $this->checkPostRequest(); - $post = $this->request->post(); - $rule = [ - 'id|ID' => 'require', - 'field|字段' => 'require', - 'value|值' => 'require', - ]; - $this->validate($post, $rule); - if (!in_array($post['field'], $this->allowModifyFields)) { - $this->error('该字段不允许修改:' . $post['field']); - } - // concurrency 行内修改必须为正整数(M5 防静默吞任务;继承默认请走 edit 表单传空值) - if ($post['field'] === 'concurrency' && (!is_numeric($post['value']) || (int) $post['value'] < 1)) { - $this->error('concurrency 必须是正整数'); - } - $row = $this->model->find($post['id']); - empty($row) && $this->error('数据不存在'); - $value = $post['field'] === 'concurrency' ? (int) $post['value'] : $post['value']; - try { - $save = $row->save([$post['field'] => $value]); - } catch (\Exception $e) { - $this->error($e->getMessage()); - } - if ($save) { - // D18:DB commit 后递增脏标记,触发 timer reload(行内改 concurrency/status/run_type,同 edit) - Cache::inc('timer_config_version'); - } - $this->success('保存成功'); - } - - /** - * 归一化 concurrency 入库值. - * 空字符串/null → null(继承代码默认);正整数 → int;0或负数/非数字 → 拒绝. - * - * @param mixed $value 提交值 - * @return int|null - */ - private function normalizeConcurrency($value) - { - if ($value === '' || $value === null) { - return null; - } - if (!is_numeric($value) || (int) $value < 1) { - $this->error('concurrency 必须是正整数或空(继承默认)'); - } - - return (int) $value; - } } diff --git a/extend/base/admin/controller/system/TimerConfigBase.php b/extend/base/admin/controller/system/TimerConfigBase.php new file mode 100644 index 0000000..a76b3af --- /dev/null +++ b/extend/base/admin/controller/system/TimerConfigBase.php @@ -0,0 +1,179 @@ +model = new \app\admin\model\SystemTimerConfig(); + + $this->assign('select_list_run_type', $this->model::SELECT_LIST_RUN_TYPE, true); + $this->assign('select_list_status', $this->model::SELECT_LIST_STATUS, true); + $this->assign('select_list_is_synced', $this->model::SELECT_LIST_IS_SYNCED, true); + $this->assign('select_list_manual_trigger', $this->model::SELECT_LIST_MANUAL_TRIGGER, true); + + // 允许通过行内修改的字段 + $this->allowModifyFields = [ + 'status', + 'run_type', + 'concurrency', + ]; + } + + /** + * @NodeAnotation(title="添加") + */ + public function add() + { + $this->error('定时任务配置由系统同步生成,不支持手动添加'); + } + + /** + * @NodeAnotation(title="删除") + */ + public function delete($id) + { + $this->error('定时任务配置由系统管理,不支持删除'); + } + + /** + * @NodeAnotation(title="编辑") + */ + public function edit($id) + { + $row = $this->model->find($id); + empty($row) && $this->error('数据不存在'); + if ($this->request->isPost()) { + $post = $this->request->post(); + // 只允许修改 run_type、status、concurrency + $post = array_intersect_key($post, array_flip(['run_type', 'status', 'concurrency'])); + if (array_key_exists('concurrency', $post)) { + // 空/null=继承代码默认写 NULL;正整数=覆盖;0或负数=拒绝(M5 防静默吞任务) + $post['concurrency'] = $this->normalizeConcurrency($post['concurrency']); + } + try { + $save = $row->save($post); + } catch (\Exception $e) { + $this->error('保存失败:' . $e->getMessage()); + } + if ($save) { + // D18:DB commit 后递增脏标记,触发 timer reload(edit 改 concurrency/run_type/status,与 trigger 一致) + Cache::inc('timer_config_version'); + $this->success('保存成功'); + } + $this->error('保存失败'); + } + $this->assign('row', $row); + + return $this->fetch(); + } + + /** + * @NodeAnotation(title="手动触发") + */ + public function trigger($id) + { + $this->checkPostRequest(); + $row = $this->model->find($id); + if (!$row) { + $this->error('数据不存在'); + } + $trigger_node_id = $this->request->post('trigger_node_id'); + if (empty($trigger_node_id)) { + $this->error('请选择节点'); + } + try { + // 手动触发跨 run_type、穿透 status(D19:调试工作流,关闭+手动触发+打开) + $row->save([ + 'manual_trigger' => 1, + 'trigger_node_id' => $trigger_node_id, + 'last_trigger_time' => time(), + ]); + // D18:DB commit 成功后才递增 version,防脏读(commit 前递增→节点 reload 读旧值→永不 reload) + Cache::inc('timer_config_version'); + } catch (\Exception $e) { + $this->error('触发失败:' . $e->getMessage()); + } + $this->success('触发成功,等待定时器执行'); + } + + /** + * @NodeAnotation(title="节点列表") + */ + public function hostList() + { + $list = \app\admin\model\SystemHost::where('status', 1) + ->field('node_id,is_master,ip_address,last_heartbeat_at') + ->order('is_master desc, node_id asc') + ->select(); + $this->success('', $list); + } + + /** + * @NodeAnotation(title="属性修改") + */ + public function modify() + { + $this->checkPostRequest(); + $post = $this->request->post(); + $rule = [ + 'id|ID' => 'require', + 'field|字段' => 'require', + 'value|值' => 'require', + ]; + $this->validate($post, $rule); + if (!in_array($post['field'], $this->allowModifyFields)) { + $this->error('该字段不允许修改:' . $post['field']); + } + // concurrency 行内修改必须为正整数(M5 防静默吞任务;继承默认请走 edit 表单传空值) + if ($post['field'] === 'concurrency' && (!is_numeric($post['value']) || (int) $post['value'] < 1)) { + $this->error('concurrency 必须是正整数'); + } + $row = $this->model->find($post['id']); + empty($row) && $this->error('数据不存在'); + $value = $post['field'] === 'concurrency' ? (int) $post['value'] : $post['value']; + try { + $save = $row->save([$post['field'] => $value]); + } catch (\Exception $e) { + $this->error($e->getMessage()); + } + if ($save) { + // D18:DB commit 后递增脏标记,触发 timer reload(行内改 concurrency/status/run_type,同 edit) + Cache::inc('timer_config_version'); + } + $this->success('保存成功'); + } + + /** + * 归一化 concurrency 入库值. + * 空字符串/null → null(继承代码默认);正整数 → int;0或负数/非数字 → 拒绝. + * + * @param mixed $value 提交值 + * @return int|null + */ + protected function normalizeConcurrency($value) + { + if ($value === '' || $value === null) { + return null; + } + if (!is_numeric($value) || (int) $value < 1) { + $this->error('concurrency 必须是正整数或空(继承默认)'); + } + + return (int) $value; + } +} diff --git a/app/admin/view/system/timer_config/_common.js b/extend/base/admin/view/system/timer_config/_common.js similarity index 100% rename from app/admin/view/system/timer_config/_common.js rename to extend/base/admin/view/system/timer_config/_common.js diff --git a/app/admin/view/system/timer_config/add.html b/extend/base/admin/view/system/timer_config/add.html similarity index 100% rename from app/admin/view/system/timer_config/add.html rename to extend/base/admin/view/system/timer_config/add.html diff --git a/app/admin/view/system/timer_config/add.js b/extend/base/admin/view/system/timer_config/add.js similarity index 100% rename from app/admin/view/system/timer_config/add.js rename to extend/base/admin/view/system/timer_config/add.js diff --git a/app/admin/view/system/timer_config/edit.html b/extend/base/admin/view/system/timer_config/edit.html similarity index 100% rename from app/admin/view/system/timer_config/edit.html rename to extend/base/admin/view/system/timer_config/edit.html diff --git a/app/admin/view/system/timer_config/edit.js b/extend/base/admin/view/system/timer_config/edit.js similarity index 100% rename from app/admin/view/system/timer_config/edit.js rename to extend/base/admin/view/system/timer_config/edit.js diff --git a/app/admin/view/system/timer_config/index.html b/extend/base/admin/view/system/timer_config/index.html similarity index 100% rename from app/admin/view/system/timer_config/index.html rename to extend/base/admin/view/system/timer_config/index.html diff --git a/app/admin/view/system/timer_config/index.js b/extend/base/admin/view/system/timer_config/index.js similarity index 98% rename from app/admin/view/system/timer_config/index.js rename to extend/base/admin/view/system/timer_config/index.js index 003202b..de4ce82 100644 --- a/app/admin/view/system/timer_config/index.js +++ b/extend/base/admin/view/system/timer_config/index.js @@ -223,8 +223,10 @@ $(function(){ ]]; // ============ 初始化表格(配置基础层,永远渲染)============ + // C 类页面:有限操作(add/delete 已在控制器禁用),仅保留 refresh + export ua.table.render({ init: init, + toolbar: ['refresh', 'export'], cols: cols, }); diff --git a/app/admin/view/system/timer_config/read.html b/extend/base/admin/view/system/timer_config/read.html similarity index 100% rename from app/admin/view/system/timer_config/read.html rename to extend/base/admin/view/system/timer_config/read.html diff --git a/app/admin/view/system/timer_config/read.js b/extend/base/admin/view/system/timer_config/read.js similarity index 100% rename from app/admin/view/system/timer_config/read.js rename to extend/base/admin/view/system/timer_config/read.js diff --git a/app/admin/view/system/timer_log/_common.js b/extend/base/admin/view/system/timer_log/_common.js similarity index 100% rename from app/admin/view/system/timer_log/_common.js rename to extend/base/admin/view/system/timer_log/_common.js diff --git a/app/admin/view/system/timer_log/index.html b/extend/base/admin/view/system/timer_log/index.html similarity index 100% rename from app/admin/view/system/timer_log/index.html rename to extend/base/admin/view/system/timer_log/index.html diff --git a/app/admin/view/system/timer_log/index.js b/extend/base/admin/view/system/timer_log/index.js similarity index 100% rename from app/admin/view/system/timer_log/index.js rename to extend/base/admin/view/system/timer_log/index.js diff --git a/app/admin/view/system/timer_log/read.html b/extend/base/admin/view/system/timer_log/read.html similarity index 100% rename from app/admin/view/system/timer_log/read.html rename to extend/base/admin/view/system/timer_log/read.html diff --git a/app/admin/view/system/timer_log/read.js b/extend/base/admin/view/system/timer_log/read.js similarity index 100% rename from app/admin/view/system/timer_log/read.js rename to extend/base/admin/view/system/timer_log/read.js