Przeglądaj źródła

core(request): 修改requst中间字段,保持旧的数据结构

gemercheung 3 lat temu
rodzic
commit
26a7be040d
5 zmienionych plików z 39 dodań i 21 usunięć
  1. 1 0
      component/auth/auth.js
  2. 2 0
      pages/auth/btnAuth/btnAuth.js
  3. 1 1
      pages/user/user.js
  4. 5 4
      services/user.js
  5. 30 16
      utils/util.js

+ 1 - 0
component/auth/auth.js

@@ -95,6 +95,7 @@ Component({
       });
     },
     getUserProfile: function (e) {
+
       wx.getUserProfile({
         desc: '用于完善用户资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
         success: async  (userInfo_res) => {

+ 2 - 0
pages/auth/btnAuth/btnAuth.js

@@ -38,12 +38,14 @@ Page({
   bindGetUserInfo: function(e) {
     let that = this;
     //登录远程服务器
+    debugger;
     if (that.data.code) {
       util.request(api.AuthLoginByWeixin, {
         code: that.data.code,
         userInfo: e.detail
       }, 'POST', 'application/json').then(res => {
         if (res.errno === 0) {
+          debugger
           //存储用户信息
           res.data.userInfo.userId = res.data.userId
           res.data.userInfo.sessionKey = res.data.sessionKey

+ 1 - 1
pages/user/user.js

@@ -24,7 +24,7 @@ Page({
    */
   onLoad: function (options) {
     this.getUserInfo()
-    let userInfo = wx.getStorageSync('userInfo')
+    let userInfo = wx.getStorageSync('userInfo') || {}
     userInfo.avatar = userInfo.avatarUrl
     this.sessionKey = userInfo.sessionKey
     delete userInfo.avatarUrl

+ 5 - 4
services/user.js

@@ -10,7 +10,7 @@ const api = require('../config/api.js');
  * 调用微信登录
  */
 function loginByWeixin(userInfo) {
-
+  debugger;
   let code = null;
   return new Promise(function (resolve, reject) {
     return util.login().then((res) => {
@@ -21,9 +21,10 @@ function loginByWeixin(userInfo) {
       util.request(api.AuthLoginByWeixin, { code: code, userInfo: userInfo }, 'POST', 'application/json').then(res => {
         if (res.errno === 0) {
           //存储用户信息
-          res.data.userInfo.userId = res.data.userId
-          res.data.userInfo.sessionKey = res.data.sessionKey
-          res.data.userInfo.isAdmin = res.data.isAdmin
+         
+          res.data.userInfo.userId = res.data.userId;
+          res.data.userInfo.sessionKey = res.data.sessionKey;
+          res.data.userInfo.isAdmin = res.data.isAdmin;
           wx.setStorageSync('userInfo', res.data.userInfo);
           wx.setStorageSync('token', res.data.token);
 

+ 30 - 16
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,8 +56,20 @@ 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);
+
+                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']
@@ -79,13 +91,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 +110,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 +203,7 @@ function checkNetStatu() {
             }
         })
     })
-  }
+}
 
 module.exports = {
     formatTime,
@@ -199,6 +215,4 @@ module.exports = {
     login,
     imgServer,
     checkNetStatu
-}
-
-
+}