app.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. watchEffect(() => {
  34. if (!scene.value) return;
  35. const type = scene.value.type
  36. const urls = {
  37. [SceneType.SWKK]: `/swkk/spg.html?m=${scene.value.num}`,
  38. [SceneType.SWKJ]: `/swkk/spg.html?m=${scene.value.num}`,
  39. [SceneType.SWSS]: `/swss/index.html?m=${scene.value.num}`,
  40. [SceneType.SWSSMX]: `/swkk/spg.html?m=${scene.value.num}`,
  41. [SceneType.SWMX]: `index.html?caseId=${params.caseId}&modelId=${scene.value.num}&share=1#sign-model`
  42. }
  43. if (urls[type] !== url.value) {
  44. setTimeout(() => {
  45. const hook = (iframeRef.value?.contentWindow as any)?.beforeDestroy
  46. console.log(hook)
  47. if (hook) {
  48. hook()
  49. setTimeout(() => url.value = urls[type], 300)
  50. } else {
  51. url.value = urls[type]
  52. }
  53. })
  54. }
  55. })
  56. const fuseRef = ref<HTMLDivElement>()
  57. const iframeRef = ref<HTMLIFrameElement>()
  58. watch(
  59. () => [modelProps.type, url.value],
  60. async ([type, url], oldType, onCleanup) => {
  61. if (type !== fuseModel && !url) {
  62. return;
  63. }
  64. const callback = modelProps.callback
  65. await nextTick()
  66. const { typePromise, typeCleanup } = typeChange()
  67. const modelPromise = modelSDKFactory(type, type === fuseModel ? fuseRef.value! : iframeRef.value!)
  68. let result: any = null, error = null
  69. try {
  70. result = await Promise.race([typePromise, modelPromise])
  71. } catch (err: any) {
  72. error = err
  73. }
  74. typeCleanup()
  75. callback && callback(result, error)
  76. },
  77. { immediate: true, flush: 'post' }
  78. )
  79. return {
  80. iframeRef,
  81. fuseRef,
  82. url
  83. }
  84. }
  85. })
  86. export default Model
  87. </script>
  88. <style scoped lang="scss">
  89. .laser-layer {
  90. position: absolute;
  91. z-index: 1;
  92. left: 0;
  93. top: 0;
  94. width: 100%;
  95. height: 100%;
  96. .scene-canvas {
  97. width: 100%;
  98. height: 100%;
  99. background-color: #ccc;
  100. }
  101. }
  102. .external {
  103. position: absolute;
  104. z-index: 1;
  105. border: none;
  106. top: calc(var(--editor-head-height) + var(--header-top));
  107. left: 0;
  108. height: calc(100% - (var(--editor-head-height) + var(--header-top)));
  109. width: 100%;
  110. }
  111. #direction {
  112. right: calc(var(--editor-menu-right) + var(--editor-toolbox-width)) !important;
  113. top: calc(var(--header-top) + var(--editor-head-height)) !important;
  114. margin: 10px;
  115. transition: top .3s ease, right .3s ease;
  116. }
  117. </style>