openSDK.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { computed, nextTick, reactive, ref, watchEffect } from "vue";
  2. import { FixPoint, fixPoints } from "./store/fixPoint";
  3. import { baseLines } from "./store/baseLine";
  4. import { MeasureAtom, MeasuresRaw, Pos, Pos3D } from "./sdk";
  5. import { list } from "./store/measure";
  6. import { PhotoRaw } from "./store/photos";
  7. import { useSDK } from "./hook";
  8. const global = window as any;
  9. type SDKAPI = {
  10. getFixPoints: () => FixPoint[];
  11. getBaseLine: () => MeasureAtom;
  12. getMeasures: () => MeasuresRaw;
  13. photo: (callback: (data: PhotoRaw) => void) => void;
  14. getScreenPosition: (pos3d: Pos3D) => null | Pos;
  15. };
  16. const mustAPI: (keyof SDKAPI)[] = ["photo"] as const;
  17. export const loaded = ref(false);
  18. export const sdkAPI = reactive({
  19. getFixPoints: () => JSON.parse(JSON.stringify(fixPoints.value)),
  20. getBaseLine: () => JSON.parse(JSON.stringify(baseLines.value[0])),
  21. getMeasures: () => JSON.parse(JSON.stringify(list.value)),
  22. getScreenPosition: (pos3d: Pos3D) => {
  23. const sdk = useSDK();
  24. const data = sdk.scene.getScreenByPoint(pos3d);
  25. return data.trueSide ? JSON.parse(JSON.stringify(data.pos)) : null;
  26. },
  27. }) as SDKAPI;
  28. const sdkLoaded = computed(
  29. () => loaded.value && mustAPI.every((api) => api in sdkAPI)
  30. );
  31. global.photo = () => {
  32. sdkAPI.photo((data) => {
  33. console.log("告诉anzhuo", JSON.stringify(data, null, 2));
  34. global.android.photoCallback(JSON.stringify(data, null, 2));
  35. });
  36. };
  37. global.getSDK = (callback: (sdk: SDKAPI) => {}) => {
  38. const stopWatch = watchEffect(() => {
  39. if (sdkLoaded.value) {
  40. callback({ ...sdkAPI });
  41. nextTick(() => stopWatch());
  42. }
  43. });
  44. };