Jelajahi Sumber

自动请求token

panxingxin 5 tahun lalu
induk
melakukan
4a1ed4989a
1 mengubah file dengan 54 tambahan dan 4 penghapusan
  1. 54 4
      api/request.js

+ 54 - 4
api/request.js

@@ -10,7 +10,8 @@ let reqCount = 0 ;// 重发请求的次数
 // 获取白名单
 import whiteList from './whiteList';
 // Add interceptors
-fly.interceptors.request.use((request) => {
+fly.interceptors.request.use(function (request) {
+  fly.lock();
 // console.log('进入fly-request', request);
 wx.showLoading({
   'title': '加载中',
@@ -29,6 +30,7 @@ wx.showLoading({
       'X-Tag': 'flyio'
     };
     console.log('nullHeaderToken()')
+    fly.unlock();
     return request;
   }
   //头文件带Authorization为请求token,不添加token
@@ -39,6 +41,7 @@ wx.showLoading({
       'content-type': 'application/json',
       'X-Tag': 'flyio'
     })
+    fly.unlock();
     return request;
   }
 //其他请求需要在头文件中自动附加token
@@ -50,8 +53,54 @@ wx.showLoading({
        'X-Tag': 'flyio',
        'Authorization': `Bearer ${wx.getStorageSync('token')}`
      })
+     fly.unlock();
      return request;
   } 
+  else{
+    console.log("没有token,先请求token...");
+    wx.login({
+      success(wxres) {
+/*         requestAPI.userLogin(wxres.code).then(res => {
+          if (res.status === 401) {
+            self.setData({
+              ifUser: false
+            })
+          } else {
+            if (res.data.access_token) {
+              wx.setStorageSync('token', res.data.access_token);
+              wx.switchTab({
+                url: '/pages/home/home',
+              })
+            }
+          }
+        }) */
+
+    //锁定当天实例,后续请求会在拦截器外排队
+  
+     return newFly.request({
+      url: '/auth/mobile/token/social?grant_type=mobil&mobile=MINI@'+wxres.code,
+      method: 'post',
+      headers:{
+        'content-type': 'application/json',
+        'X-Tag': 'flyio',
+        'Authorization': 'Basic dGVzdDp0ZXN0'
+      }
+    }).then((res) => {
+      if (res.data.access_token) {
+        wx.setStorageSync('token', res.data.access_token);
+      }
+      console.log("token请求成功,值为: " +  res.data.access_token);
+      console.log(`继续完成请求:path:${request.url},baseURL:${request.baseURL}`)
+        return request
+    }).finally(() => fly.unlock()) //解锁后,会继续发起请求队列中的任务
+
+
+
+
+      }
+    })
+
+  }
 }, (error, promise) => {
   // Do something with request error
   console.log(error); // for debug
@@ -75,9 +124,9 @@ fly.interceptors.response.use(
       msg = '网络连接超时'
     } 
     else if (err.status === 401) {
-      reqCount += 1    
+      //reqCount += 1    
       wx.clearStorageSync('token')
-            if (reqCount < 3) {
+/*             if (reqCount < 3) {
                msg = '用户已过期,正在自动刷新用户,尝试次数'+reqCount.toString();
                this.lock(); //锁定响应拦截器
                wx.login({ success(wxres)  {
@@ -88,7 +137,8 @@ fly.interceptors.response.use(
                      .then(res => { if (res.data.access_token) {wx.setStorageSync('token', res.data.access_token); }})
                      .finally(() => this.unlock())
                      .then(() => { return fly.request(err.request); })}})
-    } 
+    }  */
+      return fly.request(err.request);
   }
     else if (err.status === 502) {
       reqCount += 1