index.vue 15.3 KB
Newer Older
sunhongwei's avatar
sunhongwei committed
1 2 3
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
Marcus's avatar
Marcus committed
4 5
      <el-form-item :label="$t('任务名称')" prop="name">
        <el-input v-model="queryParams.name" :placeholder="$t('请输入任务名称')" clearable @keyup.enter.native="handleQuery"/>
sunhongwei's avatar
sunhongwei committed
6
      </el-form-item>
Marcus's avatar
Marcus committed
7 8
      <el-form-item :label="$t('任务状态')" prop="status">
        <el-select v-model="queryParams.status" :placeholder="$t('请选择任务状态')" clearable>
sunhongwei's avatar
sunhongwei committed
9 10 11 12
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.INFRA_JOB_STATUS)"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
Marcus's avatar
Marcus committed
13 14
      <el-form-item :label="$t('处理器的名字')" prop="handlerName">
        <el-input v-model="queryParams.handlerName" :placeholder="$t('请输入处理器的名字')" clearable @keyup.enter.native="handleQuery"/>
sunhongwei's avatar
sunhongwei committed
15 16
      </el-form-item>
      <el-form-item>
Marcus's avatar
Marcus committed
17 18
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">{{ $t('重置') }}</el-button>
sunhongwei's avatar
sunhongwei committed
19 20 21 22 23 24
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
Marcus's avatar
Marcus committed
25
                   v-hasPermi="['infra:job:create']">{{ $t('新增') }}</el-button>
sunhongwei's avatar
sunhongwei committed
26 27 28
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading"
Marcus's avatar
Marcus committed
29
                   v-hasPermi="['infra:job:export']">{{ $t('导出') }}</el-button>
sunhongwei's avatar
sunhongwei committed
30 31 32
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" icon="el-icon-s-operation" size="mini" @click="handleJobLog"
Marcus's avatar
Marcus committed
33
                   v-hasPermi="['infra:job:query']">{{ $t('执行日志') }}</el-button>
sunhongwei's avatar
sunhongwei committed
34 35 36 37 38
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="jobList">
Marcus's avatar
Marcus committed
39 40 41
      <el-table-column :label="$t('任务编号')" align="center" prop="id" />
      <el-table-column :label="$t('任务名称')" align="center" prop="name" />
      <el-table-column :label="$t('任务状态')" align="center" prop="status">
sunhongwei's avatar
sunhongwei committed
42 43 44 45
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.INFRA_JOB_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>>
Marcus's avatar
Marcus committed
46 47
      <el-table-column :label="$t('处理器的名字')" align="center" prop="handlerName" />
      <el-table-column :label="$t('处理器的参数')" align="center" prop="handlerParam" />
sunhongwei's avatar
sunhongwei committed
48
      <el-table-column label="CRON 表达式" align="center" prop="cronExpression" />
Marcus's avatar
Marcus committed
49
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width">
sunhongwei's avatar
sunhongwei committed
50 51
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
Marcus's avatar
Marcus committed
52
                     v-hasPermi="['infra:job:update']">{{ $t('修改') }}</el-button>
sunhongwei's avatar
sunhongwei committed
53
          <el-button size="mini" type="text" icon="el-icon-check" @click="handleChangeStatus(scope.row, true)"
Marcus's avatar
Marcus committed
54
                     v-if="scope.row.status === InfJobStatusEnum.STOP" v-hasPermi="['infra:job:update']">{{ $t('开启') }}</el-button>
sunhongwei's avatar
sunhongwei committed
55
          <el-button size="mini" type="text" icon="el-icon-close" @click="handleChangeStatus(scope.row, false)"
Marcus's avatar
Marcus committed
56
                     v-if="scope.row.status === InfJobStatusEnum.NORMAL" v-hasPermi="['infra:job:update']">{{ $t('暂停') }}</el-button>
sunhongwei's avatar
sunhongwei committed
57
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
Marcus's avatar
Marcus committed
58
                     v-hasPermi="['infra:job:delete']">{{ $t('删除') }}</el-button>
sunhongwei's avatar
sunhongwei committed
59 60 61
          <el-dropdown size="mini" @command="(command) => handleCommand(command, scope.row)"
                       v-hasPermi="['infra:job:trigger', 'infra:job:query']">
            <span class="el-dropdown-link">
Marcus's avatar
Marcus committed
62
              <i class="el-icon-d-arrow-right el-icon--right"></i>{{ $t('更多') }}</span>
sunhongwei's avatar
sunhongwei committed
63 64
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="handleRun" icon="el-icon-caret-right"
Marcus's avatar
Marcus committed
65
                                v-hasPermi="['infra:job:trigger']">{{ $t('执行一次') }}</el-dropdown-item>
sunhongwei's avatar
sunhongwei committed
66
              <el-dropdown-item command="handleView" icon="el-icon-view"
Marcus's avatar
Marcus committed
67
                                v-hasPermi="['infra:job:query']">{{ $t('任务详细') }}</el-dropdown-item>
sunhongwei's avatar
sunhongwei committed
68
              <el-dropdown-item command="handleJobLog" icon="el-icon-s-operation"
Marcus's avatar
Marcus committed
69
                                v-hasPermi="['infra:job:query']">{{ $t('调度日志') }}</el-dropdown-item>
sunhongwei's avatar
sunhongwei committed
70 71 72 73 74 75 76 77 78 79 80 81
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 添加或修改定时任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
Marcus's avatar
Marcus committed
82 83
        <el-form-item :label="$t('任务名称')" prop="name">
          <el-input v-model="form.name" :placeholder="$t('请输入任务名称')" />
sunhongwei's avatar
sunhongwei committed
84
        </el-form-item>
Marcus's avatar
Marcus committed
85 86
        <el-form-item :label="$t('处理器的名字')" prop="handlerName">
          <el-input v-model="form.handlerName" :placeholder="$t('请输入处理器的名字')" v-bind:readonly="form.id !== undefined" />
sunhongwei's avatar
sunhongwei committed
87
        </el-form-item>
Marcus's avatar
Marcus committed
88 89
        <el-form-item :label="$t('处理器的参数')" prop="handlerParam">
          <el-input v-model="form.handlerParam" :placeholder="$t('请输入处理器的参数')" />
sunhongwei's avatar
sunhongwei committed
90 91 92 93
        </el-form-item>
        <el-form-item label="CRON 表达式" prop="cronExpression">
          <el-input v-model="form.cronExpression" placeholder="请输入CRON 表达式">
            <template slot="append">
Marcus's avatar
Marcus committed
94
              <el-button type="primary" @click="handleShowCron">{{ $t('生成表达式') }}<i class="el-icon-time el-icon--right"></i>
sunhongwei's avatar
sunhongwei committed
95 96 97 98
              </el-button>
            </template>
          </el-input>
        </el-form-item>
Marcus's avatar
Marcus committed
99
        <el-form-item :label="$t('重试次数')" prop="retryCount">
sunhongwei's avatar
sunhongwei committed
100 101
          <el-input v-model="form.retryCount" placeholder="请输入重试次数。设置为 0 时,不进行重试" />
        </el-form-item>
Marcus's avatar
Marcus committed
102
        <el-form-item :label="$t('重试间隔')" prop="retryInterval">
sunhongwei's avatar
sunhongwei committed
103 104
          <el-input v-model="form.retryInterval" placeholder="请输入重试间隔,单位:毫秒。设置为 0 时,无需间隔" />
        </el-form-item>
Marcus's avatar
Marcus committed
105 106
        <el-form-item :label="$t('监控超时时间')" prop="monitorTimeout">
          <el-input v-model="form.monitorTimeout" :placeholder="$t('请输入监控超时时间,单位:毫秒')" />
sunhongwei's avatar
sunhongwei committed
107 108 109
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
Marcus's avatar
Marcus committed
110 111
        <el-button type="primary" @click="submitForm">{{ $t('确 定') }}</el-button>
        <el-button @click="cancel">{{ $t('取 消') }}</el-button>
sunhongwei's avatar
sunhongwei committed
112 113 114 115 116 117 118 119
      </div>
    </el-dialog>

    <el-dialog title="Cron表达式生成器" :visible.sync="openCron" append-to-body class="scrollbar" destroy-on-close>
      <crontab @hide="openCron=false" @fill="crontabFill" :expression="expression"></crontab>
    </el-dialog>

    <!-- 任务详细 -->
Marcus's avatar
Marcus committed
120
    <el-dialog :title="$t('任务详细')" :visible.sync="openView" width="700px" append-to-body>
sunhongwei's avatar
sunhongwei committed
121 122 123
      <el-form ref="form" :model="form" label-width="200px" size="mini">
        <el-row>
          <el-col :span="24">
Marcus's avatar
Marcus committed
124 125 126
            <el-form-item :label="$t('任务编号:')">{{ form.id }}</el-form-item>
            <el-form-item :label="$t('任务名称:')">{{ form.name }}</el-form-item>
            <el-form-item :label="$t('任务名称:')">
sunhongwei's avatar
sunhongwei committed
127 128
              <dict-tag :type="DICT_TYPE.INFRA_JOB_STATUS" :value="form.status" />
            </el-form-item>
Marcus's avatar
Marcus committed
129 130
            <el-form-item :label="$t('处理器的名字:')">{{ form.handlerName }}</el-form-item>
            <el-form-item :label="$t('处理器的参数:')">{{ form.handlerParam }}</el-form-item>
sunhongwei's avatar
sunhongwei committed
131
            <el-form-item label="cron表达式:">{{ form.cronExpression }}</el-form-item>
Marcus's avatar
Marcus committed
132 133 134 135
            <el-form-item :label="$t('重试次数:')">{{ form.retryCount }}</el-form-item>
            <el-form-item :label="$t('重试间隔:')">{{ form.retryInterval + " 毫秒" }}</el-form-item>
            <el-form-item :label="$t('监控超时时间:')">{{ form.monitorTimeout > 0 ? form.monitorTimeout + " 毫秒" : "未开启" }}</el-form-item>
            <el-form-item :label="$t('后续执行时间:')">{{ Array.from(nextTimes, x => parseTime(x)).join('; ')}}</el-form-item>
sunhongwei's avatar
sunhongwei committed
136 137 138 139
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
Marcus's avatar
Marcus committed
140
        <el-button @click="openView = false">{{ $t('关 闭') }}</el-button>
sunhongwei's avatar
sunhongwei committed
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
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listJob, getJob, delJob, addJob, updateJob, exportJob, runJob, updateJobStatus, getJobNextTimes } from "@/api/infra/job";
import { InfraJobStatusEnum } from "@/utils/constants";
import Crontab from '@/components/Crontab'

export default {
  components: { Crontab },
  name: "Job",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 定时任务表格数据
      jobList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示详细弹出层
      openView: false,
      // 是否显示Cron表达式弹出层
      openCron: false,
      // 传入的表达式
      expression: "",
      // 状态字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        name: undefined,
        status: undefined,
        handlerName: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
Marcus's avatar
Marcus committed
190 191
        name: [{ required: true, message: this.$t("任务名称不能为空"), trigger: "blur" }],
        handlerName: [{ required: true, message: this.$t("处理器的名字不能为空"), trigger: "blur" }],
sunhongwei's avatar
sunhongwei committed
192
        cronExpression: [{ required: true, message: "CRON 表达式不能为空", trigger: "blur" }],
Marcus's avatar
Marcus committed
193 194
        retryCount: [{ required: true, message: this.$t("重试次数不能为空"), trigger: "blur" }],
        retryInterval: [{ required: true, message: this.$t("重试间隔不能为空"), trigger: "blur" }],
sunhongwei's avatar
sunhongwei committed
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
      },
      nextTimes: [], // 后续执行时间

      // 枚举
      InfJobStatusEnum: InfraJobStatusEnum
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询定时任务列表 */
    getList() {
      this.loading = true;
      listJob(this.queryParams).then(response => {
        this.jobList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        handlerName: undefined,
        handlerParam: undefined,
        cronExpression: undefined,
        retryCount: undefined,
        retryInterval: undefined,
        monitorTimeout: undefined,
      };
      this.nextTimes = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 立即执行一次 **/
    handleRun(row) {
      this.$modal.confirm('确认要立即执行一次"' + row.name + '"任务吗?').then(function() {
          return runJob(row.id);
        }).then(() => {
          this.$modal.msgSuccess("执行成功");
      }).catch(() => {});
    },
    /** 任务详细信息 */
    handleView(row) {
      getJob(row.id).then(response => {
        this.form = response.data;
        this.openView = true;
      });
      // 获取下一次执行时间
      getJobNextTimes(row.id).then(response => {
        this.nextTimes = response.data;
      });
    },
    /** cron表达式按钮操作 */
    handleShowCron() {
      this.expression = this.form.cronExpression;
      this.openCron = true;
    },
    /** 确定后回传值 */
    crontabFill(value) {
      this.form.cronExpression = value;
    },
    /** 任务日志列表查询 */
    handleJobLog(row) {
      if (row.id) {
        this.$router.push({
          path:"/job/log",
          query:{
            jobId: row.id
          }
        });
      } else {
        this.$router.push("/job/log");
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
Marcus's avatar
Marcus committed
290
      this.title = this.$t("添加任务");
sunhongwei's avatar
sunhongwei committed
291 292 293 294 295 296 297 298
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getJob(id).then(response => {
        this.form = response.data;
        this.open = true;
Marcus's avatar
Marcus committed
299
        this.title = this.$t("修改任务");
sunhongwei's avatar
sunhongwei committed
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
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            updateJob(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addJob(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id;
      this.$modal.confirm('是否确认删除定时任务编号为"' + ids + '"的数据项?').then(function() {
          return delJob(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 更新状态操作 */
    handleChangeStatus(row, open) {
      const id = row.id;
      let status = open ? InfraJobStatusEnum.NORMAL : InfraJobStatusEnum.STOP;
Marcus's avatar
Marcus committed
336
      let statusStr = open ? this.$t('开启') : this.$t('关闭');
sunhongwei's avatar
sunhongwei committed
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
      this.$modal.confirm('是否确认' + statusStr + '定时任务编号为"' + id + '"的数据项?').then(function() {
        return updateJobStatus(id, status);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(statusStr + "成功");
      }).catch(() => {});
    },
    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case "handleRun":
          this.handleRun(row);
          break;
        case "handleView":
          this.handleView(row);
          break;
        case "handleJobLog":
          this.handleJobLog(row);
          break;
        default:
          break;
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm("是否确认导出所有定时任务数据项?").then(() => {
          this.exportLoading = true;
          return exportJob(queryParams);
        }).then(response => {
          this.$download.excel(response, '定时任务.xls');
          this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>