index.vue 7.6 KB

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