Fixiaobai
2023-10-13 e8308ddac0ba4a1f406e8f63d7e6b6f2541cb770
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
<template>
  <div class="jobIndex">
    <basic-container>
      <avue-crud
        ref="crud"
        :page="page"
        v-model="form"
        :data="tableData"
        :table-loading="tableLoading"
        :option="tableOption"
        @on-load="getList"
        @row-save="save"
        @row-update="update"
        @refresh-change="refreshChange"
        @size-change="sizeChange"
        @current-change="currentChange"
        @search-change="handleFilter"
        @search-reset="handleSearchReset">
        <template slot-scope="scope" slot="jobStatus">
          <div v-if="scope.row.jobStatus === '1'">
            <el-tag type="info">{{ getDicNameJobStatus(scope.row.jobStatus) }}</el-tag>
          </div>
          <div v-else-if="scope.row.jobStatus === '2'">
            <el-tag type="success">{{ getDicNameJobStatus(scope.row.jobStatus) }}</el-tag>
          </div>
          <div v-else-if="scope.row.jobStatus === '3'">
            <el-tag type="danger">{{ getDicNameJobStatus(scope.row.jobStatus) }}</el-tag>
          </div>
        </template>
        <template slot-scope="scope" slot="jobExecuteStatus">
          <div v-if="scope.row.jobExecuteStatus === '0'">
            <el-tag type="success">{{ getDicNameJobExecuteStatus(scope.row.jobExecuteStatus) }}</el-tag>
          </div>
          <div v-else>
            <el-tag type="danger">{{ getDicNameJobExecuteStatus(scope.row.jobExecuteStatus) }}</el-tag>
          </div>
        </template>
        <template slot="cronExpressionForm" slot-scope="scope">
          <div class="cron">
            <el-popover v-model="cronPopover">
              <cron i18n="cn" @change="changeCron" @close="cronPopover=false"/>
              <el-input
                slot="reference"
                v-model="form.cronExpression"
                placeholder="请输入定时策略"
                @click="cronPopover=true"/>
            </el-popover>
          </div>
        </template>
        <template slot="menuLeft">
          <el-button v-if="permissions.job_sys_job_add" type="primary" @click="handleAdd">
            新建任务
          </el-button>
          <el-tooltip content="暂停全部运行状态的定时任务" placement="top">
            <el-button v-if="permissions.job_sys_job_shutdown_job" type="warning" @click="shutdownJobs">
              暂停全部任务
            </el-button>
          </el-tooltip>
          <el-tooltip content="启动全部暂停状态的定时任务" placement="top">
            <el-button v-if="permissions.job_sys_job_start_job" type="success" @click="startJobs">
              启动全部任务
            </el-button>
          </el-tooltip>
          <el-tooltip content="谨慎使用" placement="top">
            <el-button v-if="permissions.job_sys_job_refresh_job" type="danger" @click="refreshJobs">重置全部任务
            </el-button>
          </el-tooltip>
        </template>
        <template
          slot="menu"
          slot-scope="scope">
          <el-button
            type="text"
            icon="el-icon-info"
            @click="handleJobLog(scope.row)">日志
          </el-button>
          <el-button
            v-if="permissions.job_sys_job_start_job"
            type="text"
            icon="el-icon-caret-right"
            @click="handleStartJob(scope.row)">启动
          </el-button>
 
          <el-button
            v-if="permissions.job_sys_job_shutdown_job"
            type="text"
            icon="el-icon-error"
            @click="handleShutDownJob(scope.row)">暂停
          </el-button>
          <el-button
            v-if="permissions.job_sys_job_edit"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row,scope.index)">修改
          </el-button>
          <el-button
            v-if="permissions.job_sys_job_run_job"
            type="text"
            icon="el-icon-s-promotion"
            @click="handleRunJob(scope.row)">执行
          </el-button>
          <el-button
            v-if="permissions.job_sys_job_del"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row,scope.index)">删除
          </el-button>
        </template>
      </avue-crud>
    </basic-container>
    <jobLog v-if="dialogFormVisible" ref="logRef"></jobLog>
  </div>
 
</template>
 
<script>
  import {
    addObj, delObj, fetchList, isValidTaskName, putObj, refreshJobsRa,
    runJobRa, shutDownJobRa, shutdownJobsRa, startJobRa, startJobsRa
  } from '@/api/daemon/sys-job'
  import {tableOption} from '@/const/crud/daemon/sys-job'
  import {remote} from '@/api/admin/dict'
  import {mapGetters} from 'vuex'
  import {cron} from 'vue-cron'
  import jobLog from './job-log'
 
  export default {
    name: 'jobIndex',
    components: {cron, jobLog},
    data() {
      return {
        form: {},
        cronPopover: false,
        queryParams: {}, // 全局检索条件
        tableData: [],
        dialogFormVisible: false,
        jobId: '',
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 10 // 每页显示多少条,
        },
        tableLoading: false,
        tableOption: tableOption,
        JobExecuteStatusDicCache: [],
        JobStatusDicCache: []
      }
    },
    computed: {
      ...mapGetters(['permissions'])
    },
    mounted: function () {
      this.getDicJobExecuteStatusCache('job_execute_status')// 获取定时任务运行时状态
      this.getDicJobStatusCache('job_status')// 获取定时任务状态
    },
    methods: {
      changeCron(val) {
        this.form.cronExpression = val
      },
      /**
       * 定时任务分页查询
       */
      getList() {
        this.tableLoading = true
        fetchList(Object.assign({
          descs: 'create_time',
          current: this.page.currentPage,
          size: this.page.pageSize
        }, this.queryParams)).then(response => {
          this.tableData = response.data.data.records
          this.page.pageSize = response.data.data.pageSize
          this.page.total = response.data.data.total
          this.tableLoading = false
        })
      },
      /**
       * 清除全局检索条件
       */
      handleSearchReset() {
        this.queryParams = {}
      },
      /**
       * 定时任务检索查询分页查询
       */
      handleFilter(params, done) {
        this.queryParams = params
        this.page.currentPage = 1
        this.getList(this.page)
        done()
      },
      /**
       * 启动定时任务
       */
      handleStartJob(row) {
        const jobStatus = row.jobStatus
        if (jobStatus === '1' || jobStatus === '3') {
          this.$confirm(
            '即将发布或启动(任务名称:' + row.jobName + '), 是否继续?',
            '提示',
            {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              closeOnClickModal:false,
              type: 'warning'
            }
          ).then(() => {
            startJobRa(row.jobId).then(response => {
              const code = response.data.code
              if (code === 0) {
                this.$notify({
                  title: '成功',
                  message: '启动成功',
                  type: 'success'
                })
                this.refreshChange()
              }
            }).catch(() => {
              this.$notify.error({
                title: '错误',
                message: '启动失败'
              })
            })
          })
        } else {
          this.$notify.error({
            title: '错误',
            message: '定时任务已运行'
          })
        }
      },
      /**
       * 执行定时任务
       */
      handleRunJob(row) {
        this.$confirm(
          '立刻执行一次任务(任务名称:' + row.jobName + '), 是否继续?',
          '提示',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            closeOnClickModal:false,
            type: 'warning'
          }
        ).then(() => {
          runJobRa(row.jobId).then(response => {
            const code = response.data.code
            if (code === 0) {
              this.$notify({
                title: '成功',
                message: '执行成功',
                type: 'success'
              })
              this.refreshChange()
            }
          }).catch(() => {
            this.$notify.error({
              title: '错误',
              message: '执行失败'
            })
          })
        })
 
      },
      handleAdd() {
        this.$refs.crud.rowAdd()
      },
      handleUpdate(row, index) {
        const jobStatus = row.jobStatus
        if (jobStatus === '1' || jobStatus === '3') {
          this.$refs.crud.rowEdit(row, index)
        } else {
          this.$notify.error({
            title: '错误',
            message: '运行中定时任务不可修改,请先暂停后操作'
          })
        }
      },
      /**
       * 暂停定时任务
       */
      handleShutDownJob(row) {
        const jobStatus = row.jobStatus
        if (jobStatus === '2') {
          this.$confirm(
            '即将暂停(任务名称:' + row.jobName + '), 是否继续?',
            '提示',
            {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              closeOnClickModal:false,
              type: 'warning'
            }
          ).then(() => {
            shutDownJobRa(row.jobId).then(response => {
              const code = response.data.code
              if (code === 0) {
                this.getList(this.page)
                this.$notify({
                  title: '成功',
                  message: '暂停成功',
                  type: 'success'
                })
              }
              this.refreshChange()
            }).catch(() => {
              this.$notify.error({
                title: '错误',
                message: '暂停失败'
              })
            })
          })
        } else {
          this.$notify.error({
            title: '错误',
            message: '已暂停,不要重复操作'
          })
        }
      },
      /**
       * 刷新回调
       */
      refreshChange() {
        this.getList(this.page)
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize
      },
      currentChange(current){
        this.page.currentPage = current
      },
      shutdownJobs() {
        this.$confirm(
          '即将暂停全部运行中定时任务, 是否继续?',
          '提示',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            closeOnClickModal:false,
            type: 'warning'
          }
        ).then(() => {
          shutdownJobsRa().then(response => {
            const code = response.data.code
            const msg = response.data.msg
            if (code === 0) {
              this.getList(this.page)
              this.$notify.success(msg)
            } else {
              this.$notify.error('暂停失败')
            }
          }).catch(() => {
            this.$notify.error('暂停失败')
          })
        })
      },
      /**
       * 启动全部暂停定时任务
       */
      startJobs() {
        this.$confirm(
          '即将启动全部暂定中定时任务, 是否继续?',
          '提示',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            closeOnClickModal:false,
            type: 'warning'
          }
        ).then(() => {
          startJobsRa().then(response => {
            const code = response.data.code
            if (code === 0) {
              this.getList(this.page)
              this.$notify.success('启动成功')
            } else {
              this.$notify.error('启动失败')
            }
          }).catch(() => {
            this.$notify.error('启动失败')
          })
        })
      },
      /**
       * 刷新定时任务
       */
      refreshJobs() {
        this.$confirm(
          '即将刷新全部定时任务, 是否继续?',
          '提示',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            closeOnClickModal:false,
            type: 'warning'
          }
        ).then(() => {
          refreshJobsRa().then(response => {
            const code = response.data.code
            if (code === 0) {
              this.getList(this.page)
              this.$notify.success('重置成功')
            } else {
              this.$notify.error('重置失败')
            }
          }).catch(() => {
              this.$notify.error('重置失败')
            }
          )
        })
      },
      /**
       * 新增定时任务持久化处理
       */
      save(row, done, loading) {
        isValidTaskName({
          'jobName': row.jobName,
          'jobGroup': row.jobGroup
        }).then(response => {
          const result = response.data.code
          if (result !== 0) {
            this.$notify.error('任务名称与任务组重复,请确认后重新添加')
          } else {
            addObj(row).then(() => {
              this.$notify.success('创建成功')
              this.getList(this.page)
            }).catch(() => {
              loading()
            })
          }
          done()
        })
      },
      /**
       * 更新定时任务持久化处理
       */
      update(row, index, done, loading) {
        putObj(row).then(() => {
          this.getList(this.page)
          done()
          this.$notify.success('修改成功')
        }).catch(() => {
          loading()
        })
      },
      /**
       * 删除定时任务持久化处理
       */
      handleDelete(row) {
        const jobStatus = row.jobStatus
        if (jobStatus === '1' || jobStatus === '3') {
          this.$confirm('是否确认删除(任务名称:' + row.jobName + '), 是否继续?删除后不可恢复', '警告', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            closeOnClickModal:false,
            type: 'warning'
          }).then(function () {
            return delObj(row.jobId)
          }).then(() => {
            this.getList(this.page)
            this.$notify.success('删除成功')
          }).catch(function () {
          })
        } else {
          this.$notify.error('运行中定时任务不可删除,请先暂停后操作')
        }
      },
      handleJobLog(row) {
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs.logRef.getJobLog(row)
        })
      },
      /**
       * 获取字典显示名称并缓存
       */
      getDicJobExecuteStatusCache(type) {
        remote(type).then(response => {
          const code = response.data.code
          if (code === 0) {
            const _data = response.data.data
            this.JobExecuteStatusDicCache = _data
          }
        })
      },
      /**
       * 获取字典显示名称并缓存
       */
      getDicJobStatusCache(type) {
        remote(type).then(response => {
          const code = response.data.code
          if (code === 0) {
            const _data = response.data.data
            this.JobStatusDicCache = _data
          }
        })
      },
      /**
       * 获取字典定时任务执行状态字典值显示名称
       */
      getDicNameJobExecuteStatus(value) {
        let re = ''
        this.JobExecuteStatusDicCache.forEach(obj => {
          if (obj.value === value) {
            re = obj.label
            return
          }
        })
        return re
      },
      /**
       * 获取字典定时任务状态字典值显示名称
       */
      getDicNameJobStatus(value) {
        let re = ''
        this.JobStatusDicCache.forEach(obj => {
          if (obj.value === value) {
            re = obj.label
            return
          }
        })
        return re
      }
    }
  }
</script>
 
<style lang="scss" scoped>
</style>