Preview.vue 6.2 KB

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