chenzhen2 пре 1 година
родитељ
комит
694a5e7919
6 измењених фајлова са 72 додато и 60 уклоњено
  1. 11 8
      src/api/user.js
  2. 12 5
      src/common/request.js
  3. 1 1
      src/common/utils.js
  4. 1 12
      src/pages/auth/index.wpy
  5. 46 34
      src/service/user.js
  6. 1 0
      src/store/user.js

+ 11 - 8
src/api/user.js

@@ -39,7 +39,7 @@ function register(params) {
   return $http({
     url: `${config.duoduoenvService}userNew/wechat/register`,
     method: 'POST',
-    isComParams:true,
+    isComParams: true,
     data: JSON.stringify(params)
   })
 }
@@ -63,19 +63,22 @@ function getHttpPhoneNumber(code) {
 // 续约
 function refreshToken(refreshToken) {
   return $http({
-    url: `${config.duoduoenvService}userNew/wechat/refreshToken?refreshToken=${refreshToken}`,
-    method: 'get'
+    header: {
+      "refreshToken": refreshToken,
+    },
+    url: `${config.duoduoenvService}userNew/refreshToken`,
+    method: 'post'
   })
 }
 
 // 获取用户空间权限
 function getUserControlSpace(params) {
   return $http({
-      // url: `http://192.168.88.4:52009/userControlSpace/query`,
-      url: `${config.duoduoenvService}userControlSpace/query`,
-      method: 'POST',
-      data: JSON.stringify(params),
-      isComParams: false    // 是否携带通用的参数
+    // url: `http://192.168.88.4:52009/userControlSpace/query`,
+    url: `${config.duoduoenvService}userControlSpace/query`,
+    method: 'POST',
+    data: JSON.stringify(params),
+    isComParams: false    // 是否携带通用的参数
   })
 }
 

+ 12 - 5
src/common/request.js

@@ -1,7 +1,7 @@
 /* eslint-disable prefer-promise-reject-errors */
 import config from '@/config';
 import store from '@/store';
-import { checkTokenIsValid } from '@/service/user'
+import { checkTokenIsValid, refreshTokenPage, checkLoginNew } from '@/service/user'
 import errorRequest from './errorRequest.js';
 // get参数反序列化
 const setQueryConfig = function (queryConfig) {
@@ -94,17 +94,24 @@ export default function (info) {
           console.error(error);
         }
         if (res.statusCode === 401) {
+          console.log(checkTokenIsValid())
           if (checkTokenIsValid() == 2) { // 登录过期后重新从首页刷新进入
             store.commit('clearToken', '');
-            wx.redirectTo({ url: '/pages/index' });
-          } else {
+            wx.redirectTo({ url: '/packagesEnv/pages/home/index' });
+          } else { // 续约
             if (!store.state.user.refreshUse) {
+              console.log("开始续约--");
               store.commit('setRefreshUse', true);
-              checkLoginNew(1).then(res => {
+              refreshTokenPage().then(res => {
+                store.commit('setRefreshUse', false);
+              }).catch((err) => {
+                store.commit('clearToken', '');
+                wx.redirectTo({ url: '/packagesEnv/pages/home/index' });
                 store.commit('setRefreshUse', false);
-              });
+              })
             }
           }
+          reject('登录过期');
         } else if (res.statusCode >= 400) {
           reject(res.errMsg);
           if (!isNotShowErrorToast) {

+ 1 - 1
src/common/utils.js

@@ -1030,7 +1030,7 @@ function getVersion() {
 
 // 获取时间戳
 function getTimeStamp() {
-  let timeStamp = Date.parse(new Date());
+  let timeStamp = new Date().getTime();
   return timeStamp;
 }
 

+ 1 - 12
src/pages/auth/index.wpy

@@ -110,18 +110,7 @@ wepy.page({
       this.canIUseGetUserProfile = true;
     }
   },
-  onShow() {
-    // 过期重定向处理
-    // checkLoginNew().then(res => {
-    //   if (res.data && res.data.isActivated == 1) {
-    //     this.goHome();
-    //   } else {
-    //     wx.navigateTo({
-    //       url: '/pages/index'
-    //     });
-    //   }
-    // });
-  },
+  onShow() {},
   methods: {
     // 绑定租户
     bindTenant() {},

+ 46 - 34
src/service/user.js

@@ -3,19 +3,24 @@ import store from '@/store';
 import config from '@/config';
 import { login, getCompanyByPhone, refreshToken, getUserControlSpace } from '@/api/user.js';
 import utils from '@/common/utils.js';
-//  检查用户信息-(废弃)
+//  定时检查用户信息并且续约
 function checkHasUserInfo() {
-  let userInfo = store.state.user.userInfo;
-  if (userInfo && JSON.stringify(userInfo) !== '{}') {
-    return Promise.resolve(userInfo);
-  } else {
-    // return checkLogin(false).then(() => {
-    //   return getUserData();
-    // })
-    // wxLogin();
-  }
+  let timer = setTimeout(() => {
+    let tokenInfo = store.state.user.tokenInfo;
+    let expireTime = tokenInfo.expireTime;
+    let refreshTokenExpireTime = tokenInfo.refreshTokenExpireTime;
+    let nowtime = utils.getTimeStamp();
+    if (expireTime && refreshTokenExpireTime >= nowtime && expireTime - nowtime <= 1800000) {  // 过期可续约
+      refreshTokenPage()
+    }
+    clearTimeout(timer);
+    checkHasUserInfo();
+  }, 600000)
+
 }
 
+
+
 // 登录
 function wxLogin(type) {
   // type=1 如果type未激活和作废不需要跳转到登录页面
@@ -109,7 +114,9 @@ function getCompanyDataByPhone() {
 // 登录续约
 function refreshTokenPage() {
   return new Promise((resolve, reject) => {
-    refreshToken().then(res => {
+    let storeTokenInfo = store.state.user.tokenInfo
+    let refreshTokenValue = storeTokenInfo && storeTokenInfo.refreshToken ? storeTokenInfo.refreshToken : '';
+    refreshToken(refreshTokenValue).then(res => {
       if (res.result == 'success') {
         let data = res.data || []
         let tokenInfo = {
@@ -125,16 +132,19 @@ function refreshTokenPage() {
           store.commit('setUserInfo', data.tokenUser);
           resolve(data)
         } else {
-          store.commit('setToken', '')
-          // store.commit('setUserInfo', {});
-          wx.redirectTo({ url: '/pages/index' });
+          store.commit('clearToken', '');
+          refreshHome()
           resolve(data)
         }
+      } else { 
+        store.commit('clearToken', '');
+        refreshHome();
+        reject("续约失败")
       }
     }).catch(() => {
-      resolve("续约失败")
-      store.commit('setToken', '')
-      wx.redirectTo({ url: '/pages/index' });
+      store.commit('clearToken', '');
+      refreshHome();
+      reject("续约失败")
     })
   })
 }
@@ -142,6 +152,7 @@ function refreshTokenPage() {
 // 刷新首页
 function refreshHome() {
   // wx.redirectTo({ url: '/pages/index' });
+  wx.redirectTo({ url: '/packagesEnv/pages/home/index' });
 }
 // 检查用户是否有空间控制权限
 function checkUserControl() {
@@ -190,7 +201,10 @@ function checkUserControl() {
 function checkLoginNew(type) {
   return new Promise((resolve, reject) => {
     let token = store.state.user.token;
-    if (!token) {
+    let storeTokenInfo = store.state.user.tokenInfo
+    let refreshToken = storeTokenInfo && storeTokenInfo.refreshToken ? storeTokenInfo.refreshToken : '';
+    console.log("执行checkLoginNew--");
+    if (!token || !refreshToken) {
       wxLogin().then((res) => {
         resolve(res)
       }).catch((error) => {
@@ -198,25 +212,22 @@ function checkLoginNew(type) {
       });
     } else {
       let flag = checkTokenIsValid();
-      if (flag == 1) {
+      console.log("flag==", flag);
+      if (flag == 1) {  // 续约
         refreshTokenPage().then(res => {
           resolve(res)
-        }).catch(() => {
+        }).catch((error) => {
           reject("续约失败!");
-        }); // 续约
+        });;
       } else if (flag == 2) { // 重新登录
         wxLogin().then((res) => {
           resolve(res)
-        });
-      } else if (flag == 3) {  // 差2分钟过期
-        if (type !== 1) {
-          let timeout = setTimeout(() => {
-            clearTimeout(timeout)
-            checkLoginNew();
-          }, 300000)  // 5分钟后检查一下是否过期
-        }
+        }).catch((error) => {
+          reject("登录失败!");
+        });;
+      } else if (flag == 3) {  // 未过期
         resolve({
-          isActivated: 1
+          isActivated: store.state.user.isActivated
         })
       }
     }
@@ -228,11 +239,11 @@ function checkTokenIsValid() {
   let flag = 1
   let tokenInfo = store.state.user.tokenInfo;
   let expireTime = tokenInfo.expireTime;
-  let refreshToken = tokenInfo.refreshToken;
+  let refreshTokenExpireTime = tokenInfo.refreshTokenExpireTime;
   let nowtime = utils.getTimeStamp();
-  if (expireTime && expireTime < nowtime && refreshToken >= nowtime) {  // 过期可续约
+  if (expireTime && expireTime <= nowtime && refreshTokenExpireTime >= nowtime) {  // 过期可续约
     flag = 1
-  } else if (expireTime && expireTime >= (nowtime - 120000)) { // 还差2分钟过期
+  } else if (expireTime && expireTime >= nowtime) { //未过期
     flag = 3
   }
   else { // 需要重新登录
@@ -265,5 +276,6 @@ export {
   checkLoginNew,
   checkTokenIsValid,
   getCompanyDataByPhone,
-  checkUserControl
+  checkUserControl,
+  refreshTokenPage
 }

+ 1 - 0
src/store/user.js

@@ -33,6 +33,7 @@ export default {
     },
     // 设置token用户信息
     setTokenInfo(state, tokenInfo) {
+      console.log("重新存入token");
       wx.setStorageSync('tokenInfo', tokenInfo);
       state.tokenInfo = tokenInfo;
     },