app.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 = computed(() => {
  33. if (!scene.value) return;
  34. const type = scene.value.type
  35. const urls = {
  36. [SceneType.SWKK]: `/swkk/spg.html?m=${scene.value.num}`,
  37. [SceneType.SWKJ]: `/swkk/spg.html?m=${scene.value.num}`,
  38. [SceneType.SWSS]: `/swss/index.html?m=${scene.value.num}`,
  39. [SceneType.SWSSMX]: `/swkk/spg.html?m=${scene.value.num}`,
  40. [SceneType.SWMX]: `index.html?caseId=${params.caseId}&modelId=${scene.value.num}&share=1#sign-model`
  41. }
  42. return urls[type]
  43. })
  44. const fuseRef = ref<HTMLDivElement>()
  45. const iframeRef = ref<HTMLIFrameElement>()
  46. watch(
  47. () => modelProps.type,
  48. async (type, oldType, onCleanup) => {
  49. const callback = modelProps.callback
  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. .laser-layer {
  75. position: absolute;
  76. z-index: 1;
  77. left: 0;
  78. top: 0;
  79. width: 100%;
  80. height: 100%;
  81. .scene-canvas {
  82. width: 100%;
  83. height: 100%;
  84. background-color: #ccc;
  85. }
  86. }
  87. .external {
  88. position: absolute;
  89. z-index: 1;
  90. border: none;
  91. top: calc(var(--editor-head-height) + var(--header-top));
  92. left: 0;
  93. height: calc(100% - (var(--editor-head-height) + var(--header-top)));
  94. width: 100%;
  95. }
  96. #direction {
  97. right: calc(var(--editor-menu-right) + var(--editor-toolbox-width)) !important;
  98. top: calc(var(--header-top) + var(--editor-head-height)) !important;
  99. margin: 10px;
  100. transition: top .3s ease, right .3s ease;
  101. }
  102. </style>