Preview.vue 6.3 KB

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