app.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. if (hook) {
  38. try {
  39. hook()
  40. } catch(e) {
  41. console.error(e)
  42. }
  43. url.value = ""
  44. setTimeout(() => url.value = newURL, 300)
  45. } else {
  46. url.value = newURL
  47. }
  48. })
  49. }
  50. }
  51. watchEffect(() => {
  52. if (!scene.value) {
  53. return setUrl("");
  54. }
  55. const type = scene.value.type
  56. const urls = {
  57. [SceneType.SWKK]: `/swkk/spg.html?m=${scene.value.num}`,
  58. [SceneType.SWKJ]: `/swkk/spg.html?m=${scene.value.num}`,
  59. [SceneType.SWSS]: `/swss/index.html?m=${scene.value.num}`,
  60. [SceneType.SWSSMX]: `/swkk/spg.html?m=${scene.value.num}`,
  61. [SceneType.SWMX]: `index.html?caseId=${params.caseId}&app=${params.app}&modelId=${scene.value.num}&share=1#sign-model`,
  62. [SceneType.SWYDSS]: `/swss/index.html?m=${scene.value.num}`,
  63. [SceneType.SWYDMX]: `/swkk/spg.html?m=${scene.value.num}`,
  64. }
  65. setUrl(urls[type])
  66. })
  67. const fuseRef = ref<HTMLDivElement>()
  68. const iframeRef = ref<HTMLIFrameElement>()
  69. watch(
  70. () => [modelProps.type, url.value],
  71. async ([type, url], oldType, onCleanup) => {
  72. if (type !== fuseModel && !url) {
  73. return;
  74. }
  75. const callback = modelProps.callback
  76. await nextTick()
  77. const { typePromise, typeCleanup } = typeChange()
  78. const modelPromise = modelSDKFactory(type as any, type === fuseModel ? fuseRef.value! : iframeRef.value!)
  79. let result: any = null, error = null
  80. try {
  81. result = await Promise.race([typePromise, modelPromise])
  82. } catch (err: any) {
  83. error = err
  84. }
  85. typeCleanup()
  86. callback && callback(result, error)
  87. },
  88. { immediate: true, flush: 'post' }
  89. )
  90. // 处理iframe 定制页面
  91. watch(
  92. () => [scene.value && scene.value.type, url.value],
  93. ([type], oldType, onCleanup) => {
  94. if (type === false) {
  95. // 手动渲染融合场景
  96. console.log("手动渲染!");
  97. setTimeout(() => {
  98. (window as any).viewer.setDisplay(true)
  99. }, 100)
  100. };
  101. const interval = setInterval(async () => {
  102. const doc = iframeRef.value?.contentWindow?.document!
  103. if (!doc || !doc.querySelector("div")) return;
  104. console.error(doc, doc.head)
  105. const target = doc.head
  106. clearInterval(interval);
  107. ;(window as any).iframeCreated && (window as any).iframeCreated(iframeRef.value);
  108. if (type === SceneType.SWSS) {
  109. const $style = document.createElement("style");
  110. $style.type = "text/css";
  111. var textNode = document.createTextNode(`
  112. .mode-tab > .model-mode-tab.strengthen {
  113. display: none !important;
  114. }
  115. `);
  116. $style.appendChild(textNode);
  117. target.appendChild($style);
  118. console.error(target, $style)
  119. }
  120. }, 16);
  121. onCleanup(() => clearInterval(interval));
  122. },
  123. {flush: 'post', immediate: true}
  124. )
  125. return {
  126. iframeRef,
  127. fuseRef,
  128. url
  129. }
  130. }
  131. })
  132. export default Model
  133. </script>
  134. <style scoped lang="scss">
  135. .laser-layer {
  136. position: absolute;
  137. z-index: 1;
  138. left: 0;
  139. top: 0;
  140. width: 100%;
  141. height: 100%;
  142. .scene-canvas {
  143. width: 100%;
  144. height: 100%;
  145. background-color: #ccc;
  146. }
  147. }
  148. .external {
  149. position: absolute;
  150. z-index: 1;
  151. border: none;
  152. top: calc(var(--editor-head-height) + var(--header-top));
  153. left: 0;
  154. height: calc(100% - (var(--editor-head-height) + var(--header-top)));
  155. width: 100%;
  156. }
  157. #direction {
  158. right: calc(var(--editor-menu-right) + var(--editor-toolbox-width)) !important;
  159. top: calc(var(--header-top) + var(--editor-head-height)) !important;
  160. margin: 10px;
  161. transition: top .3s ease, right .3s ease;
  162. }
  163. </style>