提交 | 用户 | 时间
|
820397
|
1 |
<template> |
H |
2 |
<Dialog :title="dialogTitle" v-model="dialogVisible"> |
|
3 |
<el-form |
|
4 |
ref="formRef" |
|
5 |
:model="formData" |
|
6 |
:rules="formRules" |
|
7 |
label-width="120px" |
|
8 |
v-loading="formLoading" |
|
9 |
> |
|
10 |
<el-form-item label="任务名称" prop="name"> |
|
11 |
<el-input v-model="formData.name" placeholder="请输入任务名称" /> |
|
12 |
</el-form-item> |
|
13 |
<el-form-item label="处理器的名字" prop="handlerName"> |
|
14 |
<el-input |
|
15 |
:readonly="formData.id !== undefined" |
|
16 |
v-model="formData.handlerName" |
|
17 |
placeholder="请输入处理器的名字" |
|
18 |
/> |
|
19 |
</el-form-item> |
|
20 |
<el-form-item label="处理器的参数" prop="handlerParam"> |
|
21 |
<el-input v-model="formData.handlerParam" placeholder="请输入处理器的参数" /> |
|
22 |
</el-form-item> |
|
23 |
<el-form-item label="CRON 表达式" prop="cronExpression"> |
|
24 |
<crontab v-model="formData.cronExpression" /> |
|
25 |
</el-form-item> |
|
26 |
<el-form-item label="重试次数" prop="retryCount"> |
|
27 |
<el-input |
|
28 |
v-model="formData.retryCount" |
|
29 |
placeholder="请输入重试次数。设置为 0 时,不进行重试" |
|
30 |
/> |
|
31 |
</el-form-item> |
|
32 |
<el-form-item label="重试间隔" prop="retryInterval"> |
|
33 |
<el-input |
|
34 |
v-model="formData.retryInterval" |
|
35 |
placeholder="请输入重试间隔,单位:毫秒。设置为 0 时,无需间隔" |
|
36 |
/> |
|
37 |
</el-form-item> |
|
38 |
<el-form-item label="监控超时时间" prop="monitorTimeout"> |
|
39 |
<el-input v-model="formData.monitorTimeout" placeholder="请输入监控超时时间,单位:毫秒" /> |
|
40 |
</el-form-item> |
|
41 |
</el-form> |
|
42 |
<template #footer> |
|
43 |
<el-button type="primary" @click="submitForm" :loading="formLoading">确 定</el-button> |
|
44 |
<el-button @click="dialogVisible = false">取 消</el-button> |
|
45 |
</template> |
|
46 |
</Dialog> |
|
47 |
</template> |
|
48 |
<script lang="ts" setup> |
|
49 |
import * as JobApi from '@/api/infra/job' |
|
50 |
|
|
51 |
defineOptions({ name: 'JobForm' }) |
|
52 |
|
|
53 |
const { t } = useI18n() // 国际化 |
|
54 |
const message = useMessage() // 消息弹窗 |
|
55 |
|
|
56 |
const dialogVisible = ref(false) // 弹窗的是否展示 |
|
57 |
const dialogTitle = ref('') // 弹窗的标题 |
|
58 |
const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用 |
|
59 |
const formType = ref('') // 表单的类型:create - 新增;update - 修改 |
|
60 |
const formData = ref({ |
|
61 |
id: undefined, |
|
62 |
name: '', |
|
63 |
handlerName: '', |
|
64 |
handlerParam: '', |
|
65 |
cronExpression: '', |
|
66 |
retryCount: undefined, |
|
67 |
retryInterval: undefined, |
|
68 |
monitorTimeout: undefined |
|
69 |
}) |
|
70 |
const formRules = reactive({ |
|
71 |
name: [{ required: true, message: '任务名称不能为空', trigger: 'blur' }], |
|
72 |
handlerName: [{ required: true, message: '处理器的名字不能为空', trigger: 'blur' }], |
|
73 |
cronExpression: [{ required: true, message: 'CRON 表达式不能为空', trigger: 'blur' }], |
|
74 |
retryCount: [{ required: true, message: '重试次数不能为空', trigger: 'blur' }], |
|
75 |
retryInterval: [{ required: true, message: '重试间隔不能为空', trigger: 'blur' }] |
|
76 |
}) |
|
77 |
const formRef = ref() // 表单 Ref |
|
78 |
|
|
79 |
/** 打开弹窗 */ |
|
80 |
const open = async (type: string, id?: number) => { |
|
81 |
dialogVisible.value = true |
|
82 |
dialogTitle.value = t('action.' + type) |
|
83 |
formType.value = type |
|
84 |
resetForm() |
|
85 |
// 修改时,设置数据 |
|
86 |
if (id) { |
|
87 |
formLoading.value = true |
|
88 |
try { |
|
89 |
formData.value = await JobApi.getJob(id) |
|
90 |
} finally { |
|
91 |
formLoading.value = false |
|
92 |
} |
|
93 |
} |
|
94 |
} |
|
95 |
defineExpose({ open }) // 提供 open 方法,用于打开弹窗 |
|
96 |
|
|
97 |
/** 提交按钮 */ |
|
98 |
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调 |
|
99 |
const submitForm = async () => { |
|
100 |
// 校验表单 |
|
101 |
if (!formRef) return |
|
102 |
const valid = await formRef.value.validate() |
|
103 |
if (!valid) return |
|
104 |
// 提交请求 |
|
105 |
formLoading.value = true |
|
106 |
try { |
|
107 |
const data = formData.value as unknown as JobApi.JobVO |
|
108 |
if (formType.value === 'create') { |
|
109 |
await JobApi.createJob(data) |
|
110 |
message.success(t('common.createSuccess')) |
|
111 |
} else { |
|
112 |
await JobApi.updateJob(data) |
|
113 |
message.success(t('common.updateSuccess')) |
|
114 |
} |
|
115 |
dialogVisible.value = false |
|
116 |
// 发送操作成功的事件 |
|
117 |
emit('success') |
|
118 |
} finally { |
|
119 |
formLoading.value = false |
|
120 |
} |
|
121 |
} |
|
122 |
|
|
123 |
/** 重置表单 */ |
|
124 |
const resetForm = () => { |
|
125 |
formData.value = { |
|
126 |
id: undefined, |
|
127 |
name: '', |
|
128 |
handlerName: '', |
|
129 |
handlerParam: '', |
|
130 |
cronExpression: '', |
|
131 |
retryCount: undefined, |
|
132 |
retryInterval: undefined, |
|
133 |
monitorTimeout: undefined |
|
134 |
} |
|
135 |
formRef.value?.resetFields() |
|
136 |
} |
|
137 |
</script> |