Preview.vue 6.1 KB

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