Commit 27c3e111 authored by 杨硕's avatar 杨硕

修改多次请求getinfo的问题

parent fec25cc2
......@@ -5,7 +5,7 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<link rel="icon" href="<%= BASE_URL %>liulanqi_logo.png">
<link rel="stylesheet" href="//at.alicdn.com/t/font_2011217_f70ptj9y4oc.css">
<!-- <link rel="stylesheet" href="//at.alicdn.com/t/font_2011217_f70ptj9y4oc.css">-->
<title><%= webpackConfig.name %></title>
</head>
<body>
......
......@@ -71,7 +71,7 @@ export default {
data() {
return {
appList: [],
userName: this.$store.state.user.name,
userName: '',
specialTag: this.$store.state.user.specialTag
}
},
......@@ -83,6 +83,9 @@ export default {
},
mounted() {
// this.getInAdhibitions()
const user = sessionStorage.getItem('userInfo')
const userInfo = JSON.parse(user)
this.userName = userInfo.user.name
},
methods: {
handleCommand(param) {
......
......@@ -26,20 +26,20 @@ router.beforeEach((to, from, next) => {
next({ path: '/login' })
} else {
if (store.getters.roles.length === 0) {
console.log('111')
// 判断当前用户是否已拉取完user_info信息
store.dispatch('GetInfo').then(res => {
// store.dispatch('GetInfo').then(res => {
// 判断用户是否有权限 有角色
if (res.data.permissions && res.data.permissions.length > 0) {
// const permissions = Cookies.get('permissions')
// if (store.getters.permissions && store.getters.permissions.length > 0) {
// 拉取user_info
const roles = res.data.roles
const roles = store.getters.roles
const menuName = Cookies.get('menuname')
store.dispatch('GenerateRoutes', { roles, menuName }).then((routers) => {
// 判断用户是否有路由
if (routers.accessedRoutes && routers.accessedRoutes.length > 0 && routers.getRouters && routers.getRouters.length > 0) {
// 测试 默认静态页面
// store.dispatch('permission/generateRoutes', { roles }).then(accessRoutes => {
// TODO: clear this log
console.log(`routers.accessedRoutes`, routers.accessedRoutes)
// 根据roles权限生成可访问的路由表
router.addRoutes(routers.accessedRoutes) // 动态添加可访问路由表
// TODO: clear this log
......@@ -60,21 +60,21 @@ router.beforeEach((to, from, next) => {
// })
// }
})
} else {
alert('用户无权限')
store.dispatch('FedLogOut').then(() => {
next({ path: '/login' })
// window.location.href = `${process.env.VUE_APP_LOGIN}?redirect=${to.fullPath}&reLocation=${process.env.VUE_APP_NQ}`
})
}
})
.catch(err => {
store.dispatch('FedLogOut').then(() => {
Message.error(err)
next({ path: '/login' })
// window.location.href = `${process.env.VUE_APP_LOGIN}?redirect=${to.fullPath}&reLocation=${process.env.VUE_APP_NQ}`
})
})
// } else {
// alert('用户无权限')
// store.dispatch('FedLogOut').then(() => {
// next({ path: '/login' })
// // window.location.href = `${process.env.VUE_APP_LOGIN}?redirect=${to.fullPath}&reLocation=${process.env.VUE_APP_NQ}`
// })
// }
// })
// .catch(err => {
// store.dispatch('FedLogOut').then(() => {
// Message.error(err)
// next({ path: '/login' })
// // window.location.href = `${process.env.VUE_APP_LOGIN}?redirect=${to.fullPath}&reLocation=${process.env.VUE_APP_NQ}`
// })
// })
} else {
next()
// 没有动态改变权限的需求可直接next() 删除下方权限判断 ↓
......
......@@ -7,6 +7,7 @@ const getters = {
specialTag: state => state.user.specialTag,
roles: state => state.user.roles,
menuName: state => state.user.menuName,
menus: state => state.user.menus,
introduction: state => state.user.introduction,
permissions: state => state.user.permissions,
permission_routes: state => state.permission.routes,
......
......@@ -2,6 +2,8 @@ import { constantRoutes } from '@/router'
import { getRouters } from '@/api/menu'
import { getInfo } from '@/api/login'
import Layout from '@/layout/index'
import store from '@/store'
import Cookies from 'js-cookie'
const permission = {
state: {
......@@ -20,10 +22,31 @@ const permission = {
return new Promise(resolve => {
const menuList = menuName.menuName.split(',')
// 向后端请求路由数据
getInfo().then(res => {
// getInfo().then(res => {
// const newRoute = []
// // 根据点击的菜单名匹配对应的菜单
// res.data.menus.forEach(item => {
// menuList.forEach(menuItem => {
// if (item.meta.title === menuItem) {
// newRoute.push(item)
// }
// })
// })
// const accessedRoutes = filterAsyncRouter(newRoute)
// if (accessedRoutes[0]) {
// const firstChild = getFirstChild(accessedRoutes[0])
// accessedRoutes.push({ path: '*', redirect: '/404', hidden: true })
// accessedRoutes.push({ path: '/', redirect: firstChild, hidden: true })
// commit('SET_ROUTES', accessedRoutes)
// }
// const routers = { accessedRoutes: accessedRoutes, getRouters: newRoute }
// resolve(routers)
// })
const newRoute = []
const user = sessionStorage.getItem('userInfo')
const userInfo = JSON.parse(user)
// 根据点击的菜单名匹配对应的菜单
res.data.menus.forEach(item => {
userInfo.menus.forEach(item => {
menuList.forEach(menuItem => {
if (item.meta.title === menuItem) {
newRoute.push(item)
......@@ -38,9 +61,10 @@ const permission = {
commit('SET_ROUTES', accessedRoutes)
}
const routers = { accessedRoutes: accessedRoutes, getRouters: newRoute }
const role = Cookies.get('role')
commit('SET_ROLES', role)
resolve(routers)
})
})
}
}
}
......
......@@ -13,7 +13,8 @@ const user = {
roles: [],
permissions: [],
specialTag: '',
menuName: []
menuName: [],
menus: []
},
......@@ -38,6 +39,9 @@ const user = {
},
SET_PERMISSIONS: (state, permissions) => {
state.permissions = permissions
},
SET_MENUS: (state, menus) => {
state.menus = menus
}
},
......@@ -65,19 +69,20 @@ const user = {
getInfo(state.token).then(res => {
const user = res.data.user
const avatar = user.avatar === null ? require('@/assets/image/profile.jpg') : user.avatar
if (res.data.roles && res.data.roles.length > 0) { // 验证返回的roles是否是一个非空数组
commit('SET_ROLES', res.data.roles)
// const a = res.data.permissions.join(',')
// // TODO: clear this log
// console.log(`a`, a)
// Cookies.set('permissions', a)
} else {
commit('SET_ROLES', ['ROLE_DEFAULT'])
}
// if (res.data.roles && res.data.roles.length > 0) { // 验证返回的roles是否是一个非空数组
// commit('SET_ROLES', res.data.roles)
// } else {
// commit('SET_ROLES', ['ROLE_DEFAULT'])
// }
commit('SET_ROLES', [])
Cookies.set('role', res.data.roles)
commit('SET_PERMISSIONS', res.data.permissions)
Cookies.set('permissions', res.data.permissions)
commit('SET_NAME', user.username)
commit('SET_MENUS', res.data.menus)
commit('SET_SPECIALTAG', user.specialTag)
commit('SET_AVATAR', avatar)
sessionStorage.setItem('userInfo', JSON.stringify(res.data))
res.menuName = store.getters.menuName
resolve(res)
}).catch(error => {
......@@ -110,6 +115,7 @@ const user = {
commit('SET_PERMISSIONS', [])
commit('SET_MENUNAME', '')
removeToken()
sessionStorage.removeItem('userInfo')
resolve()
}).catch(error => {
reject(error)
......
......@@ -41,6 +41,7 @@
<script>
import { getInfo } from '@/api/login'
import store from '@/store'
export default {
name: 'Home',
......@@ -66,8 +67,9 @@ export default {
this.$store.dispatch('removeRole')
},
gotoIndex(menuName) {
getInfo().then(res => {
store.dispatch('GetInfo').then(res => {
if (res.data.permissions) {
// this.removeRole()
// 判断用户是否有点击菜单的权限
var index = 0
const routerItem = []
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment