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

修改多次请求getinfo的问题

parent fec25cc2
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <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"> <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="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> <title><%= webpackConfig.name %></title>
</head> </head>
<body> <body>
......
...@@ -71,7 +71,7 @@ export default { ...@@ -71,7 +71,7 @@ export default {
data() { data() {
return { return {
appList: [], appList: [],
userName: this.$store.state.user.name, userName: '',
specialTag: this.$store.state.user.specialTag specialTag: this.$store.state.user.specialTag
} }
}, },
...@@ -83,6 +83,9 @@ export default { ...@@ -83,6 +83,9 @@ export default {
}, },
mounted() { mounted() {
// this.getInAdhibitions() // this.getInAdhibitions()
const user = sessionStorage.getItem('userInfo')
const userInfo = JSON.parse(user)
this.userName = userInfo.user.name
}, },
methods: { methods: {
handleCommand(param) { handleCommand(param) {
......
...@@ -26,55 +26,55 @@ router.beforeEach((to, from, next) => { ...@@ -26,55 +26,55 @@ router.beforeEach((to, from, next) => {
next({ path: '/login' }) next({ path: '/login' })
} else { } else {
if (store.getters.roles.length === 0) { if (store.getters.roles.length === 0) {
console.log('111')
// 判断当前用户是否已拉取完user_info信息 // 判断当前用户是否已拉取完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')
// 拉取user_info // if (store.getters.permissions && store.getters.permissions.length > 0) {
const roles = res.data.roles // 拉取user_info
const menuName = Cookies.get('menuname') const roles = store.getters.roles
store.dispatch('GenerateRoutes', { roles, menuName }).then((routers) => { 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) { // 判断用户是否有路由
// 测试 默认静态页面 if (routers.accessedRoutes && routers.accessedRoutes.length > 0 && routers.getRouters && routers.getRouters.length > 0) {
// store.dispatch('permission/generateRoutes', { roles }).then(accessRoutes => { // 测试 默认静态页面
// TODO: clear this log // store.dispatch('permission/generateRoutes', { roles }).then(accessRoutes => {
console.log(`routers.accessedRoutes`, routers.accessedRoutes) // 根据roles权限生成可访问的路由表
// 根据roles权限生成可访问的路由表 router.addRoutes(routers.accessedRoutes) // 动态添加可访问路由表
router.addRoutes(routers.accessedRoutes) // 动态添加可访问路由表 // TODO: clear this log
// TODO: clear this log // console.log(`routers.accessedRoutes`, routers.accessedRoutes)
// console.log(`routers.accessedRoutes`, routers.accessedRoutes) // 如果是从中间页面跳转系统首页,默认打开第一个菜单
// 如果是从中间页面跳转系统首页,默认打开第一个菜单 if (to.path === '/welcome') {
if (to.path === '/welcome') { next({ path: routers.accessedRoutes[0].children[0].path, replace: true })
next({ path: routers.accessedRoutes[0].children[0].path, replace: true }) } else {
} else { next({ ...to, replace: true }) // hack方法 确保addRoutes已完成
next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 }
} // next({ ...to, replace: true }) // hack方法 确保addRoutes已完成
// next({ ...to, replace: true }) // hack方法 确保addRoutes已完成
}
// 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}`
// })
// }
})
} 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}`
})
} }
// 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 => { // } else {
store.dispatch('FedLogOut').then(() => { // alert('用户无权限')
Message.error(err) // store.dispatch('FedLogOut').then(() => {
next({ path: '/login' }) // next({ path: '/login' })
// window.location.href = `${process.env.VUE_APP_LOGIN}?redirect=${to.fullPath}&reLocation=${process.env.VUE_APP_NQ}` // // 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 { } else {
next() next()
// 没有动态改变权限的需求可直接next() 删除下方权限判断 ↓ // 没有动态改变权限的需求可直接next() 删除下方权限判断 ↓
......
...@@ -7,6 +7,7 @@ const getters = { ...@@ -7,6 +7,7 @@ const getters = {
specialTag: state => state.user.specialTag, specialTag: state => state.user.specialTag,
roles: state => state.user.roles, roles: state => state.user.roles,
menuName: state => state.user.menuName, menuName: state => state.user.menuName,
menus: state => state.user.menus,
introduction: state => state.user.introduction, introduction: state => state.user.introduction,
permissions: state => state.user.permissions, permissions: state => state.user.permissions,
permission_routes: state => state.permission.routes, permission_routes: state => state.permission.routes,
......
...@@ -2,6 +2,8 @@ import { constantRoutes } from '@/router' ...@@ -2,6 +2,8 @@ import { constantRoutes } from '@/router'
import { getRouters } from '@/api/menu' import { getRouters } from '@/api/menu'
import { getInfo } from '@/api/login' import { getInfo } from '@/api/login'
import Layout from '@/layout/index' import Layout from '@/layout/index'
import store from '@/store'
import Cookies from 'js-cookie'
const permission = { const permission = {
state: { state: {
...@@ -20,26 +22,48 @@ const permission = { ...@@ -20,26 +22,48 @@ const permission = {
return new Promise(resolve => { return new Promise(resolve => {
const menuList = menuName.menuName.split(',') const menuList = menuName.menuName.split(',')
// 向后端请求路由数据 // 向后端请求路由数据
getInfo().then(res => { // getInfo().then(res => {
const newRoute = [] // const newRoute = []
// 根据点击的菜单名匹配对应的菜单 // // 根据点击的菜单名匹配对应的菜单
res.data.menus.forEach(item => { // res.data.menus.forEach(item => {
menuList.forEach(menuItem => { // menuList.forEach(menuItem => {
if (item.meta.title === menuItem) { // if (item.meta.title === menuItem) {
newRoute.push(item) // 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)
// 根据点击的菜单名匹配对应的菜单
userInfo.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 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 }
const role = Cookies.get('role')
commit('SET_ROLES', role)
resolve(routers)
}) })
} }
} }
......
...@@ -13,7 +13,8 @@ const user = { ...@@ -13,7 +13,8 @@ const user = {
roles: [], roles: [],
permissions: [], permissions: [],
specialTag: '', specialTag: '',
menuName: [] menuName: [],
menus: []
}, },
...@@ -38,6 +39,9 @@ const user = { ...@@ -38,6 +39,9 @@ const user = {
}, },
SET_PERMISSIONS: (state, permissions) => { SET_PERMISSIONS: (state, permissions) => {
state.permissions = permissions state.permissions = permissions
},
SET_MENUS: (state, menus) => {
state.menus = menus
} }
}, },
...@@ -65,19 +69,20 @@ const user = { ...@@ -65,19 +69,20 @@ const user = {
getInfo(state.token).then(res => { getInfo(state.token).then(res => {
const user = res.data.user const user = res.data.user
const avatar = user.avatar === null ? require('@/assets/image/profile.jpg') : user.avatar const avatar = user.avatar === null ? require('@/assets/image/profile.jpg') : user.avatar
if (res.data.roles && res.data.roles.length > 0) { // 验证返回的roles是否是一个非空数组 // if (res.data.roles && res.data.roles.length > 0) { // 验证返回的roles是否是一个非空数组
commit('SET_ROLES', res.data.roles) // commit('SET_ROLES', res.data.roles)
// const a = res.data.permissions.join(',') // } else {
// // TODO: clear this log // commit('SET_ROLES', ['ROLE_DEFAULT'])
// console.log(`a`, a) // }
// Cookies.set('permissions', a) commit('SET_ROLES', [])
} else { Cookies.set('role', res.data.roles)
commit('SET_ROLES', ['ROLE_DEFAULT'])
}
commit('SET_PERMISSIONS', res.data.permissions) commit('SET_PERMISSIONS', res.data.permissions)
Cookies.set('permissions', res.data.permissions)
commit('SET_NAME', user.username) commit('SET_NAME', user.username)
commit('SET_MENUS', res.data.menus)
commit('SET_SPECIALTAG', user.specialTag) commit('SET_SPECIALTAG', user.specialTag)
commit('SET_AVATAR', avatar) commit('SET_AVATAR', avatar)
sessionStorage.setItem('userInfo', JSON.stringify(res.data))
res.menuName = store.getters.menuName res.menuName = store.getters.menuName
resolve(res) resolve(res)
}).catch(error => { }).catch(error => {
...@@ -110,6 +115,7 @@ const user = { ...@@ -110,6 +115,7 @@ const user = {
commit('SET_PERMISSIONS', []) commit('SET_PERMISSIONS', [])
commit('SET_MENUNAME', '') commit('SET_MENUNAME', '')
removeToken() removeToken()
sessionStorage.removeItem('userInfo')
resolve() resolve()
}).catch(error => { }).catch(error => {
reject(error) reject(error)
......
...@@ -41,6 +41,7 @@ ...@@ -41,6 +41,7 @@
<script> <script>
import { getInfo } from '@/api/login' import { getInfo } from '@/api/login'
import store from '@/store'
export default { export default {
name: 'Home', name: 'Home',
...@@ -66,8 +67,9 @@ export default { ...@@ -66,8 +67,9 @@ export default {
this.$store.dispatch('removeRole') this.$store.dispatch('removeRole')
}, },
gotoIndex(menuName) { gotoIndex(menuName) {
getInfo().then(res => { store.dispatch('GetInfo').then(res => {
if (res.data.permissions) { if (res.data.permissions) {
// this.removeRole()
// 判断用户是否有点击菜单的权限 // 判断用户是否有点击菜单的权限
var index = 0 var index = 0
const routerItem = [] 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