feat: 支持创建任务时自定义crontab并清理冗余代码

- 添加创建任务时的crontab编辑控件
- 修复创建任务按钮状态重置问题
- 创建任务后自动加载到调度器
- 删除废弃的手动创建任务API和相关代码
This commit is contained in:
2026-01-05 20:53:25 +08:00
parent 0fd21960e8
commit a5de813c82
8 changed files with 40 additions and 77 deletions
-5
View File
@@ -89,11 +89,6 @@ export const taskAPI = {
return client.get('/api/tasks', { params });
},
// 创建任务
createTask: taskData => {
return client.post('/api/tasks', taskData);
},
// 获取任务详情
getTask: taskId => {
return client.get(`/api/tasks/${taskId}`);
-19
View File
@@ -46,25 +46,6 @@ export const useTaskStore = defineStore('task', {
}
},
// 创建新任务
async createTask(taskData) {
this.loading = true;
this.error = null;
try {
const newTask = await api.task.createTask(taskData);
this.tasks.unshift(newTask); // 添加到列表开头
return newTask;
} catch (error) {
// 解析后端错误信息
let errorMsg = error.message || '创建任务失败';
this.error = errorMsg;
throw new Error(errorMsg);
} finally {
this.loading = false;
}
},
// 更新任务
async updateTask(taskId, taskData) {
this.loading = true;
+2 -1
View File
@@ -132,7 +132,7 @@ export const useTemplateStore = defineStore('template', {
}
},
async createTaskFromTemplate(templateId, threadId, fieldValues, taskName = null) {
async createTaskFromTemplate(templateId, threadId, fieldValues, taskName = null, cronExpression = '0 20 * * *') {
this.loading = true;
this.error = null;
try {
@@ -141,6 +141,7 @@ export const useTemplateStore = defineStore('template', {
thread_id: threadId,
field_values: fieldValues,
task_name: taskName,
cron_expression: cronExpression,
});
return task;
} catch (error) {
+20 -16
View File
@@ -13,7 +13,7 @@
type="primary"
size="large"
class="shadow-md3-3"
@click="showCreateDialog = true"
@click="openCreateDialog"
>
<template #icon>
<PlusOutlined />
@@ -99,7 +99,7 @@
<p class="text-on-surface-variant mb-6">
点击右上角的"创建任务"按钮开始添加您的第一个打卡任务
</p>
<a-button type="primary" @click="showCreateDialog = true"> 创建第一个任务 </a-button>
<a-button type="primary" @click="openCreateDialog"> 创建第一个任务 </a-button>
</a-card>
<a-row v-else :gutter="[16, 16]">
@@ -263,6 +263,10 @@
<a-input v-model:value="templateTaskForm.thread_id" placeholder="请输入接龙项目 ID" />
</a-form-item>
<a-form-item label="打卡时间表">
<CrontabEditor v-model="templateTaskForm.cron_expression" />
</a-form-item>
<a-divider orientation="left">填写字段信息</a-divider>
<!-- Dynamic Fields -->
@@ -419,19 +423,18 @@ const templateFormRef = ref(null);
const checkInLoading = ref({});
// Template mode
const createMode = ref('template'); // 'template' or 'manual'
const loadingTemplates = ref(false);
const activeTemplates = ref([]);
const selectedTemplate = ref(null);
const templatePreview = ref(null); // 存储从 preview 接口获取的合并后配置
// Manual create form
// Edit task form (仅用于编辑任务)
const taskForm = reactive({
name: '',
thread_id: '',
is_active: true,
payload_config: '',
cron_expression: '0 20 * * *', // 新增:Crontab 表达式,默认每天 20:00
cron_expression: '0 20 * * *',
});
// Template create form
@@ -439,6 +442,7 @@ const templateTaskForm = reactive({
task_name: '',
thread_id: '',
field_values: {},
cron_expression: '0 20 * * *',
});
const taskRules = {
@@ -750,7 +754,7 @@ const handleSubmit = async () => {
message.success('任务更新成功');
}
// Create from template
else if (createMode.value === 'template') {
else {
if (!selectedTemplate.value) {
message.warning('请选择一个模板');
return;
@@ -765,19 +769,12 @@ const handleSubmit = async () => {
selectedTemplate.value.id,
templateTaskForm.thread_id,
templateTaskForm.field_values,
templateTaskForm.task_name || null
templateTaskForm.task_name || null,
templateTaskForm.cron_expression || '0 20 * * *'
);
message.success('任务创建成功');
}
// Create manually
else {
if (!taskFormRef.value) return;
await taskFormRef.value.validate();
await taskStore.createTask(taskForm);
message.success('任务创建成功');
}
showCreateDialog.value = false;
resetForm();
@@ -793,22 +790,29 @@ const handleSubmit = async () => {
const resetForm = () => {
editingTask.value = null;
selectedTemplate.value = null;
createMode.value = 'template';
Object.assign(taskForm, {
name: '',
thread_id: '',
is_active: true,
payload_config: '',
cron_expression: '0 20 * * *',
});
templateTaskForm.task_name = '';
templateTaskForm.thread_id = '';
templateTaskForm.field_values = {};
templateTaskForm.cron_expression = '0 20 * * *';
taskFormRef.value?.resetFields();
};
// 打开创建任务对话框
const openCreateDialog = () => {
resetForm(); // 重置表单状态,确保不会显示编辑界面
showCreateDialog.value = true;
};
// Watch dialog open to load templates
watch(showCreateDialog, isOpen => {
if (isOpen && !editingTask.value) {