feat(permission): 登录权限控制

duizhaopin_ui
duan 2 years ago
parent 42ff14adc1
commit 1acd37d881

@ -71,14 +71,11 @@ export default {
selectChange(val) { selectChange(val) {
if (val === 'loginout') { if (val === 'loginout') {
this.$store.commit('user/RESET_STATE') this.$store.commit('user/RESET_STATE')
this.$store.commit('permission/SET_ROUTES', [])
this.$router.push('/login') this.$router.push('/login')
return return
} }
this.$router.push('/' + val) this.$router.push('/' + val)
},
async logout() {
await this.$store.dispatch('user/logout')
this.$router.push(`/login?redirect=${this.$route.fullPath}`)
} }
} }
} }

@ -4,6 +4,7 @@ import store from './store'
import NProgress from 'nprogress' // progress bar import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style import 'nprogress/nprogress.css' // progress bar style
import getPageTitle from '@/utils/get-page-title' import getPageTitle from '@/utils/get-page-title'
import { filterAsyncRoutes } from '@/utils'
NProgress.configure({ showSpinner: false }) // NProgress Configuration NProgress.configure({ showSpinner: false }) // NProgress Configuration
@ -43,9 +44,11 @@ router.beforeEach(async(to, from, next) => {
next() next()
} else { } else {
store.commit('user/SET_CLASS', pat) store.commit('user/SET_CLASS', pat)
store.commit('permission/SET_ROUTES', asyncRoutes) const asr = filterAsyncRoutes(asyncRoutes, store.getters.roleData.drug_manage)
// store.commit('permission/SET_ROUTES', asr)
// store.commit('permission/SET_ROUTES', asyncRoutes)
resetRouter() resetRouter()
router.addRoutes(asyncRoutes) router.addRoutes(asr)
next({ ...to, replace: true }) next({ ...to, replace: true })
} }
} }

@ -1,16 +1,17 @@
import Layout from '@/layout' import Layout from '@/layout'
const reagentRouter = [ const reagentRouter = [
{ // {
path: '/reagent', // path: '/reagent',
hidden: true, // hidden: true,
redirect: '/reagent/mainoverview/index', // redirect: '/reagent/mainoverview/index',
meta: { classification: 'reagent' } // meta: { classification: 'reagent', module_code: 'MainOverview' }
}, // },
{ {
path: '/reagent/mainoverview', path: '/reagent/mainoverview',
component: Layout, component: Layout,
meta: { classification: 'reagent' }, redirect: '/reagent/mainoverview/index',
meta: { classification: 'reagent', module_code: 'MainOverview' },
children: [ children: [
{ {
path: 'index', path: 'index',
@ -23,7 +24,8 @@ const reagentRouter = [
{ {
path: '/reagent/report', path: '/reagent/report',
component: Layout, component: Layout,
meta: { classification: 'reagent' }, redirect: '/reagent/report/index',
meta: { classification: 'reagent', module_code: 'DataReportIndex' },
children: [ children: [
{ {
path: 'index', path: 'index',
@ -92,7 +94,8 @@ const reagentRouter = [
{ {
path: '/reagent/warehousing', path: '/reagent/warehousing',
component: Layout, component: Layout,
meta: { classification: 'reagent' }, redirect: '/reagent/warehousing/index',
meta: { classification: 'reagent', module_code: 'DataReportIndex' },
children: [ children: [
{ {
path: 'index', path: 'index',
@ -105,7 +108,8 @@ const reagentRouter = [
{ {
path: '/reagent/receiving', path: '/reagent/receiving',
component: Layout, component: Layout,
meta: { classification: 'reagent' }, redirect: '/reagent/receiving/index',
meta: { classification: 'reagent', module_code: 'DrugUseView' },
children: [ children: [
{ {
path: 'index', path: 'index',
@ -118,7 +122,8 @@ const reagentRouter = [
{ {
path: '/reagent/sendback', path: '/reagent/sendback',
component: Layout, component: Layout,
meta: { classification: 'reagent' }, redirect: '/reagent/sendback/index',
meta: { classification: 'reagent', module_code: 'DrugReturnView' },
children: [ children: [
{ {
path: 'index', path: 'index',
@ -131,7 +136,8 @@ const reagentRouter = [
{ {
path: '/reagent/weighing', path: '/reagent/weighing',
component: Layout, component: Layout,
meta: { classification: 'reagent' }, redirect: '/reagent/weighing/index',
meta: { classification: 'reagent', module_code: 'DrugWeigh' },
children: [ children: [
{ {
path: 'index', path: 'index',
@ -144,7 +150,8 @@ const reagentRouter = [
{ {
path: '/reagent/inventory', path: '/reagent/inventory',
component: Layout, component: Layout,
meta: { classification: 'reagent' }, redirect: '/reagent/inventory/index',
meta: { classification: 'reagent', module_code: 'StockTakingIndex' },
children: [ children: [
{ {
path: 'index', path: 'index',
@ -158,7 +165,7 @@ const reagentRouter = [
path: '/reagent/database', path: '/reagent/database',
component: Layout, component: Layout,
redirect: '/reagent/database/msds', redirect: '/reagent/database/msds',
meta: { title: '化学品数据库', icon: '化学品数据库', classification: 'reagent' }, meta: { title: '化学品数据库', icon: '化学品数据库', classification: 'reagent', module_code: 'ChemicaDatabase' },
children: [ children: [
{ {
path: 'msds', path: 'msds',
@ -177,7 +184,8 @@ const reagentRouter = [
{ {
path: '/reagent/management', path: '/reagent/management',
component: Layout, component: Layout,
meta: { classification: 'reagent' }, redirect: '/reagent/management/index',
meta: { classification: 'reagent', module_code: 'DrugIndex' },
children: [ children: [
{ {
path: 'index', path: 'index',
@ -190,7 +198,8 @@ const reagentRouter = [
{ {
path: '/reagent/client', path: '/reagent/client',
component: Layout, component: Layout,
meta: { classification: 'reagent' }, redirect: '/reagent/client/index',
meta: { classification: 'reagent', module_code: 'CabinetIndex' },
children: [ children: [
{ {
path: 'index', path: 'index',
@ -203,7 +212,8 @@ const reagentRouter = [
{ {
path: '/reagent/envrecord', path: '/reagent/envrecord',
component: Layout, component: Layout,
meta: { classification: 'reagent' }, redirect: '/reagent/envrecord/index',
meta: { classification: 'reagent', module_code: 'HumitureIndex' },
children: [ children: [
{ {
path: 'index', path: 'index',
@ -216,7 +226,8 @@ const reagentRouter = [
{ {
path: '/reagent/form', path: '/reagent/form',
component: Layout, component: Layout,
meta: { classification: 'reagent' }, redirect: '/reagent/form/index',
meta: { classification: 'reagent', module_code: 'DrugCustomForm' },
children: [ children: [
{ {
path: 'index', path: 'index',
@ -226,19 +237,19 @@ const reagentRouter = [
} }
] ]
}, },
{ // {
path: '/reagent/buy', // path: '/reagent/buy',
component: Layout, // component: Layout,
meta: { classification: 'reagent' }, // meta: { classification: 'reagent' },
children: [ // children: [
{ // {
path: 'index', // path: 'index',
name: 'Buy', // name: 'Buy',
component: () => import('@/views/reagent/buy/index'), // component: () => import('@/views/reagent/buy/index'),
meta: { title: '请购', icon: '请购', classification: 'reagent' } // meta: { title: '请购', icon: '请购', classification: 'reagent' }
} // }
] // ]
}, // },
{ {
path: '/gotohome', path: '/gotohome',
redirect: '/home', redirect: '/home',

@ -3,16 +3,11 @@
import Layout from '@/layout' import Layout from '@/layout'
const userRouter = [ const userRouter = [
{
path: '/user',
redirect: '/user/info/index',
hidden: true,
meta: { classification: 'user' }
},
{ {
path: '/user/info', path: '/user/info',
component: Layout, component: Layout,
meta: { classification: 'user' }, redirect: '/user/info/index',
meta: { classification: 'user', 'module_code': 'UserIndex' },
children: [ children: [
{ {
path: 'index', path: 'index',
@ -25,7 +20,8 @@ const userRouter = [
{ {
path: '/user/role', path: '/user/role',
component: Layout, component: Layout,
meta: { classification: 'user' }, redirect: '/user/role/index',
meta: { classification: 'user', module_code: 'UserRoleIndex' },
children: [ children: [
{ {
path: 'index', path: 'index',

@ -6,6 +6,7 @@ const getters = {
name: state => state.user.name, name: state => state.user.name,
permission_routes: state => state.permission.routes, permission_routes: state => state.permission.routes,
classification: state => state.user.classification, classification: state => state.user.classification,
isMain: state => state.settings.isMain isMain: state => state.settings.isMain,
roleData: state => state.permission.roleData
} }
export default getters export default getters

@ -36,13 +36,17 @@ export function filterAsyncRoutes(routes, roles) {
const state = { const state = {
routes: [], routes: [],
addRoutes: [] addRoutes: [],
roleData: {}
} }
const mutations = { const mutations = {
SET_ROUTES: (state, routes) => { SET_ROUTES: (state, routes) => {
state.addRoutes = routes state.addRoutes = routes
state.routes = constantRoutes.concat(routes) state.routes = constantRoutes.concat(routes)
},
SET_ROLE_DATA: (state, data) => {
state.roleData = data
} }
} }

@ -149,3 +149,15 @@ export function throttle(fn, time) {
} }
} }
export function filterAsyncRoutes(routers, roles) {
return routers.filter(item => {
if (item.path === '*') {
return true
}
if (item.meta && item.meta.title === '返回主页') {
return true
}
return !!roles.find(it => it.module_code === item.meta.module_code)
})
}

@ -71,6 +71,7 @@ service.interceptors.response.use(
type: 'warning' type: 'warning'
}).then(() => { }).then(() => {
store.commit('user/RESET_STATE') store.commit('user/RESET_STATE')
store.commit('permission/SET_ROUTES', [])
router.push('/login') router.push('/login')
}) })
} }

@ -70,8 +70,9 @@
<script> <script>
// import { validUsername } from '@/utils/validate' // import { validUsername } from '@/utils/validate'
import stringify from '@/utils/stringify' import stringify from '@/utils/stringify'
import { login } from '@/api/user/user' import { login, user_power } from '@/api/user/user'
import { asyncRoutes } from '@/router' import { asyncRoutes } from '@/router'
import { filterAsyncRoutes } from '@/utils'
export default { export default {
name: 'Login', name: 'Login',
data() { data() {
@ -131,9 +132,17 @@ export default {
this.loading = true this.loading = true
login(stringify(this.loginForm)).then(res => { login(stringify(this.loginForm)).then(res => {
this.$store.commit('user/SET_TOKEN', res.data.token) this.$store.commit('user/SET_TOKEN', res.data.token)
this.$store.commit('permission/SET_ROUTES', asyncRoutes) // this.$store.commit('permission/SET_ROUTES', asyncRoutes)
this.$router.addRoutes(asyncRoutes) // this.$router.addRoutes(asyncRoutes)
// this.$router.replace('/home')
user_power().then(resp => {
//
this.$store.commit('permission/SET_ROLE_DATA', resp.data)
const asr = filterAsyncRoutes(asyncRoutes, resp.data.drug_manage)
this.$store.commit('permission/SET_ROUTES', asr)
this.$router.addRoutes(asr)
this.$router.replace('/home') this.$router.replace('/home')
})
}).finally( }).finally(
() => { () => {
this.loading = false this.loading = false

Loading…
Cancel
Save