()
| 388 | return color; |
| 389 | }, |
| 390 | SSEBind() { |
| 391 | // 从SSE中获取数据 |
| 392 | if (this.EventSource || !this.basicConfig.SSEServer) return; |
| 393 | this.EventSource = new EventSource(this.basicConfig.SSEServer) |
| 394 | // this.EventSource.onerror = event => { |
| 395 | // this.EventSource.close() |
| 396 | // // this.Toast(item.name || `${item.type}连接中断`, 'warning') |
| 397 | // }; |
| 398 | this.EventSource.onmessage = async event => { |
| 399 | this.templateList.map(item => { |
| 400 | const { SSEDataPath, option, componentsType, SSEDataConversionFunction, dataBindType, dataIsAdd, elementId, dataMaxLength } = item.property |
| 401 | if (dataBindType.default === 'SSE') { // 配置了SSE |
| 402 | if (!SSEDataConversionFunction) { |
| 403 | this.dataBindPath(JSON.parse(event.data), SSEDataPath, item.property, dataIsAdd) |
| 404 | } else { |
| 405 | // 配置了数据转换器 |
| 406 | const func = new Function(`return ${SSEDataConversionFunction}`)() |
| 407 | const result = func(JSON.parse(event.data)) |
| 408 | if (componentsType === 'charts') { |
| 409 | if (dataIsAdd) { |
| 410 | result.forEach((da, daIndex) => { |
| 411 | if (!option.data[daIndex]) option.data[daIndex] = { data: [] } // 不存在初始化一个值防止报错 |
| 412 | option.data[daIndex].data = [...option.data[daIndex].data, ...da.data] |
| 413 | option.data[daIndex].name = da.name |
| 414 | option.data[daIndex].data = option.data[daIndex].data.slice(-dataMaxLength) // 截取后两千的数组 |
| 415 | }) |
| 416 | } else { |
| 417 | this.$set(option, 'data', result) |
| 418 | } |
| 419 | } else if (dataIsAdd) { |
| 420 | option.data.unshift(result) |
| 421 | option.data.length = 20 // 限制长度 防止无限增加 |
| 422 | this.$set(option, 'data', option.data) |
| 423 | } else { |
| 424 | this.$set(option, 'data', result) |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | // 让组件更新 |
| 429 | if (componentsType === 'charts') { |
| 430 | item.property.option = {...item.property.option } |
| 431 | } |
| 432 | |
| 433 | } |
| 434 | }) |
| 435 | }; |
| 436 | }, |
| 437 | dataBindPath(data, dataPath, item, isAdd = false) { |
| 438 | // 根据用户选择的映射字段 自定义映射数据 |
| 439 | const tempData = item.option.data |
nothing calls this directly
no outgoing calls
no test coverage detected