index.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <template>
  2. <div
  3. v-loading="loading"
  4. class="bs-remote-wrap"
  5. element-loading-text="远程组件加载中..."
  6. >
  7. <component
  8. :is="remoteComponent"
  9. :ref="'remoteComponent'+config.code"
  10. :config="config"
  11. @linkage="linkEvent"
  12. />
  13. </div>
  14. </template>
  15. <script>
  16. import linkageMixins from 'data-room-ui/js/mixins/linkageMixins'
  17. import commonMixins from 'data-room-ui/js/mixins/commonMixins'
  18. import remoteVueLoader from 'remote-vue-loader'
  19. import * as g2Plot from '@antv/g2plot'
  20. import * as _echarts from 'echarts'
  21. import { mapMutations, mapState } from 'vuex'
  22. import innerRemoteComponents, { getRemoteComponents } from 'data-room-ui/RemoteComponents/remoteComponentsList'
  23. import { getBizComponentInfo } from 'data-room-ui/js/api/bigScreenApi'
  24. import { settingToTheme } from 'data-room-ui/js/utils/themeFormatting'
  25. import cloneDeep from 'lodash/cloneDeep'
  26. export default {
  27. name: 'LcdpRemoteComponent',
  28. mixins: [linkageMixins, commonMixins],
  29. props: {
  30. config: {
  31. type: Object,
  32. default: () => ({})
  33. }
  34. },
  35. computed: {
  36. ...mapState('bigScreen', {
  37. pageInfo: state => state.pageInfo,
  38. customTheme: state => state.pageInfo.pageConfig.customTheme
  39. }),
  40. isPreview () {
  41. return (this.$route.path === window?.BS_CONFIG?.routers?.previewUrl) || (this.$route.path === '/big-screen/preview')
  42. }
  43. },
  44. data () {
  45. return {
  46. loading: false,
  47. remoteComponent: null,
  48. componentInstance: null
  49. }
  50. },
  51. watch: {
  52. },
  53. created () {
  54. this.getRemoteComponent()
  55. },
  56. mounted () {
  57. this.chartInit()
  58. },
  59. methods: {
  60. ...mapMutations('bigScreen', ['changeChartConfig']),
  61. // 尝试渲染远程文件或远程字符串
  62. getRemoteComponent () {
  63. this.loading = true
  64. // 1. 系统组件 通过配置获取
  65. if (this.config.customize.vueSysComponentDirName) {
  66. const remoteComponentList = [...innerRemoteComponents, ...getRemoteComponents()]
  67. // 获得系统组件最新的配置, 同步
  68. const config = remoteComponentList?.find(item => item.customize.vueSysComponentDirName === this.config.customize.vueSysComponentDirName)
  69. const vueFile = config?.customize?.vueFile
  70. const option = config?.option
  71. const setting = config?.setting
  72. // 同步配置
  73. this.synchConfig(option, setting)
  74. this.remoteComponent = vueFile
  75. this.loading = false
  76. return
  77. }
  78. // 2. 业务组件 通过请求获取
  79. if (this.config.customize.vueBizComponentCode) {
  80. getBizComponentInfo(this.config.customize.vueBizComponentCode).then(data => {
  81. const vueContent = data.vueContent
  82. const settingContent = data.settingContent
  83. if (!this.config?.option?.data) {
  84. this.resolveStrSetting(settingContent)
  85. this.config = this.dataFormatting(this.config, { success: false })
  86. }
  87. this.remoteComponent = remoteVueLoader('data:text/plain,' + encodeURIComponent(vueContent))
  88. }).finally(() => {
  89. this.loading = false
  90. })
  91. }
  92. },
  93. async chartInit () {
  94. let config = this.config
  95. config.g2Plots = g2Plot
  96. config.echarts = _echarts
  97. this.componentInstance = this.$refs['remoteComponent' + config.code]
  98. // key和code相等,说明是一进来刷新,调用list接口
  99. if (this.config.code === this.config.key || this.isPreview) {
  100. // 改变样式
  101. config = this.changeStyle(config) ? this.changeStyle(config) : config
  102. // 改变数据
  103. config = await this.changeDataByCode(config)
  104. } else {
  105. // 否则说明是更新,这里的更新只指更新数据(改变样式时是直接调取changeStyle方法),因为更新数据会改变key,调用chart接口
  106. // TODO 直接改变prop控制台会报错,待优化
  107. try {
  108. this.config = await this.changeData(config)
  109. } catch (e) {
  110. console.error(e)
  111. }
  112. }
  113. },
  114. linkEvent (formData) {
  115. this.linkage(formData)
  116. },
  117. /**
  118. * 处理当前组件的字符串配置
  119. */
  120. resolveStrSetting (settingContent) {
  121. // eslint-disable-next-line prefer-const
  122. let option = {}
  123. // eslint-disable-next-line prefer-const
  124. let setting = []
  125. // eslint-disable-next-line prefer-const, no-unused-vars
  126. let title = []
  127. // eslint-disable-next-line prefer-const, no-unused-vars
  128. let data = []
  129. // eslint-disable-next-line prefer-const
  130. settingContent = settingContent.replaceAll('const ', '')
  131. // 去掉 export default及后面代码
  132. settingContent = settingContent.replace(/export default[\s\S]*/, '')
  133. eval(settingContent)
  134. // this.config.option = {
  135. // ...this.config.option,
  136. // ...option
  137. // }
  138. this.config.option = {
  139. ...option,
  140. ...this.config.option
  141. }
  142. this.config.setting = setting
  143. // 样式改变时更新主题配置
  144. // this.config.theme = settingToTheme(cloneDeep(this.config), this.customTheme)
  145. // 获取到setting后将其转化为theme
  146. // this.config.theme = settingToTheme(this.config, this.customTheme)
  147. return {
  148. option,
  149. setting
  150. }
  151. },
  152. /**
  153. * 组件的配置
  154. * @returns {Promise<unknown>}
  155. */
  156. // 将config.setting的配置转化为option里的配置,这里之所以将转化的方法提出来,是因为在改变维度指标和样式的时候都需要转化
  157. transformSettingToOption (config, type) {
  158. let option = null
  159. config.setting.forEach(set => {
  160. if (set.optionField) {
  161. const optionField = set.optionField.split('.')
  162. option = config.option
  163. optionField.forEach((field, index) => {
  164. if (index === optionField.length - 1) {
  165. // 数据配置时,必须有值才更新
  166. if ((set.tabName === type && type === 'data' && set.value) || (set.tabName === type && type === 'custom')) {
  167. option[field] = set.value
  168. }
  169. } else {
  170. option = option[field]
  171. }
  172. })
  173. }
  174. })
  175. config.option = { ...config.option, ...option }
  176. return config
  177. },
  178. dataFormatting (config, data) {
  179. // 数据返回成功则赋值
  180. if (data.success) {
  181. data = data.data
  182. config = this.transformSettingToOption(config, 'data')
  183. // 获取到后端返回的数据,有则赋值
  184. const option = config.option
  185. const setting = config.setting
  186. if (config.dataHandler) {
  187. try {
  188. // 此处函数处理data
  189. eval(config.dataHandler)
  190. } catch (e) {
  191. console.error(e)
  192. }
  193. }
  194. config.option.data = data
  195. } else {
  196. // 数据返回失败则赋前端的模拟数据
  197. config.option.data = this.plotList?.find(plot => plot.name === config.name)?.option?.data || config?.option?.data
  198. }
  199. return config
  200. },
  201. // 组件的样式改变,返回改变后的config
  202. changeStyle (config, isUpdateTheme) {
  203. config = { ...this.config, ...config }
  204. config = this.transformSettingToOption(config, 'custom')
  205. // 这里定义了option和setting是为了保证在执行eval时,optionHandler、dataHandler里面可能会用到,
  206. const option = config.option
  207. const setting = config.setting
  208. if (this.config.optionHandler) {
  209. try {
  210. // 此处函数处理config
  211. eval(this.config.optionHandler)
  212. } catch (e) {
  213. console.error(e)
  214. }
  215. }
  216. // 只有样式改变时更新主题配置,切换主题时不需要保存
  217. if (!isUpdateTheme) {
  218. config.theme = settingToTheme(cloneDeep(config), this.customTheme)
  219. }
  220. if (this.chart) {
  221. this.chart.update(config.option)
  222. }
  223. this.changeChartConfig(config)
  224. if (config.code === this.activeCode) {
  225. this.changeActiveItemConfig(config)
  226. }
  227. // this.changeChartConfig(config)
  228. // 在this.$refs['remoteComponent' + config.code]这个实例里判断是否存在customStyle方法,如果存在则执行
  229. const componentInstance = this.$refs['remoteComponent' + config.code]
  230. if (componentInstance && componentInstance.$options.methods && componentInstance.$options.methods.customStyle) {
  231. // 调用 customStyle 方法
  232. this.$refs['remoteComponent' + config.code]?.customStyle(config)
  233. }
  234. return config
  235. },
  236. // 同步配置
  237. synchConfig (option, setting) {
  238. // 对比this.config.setting 和 setting,进行合并,数据以this.config.option对象的value为准
  239. // TODO
  240. }
  241. }
  242. }
  243. </script>
  244. <style lang="scss" scoped>
  245. .bs-remote-wrap {
  246. width: 100%;
  247. }
  248. </style>