bill 2 miesięcy temu
rodzic
commit
d42fdfa187
2 zmienionych plików z 47 dodań i 31 usunięć
  1. 11 10
      src/dbo/main.ts
  2. 36 21
      src/store/sync.ts

+ 11 - 10
src/dbo/main.ts

@@ -1,16 +1,17 @@
-import axios from 'axios'
-import {params} from "@/hook";
+import axios from "axios";
+import { params } from "@/hook";
 
-const instance = axios.create()
+const instance = axios.create();
 
-console.log(import.meta.env)
+console.log(import.meta.env);
 export const baseURL =
-  import.meta.env.VITE_API_BASE_URL
-    ? `/${import.meta.env.VITE_API_BASE_URL}/${params.m}`
-    : `/${params.m}`;
+  // import.meta.env.VITE_API_BASE_URL
+  //   ? `/${import.meta.env.VITE_API_BASE_URL}/${params.m}`
+  //   :
+  `/${params.m}`;
 
-instance.defaults.baseURL = baseURL
+instance.defaults.baseURL = baseURL;
 
-export const getStaticFile = (url: string) => baseURL + url
+export const getStaticFile = (url: string) => baseURL + url;
 
-export default instance
+export default instance;

+ 36 - 21
src/store/sync.ts

@@ -77,27 +77,33 @@ export const api = !global.android
         await window.open(await api.getFile(filename));
       },
       async getFile(url) {
-        const urlRaw = url;
-        getFilesIng.push(urlRaw);
+        try {
+          const urlRaw = url;
+          getFilesIng.push(urlRaw);
 
-        if (url.includes(baseURL)) {
-          url = url.substring(baseURL.length);
-        }
-        url = url.trim();
-        const paths = url.split("/");
-        const notBase64BaseTypes = [".png", ".jpg"];
-        const notBase64 = notBase64BaseTypes.some((type) =>
-          paths[paths.length - 1].includes(type)
-        );
-        if (notBase64) {
-          getFilesSuccess.push(urlRaw);
-          // await new Promise((resolve) => setTimeout(resolve, 2000));
-          return baseURL + url;
-        } else {
-          const data = await axios.get(url, { responseType: "blob" });
-          const base64 = await blobToBase64(data.data);
-          getFilesSuccess.push(urlRaw);
-          return URL.createObjectURL(base64ToBlob(base64));
+          if (url.includes(baseURL)) {
+            url = url.substring(baseURL.length);
+          }
+          url = url.trim();
+          const paths = url.split("/");
+          const notBase64BaseTypes = [".png", ".jpg"];
+          const notBase64 = notBase64BaseTypes.some((type) =>
+            paths[paths.length - 1].includes(type)
+          );
+          if (notBase64) {
+            getFilesSuccess.push(urlRaw);
+            // await new Promise((resolve) => setTimeout(resolve, 2000));
+            console.error(baseURL + url);
+            return baseURL + url;
+          } else {
+            const data = await axios.get(url, { responseType: "blob" });
+            const base64 = await blobToBase64(data.data);
+            getFilesSuccess.push(urlRaw);
+            return URL.createObjectURL(base64ToBlob(base64));
+          }
+        } catch (e) {
+          console.error("获取文件失败 url:", url);
+          throw e;
         }
       },
       async photograph(rotate = true) {
@@ -180,7 +186,16 @@ export const api = !global.android
             const apiName = `getImageCallback${global.count++}`;
             global[apiName] = (base64) => {
               getFilesSuccess.push(urlRaw);
-              resolve(URL.createObjectURL(base64ToBlob(base64)));
+              try {
+                resolve(URL.createObjectURL(base64ToBlob(base64)));
+              } catch (e) {
+                console.error(
+                  "getFile 失败 获取url:",
+                  fileUrl,
+                  "获取结果:",
+                  base64
+                );
+              }
               delete global[apiName];
             };
             global.android.getImage(fileUrl, apiName);