ConfigurationCenterDrawer.vue
9.9 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<template>
<BasicDrawer
v-bind="$attrs"
@register="registerDrawer"
showFooter
:title="getTitle"
width="30%"
@ok="handleSubmit"
>
<BasicForm @register="registerForm">
<!-- 模板选择 -->
<template #templateId="{ model }">
<span style="display: none">{{ getFieldOrg(model['organizationId']) }}</span>
<Select
v-model:value="model['templateId']"
placeholder="请选择模板"
style="width: 100%"
:options="
selectTemplateOptions?.filter((item) => item.platform === model['platform']) || []
"
@change="handleTemplateChange"
v-bind="createPickerSearch()"
:disabled="templateDisabled"
/>
</template>
<!-- 产品选择 -->
<template #productIds="{ model }">
<SelectDeviceProfile
v-if="model['templateId']"
ref="selectDeviceProfileRef"
:selectOptions="selectOptions"
:organizationId="model?.['organizationId']"
/>
</template>
</BasicForm>
</BasicDrawer>
</template>
<script lang="ts">
import { defineComponent, ref, computed, unref, Ref, onMounted, nextTick, watch } from 'vue';
import { BasicForm, useForm } from '/@/components/Form';
import { Select } from 'ant-design-vue';
import { formSchema, PC_DEFAULT_CONTENT, PHONE_DEFAULT_CONTENT, Platform } from './center.data';
import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
import { useMessage } from '/@/hooks/web/useMessage';
import {
saveOrUpdateConfigurationCenter,
getPage,
} from '/@/api/configuration/center/configurationCenter';
import { FileItem } from '/@/components/Form/src/components/ApiUpload.vue';
import { buildUUID } from '/@/utils/uuid';
import SelectDeviceProfile from './components/SelectDeviceProfile.vue';
import { createPickerSearch } from '/@/utils/pickerSearch';
import type { queryPageParams } from '/@/api/configuration/center/model/configurationCenterModal';
import { getDeviceProfile } from '/@/api/alarm/position';
export default defineComponent({
name: 'ConfigurationDrawer',
components: { BasicDrawer, BasicForm, SelectDeviceProfile, Select },
emits: ['success', 'register'],
setup(_, { emit }) {
const isUpdate = ref(true);
const selectDeviceProfileRef = ref<InstanceType<typeof SelectDeviceProfile>>();
const [registerForm, { validate, setFieldsValue, resetFields, updateSchema, clearValidate }] =
useForm({
labelWidth: 120,
schemas: formSchema,
showActionButtonGroup: false,
});
const updateEnableTemplate = async (enable: number, disabled: boolean) => {
await setFieldsValue({
enableTemplate: enable,
});
await updateSchema({
field: 'enableTemplate',
componentProps: ({ formActionType }) => {
return {
disabled,
checkedValue: 1,
unCheckedValue: 0,
checkedChildren: '开',
unCheckedChildren: '关',
onChange: () => {
formActionType.setFieldsValue({
productIds: [],
templateId: null,
productId: undefined,
});
},
};
},
});
await updateSchema({
field: 'organizationId',
componentProps: () => {
return {
disabled,
};
},
});
};
const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
await nextTick();
await resetFields();
setDrawerProps({ confirmLoading: false });
isUpdate.value = !!data?.isUpdate;
selectDeviceProfileRef.value?.retValue();
await updateEnableTemplate(0, false);
templateDisabled.value = false;
if (unref(isUpdate)) {
if (data.record.thumbnail) {
data.record.thumbnail = [
{ uid: buildUUID(), name: 'name', url: data.record.thumbnail } as FileItem,
];
}
if (data.record.organizationDTO) {
organizationIdStr.value = data.record['organizationId'];
await setFieldsValue(data.record);
} else {
Reflect.deleteProperty(data.record, 'organizationId');
await setFieldsValue(data.record);
}
// 业务 编辑如果有templateId 则启用模板开关禁用,模板不能选择,产品和设备可以选择
if (Reflect.get(data.record, 'templateId')) {
await updateEnableTemplate(1, true);
templateDisabled.value = true;
//回显产品和设备
const { productAndDevice } = data.record;
selectOptions.value = productAndDevice?.map((item) => ({
label: item.name,
value: item.profileId,
}));
selectDeviceProfileRef.value?.setValue(productAndDevice);
} else {
const productAndDevice = data.record['productAndDevice'];
setFieldsValue({
productId: productAndDevice?.map((item) => item.profileId),
});
}
}
});
//新增修改
const templateDisabled = ref(false);
onMounted(() => {
getTemplate(
{
page: 1,
pageSize: 30,
},
null
);
});
const selectTemplateOptions: Ref<any[]> = ref([]);
const organizationIdStr = ref('');
const getFieldOrg = (org: string) => (organizationIdStr.value = org);
watch(
() => organizationIdStr.value,
(newValue) => {
getTemplate(
{
page: 1,
pageSize: 30,
},
newValue
);
if (!isUpdate.value) {
setFieldsValue({
templateId: null,
});
}
}
);
const getTemplate = async (params: queryPageParams, organizationId) => {
const { items } = await getPage({ ...params, isTemplate: 1, organizationId });
selectTemplateOptions.value = items.map((item) => ({
...item,
label: item.name,
value: item.id,
}));
};
const selectOptions: Ref<any[]> = ref([]);
const handleTemplateChange = async (_, option) => {
clearValidate('templateId');
const { productAndDevice } = option;
setFieldsValue({ platform: option?.platform });
await nextTick();
// 赋值
selectDeviceProfileRef.value?.setFieldsValue(
productAndDevice?.map((item) => ({
label: item.profileName || item.name,
value: item.profileId,
transportType: item?.transportType,
deviceType: item?.deviceType,
}))
);
};
//
const getTitle = computed(() => (!unref(isUpdate) ? '新增组态中心' : '编辑组态中心'));
const getDefaultContent = (platform: Platform) => {
if (platform === Platform.PC) {
return PC_DEFAULT_CONTENT;
}
return PHONE_DEFAULT_CONTENT;
};
// 获取产品
const getCurrentAllProduct = async (products: string[]) => {
const resp = (await getDeviceProfile()) as any;
if (!resp) return;
const values = resp?.map((item) => ({
name: item.name,
profileId: item.id,
deviceType: item.deviceType,
transportType: item.transportType,
}));
return values.filter((item) => products?.includes(item.profileId));
};
async function handleSubmit() {
try {
const { createMessage } = useMessage();
const values = await validate();
if (!values) return;
const selectDevice = selectDeviceProfileRef.value?.getSelectDevice();
if (values['enableTemplate'] === 1) {
if (!values['templateId']) return createMessage.error('未选择模板');
// 业务 启用模板,产品和设备必填
if (values['templateId']) {
values.templateId = values['templateId'];
if (Array.isArray(selectDevice) && selectDevice.length == 0)
return createMessage.error('您已经选择了模板,但产品或者设备未选择');
if (
selectDevice?.some(
(item) =>
!item.name ||
item.deviceList.includes('' || undefined) ||
item.deviceList.length == 0
)
) {
return createMessage.error('您已经选择了模板,但产品或者设备未选择');
}
values.productAndDevice = selectDevice;
}
} else {
const { productId } = values;
values.productAndDevice = await getCurrentAllProduct(productId);
Reflect.deleteProperty(values, 'productId');
}
if (Reflect.has(values, 'thumbnail')) {
const file = (values.thumbnail || []).at(0) || {};
values.thumbnail = file.url || null;
}
setDrawerProps({ confirmLoading: true });
let saveMessage = '添加成功';
let updateMessage = '修改成功';
values.defaultContent = getDefaultContent(values.platform);
await saveOrUpdateConfigurationCenter(values, unref(isUpdate));
closeDrawer();
emit('success');
createMessage.success(unref(isUpdate) ? updateMessage : saveMessage);
} finally {
setDrawerProps({ confirmLoading: false });
}
}
return {
getTitle,
registerDrawer,
registerForm,
handleSubmit,
selectOptions,
selectTemplateOptions,
handleTemplateChange,
createPickerSearch,
selectDeviceProfileRef,
templateDisabled,
getFieldOrg,
};
},
});
</script>