wanshenmean
2026-03-24 78e153a3bec824964a52c3a0c2744ef0191f44ac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<template>
  <div class="scheduler-page">
    <el-card shadow="never" class="card-item">
      <template #header>
        <span>调度服务控制</span>
      </template>
      <div class="button-row">
        <el-button type="success" @click="handleSchedulerAction('StartSchedule')">启动调度服务</el-button>
        <el-button type="warning" @click="handleSchedulerAction('StopSchedule')">停止调度服务</el-button>
      </div>
    </el-card>
 
    <el-card shadow="never" class="card-item">
      <template #header>
        <span>任务调度操作</span>
      </template>
      <el-form :model="jobForm" label-width="110px" class="form-wrap">
        <el-row :gutter="16">
          <el-col :span="8">
            <el-form-item label="任务名称">
              <el-input v-model="jobForm.name" placeholder="请输入任务名称" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="任务分组">
              <el-input v-model="jobForm.jobGroup" placeholder="请输入任务分组" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="执行类名">
              <el-input v-model="jobForm.className" placeholder="请输入执行类名" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="button-row">
        <el-button type="primary" @click="handleSchedulerAction('PauseJob')">暂停任务</el-button>
        <el-button type="primary" @click="handleSchedulerAction('ResumeJob')">恢复任务</el-button>
        <el-button type="primary" @click="handleSchedulerAction('ExecuteJob')">立即执行</el-button>
        <el-button type="danger" @click="handleSchedulerAction('DeleteScheduleJob')">删除任务</el-button>
      </div>
    </el-card>
  </div>
</template>
 
<script>
import { defineComponent, ref } from "vue";
import { ElMessage } from "element-plus";
import http from "@/api/http";
 
export default defineComponent({
  setup() {
    const jobForm = ref({
      name: "",
      jobGroup: "",
      className: "",
      assemblyName: "",
      intervalSecond: 0,
      beginTime: "",
      endTime: "",
      remark: "",
    });
 
    /**
     * 方法目的:校验任务级调度操作所需参数,避免提交无效请求。
     * 参数含义:无(直接读取当前 jobForm)。
     * 返回值:true 表示校验通过;false 表示校验失败并弹出提示。
     * 异常处理:本地校验不抛异常,通过消息提示引导用户补全关键字段。
     */
    const validateJobPayload = () => {
      if (!jobForm.value.name || !jobForm.value.jobGroup || !jobForm.value.className) {
        ElMessage.error("请先填写任务名称、任务分组、执行类名");
        return false;
      }
      return true;
    };
 
    /**
     * 方法目的:统一处理调度控制请求,兼容 GET 与 POST 两类接口。
     * 参数含义:action 为调度动作名称,对应后端 api/Scheduler/{action}。
     * 返回值:Promise<void>,执行完成后通过消息提示结果。
     * 关键逻辑:
     * 1. 全局动作(启动/停止)走 GET。
     * 2. 任务动作(暂停/恢复/执行/删除)走 POST,并提交 jobForm。
     * 3. 接口返回 status=false 时提示后端 message。
     * 异常处理:使用 try-catch 捕获请求异常并保留错误上下文输出。
     */
    const handleSchedulerAction = async (action) => {
      try {
        const isGlobalAction = action === "StartSchedule" || action === "StopSchedule";
        if (!isGlobalAction && !validateJobPayload()) {
          return;
        }
 
        let result;
        if (isGlobalAction) {
          result = await http.get(`/api/Scheduler/${action}`, {}, true);
        } else {
          result = await http.post(`/api/Scheduler/${action}`, jobForm.value, true);
        }
 
        if (result && result.status) {
          ElMessage.success(result.message || "操作成功");
          return;
        }
 
        ElMessage.error((result && result.message) || "操作失败");
      } catch (error) {
        console.error("调度请求异常", { action, jobForm: jobForm.value, error });
        ElMessage.error("调度请求失败,请稍后重试");
      }
    };
 
    return {
      jobForm,
      handleSchedulerAction,
    };
  },
});
</script>
 
<style scoped>
.scheduler-page {
  padding: 12px;
}
 
.card-item {
  margin-bottom: 12px;
}
 
.button-row {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}
</style>