index.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <template>
  2. <div class="page">
  3. <el-card shadow="nover">
  4. <el-form :model="tableData.param" ref="queryRef" inline>
  5. <el-form-item label="关键字" prop="jobName">
  6. <el-input v-model="tableData.param.jobName" placeholder="输入名称或描述关键字" clearable style="width: 180px;" @keyup.enter="dataList" />
  7. </el-form-item>
  8. <el-form-item label="任务组名" prop="jobGroup">
  9. <el-select v-model="tableData.param.jobGroup" placeholder="请选择" style="width: 100px;">
  10. <el-option v-for="dict in sys_job_group" :key="dict.value" :label="dict.label" :value="dict.value"> </el-option>
  11. </el-select>
  12. </el-form-item>
  13. <el-form-item label="任务状态" prop="status">
  14. <el-select v-model="tableData.param.status" style="width: 100px" placeholder="请选择">
  15. <el-option label="启用" :value="0" />
  16. <el-option label="禁用" :value="1" />
  17. </el-select>
  18. </el-form-item>
  19. <el-form-item>
  20. <el-button type="primary" class="ml10" @click="dataList">
  21. <el-icon>
  22. <ele-Search />
  23. </el-icon>
  24. 查询
  25. </el-button>
  26. <!-- <el-button @click="resetQuery(queryRef)">
  27. <el-icon>
  28. <ele-Refresh />
  29. </el-icon>
  30. 重置
  31. </el-button> -->
  32. <el-button type="primary" class="ml10" @click="onOpenAddDic" v-auth="'add'">
  33. <el-icon>
  34. <ele-FolderAdd />
  35. </el-icon>
  36. 新增任务
  37. </el-button>
  38. <el-button type="info" class="ml10" @click="onRowDel(null)" v-auth="'del'">
  39. <el-icon>
  40. <ele-Delete />
  41. </el-icon>
  42. 删除任务
  43. </el-button>
  44. </el-form-item>
  45. </el-form>
  46. <el-table :data="tableData.data" style="width: 100%" @selection-change="handleSelectionChange" v-loading="tableData.loading">
  47. <el-table-column type="selection" width="55" align="center" />
  48. <el-table-column label="ID" align="center" v-col="'jobId'" prop="jobId" width="100" />
  49. <el-table-column label="任务名称" v-col="'jobName'" prop="jobName" show-overflow-tooltip />
  50. <el-table-column label="任务描述" v-col="'remark'" prop="remark" show-overflow-tooltip />
  51. <el-table-column label="任务分组" v-col="'jobGroup'" prop="jobGroup" width="120" :formatter="jobGroupFormat" />
  52. <el-table-column label="任务方法名" v-col="'invokeTarget'" prop="invokeTarget" />
  53. <el-table-column label="cron执行表达式" v-col="'cronExpression'" prop="cronExpression" />
  54. <el-table-column label="状态" v-col="'status'" align="center" prop="status" width="100">
  55. <template #default="scope">
  56. <!-- {{ row.status ? '正常' : '暂停' }} -->
  57. <el-switch v-model="scope.row.status" inline-prompt :active-value="0" v-auth="'status'" :inactive-value="1" active-text="启" inactive-text="禁" @change="handleStatusChange(scope.row)">
  58. </el-switch>
  59. <span v-noauth="'status'">{{ scope.row.status ? '正常' : '暂停' }}</span>
  60. </template>
  61. </el-table-column>
  62. <el-table-column label="操作" width="180" align="center" fixed="right" v-col="'handle'">
  63. <template #default="scope">
  64. <el-button size="small" text type="warning" @click="onOpenEditDic(scope.row)" v-auth="'edit'">修改</el-button>
  65. <el-button size="small" text type="info" @click="onRowDel(scope.row)" v-auth="'del'">删除</el-button>
  66. <el-button size="small" text type="primary" @click="onRowRun(scope.row)" v-auth="'do'">执行一次</el-button>
  67. </template>
  68. </el-table-column>
  69. </el-table>
  70. <pagination v-show="tableData.total > 0" :total="tableData.total" v-model:page="tableData.param.pageNum" v-model:limit="tableData.param.pageSize" @pagination="dataList" />
  71. </el-card>
  72. <EditConfig ref="editDicRef" @dataList="dataList" />
  73. </div>
  74. </template>
  75. <script lang="ts">
  76. import { toRefs, reactive, onMounted, ref, defineComponent, unref, getCurrentInstance } from 'vue';
  77. import { ElMessageBox, ElMessage, FormInstance } from 'element-plus';
  78. import EditConfig from '/@/views/system/task/component/editConfig.vue';
  79. import api from '/@/api/system';
  80. // 定义接口来定义对象的类型
  81. interface TableDataRow {
  82. jobName: string;
  83. jobParams: string;
  84. jobGroup: string;
  85. invokeTarget: string;
  86. cronExpression: string;
  87. misfirePolicy: number;
  88. status: number;
  89. jobId: number;
  90. }
  91. interface TableDataState {
  92. ids: number[] | undefined;
  93. tableData: {
  94. data: Array<TableDataRow>;
  95. total: number;
  96. loading: boolean;
  97. param: {
  98. pageNum: number;
  99. pageSize: number;
  100. jobName: string;
  101. jobGroup: string;
  102. status: number | null;
  103. };
  104. };
  105. }
  106. export default defineComponent({
  107. name: 'apiV1SystemDictDataList',
  108. components: { EditConfig },
  109. setup() {
  110. const { proxy } = getCurrentInstance() as any;
  111. const addDicRef = ref();
  112. const editDicRef = ref();
  113. const queryRef = ref();
  114. const { sys_yes_no, sys_job_group } = proxy.useDict('sys_yes_no', 'sys_job_group');
  115. const state = reactive<TableDataState>({
  116. ids: [],
  117. tableData: {
  118. data: [],
  119. total: 0,
  120. loading: false,
  121. param: {
  122. pageNum: 1,
  123. pageSize: 20,
  124. jobName: '',
  125. jobGroup: '',
  126. status: null,
  127. },
  128. },
  129. });
  130. // 初始化表格数据
  131. const initTableData = () => {
  132. dataList();
  133. };
  134. const dataList = () => {
  135. state.tableData.loading = true;
  136. api.task
  137. .getList(state.tableData.param)
  138. .then((res: any) => {
  139. state.tableData.data = res.list;
  140. state.tableData.total = res.total;
  141. })
  142. .finally(() => (state.tableData.loading = false));
  143. };
  144. // 打开新增任务弹窗
  145. const onOpenAddDic = () => {
  146. editDicRef.value.openDialog();
  147. };
  148. // 打开修改任务弹窗
  149. const onOpenEditDic = (row: TableDataRow) => {
  150. editDicRef.value.openDialog(row);
  151. };
  152. // 删除任务
  153. const onRowDel = (row: TableDataRow) => {
  154. let msg = '你确定要删除所选数据?';
  155. let ids: any = [];
  156. if (row) {
  157. msg = `此操作将永久删除任务名称:“${row.jobName}”,是否继续?`;
  158. ids = [row.jobId];
  159. } else {
  160. ids = state.ids;
  161. }
  162. if (ids.length === 0) {
  163. ElMessage.error('请选择要删除的数据。');
  164. return;
  165. }
  166. ElMessageBox.confirm(msg, '提示', {
  167. confirmButtonText: '确认',
  168. cancelButtonText: '取消',
  169. type: 'warning',
  170. })
  171. .then(() => {
  172. api.task.del(ids).then(() => {
  173. ElMessage.success('删除成功');
  174. dataList();
  175. });
  176. })
  177. .catch(() => { });
  178. };
  179. const onRowRun = (row: TableDataRow) => {
  180. ElMessageBox.confirm(`是否确认立即执行一次该任务?`, '警告', {
  181. confirmButtonText: '确定',
  182. cancelButtonText: '取消',
  183. type: 'warning',
  184. }).then(() => {
  185. api.task.run(row.jobId).then(() => {
  186. ElMessage.success('操作成功');
  187. dataList();
  188. });
  189. });
  190. };
  191. const handleStatusChange = (row: TableDataRow) => {
  192. let text = row.status === 0 ? '启用' : '停用';
  193. ElMessageBox.confirm('确认要"' + text + '":"' + row.jobName + '"任务吗?', '警告', {
  194. confirmButtonText: '确定',
  195. cancelButtonText: '取消',
  196. type: 'warning',
  197. })
  198. .then(function () {
  199. if (row.status === 0) {
  200. return api.task.start(row.jobId);
  201. } else {
  202. return api.task.stop(row.jobId);
  203. }
  204. })
  205. .then(() => {
  206. ElMessage.success(text + '成功');
  207. })
  208. .catch(function () {
  209. row.status = row.status === 0 ? 1 : 0;
  210. });
  211. };
  212. // 页面加载时
  213. onMounted(() => {
  214. initTableData();
  215. });
  216. /** 重置按钮操作 */
  217. const resetQuery = (formEl: FormInstance | undefined) => {
  218. if (!formEl) return;
  219. formEl.resetFields();
  220. dataList();
  221. };
  222. // 多选框选中数据
  223. const handleSelectionChange = (selection: TableDataRow[]) => {
  224. state.ids = selection.map((item) => item.jobId);
  225. };
  226. const jobGroupFormat = (row: TableDataRow) => {
  227. return proxy.selectDictLabel(unref(sys_job_group), row.jobGroup);
  228. };
  229. return {
  230. addDicRef,
  231. editDicRef,
  232. queryRef,
  233. sys_yes_no,
  234. sys_job_group,
  235. onOpenAddDic,
  236. onOpenEditDic,
  237. onRowDel,
  238. onRowRun,
  239. handleStatusChange,
  240. dataList,
  241. resetQuery,
  242. handleSelectionChange,
  243. jobGroupFormat,
  244. ...toRefs(state),
  245. };
  246. },
  247. });
  248. </script>