张延森 5 vuotta sitten
vanhempi
commit
4c0cf25e2e
2 muutettua tiedostoa jossa 40 lisäystä ja 27 poistoa
  1. 2
    2
      config/dev.js
  2. 38
    25
      src/utils/request.js

+ 2
- 2
config/dev.js Näytä tiedosto

@@ -9,8 +9,8 @@ module.exports = {
9 9
     // WSS_HOST: '"wss://dev.jinchengjiaye.com"',
10 10
     // HOST: '"https://lt.pawoma.cn"',
11 11
     // WSS_HOST: '"wss://lt.pawoma.cn"',
12
-    HOST: '"http://192.168.0.218:8080"',
13
-    WSS_HOST: '"ws://192.168.0.218:8080"',
12
+    HOST: '"http://192.168.0.84:8080"',
13
+    WSS_HOST: '"ws://192.168.0.84:8080"',
14 14
   },
15 15
   weapp: {},
16 16
   h5: {}

+ 38
- 25
src/utils/request.js Näytä tiedosto

@@ -28,7 +28,7 @@ export const setStorage = function (key, data) {
28 28
  */
29 29
 export const fetch = async (options) => {
30 30
   const { url, payload, method = 'GET', showToast = true, autoLogin = true } = options
31
-  console.log(Taro.getStorageSync('token'),'----token---')
31
+  // console.log(Taro.getStorageSync('token'),'----token---')
32 32
   return Taro.request({
33 33
     url,
34 34
     method,
@@ -45,36 +45,49 @@ export const fetch = async (options) => {
45 45
         title: "请检查网络",
46 46
         icon: 'none'
47 47
       })
48
-      return Promise.reject({ message: "请检查网络" })
49
-    }
50
-    const { data, code } = res.data
51
-    if (code !== CODE_SUCCESS) {
52
-      return Promise.reject(res.data)
48
+
49
+      console.error(res)
50
+      throw new Error('请检查网络')
53 51
     }
54 52
 
55
-    // if (url.split('?')[0] === API_USER_LOGIN) {
56
-    //   await updateToken(data.miniApp)
57
-    // }
58
-    return data
59
-  }).catch((err) => {
60
-    const isExpired = (err.code === CODE_AUTH_EXPIRED || err.code === CODE_UNLOGIN)
61
-    const defaultMsg = isExpired ? '登录失效' : (err.message || '请求异常')
62
-    if (isExpired && autoLogin) {
63
-      // token失效,重新登录后,跳转到首页
64
-      login({}, () => {
65
-        Taro.switchTab({
66
-          url: '/pages/project/index'
67
-        })
68
-      })
53
+    const { data, code, message } = res.data
54
+    if (code === CODE_SUCCESS) {
55
+      return data
69 56
     } else {
70
-      if (showToast) {
71
-        Taro.showToast({
72
-          title: err && err.msg || defaultMsg,
73
-          icon: 'none'
57
+      const isExpired = (code === CODE_AUTH_EXPIRED || code === CODE_UNLOGIN)
58
+      const defaultMsg = isExpired ? '登录失效' : message
59
+      if (isExpired && autoLogin) {
60
+        // token失效,重新登录后,跳转到首页
61
+        login({}, () => {
62
+          Taro.switchTab({
63
+            url: '/pages/project/index'
64
+          })
74 65
         })
66
+      } else {
67
+        if (showToast) {
68
+          Taro.showToast({
69
+            title: defaultMsg,
70
+            icon: 'none'
71
+          })
72
+        }
75 73
       }
74
+      console.error(res.data)
75
+      throw new Error(data)
76 76
     }
77
-    return Promise.reject({ message: defaultMsg, ...err })
77
+  }).catch((err) => {
78
+    console.error(err)
79
+
80
+    let errMessage = `请求失败: ${err.errMsg}`
81
+    if (err.errMsg === 'request:fail timeout') {
82
+      errMessage = '请检查网络'
83
+    }
84
+
85
+    Taro.showToast({
86
+      title: errMessage,
87
+      icon: 'none'
88
+    })
89
+
90
+    throw new Error(errMessage)
78 91
   })
79 92
 }
80 93