Preview.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <template>
  2. <div
  3. v-loading="loading"
  4. class="bs-remote-preview"
  5. element-loading-text="远程组件加载中..."
  6. >
  7. <div class="remote-preview-inner-wrap">
  8. <component
  9. :is="remoteComponent"
  10. :config="config"
  11. />
  12. </div>
  13. </div>
  14. </template>
  15. <script>
  16. import remoteVueLoader from 'remote-vue-loader'
  17. import * as _echarts from 'echarts'
  18. import * as g2Plot from '@antv/g2plot'
  19. import { getBizComponentInfo } from 'data-room-ui/js/api/bigScreenApi'
  20. import innerRemoteComponents, { getRemoteComponents } from 'data-room-ui/RemoteComponents/remoteComponentsList'
  21. export default {
  22. name: 'BsComponentPreview',
  23. props: {
  24. vueContent: {
  25. type: String,
  26. default: ''
  27. },
  28. settingContent: {
  29. type: String,
  30. default: ''
  31. }
  32. },
  33. computed: {
  34. config: {
  35. get () {
  36. // eslint-disable-next-line prefer-const
  37. let option = {}
  38. // eslint-disable-next-line prefer-const
  39. let setting = []
  40. // eslint-disable-next-line prefer-const, no-unused-vars
  41. let title = ''
  42. let chartType = ''
  43. // eslint-disable-next-line prefer-const, no-unused-vars
  44. let data = []
  45. let g2Plots=g2Plot
  46. let echarts = _echarts
  47. // eslint-disable-next-line prefer-const
  48. let settingContent = this.settingContentInner?.replaceAll('const ', '')
  49. // 去掉 export default及后面代码
  50. settingContent = settingContent?.replace(/export default[\s\S]*/, '')
  51. eval(settingContent)
  52. return {
  53. title,
  54. option,
  55. setting,
  56. echarts,
  57. g2Plots,
  58. chartType
  59. }
  60. },
  61. set (val) {}
  62. }
  63. },
  64. watch: {
  65. settingContentInner () {
  66. this.getRemoteComponent()
  67. },
  68. vueContentInner () {
  69. this.getRemoteComponent()
  70. },
  71. vueContent (newVal) {
  72. this.vueContentInner = newVal
  73. },
  74. settingContent (newVal) {
  75. this.settingContentInner = newVal
  76. }
  77. },
  78. data () {
  79. return {
  80. loading: false,
  81. remoteComponent: null,
  82. vueContentInner: this.vueContent,
  83. settingContentInner: this.settingContent?.replaceAll('const ', ''),
  84. }
  85. },
  86. created () {
  87. this.viewComponent()
  88. },
  89. methods: {
  90. async viewComponent () {
  91. // 如果有编码,则获取组件信息
  92. if (this.$route.query?.code) {
  93. const data = await getBizComponentInfo(this.$route.query?.code)
  94. this.vueContentInner = data.vueContent
  95. this.settingContentInner = data.settingContent
  96. this.config = this.dataFormatting(this.config)
  97. this.remoteComponent = remoteVueLoader('data:text/plain,' + encodeURIComponent(this.vueContentInner))
  98. this.loading = false
  99. }
  100. // 如果有组件的dirName,则获取系统组件信息
  101. if (this.$route.query?.dirName) {
  102. const dirName = this.$route.query?.dirName
  103. const remoteComponentList = [...innerRemoteComponents, ...getRemoteComponents()]
  104. const config = remoteComponentList?.find(item => item.customize.vueSysComponentDirName === dirName)
  105. this.config.option = config?.option
  106. this.config.title = config?.title
  107. const vueFile = config.customize?.vueFile
  108. this.remoteComponent = vueFile
  109. this.loading = false
  110. }
  111. },
  112. // 尝试渲染远程文件或远程字符串
  113. getRemoteComponent () {
  114. this.loading = true
  115. this.config = this.dataFormatting(this.config, { success: false })
  116. this.remoteComponent = remoteVueLoader('data:text/plain,' + encodeURIComponent(this.vueContentInner))
  117. this.loading = false
  118. },
  119. /**
  120. * 组件的配置
  121. * @returns {Promise<unknown>}
  122. */
  123. // 将config.setting的配置转化为option里的配置,这里之所以将转化的方法提出来,是因为在改变维度指标和样式的时候都需要转化
  124. transformSettingToOption (config, type) {
  125. let option = null
  126. config.setting.forEach(set => {
  127. if (set.optionField) {
  128. const optionField = set.optionField.split('.')
  129. option = config.option
  130. optionField.forEach((field, index) => {
  131. if (index === optionField.length - 1) {
  132. // 数据配置时,必须有值才更新
  133. if ((set.tabName === type && type === 'data' && set.value) || (set.tabName === type && type === 'custom')) {
  134. option[field] = set.value
  135. }
  136. } else {
  137. option = option[field]
  138. }
  139. })
  140. }
  141. })
  142. config.option = { ...config.option, ...option }
  143. return config
  144. },
  145. dataFormatting (config, data) {
  146. // 数据返回成功则赋值
  147. if (data?.success) {
  148. data = data.data
  149. config = this.transformSettingToOption(config, 'data')
  150. // 获取到后端返回的数据,有则赋值
  151. // const option = config.option
  152. // const setting = config.setting
  153. if (config.dataHandler) {
  154. try {
  155. // 此处函数处理data
  156. eval(config.dataHandler)
  157. } catch (e) {
  158. console.error(e)
  159. }
  160. }
  161. config.option.data = data
  162. } else {
  163. // 数据返回失败则赋前端的模拟数据
  164. config.option.data = this.plotList?.find(plot => plot.name === config.name)?.option?.data || config.option.data
  165. }
  166. return config
  167. },
  168. // 组件的样式改变,返回改变后的config
  169. changeStyle (config) {
  170. config = { ...this.config, ...config }
  171. config = this.transformSettingToOption(config, 'custom')
  172. // 这里定义了option和setting是为了保证在执行eval时,optionHandler、dataHandler里面可能会用到,
  173. // const option = config.option
  174. // const setting = config.setting
  175. if (this.config.optionHandler) {
  176. try {
  177. // 此处函数处理config
  178. eval(this.config.optionHandler)
  179. } catch (e) {
  180. console.error(e)
  181. }
  182. }
  183. if (this.chart) {
  184. this.chart.update(config.option)
  185. }
  186. this.changeChartConfig(config)
  187. return config
  188. }
  189. }
  190. }
  191. </script>
  192. <style lang="scss" scoped>
  193. .bs-remote-preview {
  194. // position: absolute;
  195. height: 88%;
  196. min-width: 100%;
  197. overflow: hidden;
  198. box-sizing: border-box;
  199. .remote-preview-inner-wrap {
  200. // position: absolute;
  201. height: 100%;
  202. width: 100%;
  203. // overflow: auto;
  204. padding: 20px;
  205. background-color: var(--bs-background-1);
  206. }
  207. }
  208. </style>