li.shaoyi há 2 anos atrás
pai
commit
fe7204a23d

+ 28 - 11
src/packages/mobile/views/boot/index.vue

@@ -8,7 +8,7 @@
         <img src="@mobile/assets/images/guide-1.png" />
       </SwipeItem>
       <SwipeItem>
-        <img src="@mobile/assets/images/guide-2.png" @click="skip" />
+        <img src="@mobile/assets/images/guide-2.png" @click="skip(onLoad)" />
       </SwipeItem>
     </Swipe>
   </div>
@@ -18,13 +18,15 @@
 import { reactive } from 'vue'
 import { useRoute, useRouter } from 'vue-router'
 import { Swipe, SwipeItem } from 'vant'
-import { showLoading } from '@/utils/vant'
+import { showLoading, dialog } from '@/utils/vant'
 import { useLogin } from '@/business/login'
+import service from '@/services'
 import plus from '@/utils/h5plus'
 
 const { logining, initBaseData } = useLogin()
 const route = useRoute()
 const router = useRouter()
+const onLoad = initBaseData(true) // 初始化数据
 const countdown = 1  // 倒计时秒数
 
 const state = reactive({
@@ -34,9 +36,6 @@ const state = reactive({
   rate: 100, // 目标进度
 })
 
-// 初始化数据
-const onLoad = initBaseData(true)
-
 // 倒计时
 const timer = window.setInterval(() => {
   state.second--
@@ -45,18 +44,19 @@ const timer = window.setInterval(() => {
     clearInterval(timer)
     // 判断是否首次打开应用
     if (!state.showGuide) {
-      skip()
+      skip(onLoad)
     }
   }
 }, 1000)
 
 // 跳过广告
-const skip = () => {
+const skip = (promise: Promise<void>) => {
   clearInterval(timer)
-  localStorage.setItem('thj_app_showguide', 'false')
   const toast = logining.value ? showLoading() : undefined
 
-  onLoad.then(() => {
+  promise.then(() => {
+    plus.exitFullSreen()
+    localStorage.setItem('thj_app_showguide', 'false')
     const redirect = route.query.redirect
     if (redirect) {
       router.replace(redirect.toString())
@@ -64,10 +64,27 @@ const skip = () => {
       router.replace({ name: 'Home' })
     }
   }).catch(() => {
-    router.replace({ name: 'Home' })
+    if (service.isReady) {
+      router.replace({ name: 'Home' })
+    } else {
+      tryInit()
+    }
   }).finally(() => {
     toast?.close()
-    plus.exitFullSreen()
+  })
+}
+
+// 初始化失败重试
+const tryInit = () => {
+  dialog({
+    message: '加载失败,请稍后再试',
+    showCancelButton: plus.hasPlus(),
+    cancelButtonText: '退出',
+    confirmButtonText: '重试'
+  }).then(() => {
+    skip(initBaseData(true))
+  }).catch(() => {
+    plus.quit()
   })
 }
 

+ 4 - 5
src/services/http/index.ts

@@ -1,7 +1,7 @@
 import axios, { AxiosRequestConfig, Method, AxiosInstance } from 'axios'
 //import qs from 'qs'
 //import cryptojs from 'crypto-js'
-import { addPending, removePending } from './pending'
+//import { addPending, removePending } from './pending'
 import { loginStore } from '@/stores'
 import { HttpParams, CommonParams, HttpResponse, Payload, ResultCode } from './interface'
 import service from '@/services'
@@ -23,8 +23,8 @@ const httpService = new (class {
             // 请求拦截器
             this.axiosInstance.interceptors.request.use(
                 (config) => {
-                    removePending(config) //在请求开始前,对之前的请求做检查取消操作
-                    addPending(config) //将当前请求添加到列表中
+                    //removePending(config) //在请求开始前,对之前的请求做检查取消操作
+                    //addPending(config) //将当前请求添加到列表中
                     //请求头签名
                     const sign = {
                         token: loginStore.getters.token,
@@ -38,7 +38,6 @@ const httpService = new (class {
                         //Sign: cryptojs.SHA256(qs.stringify(sign)).toString(),
                         //Timestamp: sign.timestamp.toString(),
                     }
-
                     return config
                 },
                 (err) => {
@@ -50,7 +49,7 @@ const httpService = new (class {
             // 响应拦截器
             this.axiosInstance.interceptors.response.use(
                 (res) => {
-                    removePending(res) //在请求结束后,移除本次请求
+                    //removePending(res) //在请求结束后,移除本次请求
                     return res
                 },
                 (err) => {

+ 45 - 13
src/services/index.ts

@@ -32,16 +32,57 @@ export default new (class {
     /** 服务初始化完成状态 */
     isReady = false
 
+    /** 等待服务初始化 */
+    private isPending = true
+
     /**
      * 自动初始化,若断网或其它原因导致初始化失败,需手动初始化
      */
     private onload = this.init()
 
     /**
+     * 重试次数
+     */
+    private retryCount = 0
+
+    /** 
+     * 重试间隔秒数,默认5秒
+     */
+    private defaultRetryInterval = 5 * 1000
+
+    /**
+     * 重试间隔时长
+     */
+    private retryInterval = this.defaultRetryInterval
+
+    /**
+     * 失败时重新尝试初始化,直到成功为止
+     * @param msg 
+     */
+    private tryinit = (msg: string) => {
+        console.error(msg)
+        this.retryCount++
+        return new Promise<typeof this.config>((resolve, reject) => {
+            if (this.retryCount >= 5) {
+                this.retryCount = 0
+                this.retryInterval = this.defaultRetryInterval
+                this.isPending = false
+                reject('服务初始化失败')
+            } else {
+                setTimeout(() => {
+                    this.retryInterval += this.defaultRetryInterval
+                    resolve(this.init())
+                }, this.retryInterval)
+            }
+        })
+    }
+
+    /**
      * 初始化服务配置
      * https://uniapp.dcloud.net.cn/tutorial/app-ios-uiwebview.html
      */
     private init(): Promise<typeof this.config> {
+        this.isPending = true
         return new Promise((resolve) => {
             const filePath = './config/appconfig.json'
             const getAppConfig = async () => {
@@ -71,22 +112,13 @@ export default new (class {
     }
 
     /**
-     * 失败时重新尝试初始化,直到成功为止
-     * @param msg 
-     */
-    private tryinit = (msg: string) => {
-        console.error(msg)
-        return new Promise<typeof this.config>((resolve) => {
-            setTimeout(() => {
-                resolve(this.init())
-            }, 5000)
-        })
-    }
-
-    /**
      * 服务初始化完成时触发
      */
     onReady() {
+        if (!this.isReady && !this.isPending) {
+            this.onload = this.init()
+        }
+        // 确保当前只有一个初始化实例
         return this.onload
     }
 })

+ 29 - 20
src/utils/h5plus/index.ts

@@ -7,31 +7,31 @@ interface HTML5 extends Window {
 export default new (class {
     private h5 = new Promise<HTML5>((resolve) => {
         document.addEventListener('plusready', () => {
-            resolve(window);
+            resolve(window)
         })
     })
 
     constructor() {
         // 监听返回按钮事件
         this.onPlusReady((plus) => {
-            let firstBack = true;
-            const webview = plus.webview.currentWebview();
+            let firstBack = true
+            const webview = plus.webview.currentWebview()
 
             plus.key.addEventListener('backbutton', () => {
                 webview.canBack((e: any) => {
                     // 判断能否继续返回
                     if (e.canBack) {
-                        webview.back();
+                        webview.back()
                     } else {
                         // 1秒内连续两次按返回键退出应用
                         if (firstBack) {
-                            firstBack = false;
-                            plus.nativeUI.toast('再按一次退出应用');
+                            firstBack = false
+                            plus.nativeUI.toast('再按一次退出应用')
                             setTimeout(() => {
-                                firstBack = true;
-                            }, 1000);
+                                firstBack = true
+                            }, 1000)
                         } else {
-                            plus.runtime.quit();
+                            plus.runtime.quit()
                         }
                     }
                 })
@@ -50,6 +50,15 @@ export default new (class {
     }
 
     /**
+     * 退出应用程序
+     */
+    quit() {
+        this.onPlusReady((plus) => {
+            plus.runtime.quit()
+        })
+    }
+
+    /**
      * 客户端的版本名称
      * @returns 
      */
@@ -79,8 +88,8 @@ export default new (class {
      */
     getStatusBarHeight(callback: (statusbarHeight: number) => void) {
         this.onPlusReady((plus) => {
-            const height = plus.navigator.getStatusbarHeight();
-            callback(height);
+            const height = plus.navigator.getStatusbarHeight()
+            callback(height)
         })
     }
 
@@ -90,7 +99,7 @@ export default new (class {
      */
     setStatusBarStyle(color: 'dark' | 'light') {
         this.onPlusReady((plus) => {
-            plus.navigator.setStatusBarStyle(color);
+            plus.navigator.setStatusBarStyle(color)
         })
     }
 
@@ -99,7 +108,7 @@ export default new (class {
      */
     hideStatusBar() {
         this.onPlusReady((plus) => {
-            plus.navigator.setFullscreen(true);
+            plus.navigator.setFullscreen(true)
         })
     }
 
@@ -108,7 +117,7 @@ export default new (class {
      */
     showStatusBar() {
         this.onPlusReady((plus) => {
-            plus.navigator.setFullscreen(false);
+            plus.navigator.setFullscreen(false)
         })
     }
 
@@ -117,8 +126,8 @@ export default new (class {
      */
     setFullSreen() {
         this.onPlusReady((plus) => {
-            this.hideStatusBar();
-            plus.navigator.hideSystemNavigation();
+            this.hideStatusBar()
+            plus.navigator.hideSystemNavigation()
         })
     }
 
@@ -127,8 +136,8 @@ export default new (class {
      */
     exitFullSreen() {
         this.onPlusReady((plus) => {
-            this.showStatusBar();
-            plus.navigator.showSystemNavigation();
+            this.showStatusBar()
+            plus.navigator.showSystemNavigation()
         })
     }
 
@@ -209,12 +218,12 @@ export default new (class {
                     e.target,
                     () => {
                         //销毁Bitmap图片
-                        bitmap.clear();
+                        bitmap.clear()
                         plus.nativeUI.toast('已保存到相册中')
                     },
                     () => {
                         //销毁Bitmap图片
-                        bitmap.clear();
+                        bitmap.clear()
                         plus.nativeUI.toast('保存失败')
                     }
                 )