Commit 03af53139b6d042abd5bc4c7ac30f532278843fe

Authored by loveumiko
1 parent 723c2097

fix: 修改规则链相关的

... ... @@ -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 72 <script lang="ts" setup>
73 73 import { ref, unref, reactive, nextTick } from 'vue';
74 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 80 import { useMessage } from '/@/hooks/web/useMessage';
77 81 import { steps } from './device.profile.data';
78 82 import { isEmpty } from '/@/utils/is';
... ... @@ -119,6 +123,12 @@
119 123 unref(DevConStRef)?.editOrAddDeviceTypeStatus(true);
120 124 } else {
121 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 134 const handleChange = (e) => {
... ...
... ... @@ -196,7 +196,6 @@ export const step1Schemas: FormSchema[] = [
196 196 label: '规则链',
197 197 component: 'ApiSelect',
198 198 colProps: { span: 14 },
199   -
200 199 componentProps: {
201 200 api: async () => {
202 201 const data = await deviceConfigGetRuleChain();
... ...
... ... @@ -28,7 +28,7 @@
28 28 </div>
29 29 </template>
30 30 <script lang="ts" setup>
31   - import { ref } from 'vue';
  31 + import { ref, nextTick } from 'vue';
32 32 import { BasicForm, useForm } from '/@/components/Form';
33 33 import { step1Schemas } from '../device.profile.data';
34 34 import { uploadApi } from '/@/api/personal/index';
... ... @@ -90,6 +90,12 @@
90 90 }
91 91 return isJpgOrPng && isLt2M;
92 92 };
  93 +
  94 + const setFieldsdefaultRuleChainId = async (id) => {
  95 + await nextTick();
  96 + setFieldsValue({ defaultRuleChainId: id });
  97 + };
  98 +
93 99 async function customSubmitFunc() {
94 100 const values = await validate();
95 101 if (!values) return;
... ... @@ -129,6 +135,7 @@
129 135 resetFormData,
130 136 getFormData,
131 137 editOrAddDeviceTypeStatus,
  138 + setFieldsdefaultRuleChainId,
132 139 });
133 140 </script>
134 141 <style lang="less" scoped>
... ...
... ... @@ -5,9 +5,9 @@
5 5 <Authority>
6 6 <a-button type="primary" @click="handleAdd"> 新增规则链 </a-button>
7 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 11 <!-- <Authority>
12 12 <Popconfirm
13 13 title="您确定要批量删除数据"
... ... @@ -90,14 +90,18 @@
90 90 getRuleChinsList,
91 91 exportRuleChine,
92 92 settingRootChine,
  93 + importRuleChine,
  94 + createRuleChine,
93 95 } from '/@/api/ruleengine/ruleengineApi';
94 96 import { useModal } from '/@/components/Modal';
95 97 import { Authority } from '/@/components/Authority';
96   - import { Tag } from 'ant-design-vue';
  98 + import { Tag, Button, Upload } from 'ant-design-vue';
97 99 import { RuleChainModal } from './component/index';
98 100 import { useMessage } from '/@/hooks/web/useMessage';
99 101 import { usePermission } from '/@/hooks/web/usePermission';
100 102 import { useRouter } from 'vue-router';
  103 + import { ref } from 'vue';
  104 + import { isObject, isString } from '/@/utils/is';
101 105 // import { ChainDetailDrawer } from './chainDetail/index';
102 106 // import { useDrawer } from '/@/components/Drawer';
103 107
... ... @@ -112,7 +116,7 @@
112 116 bordered: true,
113 117 showIndexColumn: false,
114 118 formConfig: {
115   - labelWidth: 120,
  119 + labelWidth: 70,
116 120 schemas: searchFormSchema,
117 121 },
118 122 fetchSetting: {
... ... @@ -133,7 +137,7 @@
133 137 },
134 138 },
135 139 actionColumn: {
136   - width: 200,
  140 + width: 220,
137 141 title: '操作',
138 142 dataIndex: 'action',
139 143 slots: { customRender: 'action' },
... ... @@ -183,7 +187,64 @@
183 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 249 const handleExport = async (record: Recordable) => {
189 250 if (!record) return;
... ...