gemercheung пре 3 година
родитељ
комит
92e642a36a
1 измењених фајлова са 34 додато и 17 уклоњено
  1. 34 17
      utils/util.js

+ 34 - 17
utils/util.js

@@ -39,12 +39,12 @@ const gotoLogin = (function gotoLogin() {
  * 封封微信的的request
  */
 function request(url, data = {}, method = "POST", header = "application/x-www-form-urlencoded") {
-  
-//   if (!~url.indexOf('/api/cart/update') && !~url.indexOf('/api/cart/index')){
-//     wx.showLoading({
-//       title: '加载中...',
-//     });
-//   }
+
+    //   if (!~url.indexOf('/api/cart/update') && !~url.indexOf('/api/cart/index')){
+    //     wx.showLoading({
+    //       title: '加载中...',
+    //     });
+    //   }
     return new Promise(function (resolve, reject) {
         wx.request({
             url: url,
@@ -56,12 +56,27 @@ function request(url, data = {}, method = "POST", header = "application/x-www-fo
             },
             success: function (res) {
                 // wx.hideLoading();
-                if (res.statusCode == 200) {
+           
 
+                if (!Object.prototype.hasOwnProperty.call(res.data, 'data')) {
+                    res.data.data = res.data.message;
+                }
+
+                if (!Object.prototype.hasOwnProperty.call(res.data, 'errno')) {        
+                    if(Object.prototype.hasOwnProperty.call(res.data, 'code')){
+                        res.data.errno = (res.data.code === 200 || res.data.code === 0) ? 0 : res.data.code;
+                    }
+                }
+                console.log('res', res, url);
+
+                if (res.statusCode == 200) {
                     let pages = getCurrentPages()
                     let currPage = pages[pages.length - 1].route
                     let notLogins = ['pages/index/index', 'pages/catalog/catalog', 'pages/category/category', 'pages/ucenter/index/index']
-                    if (res.data.errno == 401 && !~notLogins.indexOf(currPage)) {
+                    console.log('test',res.data.code == 401 && !~notLogins.indexOf(currPage))
+
+                    if (res.data.code == 401 && !~notLogins.indexOf(currPage)) {
+                        
                         resolve(res.data);
                         gotoLogin()
                     } else {
@@ -79,13 +94,17 @@ function request(url, data = {}, method = "POST", header = "application/x-www-fo
                         resolve(res.data);
                     }
 
-                    
+
                     if (url === api.UserInfo) {
                         let info = wx.getStorageSync('userInfo')
-                        info.isAdmin = res.data.data.isAdmin
-                        wx.setStorageSync('userInfo', info);
+
+                        if (info) {
+                            info.isAdmin = res.data.data.isAdmin;
+                            wx.setStorageSync('userInfo', info);
+                        }
+
                     }
-                    
+
                 } else {
                     reject(res.errMsg);
                 }
@@ -94,7 +113,7 @@ function request(url, data = {}, method = "POST", header = "application/x-www-fo
             fail: function (err) {
                 reject(err)
             },
-            complete:function (res){
+            complete: function (res) {
                 wx.stopPullDownRefresh()
                 wx.hideLoading();
             }
@@ -187,7 +206,7 @@ function checkNetStatu() {
             }
         })
     })
-  }
+}
 
 module.exports = {
     formatTime,
@@ -199,6 +218,4 @@ module.exports = {
     login,
     imgServer,
     checkNetStatu
-}
-
-
+}