app.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <template>
  2. <iframe class="external" :src="url" ref="iframeRef" v-if="url"></iframe>
  3. <div class="laser-layer" v-show="!url">
  4. <div class="scene-canvas" ref="fuseRef">
  5. <div id="direction"></div>
  6. </div>
  7. </div>
  8. </template>
  9. <script lang="ts">
  10. import { defineComponent, ref, watchEffect, computed, watch, nextTick } from 'vue'
  11. import { SceneType } from '@/store'
  12. import { params } from '@/env'
  13. import { fuseModel, modelProps } from './index'
  14. import { modelSDKFactory } from './platform'
  15. const typeChange = () => {
  16. const oldType = modelProps.type
  17. let stopWatch = null as unknown as () => void
  18. const typePromise = new Promise((_, reject) => {
  19. stopWatch = watchEffect(() => {
  20. if (modelProps.type !== oldType) {
  21. reject(new Error('当前模型未加载完已切换到下个'))
  22. stopWatch!()
  23. }
  24. })
  25. })
  26. return { typePromise, typeCleanup: stopWatch }
  27. }
  28. export const Model = defineComponent({
  29. name: 'model',
  30. setup() {
  31. const scene = computed(() => modelProps.type !== fuseModel && modelProps.type)
  32. const url = ref("")
  33. const setUrl = (newURL: string) => {
  34. if (newURL !== url.value) {
  35. setTimeout(() => {
  36. const hook = (iframeRef.value?.contentWindow as any)?.beforeDestroy
  37. console.error("当前iframe的beforeDestroy", hook)
  38. if (hook) {
  39. hook()
  40. console.log("赋值1")
  41. setTimeout(() => url.value = newURL, 300)
  42. } else {
  43. console.log("赋值2")
  44. url.value = newURL
  45. }
  46. })
  47. }
  48. }
  49. watchEffect(() => {
  50. if (!scene.value) {
  51. return setUrl("");
  52. }
  53. const type = scene.value.type
  54. const urls = {
  55. [SceneType.SWKK]: `/swkk/spg.html?m=${scene.value.num}`,
  56. [SceneType.SWKJ]: `/swkk/spg.html?m=${scene.value.num}`,
  57. [SceneType.SWSS]: `/swss/index.html?m=${scene.value.num}`,
  58. [SceneType.SWSSMX]: `/swkk/spg.html?m=${scene.value.num}`,
  59. [SceneType.SWMX]: `index.html?caseId=${params.caseId}&modelId=${scene.value.num}&share=1#sign-model`
  60. }
  61. setUrl(urls[type])
  62. })
  63. const fuseRef = ref<HTMLDivElement>()
  64. const iframeRef = ref<HTMLIFrameElement>()
  65. watch(
  66. () => [modelProps.type, url.value],
  67. async ([type, url], oldType, onCleanup) => {
  68. if (type !== fuseModel && !url) {
  69. return;
  70. }
  71. const callback = modelProps.callback
  72. await nextTick()
  73. const { typePromise, typeCleanup } = typeChange()
  74. const modelPromise = modelSDKFactory(type as any, type === fuseModel ? fuseRef.value! : iframeRef.value!)
  75. let result: any = null, error = null
  76. try {
  77. result = await Promise.race([typePromise, modelPromise])
  78. console.error(result)
  79. } catch (err: any) {
  80. error = err
  81. }
  82. typeCleanup()
  83. callback && callback(result, error)
  84. },
  85. { immediate: true, flush: 'post' }
  86. )
  87. return {
  88. iframeRef,
  89. fuseRef,
  90. url
  91. }
  92. }
  93. })
  94. export default Model
  95. </script>
  96. <style scoped lang="scss">
  97. .laser-layer {
  98. position: absolute;
  99. z-index: 1;
  100. left: 0;
  101. top: 0;
  102. width: 100%;
  103. height: 100%;
  104. .scene-canvas {
  105. width: 100%;
  106. height: 100%;
  107. background-color: #ccc;
  108. }
  109. }
  110. .external {
  111. position: absolute;
  112. z-index: 1;
  113. border: none;
  114. top: calc(var(--editor-head-height) + var(--header-top));
  115. left: 0;
  116. height: calc(100% - (var(--editor-head-height) + var(--header-top)));
  117. width: 100%;
  118. }
  119. #direction {
  120. right: calc(var(--editor-menu-right) + var(--editor-toolbox-width)) !important;
  121. top: calc(var(--header-top) + var(--editor-head-height)) !important;
  122. margin: 10px;
  123. transition: top .3s ease, right .3s ease;
  124. }
  125. </style>