useNodeEvent.ts 5.53 KB
import { h, render, toRaw, unref } from 'vue'
import { ControlComponentEnum } from '../packages/Control'
import { doCommandDelivery, getDeviceActive } from '@/api/device'
import type { MouseUpEventDataType, NodeDataDataSourceJsonType, NodeDataEventJsonType, SingleClickEventDataType } from '@/api/node/model'
import { CommandMethodEnum, CommandWayEnum } from '@/enums/commandEnum'
import { ActRangListItemTypeEnum, CommandTypeEnum, EventActionTypeEnum } from '@/enums/datasource'
import { useMessage } from '@/hooks/web/useMessage'
import { AttributeDeliverModal, CommandDeliveryModal, SwitchCommandDeliveryModal } from '@/core/Library/components/PublicForm/components/DataEvents/CommandDeliveryModal'
import type { MxCell } from '@/fitCore/types'
import { NodeUtils } from '@/hooks/business/useNodeUtils'
import { useContentDataStoreWithOut } from '@/store/modules/contentData'

export function useNodeEvent(eventJson: NodeDataEventJsonType, dataSourceJson: NodeDataDataSourceJsonType, cell: MxCell) {
  const { createMessage } = useMessage()

  /**
   * @description 多命令下发
   * @param data
   * @returns
   */
  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: CommandTypeEnum.API },
              method: CommandMethodEnum.THINGSKIT,
              params: command,
              persistent: true,
            },
          })
          createMessage.success('下发成功')
        })
      }

      await Promise.all(promiseList.map(fn => fn()))
    }
    catch (error) {
      createMessage.error(error as string)
    }
  }

  /**
   * @description 打开链接
   * @param data
   */
  const handleOpenLink = (data: SingleClickEventDataType) => {
    const { openLink } = data
    window.open(openLink)
  }

  /**
   * @description 跳转页面
   * @param data
   */
  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 instance = h(CommandDeliveryModal)
    render(instance, document.body);
    (instance.component?.exposed as InstanceType<typeof CommandDeliveryModal>)?.open({ dataSource: toRaw(unref(dataSourceJson)), eventBindData: data, operationPasswordInfo: eventJson.OPERATION_PASSWORD })
  }

  const handleAttributeDelivery = async (data: SingleClickEventDataType) => {
    try {
      const nodeUtils = new NodeUtils()
      const operationPasswordInfo = toRaw(unref(eventJson.OPERATION_PASSWORD))

      if (nodeUtils.getNodeComponentKey(cell) === ControlComponentEnum.SWITCH) {
        const contentDataStore = useContentDataStoreWithOut()
        const currentData = contentDataStore.contentData.find(item => item.id === cell.getId())
        if (!currentData) return
        const { actJson } = currentData
        const { rangeList } = actJson.STATUS_SETTING
        const status = cell.getAttribute('SWITCH_STATUS')
        const res = rangeList.find(item => item.type === (status === ActRangListItemTypeEnum.OPEN ? ActRangListItemTypeEnum.CLOSE : ActRangListItemTypeEnum.OPEN))
        if (!res) return
        const { statusValue } = res

        const instance = h(SwitchCommandDeliveryModal)
        render(instance, document.body)
        await (instance.component?.exposed as InstanceType<typeof SwitchCommandDeliveryModal>)?.open({ operationPasswordInfo, eventBindData: toRaw(unref(data)), dataSourceJson: toRaw(unref(dataSourceJson)), sendValue: statusValue }!)
      }
      else {
        const instance = h(AttributeDeliverModal)
        render(instance, document.body);
        (instance.component?.exposed as InstanceType<typeof AttributeDeliverModal>)?.open({ dataSourceJson, operationPasswordInfo, eventBindData: data })
      }
    }
    catch (error) {
      console.error(error)
    }
  }

  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)
    else if (actionType === EventActionTypeEnum.ATTRIBUTE_DELIVERY)
      handleAttributeDelivery(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,
  }
}