li.shaoyi 2 лет назад
Родитель
Сommit
f0d2ad19bc
2 измененных файлов с 24 добавлено и 26 удалено
  1. 7 8
      src/business/login/index.ts
  2. 17 18
      src/services/index.ts

+ 7 - 8
src/business/login/index.ts

@@ -25,16 +25,15 @@ export function useLogin() {
         DeviceID: ''
     })
 
-    const aa = async () => {
-        // 等待服务初始化
-        await service.onReady()
+    const queryBaseData = async () => {
+        await service.onReady() // 等待服务初始化
         await Promise.all([
             errorInfoStore.actions.getErrorInfoList(),
             enumStore.actions.getAllEnumList(),
         ])
     }
 
-    const bb = async () => {
+    const queryLoginData = async () => {
         await checkToken() // 令牌校验
         await userStore.actions.getUserData()
         futuresStore.actions.getGoodsList()
@@ -55,7 +54,7 @@ export function useLogin() {
                 localStorage.setItem('thj_loginId', formData.LoginID) // 记住登录ID
             }
         })
-        await bb()
+        await queryLoginData()
         eventBus.$emit('LoginNotify') // 登录成功通知
     }
 
@@ -64,7 +63,7 @@ export function useLogin() {
         logining.value = true
         try {
             // 等待加载业务数据
-            await aa()
+            await queryBaseData()
             // 自动登录
             if (autoLogin) {
                 const encryptedData = localData.getValue('autoLoginEncryptedData')
@@ -78,7 +77,7 @@ export function useLogin() {
                     }
                 }
             } else if (token.value) {
-                await bb()
+                await queryLoginData()
             }
         } finally {
             logining.value = false
@@ -90,7 +89,7 @@ export function useLogin() {
         logining.value = true
         try {
             const params = { ...formData }
-            await aa()
+            await queryBaseData()
             await queryLoginId({
                 data: {
                     username: formData.LoginID

+ 17 - 18
src/services/index.ts

@@ -32,20 +32,16 @@ export default new (class {
     /** 服务初始化完成状态 */
     isReady = false
 
-    /** 等待服务初始化 */
-    private isPending = true
-
     /**
-     * 尝试自动初始化,若断网或其它原因导致初始化失败,需手动初始化
+     * 自动初始化,若断网或其它原因导致初始化失败,需手动初始化
      */
-    private tryInit = this.init()
+    private onload = this.init()
 
     /**
      * 初始化服务配置
      * https://uniapp.dcloud.net.cn/tutorial/app-ios-uiwebview.html
      */
     private init(): Promise<typeof this.config> {
-        this.isPending = true
         return new Promise((resolve, reject) => {
             const getAppConfig = async () => {
                 const filePath = './config/appconfig.json'
@@ -57,7 +53,6 @@ export default new (class {
                     return res.data
                 }
             }
-
             getAppConfig().then((res) => {
                 // 获取服务接口地址
                 axios(res.apiUrl).then((res) => {
@@ -65,13 +60,10 @@ export default new (class {
                     this.isReady = true
                     resolve(this.config)
                 }).catch(() => {
-                    reject('获取服务地址失败')
-                }).finally(() => {
-                    this.isPending = false
+                    reject('服务地址加载失败')
                 })
             }).catch(() => {
-                this.isPending = false
-                reject('加载配置文件失败')
+                reject('配置文件加载失败')
             })
         })
     }
@@ -80,11 +72,18 @@ export default new (class {
      * 服务初始化完成时触发
      */
     onReady() {
-        // 初始化失败时重新初始化
-        if (!this.isReady && !this.isPending) {
-            this.tryInit = this.init()
-        }
-        // 确保当前只有一个初始化实例
-        return this.tryInit
+        return new Promise((resolve) => {
+            this.onload.finally(() => {
+                if (this.isReady) {
+                    resolve(this.onload)
+                } else {
+                    // 失败时重新尝试,直到成功为止
+                    setTimeout(() => {
+                        this.onload = this.init()
+                        this.onReady()
+                    }, 5000)
+                }
+            })
+        })
     }
 })