index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <template>
  2. <div class="app-container">
  3. <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
  4. <el-form-item label="任务名称" prop="name">
  5. <el-input v-model="queryParams.name" placeholder="请输入任务名称" clearable size="small" @keyup.enter.native="handleQuery"/>
  6. </el-form-item>
  7. <el-form-item label="任务状态" prop="status">
  8. <el-select v-model="queryParams.status" placeholder="请选择任务状态" clearable size="small">
  9. <el-option v-for="dict in this.getDictDatas(DICT_TYPE.INFRA_JOB_STATUS)"
  10. :key="dict.value" :label="dict.label" :value="dict.value"/>
  11. </el-select>
  12. </el-form-item>
  13. <el-form-item label="处理器的名字" prop="handlerName">
  14. <el-input v-model="queryParams.handlerName" placeholder="请输入处理器的名字" clearable size="small" @keyup.enter.native="handleQuery"/>
  15. </el-form-item>
  16. <el-form-item>
  17. <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
  18. <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
  19. </el-form-item>
  20. </el-form>
  21. <el-row :gutter="10" class="mb8">
  22. <el-col :span="1.5">
  23. <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
  24. v-hasPermi="['infra:job:create']">新增</el-button>
  25. </el-col>
  26. <el-col :span="1.5">
  27. <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading"
  28. v-hasPermi="['infra:job:export']">导出</el-button>
  29. </el-col>
  30. <el-col :span="1.5">
  31. <el-button type="info" icon="el-icon-s-operation" size="mini" @click="handleJobLog"
  32. v-hasPermi="['infra:job:query']">执行日志</el-button>
  33. </el-col>
  34. <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
  35. </el-row>
  36. <el-table v-loading="loading" :data="jobList">
  37. <el-table-column label="任务编号" align="center" prop="id" />
  38. <el-table-column label="任务名称" align="center" prop="name" />
  39. <el-table-column label="任务状态" align="center" prop="status">
  40. <template slot-scope="scope">
  41. <span>{{ getDictDataLabel(DICT_TYPE.INFRA_JOB_STATUS, scope.row.status) }}</span>
  42. </template>
  43. </el-table-column>>
  44. <el-table-column label="处理器的名字" align="center" prop="handlerName" />
  45. <el-table-column label="处理器的参数" align="center" prop="handlerParam" />
  46. <el-table-column label="CRON 表达式" align="center" prop="cronExpression" />
  47. <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
  48. <template slot-scope="scope">
  49. <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)"
  50. v-hasPermi="['infra:job:query']">详细</el-button>
  51. <el-button size="mini" icon="el-icon-s-operation" @click="handleJobLog(scope.row)"
  52. v-hasPermi="['infra:job:query']">执行日志</el-button>
  53. <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
  54. v-hasPermi="['infra:job:update']">修改</el-button>
  55. <el-button size="mini" type="text" icon="el-icon-check" @click="handleChangeStatus(scope.row, true)"
  56. v-if="scope.row.status === InfJobStatusEnum.STOP" v-hasPermi="['infra:job:update']">开启</el-button>
  57. <el-button size="mini" type="text" icon="el-icon-close" @click="handleChangeStatus(scope.row, false)"
  58. v-if="scope.row.status === InfJobStatusEnum.NORMAL" v-hasPermi="['infra:job:update']">暂停</el-button>
  59. <el-button size="mini" type="text" icon="el-icon-caret-right" @click="handleRun(scope.row)"
  60. v-hasPermi="['infra:job:trigger']">执行一次</el-button>
  61. <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
  62. v-hasPermi="['infra:job:delete']">删除</el-button>
  63. </template>
  64. </el-table-column>
  65. </el-table>
  66. <!-- 分页组件 -->
  67. <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
  68. @pagination="getList"/>
  69. <!-- 添加或修改定时任务对话框 -->
  70. <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
  71. <el-form ref="form" :model="form" :rules="rules" label-width="120px">
  72. <el-form-item label="任务名称" prop="name">
  73. <el-input v-model="form.name" placeholder="请输入任务名称" />
  74. </el-form-item>
  75. <el-form-item label="处理器的名字" prop="handlerName">
  76. <el-input v-model="form.handlerName" placeholder="请输入处理器的名字" v-bind:readonly="form.id !== undefined" />
  77. </el-form-item>
  78. <el-form-item label="处理器的参数" prop="handlerParam">
  79. <el-input v-model="form.handlerParam" placeholder="请输入处理器的参数" />
  80. </el-form-item>
  81. <el-form-item label="CRON 表达式" prop="cronExpression">
  82. <el-input v-model="form.cronExpression" placeholder="请输入CRON 表达式" />
  83. </el-form-item>
  84. <el-form-item label="重试次数" prop="retryCount">
  85. <el-input v-model="form.retryCount" placeholder="请输入重试次数。设置为 0 时,不进行重试" />
  86. </el-form-item>
  87. <el-form-item label="重试间隔" prop="retryInterval">
  88. <el-input v-model="form.retryInterval" placeholder="请输入重试间隔,单位:毫秒。设置为 0 时,无需间隔" />
  89. </el-form-item>
  90. <el-form-item label="监控超时时间" prop="monitorTimeout">
  91. <el-input v-model="form.monitorTimeout" placeholder="请输入监控超时时间,单位:毫秒" />
  92. </el-form-item>
  93. </el-form>
  94. <div slot="footer" class="dialog-footer">
  95. <el-button type="primary" @click="submitForm">确 定</el-button>
  96. <el-button @click="cancel">取 消</el-button>
  97. </div>
  98. </el-dialog>
  99. <!-- 任务日志详细 -->
  100. <el-dialog title="任务详细" :visible.sync="openView" width="700px" append-to-body>
  101. <el-form ref="form" :model="form" label-width="200px" size="mini">
  102. <el-row>
  103. <el-col :span="24">
  104. <el-form-item label="任务编号:">{{ form.id }}</el-form-item>
  105. <el-form-item label="任务名称:">{{ form.name }}</el-form-item>
  106. <el-form-item label="任务名称:">{{ getDictDataLabel(DICT_TYPE.INFRA_JOB_STATUS, form.status) }}</el-form-item>
  107. <el-form-item label="处理器的名字:">{{ form.handlerName }}</el-form-item>
  108. <el-form-item label="处理器的参数:">{{ form.handlerParam }}</el-form-item>
  109. <el-form-item label="cron表达式:">{{ form.cronExpression }}</el-form-item>
  110. <el-form-item label="重试次数:">{{ form.retryCount }}</el-form-item>
  111. <el-form-item label="重试间隔:">{{ form.retryInterval + " 毫秒" }}</el-form-item>
  112. <el-form-item label="监控超时时间:">{{ form.monitorTimeout > 0 ? form.monitorTimeout + " 毫秒" : "未开启" }}</el-form-item>
  113. <el-form-item label="后续执行时间:">{{ Array.from(nextTimes, x => parseTime(x)).join('; ')}}</el-form-item>
  114. </el-col>
  115. </el-row>
  116. </el-form>
  117. <div slot="footer" class="dialog-footer">
  118. <el-button @click="openView = false">关 闭</el-button>
  119. </div>
  120. </el-dialog>
  121. </div>
  122. </template>
  123. <script>
  124. import { listJob, getJob, delJob, addJob, updateJob, exportJob, runJob, updateJobStatus, getJobNextTimes } from "@/api/infra/job";
  125. import { InfraJobStatusEnum } from "@/utils/constants";
  126. export default {
  127. name: "Job",
  128. data() {
  129. return {
  130. // 遮罩层
  131. loading: true,
  132. // 导出遮罩层
  133. exportLoading: false,
  134. // 显示搜索条件
  135. showSearch: true,
  136. // 总条数
  137. total: 0,
  138. // 定时任务表格数据
  139. jobList: [],
  140. // 弹出层标题
  141. title: "",
  142. // 是否显示弹出层
  143. open: false,
  144. // 是否显示详细弹出层
  145. openView: false,
  146. // 状态字典
  147. statusOptions: [],
  148. // 查询参数
  149. queryParams: {
  150. pageNo: 1,
  151. pageSize: 10,
  152. name: undefined,
  153. status: undefined,
  154. handlerName: undefined
  155. },
  156. // 表单参数
  157. form: {},
  158. // 表单校验
  159. rules: {
  160. name: [{ required: true, message: "任务名称不能为空", trigger: "blur" }],
  161. handlerName: [{ required: true, message: "处理器的名字不能为空", trigger: "blur" }],
  162. cronExpression: [{ required: true, message: "CRON 表达式不能为空", trigger: "blur" }],
  163. retryCount: [{ required: true, message: "重试次数不能为空", trigger: "blur" }],
  164. retryInterval: [{ required: true, message: "重试间隔不能为空", trigger: "blur" }],
  165. },
  166. nextTimes: [], // 后续执行时间
  167. // 枚举
  168. InfJobStatusEnum: InfraJobStatusEnum
  169. };
  170. },
  171. created() {
  172. this.getList();
  173. },
  174. methods: {
  175. /** 查询定时任务列表 */
  176. getList() {
  177. this.loading = true;
  178. listJob(this.queryParams).then(response => {
  179. this.jobList = response.data.list;
  180. this.total = response.data.total;
  181. this.loading = false;
  182. });
  183. },
  184. /** 取消按钮 */
  185. cancel() {
  186. this.open = false;
  187. this.reset();
  188. },
  189. /** 表单重置 */
  190. reset() {
  191. this.form = {
  192. id: undefined,
  193. name: undefined,
  194. handlerName: undefined,
  195. handlerParam: undefined,
  196. cronExpression: undefined,
  197. retryCount: undefined,
  198. retryInterval: undefined,
  199. monitorTimeout: undefined,
  200. };
  201. this.nextTimes = [];
  202. this.resetForm("form");
  203. },
  204. /** 搜索按钮操作 */
  205. handleQuery() {
  206. this.queryParams.pageNo = 1;
  207. this.getList();
  208. },
  209. /** 重置按钮操作 */
  210. resetQuery() {
  211. this.resetForm("queryForm");
  212. this.handleQuery();
  213. },
  214. /** 立即执行一次 **/
  215. handleRun(row) {
  216. this.$confirm('确认要立即执行一次"' + row.name + '"任务吗?', "警告", {
  217. confirmButtonText: "确定",
  218. cancelButtonText: "取消",
  219. type: "warning"
  220. }).then(function() {
  221. return runJob(row.id);
  222. }).then(() => {
  223. this.msgSuccess("执行成功");
  224. }).catch(() => {});
  225. },
  226. /** 任务详细信息 */
  227. handleView(row) {
  228. getJob(row.id).then(response => {
  229. this.form = response.data;
  230. this.openView = true;
  231. });
  232. // 获取下一次执行时间
  233. getJobNextTimes(row.id).then(response => {
  234. this.nextTimes = response.data;
  235. });
  236. },
  237. /** 任务日志列表查询 */
  238. handleJobLog(row) {
  239. if (row.id) {
  240. this.$router.push({
  241. path:"/job/log",
  242. query:{
  243. jobId: row.id
  244. }
  245. });
  246. } else {
  247. this.$router.push("/job/log");
  248. }
  249. },
  250. /** 新增按钮操作 */
  251. handleAdd() {
  252. this.reset();
  253. this.open = true;
  254. this.title = "添加任务";
  255. },
  256. /** 修改按钮操作 */
  257. handleUpdate(row) {
  258. this.reset();
  259. const id = row.id;
  260. getJob(id).then(response => {
  261. this.form = response.data;
  262. this.open = true;
  263. this.title = "修改任务";
  264. });
  265. },
  266. /** 提交按钮 */
  267. submitForm: function() {
  268. this.$refs["form"].validate(valid => {
  269. if (valid) {
  270. if (this.form.id !== undefined) {
  271. updateJob(this.form).then(response => {
  272. this.msgSuccess("修改成功");
  273. this.open = false;
  274. this.getList();
  275. });
  276. } else {
  277. addJob(this.form).then(response => {
  278. this.msgSuccess("新增成功");
  279. this.open = false;
  280. this.getList();
  281. });
  282. }
  283. }
  284. });
  285. },
  286. /** 删除按钮操作 */
  287. handleDelete(row) {
  288. const ids = row.id;
  289. this.$confirm('是否确认删除定时任务编号为"' + ids + '"的数据项?', "警告", {
  290. confirmButtonText: "确定",
  291. cancelButtonText: "取消",
  292. type: "warning"
  293. }).then(function() {
  294. return delJob(ids);
  295. }).then(() => {
  296. this.getList();
  297. this.msgSuccess("删除成功");
  298. }).catch(() => {});
  299. },
  300. /** 更新状态操作 */
  301. handleChangeStatus(row, open) {
  302. const id = row.id;
  303. let status = open ? InfraJobStatusEnum.NORMAL : InfraJobStatusEnum.STOP;
  304. let statusStr = open ? '开启' : '关闭';
  305. this.$confirm('是否确认' + statusStr + '定时任务编号为"' + id + '"的数据项?', "警告", {
  306. confirmButtonText: "确定",
  307. cancelButtonText: "取消",
  308. type: "warning"
  309. }).then(function() {
  310. return updateJobStatus(id, status);
  311. }).then(() => {
  312. this.getList();
  313. this.msgSuccess(statusStr + "成功");
  314. }).catch(() => {});
  315. },
  316. /** 导出按钮操作 */
  317. handleExport() {
  318. const queryParams = this.queryParams;
  319. this.$confirm("是否确认导出所有定时任务数据项?", "警告", {
  320. confirmButtonText: "确定",
  321. cancelButtonText: "取消",
  322. type: "warning"
  323. }).then(() => {
  324. this.exportLoading = true;
  325. return exportJob(queryParams);
  326. }).then(response => {
  327. this.downloadExcel(response, '定时任务.xls');
  328. this.exportLoading = false;
  329. }).catch(() => {});
  330. }
  331. }
  332. };
  333. </script>