index.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. changeData (config, filterList) {
  61. // 当前组件的方法调用
  62. let remote = this.$refs['remoteComponent'+config.code]
  63. if (remote && remote.changeData){
  64. remote.changeData(config, filterList)
  65. }
  66. // 调用混入中的方法
  67. return this.$options.mixins[1].methods.changeData.call(this, config, filterList)
  68. },
  69. ...mapMutations('bigScreen', ['changeChartConfig']),
  70. // 尝试渲染远程文件或远程字符串
  71. getRemoteComponent () {
  72. this.loading = true
  73. // 1. 系统组件 通过配置获取
  74. if (this.config.customize.vueSysComponentDirName) {
  75. const remoteComponentList = [...innerRemoteComponents, ...getRemoteComponents()]
  76. // 获得系统组件最新的配置, 同步
  77. const config = remoteComponentList?.find(item => item.customize.vueSysComponentDirName === this.config.customize.vueSysComponentDirName)
  78. const vueFile = config?.customize?.vueFile
  79. const option = config?.option
  80. const setting = config?.setting
  81. // 同步配置
  82. this.synchConfig(option, setting)
  83. this.remoteComponent = vueFile
  84. this.loading = false
  85. return
  86. }
  87. // 2. 业务组件 通过请求获取
  88. if (this.config.customize.vueBizComponentCode) {
  89. getBizComponentInfo(this.config.customize.vueBizComponentCode).then(data => {
  90. const vueContent = data.vueContent
  91. const settingContent = data.settingContent
  92. if (!this.config?.option?.data) {
  93. this.resolveStrSetting(settingContent)
  94. this.config = this.dataFormatting(this.config, { success: false })
  95. }
  96. this.remoteComponent = remoteVueLoader('data:text/plain,' + encodeURIComponent(vueContent))
  97. }).finally(() => {
  98. this.loading = false
  99. })
  100. }
  101. },
  102. async chartInit () {
  103. let config = this.config
  104. config.g2Plots = g2Plot
  105. config.echarts = _echarts
  106. this.componentInstance = this.$refs['remoteComponent' + config.code]
  107. // key和code相等,说明是一进来刷新,调用list接口
  108. if (this.config.code === this.config.key || this.isPreview) {
  109. // 改变样式
  110. config = this.changeStyle(config) ? this.changeStyle(config) : config
  111. // 改变数据
  112. config = await this.changeDataByCode(config)
  113. } else {
  114. // 否则说明是更新,这里的更新只指更新数据(改变样式时是直接调取changeStyle方法),因为更新数据会改变key,调用chart接口
  115. // TODO 直接改变prop控制台会报错,待优化
  116. try {
  117. this.config = await this.changeData(config)
  118. } catch (e) {
  119. console.error(e)
  120. }
  121. }
  122. },
  123. linkEvent (formData) {
  124. this.linkage(formData)
  125. },
  126. /**
  127. * 处理当前组件的字符串配置
  128. */
  129. resolveStrSetting (settingContent) {
  130. // eslint-disable-next-line prefer-const
  131. let option = {}
  132. // eslint-disable-next-line prefer-const
  133. let setting = []
  134. // eslint-disable-next-line prefer-const, no-unused-vars
  135. let title = []
  136. // eslint-disable-next-line prefer-const, no-unused-vars
  137. let data = []
  138. // eslint-disable-next-line prefer-const, no-unused-vars
  139. let optionHandler = ''
  140. // eslint-disable-next-line prefer-const
  141. settingContent = settingContent.replaceAll('const ', '')
  142. // 去掉 export default及后面代码
  143. settingContent = settingContent.replace(/export default[\s\S]*/, '')
  144. eval(settingContent)
  145. // this.config.option = {
  146. // ...this.config.option,
  147. // ...option
  148. // }
  149. this.config.option = {
  150. ...option,
  151. ...this.config.option
  152. }
  153. this.config.setting = setting
  154. // 样式改变时更新主题配置
  155. // this.config.theme = settingToTheme(cloneDeep(this.config), this.customTheme)
  156. // 获取到setting后将其转化为theme
  157. // this.config.theme = settingToTheme(this.config, this.customTheme)
  158. return {
  159. option,
  160. setting,
  161. optionHandler
  162. }
  163. },
  164. /**
  165. * 组件的配置
  166. * @returns {Promise<unknown>}
  167. */
  168. // 将config.setting的配置转化为option里的配置,这里之所以将转化的方法提出来,是因为在改变维度指标和样式的时候都需要转化
  169. transformSettingToOption (config, type) {
  170. let option = null
  171. config.setting.forEach(set => {
  172. if (set.optionField) {
  173. const optionField = set.optionField.split('.')
  174. option = config.option
  175. optionField.forEach((field, index) => {
  176. if (index === optionField.length - 1) {
  177. // 数据配置时,必须有值才更新
  178. if ((set.tabName === type && type === 'data' && set.value) || (set.tabName === type && type === 'custom')) {
  179. option[field] = set.value
  180. }
  181. } else {
  182. // 如果没有这个属性,则创建该属性,并赋值为空对值
  183. if (!option[field]) {
  184. option[field] = {}
  185. }
  186. option = option[field]
  187. }
  188. })
  189. }
  190. })
  191. return config
  192. },
  193. dataFormatting (config, data) {
  194. // 数据返回成功则赋值
  195. if (data.success) {
  196. data = data.data
  197. config = this.transformSettingToOption(config, 'data')
  198. // 获取到后端返回的数据,有则赋值
  199. const option = config.option
  200. const setting = config.setting
  201. if (config.dataHandler) {
  202. try {
  203. // 此处函数处理data
  204. eval(config.dataHandler)
  205. } catch (e) {
  206. console.error(e)
  207. }
  208. }
  209. config.option.data = data
  210. } else {
  211. // 数据返回失败则赋前端的模拟数据
  212. config.option.data = this.plotList?.find(plot => plot.name === config.name)?.option?.data || config?.option?.data
  213. }
  214. return config
  215. },
  216. // 组件的样式改变,返回改变后的config
  217. changeStyle (config, isUpdateTheme) {
  218. config = { ...this.config, ...config }
  219. config = this.transformSettingToOption(config, 'custom')
  220. // 这里定义了option和setting是为了保证在执行eval时,optionHandler、dataHandler里面可能会用到,
  221. const option = config.option
  222. const setting = config.setting
  223. if (this.config.optionHandler) {
  224. try {
  225. // 此处函数处理config
  226. eval(this.config.optionHandler)
  227. } catch (e) {
  228. console.error(e)
  229. }
  230. }
  231. // 只有样式改变时更新主题配置,切换主题时不需要保存
  232. if (!isUpdateTheme) {
  233. config.theme = settingToTheme(cloneDeep(config), this.customTheme)
  234. }
  235. if (this.chart) {
  236. this.chart.update(config.option)
  237. }
  238. this.changeChartConfig(config)
  239. if (config.code === this.activeCode) {
  240. this.changeActiveItemConfig(config)
  241. }
  242. // this.changeChartConfig(config)
  243. // 在this.$refs['remoteComponent' + config.code]这个实例里判断是否存在customStyle方法,如果存在则执行
  244. const componentInstance = this.$refs['remoteComponent' + config.code]
  245. if (componentInstance && componentInstance.$options.methods && componentInstance.$options.methods.customStyle) {
  246. // 调用 customStyle 方法
  247. this.$refs['remoteComponent' + config.code]?.customStyle(config)
  248. }
  249. return config
  250. },
  251. // 同步配置
  252. synchConfig (option, setting) {
  253. // 对比this.config.setting 和 setting,进行合并,数据以this.config.option对象的value为准
  254. // TODO
  255. }
  256. }
  257. }
  258. </script>
  259. <style lang="scss" scoped>
  260. .bs-remote-wrap {
  261. width: 100%;
  262. height: 100%;
  263. }
  264. </style>