Commit 03af53139b6d042abd5bc4c7ac30f532278843fe

Authored by loveumiko
1 parent 723c2097

fix: 修改规则链相关的

@@ -161,6 +161,16 @@ export const createRuleChine = (params) => { @@ -161,6 +161,16 @@ export const createRuleChine = (params) => {
161 ); 161 );
162 }; 162 };
163 163
  164 +export const importRuleChine = (params) => {
  165 + return defHttp.post(
  166 + {
  167 + url: `${ScreenManagerApi.RULE_CHAIN}/metadata`,
  168 + params,
  169 + },
  170 + { joinPrefix: false }
  171 + );
  172 +};
  173 +
164 /** 174 /**
165 * 删除规则链 175 * 删除规则链
166 */ 176 */
@@ -72,7 +72,11 @@ @@ -72,7 +72,11 @@
72 <script lang="ts" setup> 72 <script lang="ts" setup>
73 import { ref, unref, reactive, nextTick } from 'vue'; 73 import { ref, unref, reactive, nextTick } from 'vue';
74 import { BasicModal, useModalInner } from '/@/components/Modal'; 74 import { BasicModal, useModalInner } from '/@/components/Modal';
75 - import { deviceConfigAddOrEdit, deviceConfigGetDetail } from '/@/api/device/deviceConfigApi'; 75 + import {
  76 + deviceConfigAddOrEdit,
  77 + deviceConfigGetDetail,
  78 + deviceConfigGetRuleChain,
  79 + } from '/@/api/device/deviceConfigApi';
76 import { useMessage } from '/@/hooks/web/useMessage'; 80 import { useMessage } from '/@/hooks/web/useMessage';
77 import { steps } from './device.profile.data'; 81 import { steps } from './device.profile.data';
78 import { isEmpty } from '/@/utils/is'; 82 import { isEmpty } from '/@/utils/is';
@@ -119,6 +123,12 @@ @@ -119,6 +123,12 @@
119 unref(DevConStRef)?.editOrAddDeviceTypeStatus(true); 123 unref(DevConStRef)?.editOrAddDeviceTypeStatus(true);
120 } else { 124 } else {
121 unref(DevConStRef)?.editOrAddDeviceTypeStatus(false); 125 unref(DevConStRef)?.editOrAddDeviceTypeStatus(false);
  126 + const values = ref([]) as any;
  127 + if (!values.value.length) {
  128 + values.value = await deviceConfigGetRuleChain();
  129 + }
  130 + const defaultRuleChainId = unref(values).filter((item) => item.root)[0].id.id;
  131 + await unref(DevConStRef)?.setFieldsdefaultRuleChainId(defaultRuleChainId);
122 } 132 }
123 }); 133 });
124 const handleChange = (e) => { 134 const handleChange = (e) => {
@@ -196,7 +196,6 @@ export const step1Schemas: FormSchema[] = [ @@ -196,7 +196,6 @@ export const step1Schemas: FormSchema[] = [
196 label: '规则链', 196 label: '规则链',
197 component: 'ApiSelect', 197 component: 'ApiSelect',
198 colProps: { span: 14 }, 198 colProps: { span: 14 },
199 -  
200 componentProps: { 199 componentProps: {
201 api: async () => { 200 api: async () => {
202 const data = await deviceConfigGetRuleChain(); 201 const data = await deviceConfigGetRuleChain();
@@ -28,7 +28,7 @@ @@ -28,7 +28,7 @@
28 </div> 28 </div>
29 </template> 29 </template>
30 <script lang="ts" setup> 30 <script lang="ts" setup>
31 - import { ref } from 'vue'; 31 + import { ref, nextTick } from 'vue';
32 import { BasicForm, useForm } from '/@/components/Form'; 32 import { BasicForm, useForm } from '/@/components/Form';
33 import { step1Schemas } from '../device.profile.data'; 33 import { step1Schemas } from '../device.profile.data';
34 import { uploadApi } from '/@/api/personal/index'; 34 import { uploadApi } from '/@/api/personal/index';
@@ -90,6 +90,12 @@ @@ -90,6 +90,12 @@
90 } 90 }
91 return isJpgOrPng && isLt2M; 91 return isJpgOrPng && isLt2M;
92 }; 92 };
  93 +
  94 + const setFieldsdefaultRuleChainId = async (id) => {
  95 + await nextTick();
  96 + setFieldsValue({ defaultRuleChainId: id });
  97 + };
  98 +
93 async function customSubmitFunc() { 99 async function customSubmitFunc() {
94 const values = await validate(); 100 const values = await validate();
95 if (!values) return; 101 if (!values) return;
@@ -129,6 +135,7 @@ @@ -129,6 +135,7 @@
129 resetFormData, 135 resetFormData,
130 getFormData, 136 getFormData,
131 editOrAddDeviceTypeStatus, 137 editOrAddDeviceTypeStatus,
  138 + setFieldsdefaultRuleChainId,
132 }); 139 });
133 </script> 140 </script>
134 <style lang="less" scoped> 141 <style lang="less" scoped>
@@ -5,9 +5,9 @@ @@ -5,9 +5,9 @@
5 <Authority> 5 <Authority>
6 <a-button type="primary" @click="handleAdd"> 新增规则链 </a-button> 6 <a-button type="primary" @click="handleAdd"> 新增规则链 </a-button>
7 </Authority> 7 </Authority>
8 - <!-- <Authority>  
9 - <a-button type="primary" @click="handleImport"> 导入规则链 </a-button>  
10 - </Authority> --> 8 + <Upload :show-upload-list="false" :customRequest="handleImport">
  9 + <Button type="primary" :loading="importLoading"> 导入规则链 </Button>
  10 + </Upload>
11 <!-- <Authority> 11 <!-- <Authority>
12 <Popconfirm 12 <Popconfirm
13 title="您确定要批量删除数据" 13 title="您确定要批量删除数据"
@@ -90,14 +90,18 @@ @@ -90,14 +90,18 @@
90 getRuleChinsList, 90 getRuleChinsList,
91 exportRuleChine, 91 exportRuleChine,
92 settingRootChine, 92 settingRootChine,
  93 + importRuleChine,
  94 + createRuleChine,
93 } from '/@/api/ruleengine/ruleengineApi'; 95 } from '/@/api/ruleengine/ruleengineApi';
94 import { useModal } from '/@/components/Modal'; 96 import { useModal } from '/@/components/Modal';
95 import { Authority } from '/@/components/Authority'; 97 import { Authority } from '/@/components/Authority';
96 - import { Tag } from 'ant-design-vue'; 98 + import { Tag, Button, Upload } from 'ant-design-vue';
97 import { RuleChainModal } from './component/index'; 99 import { RuleChainModal } from './component/index';
98 import { useMessage } from '/@/hooks/web/useMessage'; 100 import { useMessage } from '/@/hooks/web/useMessage';
99 import { usePermission } from '/@/hooks/web/usePermission'; 101 import { usePermission } from '/@/hooks/web/usePermission';
100 import { useRouter } from 'vue-router'; 102 import { useRouter } from 'vue-router';
  103 + import { ref } from 'vue';
  104 + import { isObject, isString } from '/@/utils/is';
101 // import { ChainDetailDrawer } from './chainDetail/index'; 105 // import { ChainDetailDrawer } from './chainDetail/index';
102 // import { useDrawer } from '/@/components/Drawer'; 106 // import { useDrawer } from '/@/components/Drawer';
103 107
@@ -112,7 +116,7 @@ @@ -112,7 +116,7 @@
112 bordered: true, 116 bordered: true,
113 showIndexColumn: false, 117 showIndexColumn: false,
114 formConfig: { 118 formConfig: {
115 - labelWidth: 120, 119 + labelWidth: 70,
116 schemas: searchFormSchema, 120 schemas: searchFormSchema,
117 }, 121 },
118 fetchSetting: { 122 fetchSetting: {
@@ -133,7 +137,7 @@ @@ -133,7 +137,7 @@
133 }, 137 },
134 }, 138 },
135 actionColumn: { 139 actionColumn: {
136 - width: 200, 140 + width: 220,
137 title: '操作', 141 title: '操作',
138 dataIndex: 'action', 142 dataIndex: 'action',
139 slots: { customRender: 'action' }, 143 slots: { customRender: 'action' },
@@ -183,7 +187,64 @@ @@ -183,7 +187,64 @@
183 // console.log(record, '详情'); 187 // console.log(record, '详情');
184 // }; 188 // };
185 189
186 - // const handleImport = () => {}; 190 + const paseJSON = (string: string) => {
  191 + let data = null;
  192 + let flag = false;
  193 + try {
  194 + if (!isString(string)) return { flag: false, data };
  195 + data = JSON.parse(string);
  196 + flag = true;
  197 + if (!isObject(data)) flag = false;
  198 + } catch (error) {}
  199 + return { flag, data };
  200 + };
  201 +
  202 + const isEmptyObject = (value: any) => isObject(value) && !Object.keys(value).length;
  203 +
  204 + const importLoading = ref<boolean>(false);
  205 + const handleImport = (data: { file: File }) => {
  206 + const fileReader = new FileReader();
  207 +
  208 + fileReader.onload = async () => {
  209 + const { flag, data } = paseJSON(fileReader.result as string);
  210 + if (!flag) {
  211 + createMessage.warning('JSON解析失败,请导入正确的JSON~');
  212 + return;
  213 + }
  214 + try {
  215 + importLoading.value = true;
  216 +
  217 + Object.keys(data || {}).forEach((key) => {
  218 + const value = (data || {})[key];
  219 + if (value && isEmptyObject(value)) {
  220 + (data || {})[key] = [];
  221 + }
  222 + });
  223 + const { ruleChain, metadata } = data as any;
  224 +
  225 + const value = await createRuleChine(ruleChain);
  226 + const { id } = value;
  227 +
  228 + const values = {
  229 + ruleChainId: id,
  230 + ...metadata,
  231 + };
  232 + const rules = await importRuleChine(values);
  233 +
  234 + rules
  235 + ? createMessage.success('导入成功~')
  236 + : createMessage.error('JSON解析失败,请导入正确的JSON~');
  237 +
  238 + rules && reload();
  239 + } catch (error) {
  240 + throw error;
  241 + } finally {
  242 + importLoading.value = false;
  243 + }
  244 + };
  245 +
  246 + fileReader.readAsText(data.file, 'utf-8');
  247 + };
187 248
188 const handleExport = async (record: Recordable) => { 249 const handleExport = async (record: Recordable) => {
189 if (!record) return; 250 if (!record) return;