actions.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // 组件配置转化
  2. // import _ from 'lodash'
  3. import cloneDeep from 'lodash/cloneDeep'
  4. import { setModules, dataModules } from 'data-room-ui/js/utils/configImport'
  5. import { getScreenInfo, getDataSetDetails, getDataByDataSetId } from '../api/bigScreenApi'
  6. import plotSettings from 'data-room-ui/G2Plots/settings'
  7. import echartSettings from 'data-room-ui/Echarts/settings'
  8. import { stringToFunction } from '../utils/evalFunctions'
  9. import { EventBus } from '../utils/eventBus'
  10. import plotList from 'data-room-ui/G2Plots/plotList'
  11. import { settingToTheme, themeToSetting } from 'data-room-ui/js/utils/themeFormatting'
  12. export default {
  13. // 初始化页面数据
  14. initLayout ({ commit, dispatch }, code) {
  15. return new Promise(resolve => {
  16. getScreenInfo(code).then(data => {
  17. // 配置兼容
  18. const pageInfo = handleResData(data)
  19. // 兼容边框配置
  20. pageInfo.chartList.forEach((chart) => {
  21. // 对数据源的方式进行兼容
  22. if (chart.dataSource && ['texts', 'numbers'].includes(chart.type)) {
  23. if (chart.dataSource.source === 'dataset' && (!chart.dataSource.businessKey)) {
  24. chart.dataSource.source = 'static'
  25. }
  26. } else if (!chart.dataSource.source) {
  27. chart.dataSource.source = 'dataset'
  28. }
  29. if (!chart.border) {
  30. chart.border = { type: '', titleHeight: 60, fontSize: 16, isTitle: true, padding: [0, 0, 0, 0] }
  31. }
  32. if (!chart.border.padding) {
  33. chart.border.padding = [0, 0, 0, 0]
  34. }
  35. const plotSettingsIterator = false
  36. const echartSettingsIterator = false
  37. if (chart.type === 'customComponent') {
  38. // 为本地G2组件配置添加迭代器
  39. if (!plotSettingsIterator) {
  40. plotSettings[Symbol.iterator] = function * () {
  41. const keys = Object.keys(plotSettings)
  42. for (const k of keys) {
  43. yield [k, plotSettings[k]]
  44. }
  45. }
  46. }
  47. for (const [key, localPlotSetting] of plotSettings) {
  48. if (chart.name == localPlotSetting.name) {
  49. // 本地配置项
  50. const localSettings = JSON.parse(JSON.stringify(localPlotSetting.setting))
  51. chart.setting = localSettings.map((localSet) => {
  52. // 在远程组件配置中找到 与 本地组件的配置项 相同的项索引
  53. const index = chart.setting.findIndex(remoteSet => remoteSet.field == localSet.field)
  54. if (index !== -1) {
  55. // 使用远程的值替换本地值
  56. localSet.field = chart.setting[index].field
  57. localSet.value = chart.setting[index].value
  58. }
  59. return localSet
  60. })
  61. }
  62. }
  63. } else if (chart.type === 'echartsComponent') {
  64. // 为本地echarts组件配置添加迭代器
  65. if (!echartSettingsIterator) {
  66. echartSettings[Symbol.iterator] = function * () {
  67. const keys = Object.keys(echartSettings)
  68. for (const k of keys) {
  69. yield [k, echartSettings[k]]
  70. }
  71. }
  72. }
  73. for (const [key, localPlotSetting] of echartSettings) {
  74. if (chart.name == localPlotSetting.name) {
  75. // 本地配置项
  76. const localSettings = JSON.parse(JSON.stringify(localPlotSetting.setting))
  77. chart.setting = localSettings.map((localSet) => {
  78. // 在远程组件配置中找到 与 本地组件的配置项 相同的项索引
  79. const index = chart.setting.findIndex(remoteSet => remoteSet.field == localSet.field)
  80. if (index !== -1) {
  81. // 使用远程的值替换本地值
  82. localSet.field = chart.setting[index].field
  83. localSet.value = chart.setting[index].value
  84. }
  85. return localSet
  86. })
  87. }
  88. }
  89. }
  90. })
  91. // 改变页面数据
  92. commit('changePageInfo', pageInfo)
  93. commit('changeZIndex', pageInfo.chartList)
  94. // 初始化缓存数据集数据
  95. // eslint-disable-next-line no-unused-expressions
  96. pageInfo.pageConfig.cacheDataSets?.map((cacheDataSet) => {
  97. dispatch('getCacheDataSetData', { dataSetId: cacheDataSet.dataSetId })
  98. dispatch('getCacheDataFields', { dataSetId: cacheDataSet.dataSetId })
  99. })
  100. // 页面加载成功
  101. resolve(true)
  102. commit('saveTimeLine', '初始化')
  103. })
  104. })
  105. },
  106. // 初始化缓存数据集数据
  107. getCacheDataSetData ({ commit, dispatch }, { dataSetId }) {
  108. getDataByDataSetId(dataSetId).then(res => {
  109. const data = res.data
  110. commit('changeCacheDataSetData', { dataSetId, data })
  111. // 推送数据到各个组件
  112. emitDataToChart(dataSetId, data)
  113. })
  114. },
  115. // 初始化缓存数据集字段
  116. getCacheDataFields ({ commit, dispatch }, { dataSetId }) {
  117. getDataSetDetails(dataSetId).then(data => {
  118. commit('changeCacheDataFields', { dataSetId, data })
  119. commit('changeCacheDataParams', { dataSetId, data })
  120. })
  121. }
  122. }
  123. // 处理后端返回的数据
  124. export function handleResData (data) {
  125. let pageInfo = {}
  126. if (data.pageConfig) {
  127. pageInfo = {
  128. ...data,
  129. pageConfig: {
  130. ...data.pageConfig,
  131. lightBgColor: data.pageConfig.lightBgColor || '#f5f7fa'
  132. }
  133. }
  134. } else {
  135. pageInfo = {
  136. ...data,
  137. pageConfig: {
  138. w: 1920,
  139. h: 1080,
  140. bgColor: '#151a26', // 背景色
  141. lightBgColor: '#f5f7fa',
  142. lightBg: '',
  143. bg: '', // 背景图
  144. customTheme: 'dark',
  145. opacity: 100
  146. }
  147. }
  148. }
  149. // 如果pageConfig中的cacheDataSets为null,赋值[]
  150. pageInfo.pageConfig.cacheDataSets = pageInfo.pageConfig.cacheDataSets || []
  151. pageInfo.pageConfig.refreshConfig = pageInfo.pageConfig.refreshConfig || []
  152. let originalConfig = {}
  153. pageInfo.chartList.forEach((chart) => {
  154. if (!['customComponent', 'remoteComponent', 'echartsComponent'].includes(chart.type)) {
  155. originalConfig = { option: { ...setModules[chart.type] }, ...dataModules[chart.type] }
  156. // 如果没有版本号,或者版本号修改了则需要进行旧数据兼容
  157. if ((!chart.version) || chart.version !== originalConfig.version) {
  158. chart = compatibility(chart, originalConfig)
  159. } else {
  160. chart.option = cloneDeep(setModules[chart.type])
  161. }
  162. } else {
  163. originalConfig = plotList?.find(plot => plot.name === chart.name)
  164. chart.option = stringToFunction(chart.option)
  165. // 如果是自定义组件,且没配数据集,就给前端的模拟数据
  166. if (!chart?.dataSource?.businessKey) {
  167. chart.option.data = plotList?.find(plot => plot.name === chart.name)?.option?.data || chart?.option?.data
  168. }
  169. // 如果没有版本号,或者版本号修改了则需要进行旧数据兼容
  170. if ((!chart.version) || (originalConfig && chart.version !== originalConfig?.version)) {
  171. // TODO 远程组件需要重新写处理函数
  172. if (chart.type === 'customComponent') {
  173. chart = compatibility(chart, originalConfig)
  174. }
  175. }
  176. }
  177. // 初始化时应该判断,是否存在theme配置,没有的话添加默认的两套主题,这是为了兼容旧组件
  178. if (!chart.theme) {
  179. chart.theme = settingToTheme(chart, 'dark')
  180. chart.theme = settingToTheme(chart, 'light')
  181. }
  182. chart.key = chart.code
  183. })
  184. // 主题兼容
  185. pageInfo.chartList = themeToSetting(pageInfo.chartList, pageInfo.pageConfig.customTheme)
  186. // 存储修改后的配置
  187. localStorage.setItem('pageInfo', JSON.stringify(pageInfo))
  188. return pageInfo
  189. }
  190. // 组件属性兼容
  191. function compatibility (config, originalConfig) {
  192. const newConfig = config
  193. newConfig.version = originalConfig?.version || '2023071001'
  194. newConfig.optionHandler = originalConfig.optionHandler || ''
  195. newConfig.dataSource = objCompare(newConfig?.dataSource, originalConfig?.dataSource)
  196. newConfig.customize = objCompare(newConfig?.customize, originalConfig?.customize)
  197. newConfig.option = {
  198. ...objCompare(newConfig.option, originalConfig?.option),
  199. displayOption: originalConfig?.option?.displayOption
  200. }
  201. newConfig.setting = arrCompare(newConfig?.setting, originalConfig?.setting)
  202. return newConfig
  203. }
  204. // 对象比较
  205. function objCompare (obj1, obj2) {
  206. const keys1 = obj1 ? Object.keys(obj1) : []
  207. const keys2 = obj2 ? Object.keys(obj2) : []
  208. // 交集
  209. const intersection = keys1?.filter(function (v) {
  210. return keys2.indexOf(v) > -1
  211. }) || []
  212. // 差集
  213. const differenceSet = keys2?.filter(function (v) {
  214. return keys1.indexOf(v) === -1
  215. }) || []
  216. const obj = {}
  217. for (const item of intersection) {
  218. obj[item] = obj1[item]
  219. }
  220. for (const item of differenceSet) {
  221. obj[item] = obj2[item]
  222. }
  223. return obj
  224. }
  225. // 数组比较
  226. function arrCompare (list1, list2) {
  227. const fieldList = list1?.map(item => item.field) || []
  228. const list = list2?.map(item => {
  229. let value = null
  230. // 如果存在交集
  231. if (fieldList.includes(item.field)) {
  232. // 保留旧数据的value
  233. value = (list1.filter(j => {
  234. return j.field === item.field
  235. }))[0].value
  236. } else {
  237. // 不存在交集,直接覆盖
  238. value = item.value
  239. }
  240. return {
  241. ...item,
  242. value
  243. }
  244. }) || []
  245. return list
  246. }
  247. // 推送数据到各个组件
  248. function emitDataToChart (dataSetId, data) {
  249. if (data && data.length) {
  250. EventBus.$emit('cacheDataInit',
  251. {
  252. success: true,
  253. data: data
  254. },
  255. dataSetId
  256. )
  257. }
  258. }