useNodeEvent.ts
3.36 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
import { h, render } from 'vue'
import { doCommandDelivery, getDeviceActive } from '@/api/device'
import type { MouseUpEventDataType, NodeDataDataSourceJsonType, NodeDataEventJsonType, SingleClickEventDataType } from '@/api/node/model'
import { CommandWayEnum } from '@/enums/commandEnum'
import { EventActionTypeEnum } from '@/enums/datasource'
import { useMessage } from '@/hooks/web/useMessage'
import { CommandDeliveryModal } from '@/core/Library/components/PublicForm/components/DataEvents/CommandDeliveryModal'
export function useNodeEvent(eventJson: NodeDataEventJsonType, dataSourceJson: NodeDataDataSourceJsonType) {
const { createMessage } = useMessage()
const handlerCommandDelivery = async (data: MouseUpEventDataType) => {
try {
const promiseList: (() => Promise<any>)[] = []
const { commandList = [], enable } = data || {}
if (!enable) return
for (const item of commandList) {
const { command, way, deviceId } = item
promiseList.push(async () => {
if (way === CommandWayEnum.TWO_WAY) {
const [record] = await getDeviceActive(deviceId)
if (!record.value) {
createMessage.warning('设备不在线')
return
}
}
await doCommandDelivery({
way,
deviceId,
command: {
additionalInfo: { cmdType: 'API' },
method: 'methodThingskit',
params: command,
persistent: true,
},
})
createMessage.success('下发成功~')
})
}
await Promise.all(promiseList.map(fn => fn()))
}
catch (error) {
createMessage.error(error as string)
}
}
const handleOpenLink = (data: SingleClickEventDataType) => {
const { openLink } = data
window.open(openLink)
}
const handleSwitchPage = (data: SingleClickEventDataType) => {
const { openPage } = data
const pages = window.DrawApp.pages
const openPageFile = pages.find(item => item.getId() === openPage)
if (openPageFile)
window.DrawApp.selectPage(openPageFile)
}
const handleParamsSetting = async (_data: SingleClickEventDataType) => {
const deviceInfo = dataSourceJson
const instance = h(CommandDeliveryModal)
render(instance, document.body);
(instance.component?.exposed as InstanceType<typeof CommandDeliveryModal>)?.open({ ..._data, deviceInfo, operationPassword: eventJson?.OPERATION_PASSWORD })
}
const handlerMouseClickEvent = (data: SingleClickEventDataType) => {
const { actionType, enable } = data
if (!enable) return
if (actionType === EventActionTypeEnum.OPEN_LINK)
handleOpenLink(data)
else if (actionType === EventActionTypeEnum.OPEN_PAGE)
handleSwitchPage(data)
else if (actionType === EventActionTypeEnum.PARAMS_SETTING)
handleParamsSetting(data)
}
const handlerMouseDown = async () => {
eventJson.DOWN && handlerCommandDelivery(eventJson.DOWN)
}
const handlerMouseUp = () => {
eventJson.UP && handlerCommandDelivery(eventJson.UP)
}
const handlerMouseClick = () => {
eventJson.SINGLE && handlerMouseClickEvent(eventJson.SINGLE)
}
const handlerMouseDoubleClick = () => {
eventJson.DOUBLE && handlerMouseClickEvent(eventJson.DOUBLE)
}
return {
handlerMouseUp,
handlerMouseDown,
handlerMouseClick,
handlerMouseDoubleClick,
}
}