Browse Source

优化request

panxingxin 5 years ago
parent
commit
ccab878cc4
1 changed files with 27 additions and 36 deletions
  1. 27 36
      api/request.js

+ 27 - 36
api/request.js

@@ -7,20 +7,19 @@ fly.config.baseURL = "https://wx.sgsino.cn"
 const  newFly = new Fly(); 
 newFly.config = fly.config;
 let reqCount = 0 ;// 重发请求的次数
+let isShowing =false;
 // 获取白名单
 import whiteList from './whiteList';
 // Add interceptors
 fly.interceptors.request.use(function (request) {
   fly.lock();
-// console.log('进入fly-request', request);
-/* wx.showLoading({
-  'title': '加载中',
-  'mask': true
-}); */
   // 不显示加载页面的接口
   if (request.url.indexOf(whiteList.loading) === -1) {
-    // 隐藏loading遮罩
-   // wx.hideLoading();
+    isShowing =true;
+     wx.showLoading({
+       'title': '加载中',
+        'mask': true
+   }); 
   }
   // 白名单内url不添加token
   if (request.url.indexOf(whiteList.nullHeaderToken) !== -1) {
@@ -30,7 +29,6 @@ fly.interceptors.request.use(function (request) {
       'X-Tag': 'flyio'
     };
     console.log('nullHeaderToken()')
-  //  wx.hideLoading();
     fly.unlock();
     return request;
   }
@@ -42,7 +40,7 @@ fly.interceptors.request.use(function (request) {
       'content-type': 'application/json',
       'X-Tag': 'flyio'
     })
-    //wx.hideLoading();
+    console.log("我携带Authorization,不要给我添加token,我会请求回token给其他请求授权用");
     fly.unlock();
     return request;
   }
@@ -56,17 +54,14 @@ fly.interceptors.request.use(function (request) {
        'Authorization': `Bearer ${wx.getStorageSync('token')}`
      })
     // wx.hideLoading();
+    console.log("我请求到了token,携带token请求数据,我的token为" + wx.getStorageSync('token'));
      fly.unlock();
      return request;
   } 
   else{
-    console.log("没有token,先请求token...");
-  //  wx.hideLoading();
+    console.log("没有token也没有授权,我需要重新生成token,先请求token...");
     wx.login({
       success(wxres) {
-
-    //锁定当天实例,后续请求会在拦截器外排队
-  
      return newFly.request({
       url: '/auth/mobile/token/social?grant_type=mobil&mobile=MINI@'+wxres.code,
       method: 'post',
@@ -83,10 +78,6 @@ fly.interceptors.request.use(function (request) {
       console.log(`继续完成请求:path:${request.url},baseURL:${request.baseURL}`)
         return request
     }).finally(() => fly.unlock()) //解锁后,会继续发起请求队列中的任务
-
-
-
-
       }
     })
 
@@ -100,12 +91,20 @@ fly.interceptors.request.use(function (request) {
 fly.interceptors.response.use(
   (response) => {
     //正常返回
+    if(isShowing)
+    {
+       wx.hideLoading();
+       isShowing =false;
+    }
     reqCount =0;
-  //  wx.hideLoading();
     return response
   },
   (err, promise) => {
-  //  wx.hideLoading();
+    if(isShowing)
+    {
+      wx.hideLoading();
+      isShowing =false;
+    }
     let msg = ''; 
     if (err.status === 0) {
       msg = '网络连接异常'
@@ -114,25 +113,18 @@ fly.interceptors.response.use(
       msg = '网络连接超时'
     } 
     else if (err.status === 401) {
-      //reqCount += 1    
-      wx.clearStorageSync('token')
-/*             if (reqCount < 3) {
-               msg = '用户已过期,正在自动刷新用户,尝试次数'+reqCount.toString();
-               this.lock(); //锁定响应拦截器
-               wx.login({ success(wxres)  {
-                  newFly.request(
-                    { url: '/auth/mobile/token/social?grant_type=mobil&mobile=MINI@'+ wxres.code,
-                     method: 'post',
-                     headers:{ 'Authorization': 'Basic dGVzdDp0ZXN0'}})
-                     .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);
+      reqCount += 1    
+      if (reqCount < 3) {
+        console.log("401错误,我需要重新请求+我现在使用的过期token"+ wx.getStorageSync('token'));
+        wx.clearStorageSync('token')
+        return fly.request(err.request);
+      }    
+      
   }
     else if (err.status === 502) {
       reqCount += 1
       if (reqCount < 3) {
+        console.log("502错误,我需要重新请求重试次数" + reqCount.toString());
         msg = '网络错误,正在进行自动重试,重试次数'+reqCount.toString();
          return fly.request(err.request);
       }    
@@ -153,5 +145,4 @@ fly.interceptors.response.use(
   }
 )
 
-
 export default fly;