Browse Source

Merge branch 'master' of http://git.mydig.net/Sagoo-Cloud/sagoo-admin-ui

vera_min 3 years ago
parent
commit
971dc21758
3 changed files with 135 additions and 19 deletions
  1. 26 6
      src/views/rule-engine/edit.vue
  2. 55 6
      src/views/rule-engine/index.vue
  3. 54 7
      src/views/rule-engine/send.vue

+ 26 - 6
src/views/rule-engine/edit.vue

@@ -29,7 +29,7 @@ import { ref, reactive, nextTick } from 'vue';
 import api from '/@/api/rule';
 import axios from 'axios';
 import { ruleRequired } from '/@/utils/validator';
-import {  ElMessage } from 'element-plus';
+import { ElMessage } from 'element-plus';
 
 const emit = defineEmits(['getList']);
 
@@ -40,6 +40,13 @@ const props = defineProps({
 	},
 });
 
+const headers = {
+	Authorization: 'Bearer ' + JSON.parse(sessionStorage.token),
+};
+const flowsUrl = window.location.origin + '/rule-engine/flow';
+// const flowsUrl = 'http://zhgy.sagoo.cn/rule-engine/flow';
+
+
 const showDialog = ref(false);
 const formRef = ref();
 
@@ -65,21 +72,34 @@ const onSubmit = async () => {
 
 	if (!formData.id) {
 		const { data } = await axios.post(
-			window.location.protocol + '//' + window.location.hostname + '/rule-engine/flow',
+			flowsUrl,
 			{
 				label: formData.name,
-				disabled: false,
+				disabled: true,
 				info: '',
 				env: [],
 				nodes: [],
 			},
 			{
-				headers: {
-					Authorization: 'Bearer ' + JSON.parse(sessionStorage.token),
-				},
+				headers,
 			}
 		);
 		formData.flowId = data.id;
+	} else {
+		// 找到所有规则
+		const { data: flows } = await axios.get(flowsUrl + 's', { headers });
+
+		const flow = flows.find((item: any) => item.id === formData.flowId);
+
+		if (!flow) {
+			ElMessage.error('规则不存在');
+			return;
+		}
+
+		flow.label = formData.name;
+
+		// 设置规则状态
+		await axios.post(flowsUrl + 's', flows, { headers });
 	}
 
 	const theApi = formData.id ? api.edit : api.add;

+ 55 - 6
src/views/rule-engine/index.vue

@@ -31,8 +31,8 @@
 			</el-table-column>
 			<el-table-column label="操作" width="200" align="center">
 				<template #default="scope">
-					<el-button size="small" text type="info" v-if="scope.row.status" @click="setStatus(scope.row.id, 0)">停止</el-button>
-					<el-button size="small" text type="primary" v-else @click="setStatus(scope.row.id, 1)">启动</el-button>
+					<el-button size="small" text type="info" v-if="scope.row.status" @click="setStatus(scope.row, 0)">停止</el-button>
+					<el-button size="small" text type="primary" v-else @click="setStatus(scope.row, 1)">启动</el-button>
 					<el-button size="small" text type="warning" @click="addOrEdit(scope.row)">编辑</el-button>
 					<el-button size="small" text type="warning" @click="edit(scope.row)">规则编辑</el-button>
 					<el-button size="small" text type="danger" @click="onDel(scope.row)">删除</el-button>
@@ -51,11 +51,19 @@ import { ElMessageBox, ElMessage } from 'element-plus';
 import { useSearch } from '/@/hooks/useCommon';
 import { Session } from '/@/utils/storage';
 import EditForm from './edit.vue';
+import axios from 'axios';
 
 const editFormRef = ref();
 
 const { params, tableData, getList, loading } = useSearch<any[]>(api.getList, 'Data', { types: 0 });
 
+const headers = {
+	Authorization: 'Bearer ' + JSON.parse(sessionStorage.token),
+};
+
+const flowsUrl = window.location.origin + '/rule-engine/flows';
+// const flowsUrl = 'http://zhgy.sagoo.cn/rule-engine/flows';
+
 getList();
 
 const addOrEdit = async (row?: any) => {
@@ -67,9 +75,25 @@ const addOrEdit = async (row?: any) => {
 	}
 };
 
-const setStatus = (id: number, status: number) => {
+const setStatus = async (row: any, status: number) => {
+	// 找到所有规则
+	const { data: flows } = await axios.get(flowsUrl, { headers });
+
+	const flow = flows.find((item: any) => item.id === row.flowId);
+
+	if (!flow) {
+		ElMessage.error('规则不存在');
+		return;
+	}
+
+	// 改变指定规则状态
+	flow.disabled = status ? false : true;
+
+	// 设置规则状态
+	await axios.post(flowsUrl, flows, { headers });
+
 	api
-		.setStatus(id, status)
+		.setStatus(row.id, status)
 		.then(() => {
 			ElMessage.success('操作成功');
 			getList();
@@ -80,8 +104,8 @@ const setStatus = (id: number, status: number) => {
 };
 
 const edit = async (row: any) => {
-	localStorage.setItem('auth-tokens',`{"access_token":"${Session.get('token')}"}`);
-	// const url = window.location.protocol + '//' + window.location.hostname + ':1880/?access_token=' + Session.get('token') + '#flow/' + row.flowId;
+	localStorage.setItem('auth-tokens', `{"access_token":"${Session.get('token')}"}`);
+	// const url = window.location.protocol + '//' + window.location.hostname + ':1880/rule-engine?access_token=' + Session.get('token') + '#flow/' + row.flowId;
 	const url = '/rule-engine/#flow/' + row.flowId;
 	window.open(url);
 };
@@ -92,6 +116,31 @@ const onDel = (row: any) => {
 		cancelButtonText: '取消',
 		type: 'warning',
 	}).then(async () => {
+		// 找到所有规则
+		const { data: flows } = await axios.get(flowsUrl, { headers });
+
+		const flowIndex = flows.findIndex((item: any) => item.id === row.flowId);
+
+		if (flowIndex === -1) {
+			ElMessage.error('规则不存在');
+			return;
+		}
+
+		// 删除指定规则
+		flows.splice(flowIndex, 1);
+
+		// 删除当前规则下的各个节点信息
+		const newFlows = flows.filter((item: any) => {
+			if (item.z === row.flowId) {
+				return false;
+			} else {
+				return true;
+			}
+		});
+
+		// 设置规则状态
+		await axios.post(flowsUrl, newFlows, { headers });
+
 		await api.del([row.id as string]);
 		ElMessage.success('删除成功');
 		getList();

+ 54 - 7
src/views/rule-engine/send.vue

@@ -13,7 +13,7 @@
 						<el-icon>
 							<ele-FolderAdd />
 						</el-icon>
-						新增数据转发
+						新增规则编排
 					</el-button>
 				</el-form-item>
 			</el-form>
@@ -31,8 +31,8 @@
 			</el-table-column>
 			<el-table-column label="操作" width="200" align="center">
 				<template #default="scope">
-					<el-button size="small" text type="info" v-if="scope.row.status" @click="setStatus(scope.row.id, 0)">停止</el-button>
-					<el-button size="small" text type="primary" v-else @click="setStatus(scope.row.id, 1)">启动</el-button>
+					<el-button size="small" text type="info" v-if="scope.row.status" @click="setStatus(scope.row, 0)">停止</el-button>
+					<el-button size="small" text type="primary" v-else @click="setStatus(scope.row, 1)">启动</el-button>
 					<el-button size="small" text type="warning" @click="addOrEdit(scope.row)">编辑</el-button>
 					<el-button size="small" text type="warning" @click="edit(scope.row)">规则编辑</el-button>
 					<el-button size="small" text type="danger" @click="onDel(scope.row)">删除</el-button>
@@ -51,11 +51,17 @@ import { ElMessageBox, ElMessage } from 'element-plus';
 import { useSearch } from '/@/hooks/useCommon';
 import { Session } from '/@/utils/storage';
 import EditForm from './edit.vue';
+import axios from 'axios';
 
 const editFormRef = ref();
 
 const { params, tableData, getList, loading } = useSearch<any[]>(api.getList, 'Data', { types: 1 });
 
+const headers = {
+	Authorization: 'Bearer ' + JSON.parse(sessionStorage.token),
+};
+const flowsUrl = window.location.origin + '/rule-engine/flows';
+
 getList();
 
 const addOrEdit = async (row?: any) => {
@@ -67,9 +73,25 @@ const addOrEdit = async (row?: any) => {
 	}
 };
 
-const setStatus = (id: number, status: number) => {
+const setStatus = async (row: any, status: number) => {
+	// 找到所有规则
+	const { data: flows } = await axios.get(flowsUrl, { headers });
+
+	const flow = flows.find((item: any) => item.id === row.flowId);
+
+	if (!flow) {
+		ElMessage.error('规则不存在');
+		return;
+	}
+
+	// 改变指定规则状态
+	flow.disabled = status ? false : true;
+
+	// 设置规则状态
+	await axios.post(flowsUrl, flows, { headers });
+
 	api
-		.setStatus(id, status)
+		.setStatus(row.id, status)
 		.then(() => {
 			ElMessage.success('操作成功');
 			getList();
@@ -80,8 +102,8 @@ const setStatus = (id: number, status: number) => {
 };
 
 const edit = async (row: any) => {
-	localStorage.setItem('auth-tokens',`{"access_token":"${Session.get('token')}"}`);
-	// const url = window.location.protocol + '//' + window.location.hostname + ':1880/?access_token=' + Session.get('token') + '#flow/' + row.flowId;
+	localStorage.setItem('auth-tokens', `{"access_token":"${Session.get('token')}"}`);
+	// const url = window.location.protocol + '//' + window.location.hostname + ':1880/rule-engine?access_token=' + Session.get('token') + '#flow/' + row.flowId;
 	const url = '/rule-engine/#flow/' + row.flowId;
 	window.open(url);
 };
@@ -92,6 +114,31 @@ const onDel = (row: any) => {
 		cancelButtonText: '取消',
 		type: 'warning',
 	}).then(async () => {
+		// 找到所有规则
+		const { data: flows } = await axios.get(flowsUrl, { headers });
+
+		const flowIndex = flows.findIndex((item: any) => item.id === row.flowId);
+
+		if (flowIndex === -1) {
+			ElMessage.error('规则不存在');
+			return;
+		}
+
+		// 删除指定规则
+		flows.splice(flowIndex, 1);
+
+		// 删除当前规则下的各个节点信息
+		const newFlows = flows.filter((item: any) => {
+			if (item.z === row.flowId) {
+				return false;
+			} else {
+				return true;
+			}
+		});
+
+		// 设置规则状态
+		await axios.post(flowsUrl, newFlows, { headers });
+
 		await api.del([row.id as string]);
 		ElMessage.success('删除成功');
 		getList();