index.vue 8.3 KB

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