Browse Source

【接口看板】路由优化+token失效判断处理

faster_AG_grid
安虹睿 1 year ago
parent
commit
fd2c0805d1
  1. 20
      PC/InterFace.Dash/src/api/wms-auth.js
  2. 26
      PC/InterFace.Dash/src/permission.js
  3. 43
      PC/InterFace.Dash/src/store/modules/user.js

20
PC/InterFace.Dash/src/api/wms-auth.js

@ -89,10 +89,28 @@ export function getPageListRoles(data) {
}
//获取权限模板
export function getpermissionsRoles(data) {
export function getPermissionsRoles(data) {
return request({
url: authApi +'/permission-management/permissions',
method: 'get',
params: data
})
}
// 创建角色
export function postCreateRoles(data) {
return request({
url: authApi + '/identity/roles',
method: 'post',
data
})
}
//保存权限模板
export function savePermissionsRoles(URL, data) {
return request({
url: authApi + URL,
method: 'put',
data
})
}

26
PC/InterFace.Dash/src/permission.js

@ -14,6 +14,7 @@ NProgress.configure({
const whiteList = ['/login','/callback'] // no redirect whitelist
const noUserMsg = '获取用户信息失败,请重新登录 !'
const getErrMsg = '数据获取失败 !'
const hasToken = getToken()
// 跳转登录页
const pageToLogin = (to, from, next) => {
@ -33,9 +34,12 @@ const pageToLogin = (to, from, next) => {
const toPageInit = async (to, from, next) => {
// 获取用户信息
let _username = localStorage.getItem("isLoginTokenName")//登录成功且token通过后的登录名
if(_username && _username != null && (!store.getters.currentUserInfo || store.getters.currentUserInfo.length <= 0)){
await store.dispatch('user/getUserInfo',_username)
// 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)
})
@ -66,10 +70,17 @@ const toNextHandle = async (to, from, next) => {
// 获取菜单数据
if(!store.getters.permission_routes || store.getters.permission_routes.length <= 0){
isFresh = true
await store.dispatch('permission/getApiColumnsNames').catch(()=>{
Message.error(getErrMsg)
NProgress.done()
})
await store.dispatch('permission/getApiColumnsNames')
.then(res=>{
// 没有权限(token失效处理)
if(!res || res.length <=0){
store.dispatch("user/logout")
}
})
.catch(()=>{
Message.error(getErrMsg)
NProgress.done()
})
}
// 获取枚举数据
@ -95,7 +106,6 @@ 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

43
PC/InterFace.Dash/src/store/modules/user.js

@ -46,21 +46,11 @@ const loginSuccess = (res,username,resolve,reject) => {
const loginSuccessBySignin = (res,resolve, reject,commit) => {
let _token = res.token_type + ' ' + res.access_token
setToken(_token)
getUsersByToken(res.access_token)
.then(info=>{
if(info){
let _info = info
_info.userInfo = info.preferred_username
_info.id = info.sub
commit('SET_USERINFO', _info)
resolve(_info)
}else{
reject('用户信息获取失败')
}
})
.catch(err=>{
reject(err)
})
store.dispatch('user/getUserInfoByToken',res.access_token).then(()=>{
resolve()
}).catch(()=>{
reject()
})
}
const actions = {
@ -200,6 +190,29 @@ const actions = {
})
})
},
// get userInfo
getUserInfoByToken({
commit
},access_token) {
return new Promise((resolve, reject) => {
getUsersByToken(access_token)
.then(info=>{
if(info){
let _info = info
_info.userInfo = info.preferred_username
_info.id = info.sub
commit('SET_USERINFO', _info)
resolve(_info)
}else{
reject('用户信息获取失败')
}
})
.catch(err=>{
reject(err)
})
})
},
}
export default {

Loading…
Cancel
Save