|
@@ -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
|
|
|
}
|