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', ]; } /** * @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 $post = array_intersect_key($post, array_flip(['run_type', 'status'])); try { $save = $row->save($post); } catch (\Exception $e) { $this->error('保存失败:' . $e->getMessage()); } $save ? $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('数据不存在'); } if ($row->getAttr('run_type') !== 'manual') { $this->error('只有manual类型的任务支持手动触发'); } if ($row->getAttr('status') != 1) { $this->error('任务已停用,请先启用'); } try { $row->save(['manual_trigger' => 1]); } catch (\Exception $e) { $this->error('触发失败:' . $e->getMessage()); } $this->success('触发成功,等待定时器执行'); } }