feat: 新增壳子内置定时任务功能,支持配置备份与恢复

本次提交新增完整的定时任务系统:
1. 后端支持cron表达式调度插件执行,与手动运行共用执行链路
2. 前端新增定时任务管理页面,支持增删改查与启停操作
3. 实现配置自动备份到中心机,启动时自动从云端恢复任务
4. 配套新增数据库表与控制面API接口
5. 同时优化了考勤异常检查的规则细节
This commit is contained in:
2026-09-19 12:11:49 +08:00
parent 530cfe38dc
commit 600379933d
16 changed files with 1816 additions and 129 deletions
+26 -1
View File
@@ -137,6 +137,12 @@
</li>
</ul>
<div v-if="!store.plugins.length" class="scene-empty">当前账号暂无可用场景<br><span>请联系管理员进行授权</span></div>
<div class="side-nav" @click="openSchedules">
<span class="nav-item" :class="{ active: store.view === 'schedules' }">
<el-icon><Clock /></el-icon>
<span>定时任务</span>
</span>
</div>
<div class="side-foot">
<div class="foot-ver">
<span class="ver-num">壳子 {{ shellUpdate.current ? 'v' + shellUpdate.current : '未打包' }}</span>
@@ -151,6 +157,7 @@
<main class="main">
<div class="page-scroll panel">
<PluginPage v-if="store.view === 'plugin'" />
<SchedulePage v-else-if="store.view === 'schedules'" />
</div>
<LogPanel />
</main>
@@ -185,8 +192,9 @@
<script setup>
import { computed, onMounted, onUnmounted, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Refresh } from '@element-plus/icons-vue'
import { Clock, Refresh } from '@element-plus/icons-vue'
import PluginPage from './components/PluginPage.vue'
import SchedulePage from './components/SchedulePage.vue'
import LogPanel from './components/LogPanel.vue'
import LoginPage from './components/LoginPage.vue'
import ProfilePanel from './components/ProfilePanel.vue'
@@ -208,6 +216,10 @@ function openPlugin(id) {
store.view = 'plugin'
}
function openSchedules() {
store.view = 'schedules'
}
function openWS() {
if (window.__ws) window.__ws.close()
const ws = connectWS(handleEvent)
@@ -764,6 +776,19 @@ async function login(id) {
}
.scene-empty span { font-size: 11px; color: var(--text-faint); }
.side-foot { font-size: 10px; color: var(--text-faint); text-align: center; padding-top: 10px; border-top: 1px dashed var(--border); margin-top: 8px; display: flex; flex-direction: column; gap: 4px; }
.side-nav { padding: 6px 4px; }
.nav-item {
display: flex; align-items: center; gap: 10px;
padding: 9px 10px; border-radius: var(--radius-sm); cursor: pointer;
color: var(--text-sub); font-size: 13px; position: relative;
transition: background .13s;
}
.side-nav:hover .nav-item { background: var(--surface-2); }
.nav-item.active { background: var(--primary-weak); color: var(--primary); font-weight: 600; }
.nav-item.active::before {
content: ''; position: absolute; left: 0; top: 9px; bottom: 9px;
width: 3px; border-radius: 3px; background: var(--primary);
}
.foot-ver { display: flex; align-items: center; justify-content: center; gap: 8px; }
.ver-num { font-variant-numeric: tabular-nums; }
.check-link { font-size: 10px; vertical-align: baseline; }
+29
View File
@@ -69,6 +69,35 @@ export async function getRun(runId) {
return j(`/api/runs/${runId}`)
}
// ===== 定时任务(壳子内置定时,配置自动备份中心机) =====
export async function listSchedules() {
return j('/api/schedules')
}
export async function createSchedule(body) {
return j('/api/schedules', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
})
}
export async function updateSchedule(sid, body) {
return j(`/api/schedules/${sid}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
})
}
export async function deleteSchedule(sid) {
return j(`/api/schedules/${sid}`, { method: 'DELETE' })
}
export async function toggleSchedule(sid, enabled) {
return j(`/api/schedules/${sid}/toggle`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ enabled }),
})
}
export function connectWS(onEvent) {
const ws = new WebSocket(WS_BASE + '/ws')
ws.onmessage = (e) => {
@@ -0,0 +1,361 @@
<template>
<div class="sched-page">
<div class="head">
<div class="ident">
<span class="avatar"><el-icon><Clock /></el-icon></span>
<div class="ident-meta">
<h2>定时任务</h2>
<p>壳子内置定时:按 cron 到点自动执行插件,配置自动备份到中心机防丢失</p>
</div>
</div>
<el-button type="primary" :icon="Plus" @click="openCreate">新建任务</el-button>
</div>
<el-empty v-if="!list.length" description="暂无定时任务,点击右上角「新建任务」创建" />
<el-table v-else :data="list" class="sched-table" row-key="id">
<el-table-column label="任务" min-width="160">
<template #default="{ row }">
<div class="cell-main">
<span class="t-name">{{ row.name }}</span>
<span class="t-plugin">{{ pluginName(row.plugin_id) }}</span>
</div>
</template>
</el-table-column>
<el-table-column label="cron 表达式" width="140">
<template #default="{ row }">
<code class="cron">{{ row.cron }}</code>
</template>
</el-table-column>
<el-table-column label="下次执行" width="158">
<template #default="{ row }">{{ row.next_run_at || '-' }}</template>
</el-table-column>
<el-table-column label="最近执行" width="158">
<template #default="{ row }">{{ row.last_run_at || '从未' }}</template>
</el-table-column>
<el-table-column label="启用" width="80" align="center">
<template #default="{ row }">
<el-switch :model-value="row.enabled" @change="(v) => onToggle(row, v)" />
</template>
</el-table-column>
<el-table-column label="操作" width="130" align="right">
<template #default="{ row }">
<el-button link type="primary" @click="openEdit(row)">编辑</el-button>
<el-button link type="danger" @click="onDelete(row)">删除</el-button>
</template>
</el-table-column>
</el-table>
<el-dialog v-model="dlg" :title="editing ? '编辑定时任务' : '新建定时任务'" width="560px" align-center>
<el-form label-width="86px" class="dlg-form">
<el-form-item label="任务名称">
<el-input v-model="form.name" placeholder="如:每日考勤封存检查" maxlength="32" />
</el-form-item>
<el-form-item label="执行插件">
<el-select v-model="form.plugin_id" placeholder="选择要定时执行的插件" class="w-full" @change="onPluginChange">
<el-option v-for="p in store.plugins" :key="p.id" :value="p.id"
:label="`${p.name}(${p.id})`" />
</el-select>
</el-form-item>
<el-form-item label="触发时间">
<div class="cron-row">
<el-input v-model="form.cron" placeholder="标准 5 段 cron,如 0 8 * * *" class="cron-input">
<template #prepend>cron</template>
</el-input>
<el-select v-model="preset" placeholder="快捷项" class="preset-sel" @change="onPreset">
<el-option v-for="p in presets" :key="p.cron" :value="p.cron" :label="p.label" />
</el-select>
</div>
<div class="cron-tip">5 段格式:分 时 日 月 周(0-59 0-23 1-31 1-12 0-6,0/7 均为周日)</div>
</el-form-item>
<el-form-item v-if="pluginFields.length" label="执行参数">
<div class="fields">
<div v-for="f in pluginFields" :key="f.key" class="field">
<span class="field-label">{{ f.label }}</span>
<el-input-number v-if="f.type === 'int'" v-model="form.fields[f.key]" :controls="false"
placeholder="0" class="field-ctl" />
<el-input v-else-if="f.type === 'password'" v-model="form.fields[f.key]" type="password"
show-password :placeholder="String(f.default ?? '')" class="field-ctl" />
<el-input v-else-if="f.type === 'text'" v-model="form.fields[f.key]"
:placeholder="String(f.default ?? '')" class="field-ctl" />
<el-select v-else-if="f.type === 'multiselect'" v-model="form.fields[f.key]" multiple
filterable allow-create default-first-option placeholder="选择或输入后回车" class="field-ctl">
<el-option v-for="opt in (f.options || [])" :key="opt" :value="opt" :label="opt" />
</el-select>
<el-select v-else v-model="form.fields[f.key]" class="field-ctl">
<el-option v-for="opt in f.options" :key="opt" :value="opt" :label="opt" />
</el-select>
</div>
</div>
</el-form-item>
<el-form-item label="启用">
<el-switch v-model="form.enabled" />
</el-form-item>
</el-form>
<template #footer>
<el-button @click="dlg = false">取消</el-button>
<el-button type="primary" :loading="saving" @click="onSave">保存</el-button>
</template>
</el-dialog>
</div>
</template>
<script setup>
import { computed, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Clock, Plus } from '@element-plus/icons-vue'
import { store } from '../store'
import { listSchedules, createSchedule, updateSchedule, deleteSchedule, toggleSchedule } from '../api'
const list = ref([])
const dlg = ref(false)
const editing = ref(null)
const saving = ref(false)
const preset = ref('')
const presets = [
{ label: '每天 08:00', cron: '0 8 * * *' },
{ label: '每天 12:00', cron: '0 12 * * *' },
{ label: '每天 18:00', cron: '0 18 * * *' },
{ label: '每小时整点', cron: '0 * * * *' },
{ label: '每周一 09:00', cron: '0 9 * * 1' },
{ label: '每月 1 日 09:00', cron: '0 9 1 * *' },
]
const form = ref({ name: '', plugin_id: '', cron: '', enabled: true, fields: {} })
const pluginFields = computed(() => {
const p = store.plugins.find((x) => x.id === form.value.plugin_id)
return (p && p.fields) || []
})
function pluginName(id) {
return store.plugins.find((p) => p.id === id)?.name || id
}
function buildFields(p) {
const f = {}
if (p) p.fields.forEach((x) => (f[x.key] = x.default ?? (x.type === 'int' ? 0 : '')))
return f
}
function onPluginChange(id) {
form.value.fields = buildFields(store.plugins.find((x) => x.id === id))
}
function onPreset(cron) {
form.value.cron = cron
preset.value = ''
}
async function load() {
try {
const r = await listSchedules()
list.value = r.schedules || []
} catch (e) {
ElMessage.error('加载定时任务失败:' + (e.message || e))
}
}
function openCreate() {
editing.value = null
form.value = { name: '', plugin_id: '', cron: '', enabled: true, fields: {} }
dlg.value = true
}
function openEdit(row) {
editing.value = row
form.value = {
name: row.name || '',
plugin_id: row.plugin_id || '',
cron: row.cron || '',
enabled: !!row.enabled,
fields: buildFields(store.plugins.find((x) => x.id === row.plugin_id)),
}
Object.assign(form.value.fields, row.fields || {})
dlg.value = true
}
async function onSave() {
if (!form.value.plugin_id) { ElMessage.warning('请选择执行插件'); return }
if (!form.value.cron.trim()) { ElMessage.warning('请填写 cron 表达式'); return }
saving.value = true
try {
if (editing.value) {
await updateSchedule(editing.value.id, { ...form.value })
ElMessage.success('已保存')
} else {
await createSchedule({ ...form.value })
ElMessage.success('已创建,配置已备份到中心机')
}
dlg.value = false
await load()
} catch (e) {
ElMessage.error(e.message || String(e))
} finally {
saving.value = false
}
}
async function onToggle(row, v) {
try {
await toggleSchedule(row.id, v)
row.enabled = v
ElMessage.success(v ? '已启用' : '已停用')
} catch (e) {
ElMessage.error('操作失败:' + (e.message || e))
}
}
async function onDelete(row) {
try {
await ElMessageBox.confirm(`确定删除定时任务「${row.name}」?删除后其配置备份会一并移除。`, '删除确认', {
type: 'warning',
confirmButtonText: '删除',
cancelButtonText: '取消',
})
} catch (_) { return }
try {
await deleteSchedule(row.id)
ElMessage.success('已删除')
await load()
} catch (e) {
ElMessage.error('删除失败:' + (e.message || e))
}
}
load()
</script>
<style scoped>
.sched-page {
display: flex;
flex-direction: column;
gap: 18px;
min-height: 100%;
}
.head {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 16px;
}
.ident {
display: flex;
align-items: center;
gap: 14px;
min-width: 0;
}
.avatar {
width: 44px;
height: 44px;
border-radius: 13px;
flex-shrink: 0;
display: grid;
place-items: center;
color: #fff;
background: var(--primary-grad, linear-gradient(135deg, #3b6cff, #7c5cff));
box-shadow: var(--shadow-md);
font-size: 18px;
}
.ident-meta {
display: flex;
flex-direction: column;
gap: 2px;
min-width: 0;
}
.ident-meta h2 {
margin: 0;
font-size: 17px;
}
.ident-meta p {
margin: 0;
font-size: 12px;
color: var(--text-sub);
}
.sched-table {
--el-table-border-color: var(--border);
--el-table-header-bg-color: var(--surface-2, #f7f8fa);
}
.cell-main {
display: flex;
flex-direction: column;
gap: 2px;
}
.t-name {
font-weight: 600;
color: var(--text);
}
.t-plugin {
font-size: 11px;
color: var(--text-faint);
}
.cron {
font-family: Consolas, Monaco, monospace;
font-size: 12px;
color: var(--primary);
background: var(--primary-weak, #eef2ff);
border-radius: 6px;
padding: 2px 6px;
}
.cron-row {
display: flex;
gap: 8px;
width: 100%;
}
.cron-input {
flex: 1;
}
.preset-sel {
width: 128px;
}
.cron-tip {
font-size: 11px;
color: var(--text-faint);
margin-top: 6px;
width: 100%;
}
.fields {
display: flex;
flex-direction: column;
gap: 10px;
width: 100%;
}
.field {
display: flex;
align-items: center;
gap: 8px;
}
.field-label {
width: 96px;
flex-shrink: 0;
font-size: 13px;
color: var(--text-sub);
}
.field-ctl {
flex: 1;
}
.w-full {
width: 100%;
}
</style>