Preview.vue 6.1 KB

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