Preview.vue 6.6 KB

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