index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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"
  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. showSearch: true,
  134. // 总条数
  135. total: 0,
  136. // 定时任务表格数据
  137. jobList: [],
  138. // 弹出层标题
  139. title: "",
  140. // 是否显示弹出层
  141. open: false,
  142. // 是否显示详细弹出层
  143. openView: false,
  144. // 状态字典
  145. statusOptions: [],
  146. // 查询参数
  147. queryParams: {
  148. pageNo: 1,
  149. pageSize: 10,
  150. name: undefined,
  151. status: undefined,
  152. handlerName: undefined
  153. },
  154. // 表单参数
  155. form: {},
  156. // 表单校验
  157. rules: {
  158. name: [{ required: true, message: "任务名称不能为空", trigger: "blur" }],
  159. handlerName: [{ required: true, message: "处理器的名字不能为空", trigger: "blur" }],
  160. cronExpression: [{ required: true, message: "CRON 表达式不能为空", trigger: "blur" }],
  161. retryCount: [{ required: true, message: "重试次数不能为空", trigger: "blur" }],
  162. retryInterval: [{ required: true, message: "重试间隔不能为空", trigger: "blur" }],
  163. },
  164. nextTimes: [], // 后续执行时间
  165. // 枚举
  166. InfJobStatusEnum: InfraJobStatusEnum
  167. };
  168. },
  169. created() {
  170. this.getList();
  171. },
  172. methods: {
  173. /** 查询定时任务列表 */
  174. getList() {
  175. this.loading = true;
  176. listJob(this.queryParams).then(response => {
  177. this.jobList = response.data.list;
  178. this.total = response.data.total;
  179. this.loading = false;
  180. });
  181. },
  182. /** 取消按钮 */
  183. cancel() {
  184. this.open = false;
  185. this.reset();
  186. },
  187. /** 表单重置 */
  188. reset() {
  189. this.form = {
  190. id: undefined,
  191. name: undefined,
  192. handlerName: undefined,
  193. handlerParam: undefined,
  194. cronExpression: undefined,
  195. retryCount: undefined,
  196. retryInterval: undefined,
  197. monitorTimeout: undefined,
  198. };
  199. this.nextTimes = [];
  200. this.resetForm("form");
  201. },
  202. /** 搜索按钮操作 */
  203. handleQuery() {
  204. this.queryParams.pageNo = 1;
  205. this.getList();
  206. },
  207. /** 重置按钮操作 */
  208. resetQuery() {
  209. this.resetForm("queryForm");
  210. this.handleQuery();
  211. },
  212. /** 立即执行一次 **/
  213. handleRun(row) {
  214. this.$confirm('确认要立即执行一次"' + row.name + '"任务吗?', "警告", {
  215. confirmButtonText: "确定",
  216. cancelButtonText: "取消",
  217. type: "warning"
  218. }).then(function() {
  219. return runJob(row.id);
  220. }).then(() => {
  221. this.msgSuccess("执行成功");
  222. })
  223. },
  224. /** 任务详细信息 */
  225. handleView(row) {
  226. getJob(row.id).then(response => {
  227. this.form = response.data;
  228. this.openView = true;
  229. });
  230. // 获取下一次执行时间
  231. getJobNextTimes(row.id).then(response => {
  232. this.nextTimes = response.data;
  233. });
  234. },
  235. /** 任务日志列表查询 */
  236. handleJobLog(row) {
  237. if (row.id) {
  238. this.$router.push({
  239. path:"/job/log",
  240. query:{
  241. jobId: row.id
  242. }
  243. });
  244. } else {
  245. this.$router.push("/job/log");
  246. }
  247. },
  248. /** 新增按钮操作 */
  249. handleAdd() {
  250. this.reset();
  251. this.open = true;
  252. this.title = "添加任务";
  253. },
  254. /** 修改按钮操作 */
  255. handleUpdate(row) {
  256. this.reset();
  257. const id = row.id;
  258. getJob(id).then(response => {
  259. this.form = response.data;
  260. this.open = true;
  261. this.title = "修改任务";
  262. });
  263. },
  264. /** 提交按钮 */
  265. submitForm: function() {
  266. this.$refs["form"].validate(valid => {
  267. if (valid) {
  268. if (this.form.id !== undefined) {
  269. updateJob(this.form).then(response => {
  270. this.msgSuccess("修改成功");
  271. this.open = false;
  272. this.getList();
  273. });
  274. } else {
  275. addJob(this.form).then(response => {
  276. this.msgSuccess("新增成功");
  277. this.open = false;
  278. this.getList();
  279. });
  280. }
  281. }
  282. });
  283. },
  284. /** 删除按钮操作 */
  285. handleDelete(row) {
  286. const ids = row.id;
  287. this.$confirm('是否确认删除定时任务编号为"' + ids + '"的数据项?', "警告", {
  288. confirmButtonText: "确定",
  289. cancelButtonText: "取消",
  290. type: "warning"
  291. }).then(function() {
  292. return delJob(ids);
  293. }).then(() => {
  294. this.getList();
  295. this.msgSuccess("删除成功");
  296. })
  297. },
  298. /** 更新状态操作 */
  299. handleChangeStatus(row, open) {
  300. const id = row.id;
  301. let status = open ? InfraJobStatusEnum.NORMAL : InfraJobStatusEnum.STOP;
  302. let statusStr = open ? '开启' : '关闭';
  303. this.$confirm('是否确认' + statusStr + '定时任务编号为"' + id + '"的数据项?', "警告", {
  304. confirmButtonText: "确定",
  305. cancelButtonText: "取消",
  306. type: "warning"
  307. }).then(function() {
  308. return updateJobStatus(id, status);
  309. }).then(() => {
  310. this.getList();
  311. this.msgSuccess(statusStr + "成功");
  312. })
  313. },
  314. /** 导出按钮操作 */
  315. handleExport() {
  316. const queryParams = this.queryParams;
  317. this.$confirm("是否确认导出所有定时任务数据项?", "警告", {
  318. confirmButtonText: "确定",
  319. cancelButtonText: "取消",
  320. type: "warning"
  321. }).then(function() {
  322. return exportJob(queryParams);
  323. }).then(response => {
  324. this.downloadExcel(response, '定时任务.xls');
  325. })
  326. }
  327. }
  328. };
  329. </script>