index.vue 8.8 KB

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