|
|
@ -14,7 +14,7 @@ NProgress.configure({ |
|
|
|
const whiteList = ['/login','/callback'] // no redirect whitelist
|
|
|
|
const noUserMsg = '获取用户信息失败,请重新登录 !' |
|
|
|
const getErrMsg = '数据获取失败 !' |
|
|
|
const hasToken = getToken() |
|
|
|
|
|
|
|
|
|
|
|
// 跳转登录页
|
|
|
|
const pageToLogin = (to, from, next) => { |
|
|
@ -24,7 +24,8 @@ const pageToLogin = (to, from, next) => { |
|
|
|
NProgress.done() |
|
|
|
} else { |
|
|
|
// other pages that do not have permission to access are redirected to the login page.
|
|
|
|
removeToken() |
|
|
|
// removeToken()
|
|
|
|
store.dispatch('user/resetToken') |
|
|
|
next(`/login?redirect=${to.path}`) |
|
|
|
NProgress.done() |
|
|
|
} |
|
|
@ -32,27 +33,32 @@ const pageToLogin = (to, from, next) => { |
|
|
|
|
|
|
|
// 首次进入页面加载数据处理
|
|
|
|
const toPageInit = async (to, from, next) => { |
|
|
|
|
|
|
|
// 获取用户信息
|
|
|
|
// let _userName = localStorage.getItem("isLoginTokenName")//使用用户名获取
|
|
|
|
// if(_userName && _userName != null && (!store.getters.currentUserInfo || store.getters.currentUserInfo.length <= 0)){
|
|
|
|
// await store.dispatch('user/getUserInfo',_userName)//使用用户名获取
|
|
|
|
let _user_access = hasToken.substring(hasToken.indexOf(" ") + 1,hasToken.length) |
|
|
|
if(hasToken && hasToken != null && (!store.getters.currentUserInfo || store.getters.currentUserInfo.length <= 0)){ |
|
|
|
await store.dispatch('user/getUserInfoByToken',_user_access) |
|
|
|
.then(()=>{ |
|
|
|
toNextHandle(to, from, next) |
|
|
|
}) |
|
|
|
.catch(()=>{ |
|
|
|
// todo-fast:接口恢复后更改
|
|
|
|
toNextHandle(to, from, next) |
|
|
|
// Message.error(noUserMsg)
|
|
|
|
// pageToLogin(to, from, next)
|
|
|
|
}) |
|
|
|
return |
|
|
|
// const hasToken = getToken()
|
|
|
|
// // 获取用户信息
|
|
|
|
// // let _userName = localStorage.getItem("isLoginTokenName")//使用用户名获取
|
|
|
|
// // if(_userName && _userName != null && (!store.getters.currentUserInfo || store.getters.currentUserInfo.length <= 0)){
|
|
|
|
// // await store.dispatch('user/getUserInfo',_userName)//使用用户名获取
|
|
|
|
// let _user_access = hasToken.substring(hasToken.indexOf(" ") + 1,hasToken.length)
|
|
|
|
// if(hasToken && hasToken != null && (!store.getters.currentUserInfo || store.getters.currentUserInfo.length <= 0)){
|
|
|
|
// await store.dispatch('user/getUserInfoByToken',_user_access)
|
|
|
|
// .then(()=>{
|
|
|
|
// toNextHandle(to, from, next)
|
|
|
|
// })
|
|
|
|
// .catch(()=>{
|
|
|
|
// // todo-fast:接口恢复后更改
|
|
|
|
// toNextHandle(to, from, next)
|
|
|
|
// // Message.error(noUserMsg)
|
|
|
|
// // pageToLogin(to, from, next)
|
|
|
|
// })
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
if(getToken()){ |
|
|
|
toNextHandle(to, from, next) |
|
|
|
}else{ |
|
|
|
Message.error('暂无token,请重新登录') |
|
|
|
store.dispatch("user/logout") |
|
|
|
} |
|
|
|
|
|
|
|
toNextHandle(to, from, next) |
|
|
|
} |
|
|
|
|
|
|
|
// 跳转处理
|
|
|
@ -67,13 +73,14 @@ const toNextHandle = async (to, from, next) => { |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
// 获取菜单数据
|
|
|
|
if(!store.getters.permission_routes || store.getters.permission_routes.length <= 0){ |
|
|
|
// 获取菜单数据 permission_routes
|
|
|
|
if(!store.getters.addRoutes || store.getters.addRoutes.length <= 0){ |
|
|
|
isFresh = true |
|
|
|
await store.dispatch('permission/getApiColumnsNames') |
|
|
|
await store.dispatch('permission/getAppConfig') |
|
|
|
.then(res=>{ |
|
|
|
// 没有权限(token失效处理)
|
|
|
|
if(!res || res.length <=0){ |
|
|
|
Message.error('用户无权限或者token失效,请重新登录') |
|
|
|
store.dispatch("user/logout") |
|
|
|
} |
|
|
|
}) |
|
|
@ -106,6 +113,8 @@ router.beforeEach(async (to, from, next) => { |
|
|
|
// set page title
|
|
|
|
document.title = getPageTitle() |
|
|
|
|
|
|
|
const hasToken = getToken() |
|
|
|
|
|
|
|
// 判断是否自动登录
|
|
|
|
let isAutoLogin = JSON.parse(localStorage.getItem('isConfigLogin')) || to.query.loginName |
|
|
|
|
|
|
@ -131,25 +140,27 @@ router.beforeEach(async (to, from, next) => { |
|
|
|
console.log('没有token,是登录页,pageToLogin') |
|
|
|
pageToLogin(to, from, next) |
|
|
|
} else { |
|
|
|
// todo:自动登录,即没有密码登录情况处理
|
|
|
|
mgr.signinRedirect(); //执行重定向
|
|
|
|
|
|
|
|
// if(isAutoLogin){
|
|
|
|
// let _username = localStorage.getItem("isConfigLogin") && localStorage.getItem("isConfigLogin") != 'false' ? localStorage.getItem("configLoginName") : to.query.loginName
|
|
|
|
// await store.dispatch('user/loginOnlyName', {username:_username})
|
|
|
|
// .then(()=>{
|
|
|
|
// toPageInit(to, from, next)
|
|
|
|
// })
|
|
|
|
// .catch(()=>{
|
|
|
|
// // todo-fast:接口恢复后更改
|
|
|
|
// toNextHandle(to, from, next)
|
|
|
|
// // Message.error(noUserMsg)
|
|
|
|
// // pageToLogin(to, from, next)
|
|
|
|
// })
|
|
|
|
// }else{
|
|
|
|
// console.log('没有token,不是登录页,不是自动登录,pageToLogin')
|
|
|
|
// pageToLogin(to, from, next)
|
|
|
|
// }
|
|
|
|
// 如果有单点登录方式使用下方配置
|
|
|
|
// mgr.signinRedirect(); //执行重定向
|
|
|
|
if(isAutoLogin){ |
|
|
|
let _username = localStorage.getItem("isConfigLogin") && localStorage.getItem("isConfigLogin") != 'false' ? localStorage.getItem("configLoginName") : to.query.loginName |
|
|
|
await store.dispatch('user/login', { |
|
|
|
username:_username, |
|
|
|
password:localStorage.getItem("configLoginPass") |
|
|
|
}) |
|
|
|
.then(()=>{ |
|
|
|
toPageInit(to, from, next) |
|
|
|
}) |
|
|
|
.catch(()=>{ |
|
|
|
// todo-fast:接口恢复后更改
|
|
|
|
toNextHandle(to, from, next) |
|
|
|
// Message.error(noUserMsg)
|
|
|
|
// pageToLogin(to, from, next)
|
|
|
|
}) |
|
|
|
}else{ |
|
|
|
console.log('没有token,不是登录页,不是自动登录,pageToLogin') |
|
|
|
pageToLogin(to, from, next) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}) |
|
|
|