启动时,增加对token的检查

This commit is contained in:
daihh
2023-03-16 16:10:18 +08:00
parent 17dd44937e
commit 13e83580d3
5 changed files with 53 additions and 43 deletions

14
App.vue
View File

@@ -1,5 +1,6 @@
<script> <script>
import Vue from 'vue' import Vue from 'vue'
import apiUserbasic from '@/api/boe/userbasic.js'
export default { export default {
name: 'App', name: 'App',
onLaunch: function() { onLaunch: function() {
@@ -18,22 +19,21 @@
//console.log(this.$width,'$width'); //console.log(this.$width,'$width');
} }
}); });
//获取或加载一次用户信息
//console.log('App Launch');
//登录页面不需要,此页面用于本地测试
//console.log(location.href,'location.href');
var href=location.href; var href=location.href;
if(href.indexOf('/pages/login/login')==-1){ //不包含登录页和加载页
if(href.indexOf('/pages/login/login')==-1 && href.indexOf('/pages/login/loading')==-1){
//先做登录校验
//每次加载增加一次对token的验证直接调用用户中心的接口,如果token不对这里会直接返回登录
apiUserbasic.getUserInfo();
this.$store.dispatch('GetUserInfo'); this.$store.dispatch('GetUserInfo');
} }
}, },
mounted() { mounted() {
}, },
onShow: function() { onShow: function() {
}, },
onHide: function() { onHide: function() {

View File

@@ -1,4 +1,5 @@
import config from '@/config/index.js' import config from '@/config/index.js'
import {toLogin} from '@/utils/tologin.js'
import {getToken,removeToken} from '@/utils/token.js' import {getToken,removeToken} from '@/utils/token.js'
import qs from 'qs' import qs from 'qs'
@@ -35,21 +36,22 @@ const formRequest=function(method,url,data){
if(rs.data.status==6001){ if(rs.data.status==6001){
//uni.removeStorageSync('userInfo'); //uni.removeStorageSync('userInfo');
removeToken(); removeToken();
let loginPath=config.loginPath; toLogin(reUrl);
if(loginPath.startsWith('http')){ // let loginPath=config.loginPath;
// #ifdef APP-PLUS // if(loginPath.startsWith('http')){
plus.runtime.openURL(loginPath) //这里默认使用外部浏览器打开而不是内部web-view组件打开 // // #ifdef APP-PLUS
// #endif // plus.runtime.openURL(loginPath) //这里默认使用外部浏览器打开而不是内部web-view组件打开
// #ifdef H5 // // #endif
//window.open(loginPath) // // #ifdef H5
let returnUrl=window.location.protocol+'//'+window.location.host+config.context; // //window.open(loginPath)
location.href=config.loginPath+"?returnUrl="+encodeURIComponent(returnUrl+'/pages/login/loading'); // let returnUrl=window.location.protocol+'//'+window.location.host+config.context;
// #endif // location.href=config.loginPath+"?returnUrl="+encodeURIComponent(returnUrl+'/pages/login/loading');
}else{ // // #endif
uni.redirectTo({ // }else{
url:loginPath // uni.redirectTo({
}) // url:loginPath
} // })
// }
}else{ }else{
resolve(rs.data); resolve(rs.data);
} }
@@ -85,21 +87,22 @@ const jsonRequest=function(method,url,data){
if(rs.statusCode==200){ if(rs.statusCode==200){
if(rs.data.status==6001){ if(rs.data.status==6001){
removeToken(); removeToken();
let loginPath=config.loginPath; toLogin(reUrl);
if(loginPath.startsWith('http')){ // let loginPath=config.loginPath;
// #ifdef APP-PLUS // if(loginPath.startsWith('http')){
plus.runtime.openURL(loginPath) //这里默认使用外部浏览器打开而不是内部web-view组件打开 // // #ifdef APP-PLUS
// #endif // plus.runtime.openURL(loginPath) //这里默认使用外部浏览器打开而不是内部web-view组件打开
// #ifdef H5 // // #endif
//window.open(loginPath) // // #ifdef H5
let returnUrl=window.location.protocol+'//'+window.location.host+config.context; // //window.open(loginPath)
location.href=config.loginPath+"?returnUrl="+encodeURIComponent(returnUrl+'/pages/login/loading'); // let returnUrl=window.location.protocol+'//'+window.location.host+config.context;
// #endif // location.href=config.loginPath+"?returnUrl="+encodeURIComponent(returnUrl+'/pages/login/loading');
}else{ // // #endif
uni.redirectTo({ // }else{
url:loginPath // uni.redirectTo({
}) // url:loginPath
} // })
// }
}else{ }else{
resolve(rs.data); resolve(rs.data);
} }

View File

@@ -31,6 +31,11 @@ const getUserInfoById = function(id) {
return ajax.postJson('/user/list',{id}); return ajax.postJson('/user/list',{id});
} }
/**获取当前登录用户的信息*/
const getUserInfo = function() {
return ajax.postJson('/user/info',{});
}
/** /**
* https://u-pre.boe.com/userbasic/audience/userAudiences * https://u-pre.boe.com/userbasic/audience/userAudiences
* 获取当前用户受众信息 * 获取当前用户受众信息
@@ -81,6 +86,7 @@ export default {
getOrgInfo, getOrgInfo,
findOrgTreeByOrgId, findOrgTreeByOrgId,
getUserInfoById, getUserInfoById,
getUserInfo,
getUserCrowds, getUserCrowds,
getOrgHrbpInfo, getOrgHrbpInfo,
modifyPassword, modifyPassword,

View File

@@ -6,6 +6,7 @@
<script> <script>
import apiLogin from '@/api/login.js' import apiLogin from '@/api/login.js'
import {toLogin} from '@/utils/tologin.js'
import { getToken,setToken } from '@/utils/token' import { getToken,setToken } from '@/utils/token'
export default{ export default{
data(){ data(){
@@ -28,7 +29,7 @@
}); });
this.toUrl=this.toUrl+params; this.toUrl=this.toUrl+params;
} }
console.log(this.toUrl,'options.returnUrl'); //console.log(this.toUrl,'options.returnUrl');
}, },
mounted() { mounted() {
let token=getToken(); let token=getToken();

View File

@@ -2,14 +2,14 @@ import config from '@/config/index.js'
export function toLogin(ajaxUrl) { export function toLogin(ajaxUrl) {
let pageUrl=location.href; let pageUrl=location.href;
console.log(pageUrl,'pageUrl'); //console.log(pageUrl,'pageUrl');
let urlPre=window.location.protocol+'//'+window.location.host; let urlPre=window.location.protocol+'//'+window.location.host;
console.log(urlPre,'urlPre'); //console.log(urlPre,'urlPre');
let urlContext=config.context; let urlContext=config.context;
console.log(urlContext,'urlContext'); //console.log(urlContext,'urlContext');
let returnPage=pageUrl.substring((urlPre+urlContext).length); let returnPage=pageUrl.substring((urlPre+urlContext).length);
console.log(returnPage,'returnPage'); //console.log(returnPage,'returnPage');
console.log(config.loginPath+"?returnUrl="+urlPre+urlContext+'/pages/login/loading?returnUrl='+returnPage,'生成的url'); //console.log(config.loginPath+"?returnUrl="+urlPre+urlContext+'/pages/login/loading?returnUrl='+returnPage,'生成的url');
if(ajaxUrl && ajaxUrl.indexOf('/xboe/portal/init')>-1){ if(ajaxUrl && ajaxUrl.indexOf('/xboe/portal/init')>-1){
return; return;