index.vue 8.4 KB

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