app.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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"></div>
  5. </div>
  6. </template>
  7. <script lang="ts">
  8. import { defineComponent, ref, watchEffect, computed, watch, onMounted, nextTick } from 'vue'
  9. import { SceneType } from '@/store'
  10. import { params, showModelsMapStack } from '@/env'
  11. import { fuseModel, modelProps } from './index'
  12. import { modelSDKFactory } from './platform'
  13. const typeChange = () => {
  14. const oldType = modelProps.type
  15. let stopWatch = null as unknown as () => void
  16. const typePromise = new Promise((_, reject) => {
  17. stopWatch = watchEffect(() => {
  18. if (modelProps.type !== oldType) {
  19. reject(new Error('当前模型未加载完已切换到下个'))
  20. stopWatch!()
  21. }
  22. })
  23. })
  24. return { typePromise, typeCleanup: stopWatch }
  25. }
  26. export const Model = defineComponent({
  27. name: 'model',
  28. setup() {
  29. const scene = computed(() => modelProps.type !== fuseModel && modelProps.type)
  30. const url = computed(() => {
  31. if (!scene.value) return;
  32. const type = scene.value.type
  33. const urls = {
  34. [SceneType.SWKK]: `/swkk/spg.html?m=${scene.value.num}`,
  35. [SceneType.SWKJ]: `/swkk/spg.html?m=${scene.value.num}`,
  36. [SceneType.SWSS]: `/swss/index.html?m=${scene.value.num}`,
  37. [SceneType.SWMX]: `index.html?caseId=${params.caseId}&modelId=${scene.value.num}&token=${params.token}#sign-model`
  38. }
  39. return urls[type]
  40. })
  41. const fuseRef = ref<HTMLDivElement>()
  42. const iframeRef = ref<HTMLIFrameElement>()
  43. watch(
  44. () => modelProps.type,
  45. async (type, oldType, onCleanup) => {
  46. const callback = modelProps.callback
  47. if (oldType === fuseModel) {
  48. onCleanup(showModelsMapStack.push(ref(new Map())))
  49. }
  50. await nextTick()
  51. const { typePromise, typeCleanup } = typeChange()
  52. const modelPromise = modelSDKFactory(type, type === fuseModel ? fuseRef.value! : iframeRef.value!)
  53. let result: any = null, error = null
  54. try {
  55. result = await Promise.race([typePromise, modelPromise])
  56. } catch (err: any) {
  57. error = err
  58. }
  59. typeCleanup()
  60. callback && callback(result, error)
  61. },
  62. { immediate: true, flush: 'post' }
  63. )
  64. return {
  65. iframeRef,
  66. fuseRef,
  67. url
  68. }
  69. }
  70. })
  71. export default Model
  72. </script>
  73. <style scoped lang="scss">
  74. .external,
  75. .laser-layer {
  76. position: absolute;
  77. z-index: 1;
  78. left: 0;
  79. top: 0;
  80. width: 100%;
  81. height: 100%;
  82. .scene-canvas {
  83. width: 100%;
  84. height: 100%;
  85. background-color: #ccc;
  86. }
  87. }
  88. .external {
  89. border: none;
  90. }
  91. </style>