Browse Source

fix: 添加必须qpi

bill 1 year ago
parent
commit
9556805803
2 changed files with 20 additions and 4 deletions
  1. 18 3
      src/openSDK.ts
  2. 2 1
      src/views/scene/photo.vue

+ 18 - 3
src/openSDK.ts

@@ -1,4 +1,4 @@
-import { nextTick, reactive, ref, watchEffect } from "vue";
+import { computed, nextTick, reactive, ref, watchEffect } from "vue";
 import { FixPoint, fixPoints } from "./store/fixPoint";
 import { baseLines } from "./store/baseLine";
 import { MeasureAtom, MeasuresRaw, Pos, Pos3D } from "./sdk";
@@ -6,6 +6,8 @@ import { list } from "./store/measure";
 import { PhotoRaw } from "./store/photos";
 import { useSDK } from "./hook";
 
+const global = window as any;
+
 type SDKAPI = {
   getFixPoints: () => FixPoint[];
   getBaseLine: () => MeasureAtom;
@@ -14,6 +16,8 @@ type SDKAPI = {
   getScreenPosition: (pos3d: Pos3D) => null | Pos;
 };
 
+const mustAPI: (keyof SDKAPI)[] = ["photo"] as const;
+
 export const loaded = ref(false);
 export const sdkAPI = reactive({
   getFixPoints: () => JSON.parse(JSON.stringify(fixPoints.value)),
@@ -26,9 +30,20 @@ export const sdkAPI = reactive({
   },
 }) as SDKAPI;
 
-(window as any).getSDK = (callback: (sdk: SDKAPI) => {}) => {
+const sdkLoaded = computed(
+  () => loaded.value && mustAPI.every((api) => api in sdkAPI)
+);
+
+const stopWatch = watchEffect(() => {
+  if (sdkLoaded.value) {
+    mustAPI.forEach((api) => (global[api] = sdkAPI[api]));
+    nextTick(() => stopWatch());
+  }
+});
+
+global.getSDK = (callback: (sdk: SDKAPI) => {}) => {
   const stopWatch = watchEffect(() => {
-    if (loaded.value && sdkAPI.photo) {
+    if (sdkLoaded.value) {
       callback({ ...sdkAPI });
       nextTick(() => stopWatch());
     }

+ 2 - 1
src/views/scene/photo.vue

@@ -188,7 +188,8 @@ if (import.meta.env.VITE_APP_SDK === "true") {
   import("@/openSDK").then((data) => {
     watchEffect(() => {
       data.sdkAPI.photo = async (callback) => {
-        callback(JSON.parse(JSON.stringify(await photo())));
+        const data = await photo();
+        callback && callback(JSON.parse(JSON.stringify(data)));
       };
     });
   });