index.vue
7.62 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
<script setup lang="ts">
import { Button, Checkbox, Divider, Form, FormItem, InputPassword } from 'ant-design-vue'
import { computed, onMounted, reactive, ref, toRaw, unref } from 'vue'
import { useNodeData } from '../../hook/useNodeData'
import type { ConfigComponentProps } from '../../types'
import { useSavePageContent } from '../../hook/useSavePageContent'
import { ControlComponentEnum } from '../../packages/Control'
import { DataSourceForm } from './components/DataSourceForm'
import { DataEffects } from './components/DataEffects'
import { DataEvents } from './components/DataEvents'
import { createPublicFormContext } from './usePublicFormContext'
import { getInitStatusSettingDefaultValue } from './components/DataEffects/StatusSetting'
import type { ActTypeEnum, EventTypeEnum } from '@/enums/datasource'
import { useMessage } from '@/hooks/web/useMessage'
import type { NodeDataActJsonType, NodeDataEventJsonType, OperationPasswordDataType } from '@/api/node/model'
import { PackageCategoryEnum } from '@/core/Library/enum'
import { useContentDataStoreWithOut } from '@/store/modules/contentData'
import { BasicHelp } from '@/components/Basic'
const props = defineProps<ConfigComponentProps>()
const contentDataStore = useContentDataStoreWithOut()
const loading = ref(false)
const dataSourceElRef = ref<Nullable<InstanceType<typeof DataSourceForm>>>()
const dataEventsElRef = ref<InstanceType<typeof DataEvents>>()
const dataActElRef = ref<InstanceType<typeof DataEffects>>()
const operationPassword = reactive<OperationPasswordDataType>({
checked: false,
value: '',
label: '操作密码',
enable: true,
})
const nodeDataActinType = useNodeData({ cell: props.cell!, immediate: false })
const { getNodeData, getNodeAllData, saveNodeAllData, getCellInfo } = nodeDataActinType
const getFormSetting = computed(() => {
return props.config?.presetOption?.publicFormSetting
})
const getComponentKey = computed(() => {
return props.config?.key
})
const getEventJson = (): NodeDataEventJsonType => {
const { eventJson } = toRaw(unref(getNodeData)) || {}
if (!eventJson) return {} as any
const status = unref(dataEventsElRef)?.getFieldsValue() as Record<EventTypeEnum, boolean>
if (!status) return {} as any
Object.keys(status).forEach((key) => {
if (eventJson[key as EventTypeEnum])
eventJson[key as EventTypeEnum]!.enable = status[key as EventTypeEnum]
})
return eventJson
}
const getActJson = (): NodeDataActJsonType => {
const { actJson } = toRaw(unref(getNodeData)) || {}
if (!actJson) return {} as NodeDataActJsonType
if (unref(getCellInfo).componentKey === ControlComponentEnum.SWITCH && !actJson.STATUS_SETTING) {
actJson.STATUS_SETTING = {
enable: true,
rangeList: getInitStatusSettingDefaultValue(),
}
}
const status = unref(dataActElRef)?.getFieldsValue() as Record<ActTypeEnum, boolean>
if (!status) return {} as NodeDataActJsonType
Object.keys(status).forEach((key) => {
if (actJson[key as ActTypeEnum])
actJson[key as ActTypeEnum].enable = status[key as ActTypeEnum]
})
return actJson
}
const { createMessage } = useMessage()
const validatePassword = () => {
return operationPassword.value ? Promise.resolve(operationPassword) : Promise.reject(new Error('请输入操作密码'))
}
const getSetPasswordStatus = computed(() => {
const data = unref(getNodeData)?.eventJson?.SINGLE
return !data
})
const { savePageContent } = useSavePageContent()
const handleSave = async () => {
loading.value = true
try {
const eventJson = getEventJson()
const actJson = getActJson()
await unref(dataSourceElRef)?.validate()
const value = unref(dataSourceElRef)?.getFieldsValue()
let dataSourceJson = value
if (operationPassword.checked)
await validatePassword()
if (contentDataStore.getIsTemplate)// 判断组态是不是模板
dataSourceJson = { ...value, deviceProfileId: value?.deviceProfileTemplateId, deviceId: null, deviceName: null }
await saveNodeAllData({ dataSourceJson, eventJson: { ...eventJson, OPERATION_PASSWORD: unref(getCellInfo).category === PackageCategoryEnum.CONTROL ? operationPassword : undefined }, actJson })
createMessage.success('操作成功')
savePageContent()
}
finally {
loading.value = false
}
}
const dataSourceChange = ref(false)
const handleDataSourceChange = () => {
dataSourceChange.value = true
}
const handleBeforeOpenEventOrActModal = async () => {
if (unref(dataSourceChange)) {
await unref(dataSourceElRef)?.validate()
const value = unref(dataSourceElRef)?.getFieldsValue()
const { eventJson, actJson } = unref(getNodeData) || {}
await saveNodeAllData({ eventJson, actJson, dataSourceJson: value })
}
}
const handleSetFormValues = () => {
unref(dataSourceElRef)?.setFieldsValue(unref(getNodeData)?.dataSourceJson)
const { actJson, eventJson } = unref(getNodeData) || {}
const { OPERATION_PASSWORD } = eventJson || {}
if (OPERATION_PASSWORD)
Object.assign(operationPassword, OPERATION_PASSWORD)
const actStatus = Object.keys(actJson || {})
.reduce((prev, next) => ({ ...prev, [next]: (actJson || {})[next as ActTypeEnum]?.enable }), {})
const eventStatus = Object.keys(eventJson || {})
.reduce((prev, next) => ({ ...prev, [next]: (eventJson || {})[next as EventTypeEnum]?.enable }), {})
unref(dataActElRef)?.setFieldsValue(actStatus)
unref(dataEventsElRef)?.setFieldsValue(eventStatus)
}
const getValidateStatus = (value: string | number) => {
if (!operationPassword.checked) return ''
return value ? '' : 'error'
}
onMounted(async () => {
await getNodeAllData()
handleSetFormValues()
})
createPublicFormContext(nodeDataActinType)
</script>
<template>
<main class="form-container px-4">
<div v-if="getFormSetting && getFormSetting.dataSourceSetting">
<Divider orientation="left">
数据源
</Divider>
<DataSourceForm
ref="dataSourceElRef" :component-key="getComponentKey"
@field-value-change="handleDataSourceChange"
/>
</div>
<div v-if="getFormSetting && getFormSetting.eventSetting !== false">
<Divider orientation="left">
数据交互
</Divider>
<DataEvents ref="dataEventsElRef" :before-click="handleBeforeOpenEventOrActModal" :form-setting="getFormSetting" />
<div
v-if="getCellInfo.category === PackageCategoryEnum.CONTROL" class="flex flex-col justify-center passwordInput"
:class="getFormSetting?.actSetting === false && 'mb-4'"
>
<Checkbox v-model:checked="operationPassword.checked" :disabled="getSetPasswordStatus">
<div class="flex">
{{ operationPassword.label }}
<BasicHelp class="ml-1" text="操作密码: 需完成单击事件交互。" />
</div>
</Checkbox>
<Form>
<FormItem :validate-status="getValidateStatus(operationPassword.value!)">
<InputPassword
v-model:value="operationPassword.value" class="mt-1" :disabled="!operationPassword.checked"
placeholder="请输入操作密码"
/>
</FormItem>
</Form>
</div>
</div>
<div v-if="getFormSetting && getFormSetting.actSetting !== false">
<Divider orientation="left">
数据动效
</Divider>
<DataEffects ref="dataActElRef" :before-click="handleBeforeOpenEventOrActModal" :form-setting="getFormSetting" />
</div>
<Button class="w-full" type="primary" :loading="loading" @click="handleSave">
保存
</Button>
</main>
</template>
<style lang="less" scoped>
.form-container {
@apply text-sm;
:deep(.ant-divider) {
@apply text-sm;
}
}
.passwordInput {
:deep(.ant-form-item) {
margin: 0 !important
}
}
</style>