本次提交新增完整的定时任务系统: 1. 后端支持cron表达式调度插件执行,与手动运行共用执行链路 2. 前端新增定时任务管理页面,支持增删改查与启停操作 3. 实现配置自动备份到中心机,启动时自动从云端恢复任务 4. 配套新增数据库表与控制面API接口 5. 同时优化了考勤异常检查的规则细节
362 lines
10 KiB
Vue
362 lines
10 KiB
Vue
<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>
|