Compare commits
10 Commits
permission
...
master
Author | SHA1 | Date |
---|---|---|
|
4c18a3f47b | |
|
714ded1155 | |
|
785f19f551 | |
|
75ba8cf038 | |
|
abcd5d810d | |
|
8c4e38fb1f | |
|
fe326aa2b0 | |
|
c9cb7db3ce | |
|
cd8d52bfbd | |
|
879318692c |
|
@ -8,6 +8,15 @@
|
||||||
|
|
||||||
目前版本为 `v4.0+` 基于 `vue-cli` 进行构建,若你想使用旧版本,可以切换分支到[tag/3.11.0](https://github.com/PanJiaChen/vue-admin-template/tree/tag/3.11.0),它不依赖 `vue-cli`。
|
目前版本为 `v4.0+` 基于 `vue-cli` 进行构建,若你想使用旧版本,可以切换分支到[tag/3.11.0](https://github.com/PanJiaChen/vue-admin-template/tree/tag/3.11.0),它不依赖 `vue-cli`。
|
||||||
|
|
||||||
|
<p align="center">
|
||||||
|
<b>SPONSORED BY</b>
|
||||||
|
</p>
|
||||||
|
<p align="center">
|
||||||
|
<a href="https://finclip.com?from=vue_element" title="FinClip" target="_blank">
|
||||||
|
<img height="200px" src="https://gitee.com/panjiachen/gitee-cdn/raw/master/vue%E8%B5%9E%E5%8A%A9.png" title="FinClip">
|
||||||
|
</a>
|
||||||
|
</p>
|
||||||
|
|
||||||
## Extra
|
## Extra
|
||||||
|
|
||||||
如果你想要根据用户角色来动态生成侧边栏和 router,你可以使用该分支[permission-control](https://github.com/PanJiaChen/vue-admin-template/tree/permission-control)
|
如果你想要根据用户角色来动态生成侧边栏和 router,你可以使用该分支[permission-control](https://github.com/PanJiaChen/vue-admin-template/tree/permission-control)
|
||||||
|
|
10
README.md
10
README.md
|
@ -9,8 +9,16 @@ English | [简体中文](./README-zh.md)
|
||||||
|
|
||||||
**The current version is `v4.0+` build on `vue-cli`. If you want to use the old version , you can switch branch to [tag/3.11.0](https://github.com/PanJiaChen/vue-admin-template/tree/tag/3.11.0), it does not rely on `vue-cli`**
|
**The current version is `v4.0+` build on `vue-cli`. If you want to use the old version , you can switch branch to [tag/3.11.0](https://github.com/PanJiaChen/vue-admin-template/tree/tag/3.11.0), it does not rely on `vue-cli`**
|
||||||
|
|
||||||
## Build Setup
|
<p align="center">
|
||||||
|
<b>SPONSORED BY</b>
|
||||||
|
</p>
|
||||||
|
<p align="center">
|
||||||
|
<a href="https://finclip.com?from=vue_element" title="FinClip" target="_blank">
|
||||||
|
<img height="200px" src="https://gitee.com/panjiachen/gitee-cdn/raw/master/vue%E8%B5%9E%E5%8A%A9.png" title="FinClip">
|
||||||
|
</a>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
## Build Setup
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# clone the project
|
# clone the project
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
:collapse-transition="false"
|
:collapse-transition="false"
|
||||||
mode="vertical"
|
mode="vertical"
|
||||||
>
|
>
|
||||||
<sidebar-item v-for="route in permission_routes" :key="route.path" :item="route" :base-path="route.path" />
|
<sidebar-item v-for="route in routes" :key="route.path" :item="route" :base-path="route.path" />
|
||||||
</el-menu>
|
</el-menu>
|
||||||
</el-scrollbar>
|
</el-scrollbar>
|
||||||
</div>
|
</div>
|
||||||
|
@ -28,9 +28,11 @@ export default {
|
||||||
components: { SidebarItem, Logo },
|
components: { SidebarItem, Logo },
|
||||||
computed: {
|
computed: {
|
||||||
...mapGetters([
|
...mapGetters([
|
||||||
'permission_routes',
|
|
||||||
'sidebar'
|
'sidebar'
|
||||||
]),
|
]),
|
||||||
|
routes() {
|
||||||
|
return this.$router.options.routes
|
||||||
|
},
|
||||||
activeMenu() {
|
activeMenu() {
|
||||||
const route = this.$route
|
const route = this.$route
|
||||||
const { meta, path } = route
|
const { meta, path } = route
|
||||||
|
|
|
@ -26,25 +26,15 @@ router.beforeEach(async(to, from, next) => {
|
||||||
next({ path: '/' })
|
next({ path: '/' })
|
||||||
NProgress.done()
|
NProgress.done()
|
||||||
} else {
|
} else {
|
||||||
// determine whether the user has obtained his permission roles through getInfo
|
const hasGetUserInfo = store.getters.name
|
||||||
const hasRoles = store.getters.roles && store.getters.roles.length > 0
|
if (hasGetUserInfo) {
|
||||||
if (hasRoles) {
|
|
||||||
next()
|
next()
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
// get user info
|
// get user info
|
||||||
// note: roles must be a object array! such as: ['admin'] or ,['developer','editor']
|
await store.dispatch('user/getInfo')
|
||||||
const { roles } = await store.dispatch('user/getInfo')
|
|
||||||
|
|
||||||
// generate accessible routes map based on roles
|
next()
|
||||||
const accessRoutes = await store.dispatch('permission/generateRoutes', roles)
|
|
||||||
|
|
||||||
// dynamically add accessible routes
|
|
||||||
router.addRoutes(accessRoutes)
|
|
||||||
|
|
||||||
// hack method to ensure that addRoutes is complete
|
|
||||||
// set the replace: true, so the navigation will not leave a history record
|
|
||||||
next({ ...to, replace: true })
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// remove token and go to login page to re-login
|
// remove token and go to login page to re-login
|
||||||
await store.dispatch('user/resetToken')
|
await store.dispatch('user/resetToken')
|
||||||
|
|
|
@ -88,14 +88,8 @@ export const constantRoutes = [
|
||||||
meta: { title: 'Form', icon: 'form' }
|
meta: { title: 'Form', icon: 'form' }
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
},
|
||||||
]
|
|
||||||
|
|
||||||
/**
|
|
||||||
* asyncRoutes
|
|
||||||
* the routes that need to be dynamically loaded based on user roles
|
|
||||||
*/
|
|
||||||
export const asyncRoutes = [
|
|
||||||
{
|
{
|
||||||
path: '/nested',
|
path: '/nested',
|
||||||
component: Layout,
|
component: Layout,
|
||||||
|
@ -149,6 +143,7 @@ export const asyncRoutes = [
|
||||||
{
|
{
|
||||||
path: 'menu2',
|
path: 'menu2',
|
||||||
component: () => import('@/views/nested/menu2/index'),
|
component: () => import('@/views/nested/menu2/index'),
|
||||||
|
name: 'Menu2',
|
||||||
meta: { title: 'menu2' }
|
meta: { title: 'menu2' }
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
|
@ -3,8 +3,6 @@ const getters = {
|
||||||
device: state => state.app.device,
|
device: state => state.app.device,
|
||||||
token: state => state.user.token,
|
token: state => state.user.token,
|
||||||
avatar: state => state.user.avatar,
|
avatar: state => state.user.avatar,
|
||||||
name: state => state.user.name,
|
name: state => state.user.name
|
||||||
roles: state => state.user.roles,
|
|
||||||
permission_routes: state => state.permission.routes
|
|
||||||
}
|
}
|
||||||
export default getters
|
export default getters
|
||||||
|
|
|
@ -2,7 +2,6 @@ import Vue from 'vue'
|
||||||
import Vuex from 'vuex'
|
import Vuex from 'vuex'
|
||||||
import getters from './getters'
|
import getters from './getters'
|
||||||
import app from './modules/app'
|
import app from './modules/app'
|
||||||
import permission from './modules/permission'
|
|
||||||
import settings from './modules/settings'
|
import settings from './modules/settings'
|
||||||
import user from './modules/user'
|
import user from './modules/user'
|
||||||
|
|
||||||
|
@ -11,7 +10,6 @@ Vue.use(Vuex)
|
||||||
const store = new Vuex.Store({
|
const store = new Vuex.Store({
|
||||||
modules: {
|
modules: {
|
||||||
app,
|
app,
|
||||||
permission,
|
|
||||||
settings,
|
settings,
|
||||||
user
|
user
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,69 +0,0 @@
|
||||||
import { asyncRoutes, constantRoutes } from '@/router'
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Use meta.role to determine if the current user has permission
|
|
||||||
* @param roles
|
|
||||||
* @param route
|
|
||||||
*/
|
|
||||||
function hasPermission(roles, route) {
|
|
||||||
if (route.meta && route.meta.roles) {
|
|
||||||
return roles.some(role => route.meta.roles.includes(role))
|
|
||||||
} else {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Filter asynchronous routing tables by recursion
|
|
||||||
* @param routes asyncRoutes
|
|
||||||
* @param roles
|
|
||||||
*/
|
|
||||||
export function filterAsyncRoutes(routes, roles) {
|
|
||||||
const res = []
|
|
||||||
|
|
||||||
routes.forEach(route => {
|
|
||||||
const tmp = { ...route }
|
|
||||||
if (hasPermission(roles, tmp)) {
|
|
||||||
if (tmp.children) {
|
|
||||||
tmp.children = filterAsyncRoutes(tmp.children, roles)
|
|
||||||
}
|
|
||||||
res.push(tmp)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
return res
|
|
||||||
}
|
|
||||||
|
|
||||||
const state = {
|
|
||||||
routes: [],
|
|
||||||
addRoutes: []
|
|
||||||
}
|
|
||||||
|
|
||||||
const mutations = {
|
|
||||||
SET_ROUTES: (state, routes) => {
|
|
||||||
state.addRoutes = routes
|
|
||||||
state.routes = constantRoutes.concat(routes)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const actions = {
|
|
||||||
generateRoutes({ commit }, roles) {
|
|
||||||
return new Promise(resolve => {
|
|
||||||
let accessedRoutes
|
|
||||||
if (roles.includes('admin')) {
|
|
||||||
accessedRoutes = asyncRoutes || []
|
|
||||||
} else {
|
|
||||||
accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
|
|
||||||
}
|
|
||||||
commit('SET_ROUTES', accessedRoutes)
|
|
||||||
resolve(accessedRoutes)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default {
|
|
||||||
namespaced: true,
|
|
||||||
state,
|
|
||||||
mutations,
|
|
||||||
actions
|
|
||||||
}
|
|
|
@ -6,8 +6,7 @@ const getDefaultState = () => {
|
||||||
return {
|
return {
|
||||||
token: getToken(),
|
token: getToken(),
|
||||||
name: '',
|
name: '',
|
||||||
avatar: '',
|
avatar: ''
|
||||||
roles: []
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,9 +24,6 @@ const mutations = {
|
||||||
},
|
},
|
||||||
SET_AVATAR: (state, avatar) => {
|
SET_AVATAR: (state, avatar) => {
|
||||||
state.avatar = avatar
|
state.avatar = avatar
|
||||||
},
|
|
||||||
SET_ROLES: (state, roles) => {
|
|
||||||
state.roles = roles
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,17 +50,11 @@ const actions = {
|
||||||
const { data } = response
|
const { data } = response
|
||||||
|
|
||||||
if (!data) {
|
if (!data) {
|
||||||
reject('Verification failed, please Login again.')
|
return reject('Verification failed, please Login again.')
|
||||||
}
|
}
|
||||||
|
|
||||||
const { roles, name, avatar } = data
|
const { name, avatar } = data
|
||||||
|
|
||||||
// roles must be a non-empty array
|
|
||||||
if (!roles || roles.length <= 0) {
|
|
||||||
reject('getInfo: roles must be a non-null array!')
|
|
||||||
}
|
|
||||||
|
|
||||||
commit('SET_ROLES', roles)
|
|
||||||
commit('SET_NAME', name)
|
commit('SET_NAME', name)
|
||||||
commit('SET_AVATAR', avatar)
|
commit('SET_AVATAR', avatar)
|
||||||
resolve(data)
|
resolve(data)
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
<template>
|
<template>
|
||||||
<div class="dashboard-container">
|
<div class="dashboard-container">
|
||||||
<div class="dashboard-text">name: {{ name }}</div>
|
<div class="dashboard-text">name: {{ name }}</div>
|
||||||
<div class="dashboard-text">roles: <span v-for="role in roles" :key="role">{{ role }}</span></div>
|
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
@ -12,8 +11,7 @@ export default {
|
||||||
name: 'Dashboard',
|
name: 'Dashboard',
|
||||||
computed: {
|
computed: {
|
||||||
...mapGetters([
|
...mapGetters([
|
||||||
'name',
|
'name'
|
||||||
'roles'
|
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -80,17 +80,6 @@ module.exports = {
|
||||||
})
|
})
|
||||||
.end()
|
.end()
|
||||||
|
|
||||||
// set preserveWhitespace
|
|
||||||
config.module
|
|
||||||
.rule('vue')
|
|
||||||
.use('vue-loader')
|
|
||||||
.loader('vue-loader')
|
|
||||||
.tap(options => {
|
|
||||||
options.compilerOptions.preserveWhitespace = true
|
|
||||||
return options
|
|
||||||
})
|
|
||||||
.end()
|
|
||||||
|
|
||||||
config
|
config
|
||||||
.when(process.env.NODE_ENV !== 'development',
|
.when(process.env.NODE_ENV !== 'development',
|
||||||
config => {
|
config => {
|
||||||
|
|
Loading…
Reference in New Issue