useDrawer.vue
6.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<template>
<BasicDrawer
v-bind="$attrs"
@register="registerDrawer"
showFooter
:title="getTitle"
width="1000px"
@ok="handleSubmit"
>
<BasicForm @register="registerForm" />
<AddTriggerForm ref="getTriggerChildData" :deviceInfo="getDeviceInfo" />
<AddConditiForm ref="getConditionChildData" :deviceInfo1="getDeviceInfo1" />
<AddActionForm ref="getChildData" :deviceInfo2="getDeviceInfo2" />
</BasicDrawer>
</template>
<script lang="ts">
import { defineComponent, ref, computed, unref, reactive, watch, getCurrentInstance } from 'vue';
import { BasicForm, useForm } from '/@/components/Form';
import { formSchema, getData } from './config.d';
import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
import { screenLinkPageAddApi, screenLinkPageUpdateApi } from '/@/api/ruleengine/ruleengineApi';
import { useMessage } from '/@/hooks/web/useMessage';
import AddTriggerForm from './addForm/trigger.vue';
import AddActionForm from './addForm/doaction.vue';
import AddConditiForm from './addForm/condition.vue';
export default defineComponent({
name: 'ConfigDrawer',
components: { BasicDrawer, BasicForm, AddTriggerForm, AddActionForm, AddConditiForm },
emits: ['success', 'register'],
setup(_, { emit }) {
const { proxy } = getCurrentInstance();
const getChildData = ref(null);
const getTriggerChildData = ref(null);
const getConditionChildData = ref(null);
const { createMessage } = useMessage();
const isUpdate = ref(true);
let doConditionsArray: any[] = reactive([]);
let doActionsArray: any[] = reactive([]);
let triggersArray: any[] = reactive([]);
let getAllFormData: any = reactive({});
let getValuesFormData: any = reactive({});
let getId = ref('');
let getDeviceInfo = ref(null);
let getDeviceInfo1 = ref(null);
let getDeviceInfo2 = ref(null);
const [registerForm, { resetFields, setFieldsValue, validateFields, getFieldsValue }] =
useForm({
labelWidth: 120,
schemas: formSchema,
showActionButtonGroup: false,
});
watch(getData, (newV) => {
getDeviceInfo.value = newV;
getDeviceInfo1.value = newV;
getDeviceInfo2.value = newV;
});
const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
proxy.$refs.getChildData.funcResetFields();
proxy.$refs.getTriggerChildData.funcResetFields();
proxy.$refs.getConditionChildData.funcResetFields();
await resetFields();
setDrawerProps({ confirmLoading: false });
isUpdate.value = !!data?.isUpdate;
//编辑
if (unref(isUpdate)) {
getId.value = data.record.id;
await setFieldsValue({
...data.record,
});
try {
proxy.$refs.getTriggerChildData.setFieldsFormValue({
triggerEvent: data.record.triggers[0].triggerEvent,
attributeChoose: data.record?.triggers[0].attributeChoose,
touchWay: data.record?.triggers[0].touchWay,
deviceId: data.record?.triggers[0].deviceId,
compare: data.record?.triggers[0].compare,
value: data.record?.triggers[0].value,
sceneLinkageId: data.record?.triggers[0].sceneLinkageId,
});
proxy.$refs.getConditionChildData.setFieldsFormValue({
status: data.record?.doConditions[0].status,
deviceId: data.record?.doConditions[0].deviceId,
createTime: data.record?.doConditions[0].createTime,
updateTime: data.record?.doConditions[0].updateTime,
property: data.record?.doConditions[0].property,
compare: data.record?.doConditions[0].compare,
value: data.record?.doConditions[0].value,
});
proxy.$refs.getChildData.setFieldsFormValue({
outTarget: data.record?.doActions[0].outTarget,
deviceId: data.record?.doActions[0].deviceId,
command: data.record?.doActions[0].command,
sceneLinkageId: data.record?.doActions[0].sceneLinkageId,
});
} catch (e) {
return e;
}
} else {
await resetFields();
}
});
const getTitle = computed(() => (!unref(isUpdate) ? '新增场景联动' : '编辑场景联动'));
async function handleSubmit() {
if (!unref(isUpdate)) {
let res = validateFields();
if (!res) return;
let getChildValues = proxy.$refs.getChildData.getAllFields();
let getTriggerChildValues = proxy.$refs.getTriggerChildData.getAllFields();
let getconditionChildValues = proxy.$refs.getConditionChildData.getAllFields();
doActionsArray.push(getChildValues);
triggersArray.push(getTriggerChildValues);
doConditionsArray.push(getconditionChildValues);
getValuesFormData = getFieldsValue();
Object.assign(getAllFormData, getValuesFormData);
getAllFormData.triggers = triggersArray;
getAllFormData.doConditions = doConditionsArray;
getAllFormData.doActions = doActionsArray;
//所有的表单值
await screenLinkPageAddApi(getAllFormData);
createMessage.success('场景联动新增成功');
closeDrawer();
emit('success');
}
if (unref(isUpdate)) {
getValuesFormData = getFieldsValue();
let getChildValues = proxy.$refs.getChildData.getAllFields();
let getTriggerChildValues = proxy.$refs.getTriggerChildData.getAllFields();
let getconditionChildValues = proxy.$refs.getConditionChildData.getAllFields();
doActionsArray.push(getChildValues);
triggersArray.push(getTriggerChildValues);
doConditionsArray.push(getconditionChildValues);
getValuesFormData.id = getId.value;
Object.assign(getAllFormData, getValuesFormData);
getAllFormData.triggers = triggersArray;
getAllFormData.doConditions = doConditionsArray;
getAllFormData.doActions = doActionsArray;
await screenLinkPageUpdateApi(getAllFormData);
createMessage.success('场景联动编辑成功');
closeDrawer();
emit('success');
}
}
return {
getConditionChildData,
getTriggerChildData,
getChildData,
getDeviceInfo,
getDeviceInfo1,
getDeviceInfo2,
getAllFormData,
registerDrawer,
registerForm,
getTitle,
handleSubmit,
};
},
});
</script>