xiongzhu 2 年之前
父節點
當前提交
5440019729
共有 2 個文件被更改,包括 26 次插入3 次删除
  1. 4 3
      src/stores/system.js
  2. 22 0
      src/utils/utils.js

+ 4 - 3
src/stores/system.js

@@ -1,6 +1,7 @@
 import { ref, computed } from 'vue'
 import { defineStore } from 'pinia'
 import { http } from '@/plugins/http'
+import { retry } from '@/utils/utils'
 
 export const useSystemStore = defineStore('system', () => {
     const riseRate = ref(0)
@@ -12,13 +13,13 @@ export const useSystemStore = defineStore('system', () => {
     })
 
     function getSysConfigs() {
-        http.get('/sysConfig/get/max_rise_rate').then(res => {
+        retry(() => http.get('/sysConfig/get/max_rise_rate')).then(res => {
             riseRate.value = Number(res.value)
         })
-        http.get('/sysConfig/get/platform_commission').then(res => {
+        retry(() => http.get('/sysConfig/get/platform_commission')).then(res => {
             platformCommission.value = Number(res.value)
         })
-        http.post('/saleBatch/all', { size: 10000 }, { body: 'json' }).then(res => {
+        retry(() => http.post('/saleBatch/all', { size: 10000 }, { body: 'json' })).then(res => {
             saleBatches.value = res.content
         })
     }

+ 22 - 0
src/utils/utils.js

@@ -0,0 +1,22 @@
+function asleep(ms) {
+    return new Promise(resolve => setTimeout(resolve, ms))
+}
+const retry = async (fn, maxTry = 10) => {
+    if (typeof fn !== 'function') throw new Error('fn is not a function')
+    let count = 0
+    const ts = new Date().getTime()
+    // eslint-disable-next-line no-constant-condition
+    while (true) {
+        try {
+            return await fn()
+        } catch (e) {
+            if (++count > maxTry) {
+                console.log(`[failed after ${count - 1} retry]`)
+                throw e
+            }
+            await asleep(1500)
+            console.log(`[retry ${count} after ${new Date().getTime() - ts}ms]`)
+        }
+    }
+}
+export { asleep, retry }