send.vue 5.4 KB

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