Jelajahi Sumber

增加新规则引擎的启动停止操作

yanglzh 11 bulan lalu
induk
melakukan
23ba8711a0
1 mengubah file dengan 41 tambahan dan 26 penghapusan
  1. 41 26
      src/views/iot/rule-engine/index.vue

+ 41 - 26
src/views/iot/rule-engine/index.vue

@@ -24,10 +24,8 @@
 				</el-table-column>
 				<el-table-column label="操作" width="200" align="center">
 					<template #default="scope">
-						<template v-if="model === 'node-red'">
-							<el-button size="small" text type="info" v-auth="'startOrStop'" v-if="scope.row.status" @click="setStatus(scope.row, 0)">停止</el-button>
-							<el-button size="small" text type="primary" v-auth="'startOrStop'" v-else @click="setStatus(scope.row, 1)">启动</el-button>
-						</template>
+						<el-button size="small" text type="info" v-auth="'startOrStop'" v-if="scope.row.status" @click="setStatus(scope.row, 0)">停止</el-button>
+						<el-button size="small" text type="primary" v-auth="'startOrStop'" v-else @click="setStatus(scope.row, 1)">启动</el-button>
 						<el-button size="small" text type="primary" v-auth="'edit'" @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="info" v-auth="'del'" @click="onDel(scope.row)">删除</el-button>
@@ -52,7 +50,7 @@ import { getToken } from "/@/utils/auth";
 const editFormRef = ref();
 
 // 规则引擎模式 node-red、 sagoo-rule
-const model = import.meta.env.VITE_RULE_MODEL
+const model: 'node-red' | 'sagoo-rule' = import.meta.env.VITE_RULE_MODEL
 
 const { params, tableData, getList, loading } = useSearch<any[]>(api.getList, 'Data', { types: 0 });
 
@@ -75,31 +73,48 @@ const addOrEdit = async (row?: any) => {
 };
 
 const setStatus = async (row: any, status: number) => {
-	// 找到所有规则
-	const { data: flows } = await axios.get(flowsUrl, { headers });
+	if (model === 'sagoo-rule') {
+		axios.post(`${import.meta.env.VITE_RULE_SERVER_URL}/api/v1/rule/${row.flowId}/${status ? 'enable' : 'stop'}`, {}, { headers })
+			.then(() => {
+				api
+					.setStatus(row.id, status)
+					.then(() => {
+						ElMessage.success('操作成功');
+						getList();
+					})
+					.catch(() => {
+						ElMessage.error('操作失败');
+					});
+			})
+			.catch(() => {
+				ElMessage.error('操作失败')
+			})
+	} else {
+		// 找到所有规则
+		const { data: flows } = await axios.get(flowsUrl, { headers });
 
-	const flow = flows.find((item: any) => item.id === row.flowId);
+		const flow = flows.find((item: any) => item.id === row.flowId);
 
-	if (!flow) {
-		ElMessage.error('规则不存在');
-		return;
+		if (!flow) {
+			ElMessage.error('规则不存在');
+			return;
+		}
+
+		// 改变指定规则状态
+		flow.disabled = status ? false : true;
+		// 设置规则状态
+		await axios.post(flowsUrl, flows, { headers });
+		api
+			.setStatus(row.id, status)
+			.then(() => {
+				ElMessage.success('操作成功');
+				getList();
+			})
+			.catch(() => {
+				ElMessage.error('操作失败');
+			});
 	}
 
-	// 改变指定规则状态
-	flow.disabled = status ? false : true;
-
-	// 设置规则状态
-	await axios.post(flowsUrl, flows, { headers });
-
-	api
-		.setStatus(row.id, status)
-		.then(() => {
-			ElMessage.success('操作成功');
-			getList();
-		})
-		.catch(() => {
-			ElMessage.error('操作失败');
-		});
 };
 
 const edit = async (row: any) => {