index.vue 7.2 KB

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