li.shaoyi 2 éve
szülő
commit
8a1fcb224a
1 módosított fájl, 20 hozzáadás és 17 törlés
  1. 20 17
      src/services/index.ts

+ 20 - 17
src/services/index.ts

@@ -42,9 +42,9 @@ export default new (class {
      * https://uniapp.dcloud.net.cn/tutorial/app-ios-uiwebview.html
      */
     private init(): Promise<typeof this.config> {
-        return new Promise((resolve, reject) => {
+        return new Promise((resolve) => {
+            const filePath = './config/appconfig.json'
             const getAppConfig = async () => {
-                const filePath = './config/appconfig.json'
                 if (plus.hasPlus()) {
                     const res = await plus.getLocalFileContent(filePath)
                     return JSON.parse(res)
@@ -60,30 +60,33 @@ export default new (class {
                     this.isReady = true
                     resolve(this.config)
                 }).catch(() => {
-                    reject('服务地址加载失败')
+                    const result = this.tryinit('服务地址加载失败')
+                    resolve(result)
                 })
             }).catch(() => {
-                reject('配置文件加载失败')
+                const result = this.tryinit('配置文件加载失败')
+                resolve(result)
             })
         })
     }
 
     /**
-     * 服务初始化完成时触发
+     * 失败时重新尝试初始化,直到成功为止
+     * @param msg 
      */
-    onReady() {
+    private tryinit = (msg: string) => {
+        console.error(msg)
         return new Promise<typeof this.config>((resolve) => {
-            this.onload.finally(() => {
-                if (this.isReady) {
-                    resolve(this.onload)
-                } else {
-                    // 失败时重新尝试,直到成功为止
-                    setTimeout(() => {
-                        this.onload = this.init()
-                        this.onReady()
-                    }, 5000)
-                }
-            })
+            setTimeout(() => {
+                resolve(this.init())
+            }, 5000)
         })
     }
+
+    /**
+     * 服务初始化完成时触发
+     */
+    onReady() {
+        return this.onload
+    }
 })