Browse Source

登陆相关操作从util.js重构到user.js; 此外用户如果请求后台没有登陆时,则跳转到我的页面登陆。

Junling Bu 7 years ago
parent
commit
6bd5ec12d3
2 changed files with 60 additions and 86 deletions
  1. 56 4
      litemall-wx/services/user.js
  2. 4 82
      litemall-wx/utils/util.js

+ 56 - 4
litemall-wx/services/user.js

@@ -1,21 +1,73 @@
 /**
  * 用户相关服务
  */
-
 const util = require('../utils/util.js');
 const api = require('../config/api.js');
 
 
 /**
+ * Promise封装wx.checkSession
+ */
+function checkSession() {
+  return new Promise(function (resolve, reject) {
+    wx.checkSession({
+      success: function () {
+        resolve(true);
+      },
+      fail: function () {
+        reject(false);
+      }
+    })
+  });
+}
+
+/**
+ * Promise封装wx.login
+ */
+function login() {
+  return new Promise(function (resolve, reject) {
+    wx.login({
+      success: function (res) {
+        if (res.code) {
+          resolve(res);
+        } else {
+          reject(res);
+        }
+      },
+      fail: function (err) {
+        reject(err);
+      }
+    });
+  });
+}
+
+/**
+ * Promise封装wx.getUserInfo
+ */
+function getUserInfo() {
+  return new Promise(function (resolve, reject) {
+    wx.getUserInfo({
+      withCredentials: true,
+      success: function (res) {
+        resolve(res);
+      },
+      fail: function (err) {
+        reject(err);
+      }
+    })
+  });
+}
+
+/**
  * 调用微信登录
  */
 function loginByWeixin() {
 
   let code = null;
   return new Promise(function (resolve, reject) {
-    return util.login().then((res) => {
+    return login().then((res) => {
       code = res.code;
-      return util.getUserInfo();
+      return getUserInfo();
     }).then((userInfo) => {
       //登录远程服务器
       util.request(api.AuthLoginByWeixin, { code: code, userInfo: userInfo }, 'POST').then(res => {
@@ -44,7 +96,7 @@ function checkLogin() {
   return new Promise(function (resolve, reject) {
     if (wx.getStorageSync('userInfo') && wx.getStorageSync('token')) {
 
-      util.checkSession().then(() => {
+      checkSession().then(() => {
         resolve(true);
       }).catch(() => {
         reject(false);

+ 4 - 82
litemall-wx/utils/util.js

@@ -32,35 +32,14 @@ function request(url, data = {}, method = "GET") {
         'X-Litemall-Token': wx.getStorageSync('token')
       },
       success: function (res) {
-        // console.log("success");
 
         if (res.statusCode == 200) {
 
           if (res.data.errno == 401) {
             //需要登录后才可以操作
-
-            let code = null;
-            return login().then((res) => {
-              code = res.code;
-              return getUserInfo();
-            }).then((userInfo) => {
-              //登录远程服务器
-              request(api.AuthLoginByWeixin, { code: code, userInfo: userInfo }, 'POST').then(res => {
-                if (res.errno === 0) {
-                  //存储用户信息
-                  wx.setStorageSync('userInfo', res.data.userInfo);
-                  wx.setStorageSync('token', res.data.token);
-                  
-                  resolve(res);
-                } else {
-                  reject(res);
-                }
-              }).catch((err) => {
-                reject(err);
-              });
-            }).catch((err) => {
-              reject(err);
-            })
+            wx.switchTab({
+              url: '/pages/ucenter/index/index'
+            });
           } else {
             resolve(res.data);
           }
@@ -71,60 +50,6 @@ function request(url, data = {}, method = "GET") {
       },
       fail: function (err) {
         reject(err)
-        // console.log("failed")
-      }
-    })
-  });
-}
-
-/**
- * 检查微信会话是否过期
- */
-function checkSession() {
-  return new Promise(function (resolve, reject) {
-    wx.checkSession({
-      success: function () {
-        resolve(true);
-      },
-      fail: function () {
-        reject(false);
-      }
-    })
-  });
-}
-
-/**
- * 调用微信登录
- */
-function login() {
-  return new Promise(function (resolve, reject) {
-    wx.login({
-      success: function (res) {
-        if (res.code) {
-          //登录远程服务器
-          // console.log(res)
-          resolve(res);
-        } else {
-          reject(res);
-        }
-      },
-      fail: function (err) {
-        reject(err);
-      }
-    });
-  });
-}
-
-function getUserInfo() {
-  return new Promise(function (resolve, reject) {
-    wx.getUserInfo({
-      withCredentials: true,
-      success: function (res) {
-        // console.log(res)
-        resolve(res);
-      },
-      fail: function (err) {
-        reject(err);
       }
     })
   });
@@ -156,10 +81,7 @@ module.exports = {
   formatTime,
   request,
   redirect,
-  showErrorToast,
-  checkSession,
-  login,
-  getUserInfo,
+  showErrorToast
 }