You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
470 lines
17 KiB
470 lines
17 KiB
var messageFn = (msg) => {
|
|
layui.use('layer', function(){
|
|
const layer = layui.layer;
|
|
layer.msg(msg);
|
|
});
|
|
}
|
|
const operateVariableFunc = (lfInstance, configObj, event, response) => {
|
|
configObj.operateVariables.forEach((variable) => {
|
|
const value = variable.selectedVariable.split('=')
|
|
// eslint-disable-next-line no-new-func
|
|
const func = new Function('inputParamsTable', 'globalInputParamsCalculated', 'response', 'event', variable.newValue)
|
|
if (!configObj.globalInputParamsCalculated) {
|
|
return;
|
|
}
|
|
if (Array.isArray(configObj.globalInputParamsCalculated)) {
|
|
const globalInputParams = configObj.globalInputParamsCalculated.length > 0 ? configObj.globalInputParamsCalculated : configObj.inputParamsTable;
|
|
const returnValue = func(configObj.inputParamsTable, globalInputParams, response, event)
|
|
lfInstance.setProperties(value[0], {
|
|
[value[1]]: returnValue,
|
|
})
|
|
}
|
|
else {
|
|
const len = Object.keys(configObj.globalInputParamsCalculated).length;
|
|
const globalInputParams = len > 0 ? configObj.globalInputParamsCalculated : configObj.inputParamsTable;
|
|
const returnValue = func(configObj.inputParamsTable, globalInputParams, response, event);
|
|
lfInstance.setProperties(value[0], {
|
|
[value[1]]: returnValue,
|
|
})
|
|
}
|
|
})
|
|
}
|
|
|
|
const realParamsCalculated = async (configObj, event) => {
|
|
let params = null
|
|
if (configObj.inputParamsCalculate) {
|
|
// 实时入参计算
|
|
// eslint-disable-next-line no-new-func
|
|
const func = new Function('deviceInfo', 'command', 'event', configObj.inputParamsCalculate)
|
|
if (!(window).globalDashboardDatas)
|
|
params = func(configObj.device, configObj.command, event)
|
|
|
|
else
|
|
params = func(configObj.device, configObj.command, event)
|
|
}
|
|
if (params.event && params.event instanceof PointerEvent) {
|
|
params.event = ''
|
|
}
|
|
return params
|
|
}
|
|
|
|
const commandNextHandlers = (lfInstance, configObj, refreshFuncs, event, response) => {
|
|
if (configObj.commandNext) {
|
|
configObj.commandNext.forEach((next) => {
|
|
if (next.value === 'refreshBoard') {
|
|
// 刷新看板
|
|
refreshFuncs.forEach((func) => {
|
|
func()
|
|
})
|
|
}
|
|
else if (next.value === 'operateVariable') {
|
|
operateVariableFunc(lfInstance, configObj, event, response)
|
|
}
|
|
else if (next.value === 'showOrHide') {
|
|
// eslint-disable-next-line no-new-func
|
|
const fn = new Function('e', configObj.showHideNewValue)
|
|
const comps = fn(event)
|
|
if (comps && comps.toString() === '[object Object]') {
|
|
for (const key in comps) {
|
|
const nodeModel = lfInstance.getNodeModelById(key)
|
|
nodeModel.visible = comps[key] === 'show'
|
|
if (nodeModel.properties.showHideContainChildren) {
|
|
const areaElements = window.calcInsideAreaElements(lfInstance, {
|
|
nodes: [nodeModel],
|
|
edges: [],
|
|
})
|
|
areaElements.forEach((cNode) => {
|
|
cNode.visible = nodeModel.visible
|
|
lfInstance.setProperties(cNode.id, {
|
|
visible: nodeModel.visible,
|
|
})
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
const sendCommand = async (event, lfInstance, configObj, refreshFuncs) => {
|
|
let headers = {}
|
|
configObj.requestUrl = configObj.requestUrl.replace('/thing', '')
|
|
if (configObj.requestHeader) {
|
|
headers = configObj.requestHeader.reduce((prev, curr) => {
|
|
prev[curr.key] = curr.value
|
|
return prev
|
|
}, {})
|
|
}
|
|
if (configObj.requestMethod === 'get') {
|
|
let params = {}
|
|
if (configObj.paramsTable) {
|
|
params = configObj.paramsTable.reduce((prev, curr) => {
|
|
prev[curr.key] = curr.value
|
|
return prev
|
|
}, {})
|
|
const realParams = realParamsCalculated(configObj, event)
|
|
if (realParams) {
|
|
params = realParams
|
|
}
|
|
}
|
|
|
|
service.get(configObj.requestUrl, params, headers).then((res) => {
|
|
if (res.code !== 0)
|
|
return messageFn(res.msg)
|
|
|
|
commandNextHandlers(lfInstance, configObj, refreshFuncs, event, res)
|
|
})
|
|
}
|
|
else if (configObj.requestMethod === 'post' || configObj.requestMethod === 'put') {
|
|
let params = configObj.bodyJson
|
|
const realParams = await realParamsCalculated(configObj, event)
|
|
if (realParams) {
|
|
params = realParams
|
|
}
|
|
|
|
const commandInfo = await service.get(`/device/control/${configObj.command.id}`);
|
|
|
|
if (commandInfo.code !== 0) {
|
|
return messageFn(res.msg)
|
|
}
|
|
const { userList } = commandInfo.data
|
|
const cacheToken = sessionStorage.getItem('v1@CacheToken');
|
|
if(!cacheToken) {
|
|
return window.createLoginDialog();
|
|
}
|
|
const tokenParsed = JSON.parse(cacheToken || '{}');
|
|
if (userList && !userList.includes(tokenParsed.userid)) {
|
|
return messageFn('无用户权限')
|
|
}
|
|
|
|
service[configObj.requestMethod](configObj.requestUrl, params, {
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
...headers,
|
|
},
|
|
}).then((res) => {
|
|
if (res.code !== 0)
|
|
return messageFn(res.msg)
|
|
|
|
messageFn(res.data)
|
|
commandNextHandlers(lfInstance, configObj, refreshFuncs, event, res)
|
|
}).catch(() => {})
|
|
}
|
|
}
|
|
|
|
const eventsHandler = (event, eventConfig, lfInstance, refreshFuncs) => {
|
|
const configObj = JSON.parse(eventConfig.config)
|
|
if (configObj.eventAction === 'openLink') {
|
|
let params = ''
|
|
if (configObj.dataPointParamsCalculate) {
|
|
let newDataPointTable = [];
|
|
if (configObj.dataPointTable) {
|
|
newDataPointTable = configObj.dataPointTable.map((param) => {
|
|
const dataPoint = JSON.parse(param.dataPoint || '{}')
|
|
return dataPoint
|
|
})
|
|
}
|
|
// eslint-disable-next-line no-new-func
|
|
const func = new Function('paramsList', newDataPointTable)
|
|
const returnParams = func()
|
|
if (Array.isArray(returnParams)) {
|
|
params = returnParams.reduce((prev, curr) => {
|
|
if (curr.key) {
|
|
return `${prev}${prev ? '&' : ''}` + `${curr.key}=${curr.value}`
|
|
}
|
|
return prev
|
|
}, '')
|
|
}
|
|
}
|
|
let linkAddress = `${configObj.linkAddress}${params}`
|
|
if (configObj.openMethod === 'openNewPage') {
|
|
if (!linkAddress.includes('http'))
|
|
linkAddress = `http://${linkAddress}`
|
|
|
|
window.open(linkAddress, '_blank', ' ')
|
|
}
|
|
else {
|
|
const origin = window.location.origin
|
|
if (linkAddress.includes(origin)) {
|
|
window.location.href = linkAddress
|
|
} else {
|
|
if (origin.includes('localhost'))
|
|
window.location.href = origin + linkAddress
|
|
else
|
|
window.location.href = `${origin}/scada${linkAddress}`
|
|
}
|
|
}
|
|
}
|
|
else if (configObj.eventAction === 'openDialog') {
|
|
window.layui.use('layer', () => {
|
|
const layer = window.layui.layer
|
|
const idx = layer.confirm(
|
|
configObj.dialogContent,
|
|
{
|
|
btn: ['确认', '取消'], // 按钮
|
|
},
|
|
() => {
|
|
sendCommand(event, lfInstance, configObj, refreshFuncs);
|
|
layer.closeAll()
|
|
},
|
|
() => {
|
|
layer.closeAll();
|
|
}
|
|
)
|
|
// 重新设置标题
|
|
layer.title(configObj.dialogTitle, idx)
|
|
})
|
|
}
|
|
else if (configObj.eventAction === 'sendCommand') {
|
|
if (configObj.sendCommandMethod === 'codes') {
|
|
// eslint-disable-next-line no-new-func
|
|
const func = new Function('context', 'service', 'command', 'event', configObj.commandContent)
|
|
func(lfInstance, service, configObj.selectCommand, event)
|
|
}
|
|
else {
|
|
sendCommand(event, lfInstance, configObj, refreshFuncs)
|
|
}
|
|
}
|
|
else if (configObj.eventAction === 'operateVariable') {
|
|
// 操作变量之前,获取实时入参计算
|
|
const realParams = realParamsCalculated(configObj, event)
|
|
if (realParams) {
|
|
configObj.globalInputParamsCalculated = realParams
|
|
}
|
|
operateVariableFunc(lfInstance, configObj, event, null)
|
|
}
|
|
else if (configObj.eventAction === 'showHide') {
|
|
// eslint-disable-next-line no-new-func
|
|
const fn = new Function('e', configObj.showHideNewValue)
|
|
const comps = fn(event)
|
|
if (comps && comps.toString() === '[object Object]') {
|
|
for (const key in comps) {
|
|
const nodeModel = lfInstance.getNodeModelById(key)
|
|
nodeModel.visible = comps[key] === 'show'
|
|
if (nodeModel.properties.showHideContainChildren) {
|
|
const areaElements = window.calcInsideAreaElements(lfInstance, {
|
|
nodes: [nodeModel],
|
|
edges: [],
|
|
})
|
|
areaElements.forEach((cNode) => {
|
|
cNode.visible = nodeModel.visible
|
|
lfInstance.setProperties(cNode.id, {
|
|
visible: nodeModel.visible,
|
|
})
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
const eventsHandlerDebounced = window.myDebounce(eventsHandler, 200)
|
|
|
|
var eventHandlers = (lfInstance, emit, refreshFuncs) => {
|
|
// 单个节点点击
|
|
lfInstance.on('myNode:click', ({ data, e }) => {
|
|
const eventsData = data.properties.dynamic?.eventsData?.eventCombo || []
|
|
const clickConfig = eventsData.find((ev) => ev.eventType === 'click')
|
|
if (!clickConfig)
|
|
return
|
|
if (clickConfig.enable && clickConfig.config)
|
|
eventsHandlerDebounced(e, clickConfig, lfInstance, refreshFuncs)
|
|
})
|
|
// 节点双击
|
|
lfInstance.on('myNode:dbclick', ({ data, e }) => {
|
|
const eventsData = data.properties.dynamic?.eventsData?.eventCombo || []
|
|
const clickConfig = eventsData.find((ev) => ev.eventType === 'dblClick')
|
|
if (!clickConfig)
|
|
return
|
|
if (clickConfig.enable && clickConfig.config)
|
|
eventsHandlerDebounced(e, clickConfig, lfInstance, refreshFuncs)
|
|
})
|
|
// 节点值改变
|
|
lfInstance.on('node:change', ({ data, e }) => {
|
|
const oldProps = lfInstance.getProperties(data.id)
|
|
lfInstance.setProperties(data.id, { event: e })
|
|
const { normalData } = oldProps.dynamic || {}
|
|
const { dataPoint } = normalData || {}
|
|
const eventsData = data.properties.dynamic?.eventsData?.eventCombo || []
|
|
const clickConfig = eventsData.find((ev) => ev.eventType === 'change')
|
|
if (!clickConfig)
|
|
return
|
|
if (clickConfig.enable && clickConfig.config) {
|
|
if (window.isJSON(clickConfig.config)) {
|
|
eventsHandlerDebounced(e, clickConfig, lfInstance, refreshFuncs)
|
|
} else {
|
|
// 自定义函数代码处理
|
|
// eslint-disable-next-line no-new-func
|
|
const handlerFn = new Function('context', 'service', 'nodeId', 'event', clickConfig.config)
|
|
try {
|
|
handlerFn(lfInstance, service, data.id, e)
|
|
} catch (err) {}
|
|
}
|
|
} else if (clickConfig.enable && !clickConfig.config) {
|
|
if (clickConfig.targetNode && clickConfig.apiId) {
|
|
const evVal = e.value || e;
|
|
const apiParams = window.totalApiParams[clickConfig.apiId] || {}
|
|
const requestParams = {
|
|
entitys: window._.cloneDeep(apiParams.entitys),
|
|
attrs: window._.cloneDeep(apiParams.attrs),
|
|
}
|
|
const isArr = Object.prototype.toString.call(evVal) === '[object Array]'
|
|
if (clickConfig.targetParamsType === 'entitys') {
|
|
if (!requestParams.entitys)
|
|
requestParams.entitys = {}
|
|
|
|
requestParams.entitys[clickConfig.targetParamsEntitys] = isArr ? evVal : [evVal]
|
|
lfInstance.setProperties(clickConfig.targetNode, {
|
|
requestParams,
|
|
apiId: clickConfig.apiId
|
|
})
|
|
}
|
|
else if (clickConfig.targetParamsType === 'attrs') {
|
|
if (!requestParams.attrs)
|
|
requestParams.attrs = {}
|
|
|
|
if (clickConfig.targetParamsAttrsType === 'all') {
|
|
requestParams.attrs.type = 'all'
|
|
if (clickConfig.targetParamsAttrsKeysEntitys === 'keys')
|
|
requestParams.attrs.keys = isArr ? evVal : [evVal]
|
|
}
|
|
else {
|
|
requestParams.attrs.type = 'split'
|
|
if (clickConfig.targetParamsAttrsKeysEntitys === 'entitys') {
|
|
const newKeys = isArr ? evVal : [evVal]
|
|
const findExactEntityAttrs = []
|
|
newKeys.forEach((k) => {
|
|
if (requestParams.attrs.entitys) {
|
|
requestParams.attrs.entitys.forEach((item) => {
|
|
if (item.entitys.includes(k)) {
|
|
findExactEntityAttrs.push({
|
|
entitys: [k],
|
|
keys: item.keys,
|
|
keyType: 'SQ',
|
|
})
|
|
}
|
|
})
|
|
}
|
|
})
|
|
requestParams.attrs.keys = findExactEntityAttrs
|
|
}
|
|
else if (clickConfig.targetParamsAttrsKeysEntitys === 'keys') {
|
|
const newKeys = isArr ? evVal : [evVal]
|
|
const findExactEntityAttrs = []
|
|
newKeys.forEach((k) => {
|
|
if (requestParams.attrs.keys) {
|
|
requestParams.attrs.keys.forEach((item) => {
|
|
if (item.keys.includes(k)) {
|
|
findExactEntityAttrs.push({
|
|
entitys: item.entitys,
|
|
keys: [k],
|
|
keyType: "SQ"
|
|
})
|
|
}
|
|
})
|
|
}
|
|
})
|
|
if (findExactEntityAttrs.length > 0) {
|
|
requestParams.attrs.keys = findExactEntityAttrs
|
|
} else {
|
|
requestParams.attrs.keys = newKeys
|
|
}
|
|
}
|
|
}
|
|
lfInstance.setProperties(clickConfig.targetNode, {
|
|
requestParams,
|
|
apiId: clickConfig.apiId
|
|
})
|
|
}
|
|
else if (clickConfig.targetParamsType === 'times') {
|
|
requestParams.times = {}
|
|
if (clickConfig.targetParamsTimesType === 'nearest') {
|
|
requestParams.times.nearest = evVal
|
|
}
|
|
else if (clickConfig.targetParamsTimesType === 'range') {
|
|
const isObj = typeof e === 'object'
|
|
if (isObj) {
|
|
requestParams.times.startTime = e.startTime || e.start || e.startDate
|
|
requestParams.times.endTime = e.endTime || e.end || e.endDate
|
|
}
|
|
}
|
|
else if (clickConfig.targetParamsTimesType === 'interval') {
|
|
const isObj = typeof e === 'object'
|
|
if (isObj) {
|
|
const { duration, type } = e
|
|
requestParams.times.duration = duration
|
|
requestParams.times.type = type
|
|
}
|
|
}
|
|
lfInstance.setProperties(clickConfig.targetNode, {
|
|
requestParams,
|
|
apiId: clickConfig.apiId
|
|
})
|
|
}
|
|
// 请求数据
|
|
// 给超级api 传参, 过滤出所需的物。数据源id 可以在看板左侧数据源那里复制得到。
|
|
service.get(`/v1/api/telemetryById?id=${clickConfig.apiId}&reqParams=${encodeURIComponent(JSON.stringify(requestParams))}`).then((res) => {
|
|
if (res.code === 0) {
|
|
if (res.data.result?.values) {
|
|
const resDatas = res.data.result.values
|
|
// 用返回值给目标部件赋值。目标部件的id 需替换成对应部件的id.
|
|
const properties = lfInstance.getProperties(clickConfig.targetNode)
|
|
if (!properties)
|
|
return
|
|
lfInstance.setProperties(clickConfig.targetNode, {
|
|
...properties,
|
|
apiid: clickConfig.apiId, // apiid 一定要传入
|
|
dynamic: {
|
|
...properties.dynamic,
|
|
normalData: {
|
|
...properties.dynamic.normalData,
|
|
defaultValue: JSON.stringify(resDatas), // 数据传入
|
|
},
|
|
},
|
|
})
|
|
}
|
|
}
|
|
})
|
|
|
|
// 更多界面操作
|
|
if (clickConfig.moreOperation) {
|
|
// eslint-disable-next-line no-new-func
|
|
const handlerFn = new Function('context', 'service', 'nodeId', 'event', clickConfig.moreOperation)
|
|
try {
|
|
handlerFn(lfInstance, service, data.id, e)
|
|
}
|
|
catch (err) {}
|
|
}
|
|
} else {
|
|
// 更多界面操作
|
|
if (clickConfig.moreOperation) {
|
|
// eslint-disable-next-line no-new-func
|
|
const handlerFn = new Function('context', 'service', 'nodeId', 'event', clickConfig.moreOperation)
|
|
try {
|
|
handlerFn(lfInstance, service, data.id, e)
|
|
}
|
|
catch (err) {}
|
|
}
|
|
}
|
|
}
|
|
})
|
|
// 节点进入
|
|
lfInstance.on('myNode:mouseenter', ({ data, e }) => {
|
|
const eventsData = data.properties.dynamic?.eventsData?.eventCombo || []
|
|
const clickConfig = eventsData.find((ev) => ev.eventType === 'mouseEnter')
|
|
if (!clickConfig)
|
|
return
|
|
if (clickConfig.enable && clickConfig.config)
|
|
eventsHandlerDebounced(e, clickConfig, lfInstance, refreshFuncs)
|
|
})
|
|
// 节点离开
|
|
lfInstance.on('myNode:mouseleave', ({ data, e }) => {
|
|
const eventsData = data.properties.dynamic?.eventsData?.eventCombo || []
|
|
const clickConfig = eventsData.find((ev) => ev.eventType === 'mouseLeave')
|
|
if (!clickConfig)
|
|
return
|
|
if (clickConfig.enable && clickConfig.config)
|
|
eventsHandlerDebounced(e, clickConfig, lfInstance, refreshFuncs)
|
|
})
|
|
}
|