index.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <template>
  2. <el-card shadow="nover" class="page-full">
  3. <div class="search">
  4. <el-form :inline="true">
  5. <el-form-item>
  6. <el-button type="primary" v-auth="'add'" @click="addOrEdit()">
  7. <el-icon>
  8. <ele-FolderAdd />
  9. </el-icon>
  10. 新增规则编排
  11. </el-button>
  12. </el-form-item>
  13. </el-form>
  14. </div>
  15. <el-table :data="tableData" style="width: 100%" v-loading="loading" max-height="calc(100vh - 255px)">
  16. <el-table-column type="index" label="序号" width="80" align="center" />
  17. <el-table-column prop="name" label="名称" show-overflow-tooltip></el-table-column>
  18. <el-table-column prop="expound" label="说明" show-overflow-tooltip></el-table-column>
  19. <el-table-column prop="createdAt" label="创建时间" min-width="100" align="center"></el-table-column>
  20. <el-table-column prop="status" label="状态" width="100" align="center">
  21. <template #default="scope">
  22. <el-tag type="success" size="small" v-if="scope.row.status == 1">已启动</el-tag>
  23. <el-tag type="info" size="small" v-else>已停止</el-tag>
  24. </template>
  25. </el-table-column>
  26. <el-table-column label="操作" width="200" align="center">
  27. <template #default="scope">
  28. <el-button size="small" text type="info" v-auth="'startOrStop'" v-if="scope.row.status" @click="setStatus(scope.row, 0)">停止</el-button>
  29. <el-button size="small" text type="primary" v-auth="'startOrStop'" v-else @click="setStatus(scope.row, 1)">启动</el-button>
  30. <el-button size="small" text type="warning" v-auth="'edit'" @click="addOrEdit(scope.row)">编辑</el-button>
  31. <el-button size="small" text type="warning" @click="edit(scope.row)">规则编辑</el-button>
  32. <el-button size="small" text type="info" v-auth="'del'" @click="onDel(scope.row)">删除</el-button>
  33. </template>
  34. </el-table-column>
  35. </el-table>
  36. <pagination v-if="params.total" :total="params.total" v-model:page="params.pageNum" v-model:limit="params.pageSize" @pagination="getList()" />
  37. <EditForm ref="editFormRef" @getList="getList(1)" :types="0"></EditForm>
  38. </el-card>
  39. </template>
  40. <script lang="ts" setup>
  41. import { ref } from 'vue';
  42. import api from '/@/api/rule';
  43. import { ElMessageBox, ElMessage } from 'element-plus';
  44. import { useSearch } from '/@/hooks/useCommon';
  45. import EditForm from './edit.vue';
  46. import axios from 'axios';
  47. const editFormRef = ref();
  48. const { params, tableData, getList, loading } = useSearch<any[]>(api.getList, 'Data', { types: 0 });
  49. const headers = {
  50. Authorization: 'Bearer ' + localStorage.token,
  51. };
  52. const flowsUrl = window.location.origin + '/rule-engine/flows';
  53. // const flowsUrl = 'http://zhgy.sagoo.cn/rule-engine/flows';
  54. getList();
  55. const addOrEdit = async (row?: any) => {
  56. if (row) {
  57. editFormRef.value.open(row);
  58. return;
  59. } else {
  60. editFormRef.value.open();
  61. }
  62. };
  63. const setStatus = async (row: any, status: number) => {
  64. // 找到所有规则
  65. const { data: flows } = await axios.get(flowsUrl, { headers });
  66. const flow = flows.find((item: any) => item.id === row.flowId);
  67. if (!flow) {
  68. ElMessage.error('规则不存在');
  69. return;
  70. }
  71. // 改变指定规则状态
  72. flow.disabled = status ? false : true;
  73. // 设置规则状态
  74. await axios.post(flowsUrl, flows, { headers });
  75. api
  76. .setStatus(row.id, status)
  77. .then(() => {
  78. ElMessage.success('操作成功');
  79. getList();
  80. })
  81. .catch(() => {
  82. ElMessage.error('操作失败');
  83. });
  84. };
  85. const edit = async (row: any) => {
  86. localStorage.setItem('auth-tokens', `{"access_token":"${localStorage.token}"}`);
  87. const url = '/rule-engine/#flow/' + row.flowId;
  88. window.open(url);
  89. };
  90. const onDel = (row: any) => {
  91. ElMessageBox.confirm(`此操作将删除:“${row.name}”,是否继续?`, '提示', {
  92. confirmButtonText: '确认',
  93. cancelButtonText: '取消',
  94. type: 'warning',
  95. }).then(async () => {
  96. // 找到所有规则
  97. const { data: flows } = await axios.get(flowsUrl, { headers });
  98. const flowIndex = flows.findIndex((item: any) => item.id === row.flowId);
  99. if (flowIndex >= 0) {
  100. // 删除指定规则
  101. flows.splice(flowIndex, 1);
  102. // 删除当前规则下的各个节点信息
  103. const newFlows = flows.filter((item: any) => {
  104. if (item.z === row.flowId) {
  105. return false;
  106. } else {
  107. return true;
  108. }
  109. });
  110. // 设置规则状态
  111. await axios.post(flowsUrl, newFlows, { headers });
  112. }
  113. await api.del([row.id as string]);
  114. ElMessage.success('删除成功');
  115. getList();
  116. });
  117. };
  118. </script>