-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Closed
Labels
questionFurther information is requestedFurther information is requested
Description
请教一下想要不同权限不同菜单应该怎么实现?
我后端返回的只有身份类型,比如(admin,user);
我在前端应该怎么改写才能直接跳到对应菜单呢;
我改写到现在,变成了全部路由菜单都显示出来了
理想效果:(能直接这样写就最简单了,这个可行吗)
permiss.js
import router from './router'
import storage from 'store'
import NProgress from 'nprogress' // progress bar
import '@/components/NProgress/nprogress.less' // progress bar custom style
import { setDocumentTitle, domTitle } from '@/utils/domUtil'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import { i18nRender } from '@/locales'
NProgress.configure({ showSpinner: false }) // NProgress Configuration
// const allowList = ['login', 'register', 'registerResult'] // no redirect allowList
const loginRoutePath = '/user/login'
// const defaultRoutePath = '/dashboard/workplace'
router.beforeEach((to, from, next) => {
NProgress.start() // start progress bar
to.meta && typeof to.meta.title !== 'undefined' && setDocumentTitle(`${i18nRender(to.meta.title)} - ${domTitle}`)
/* has token */
const token = storage.get(ACCESS_TOKEN)
// https://www.cnblogs.com/AdolphWilliam/p/15562351.html
if (token) { // 是否存在token
console.log('token存在')
next()
} else {
if (to.path === loginRoutePath) { // 如果是登录页面路径,就直接next()
next()
} else { // 不然就跳转到登录;
next(loginRoutePath)
}
}
})
router.afterEach(() => {
NProgress.done() // finish progress bar
})
store/module/user.js
import storage from 'store'
import expirePlugin from 'store/plugins/expire'
import { getInfo, logout } from '@/api/login'
import { login } from '@/api/login_api'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import { welcome } from '@/utils/util'
// 修改教程:https://zhuanlan.zhihu.com/p/431139611
storage.addPlugin(expirePlugin)
const user = {
state: {
token: '',
isInitPwd: '',
loginTime: '',
loginIP: '',
roleType: '',
userName: '',
userID: '',
lastLoginTime: '',
lastLoginIP: '',
roles: []
},
mutations: {
SET_TOKEN: (state, token) => {
state.token = token
},
SET_ISINITPWD: (state, isInitPwd) => {
state.isInitPwd = isInitPwd
},
SET_LASTLOGINTIME: (state, lastLoginTime) => {
state.lastLoginTime = lastLoginTime
},
SET_LOGINTIME: (state, loginTime) => {
state.loginTime = loginTime
},
SET_LOGINIP: (state, loginIP) => {
state.loginIP = loginIP
},
SET_USERNAME: (state, userName) => {
state.userName = userName
},
SET_USERID: (state, userID) => {
state.roleType = userID
},
SET_LASTLOGINIP: (state, lastLoginIP) => {
state.lastLoginIP = lastLoginIP
},
SET_ROLETYPE: (state, roleType) => {
state.roleType = roleType
},
SET_ROLES: (state, roles) => {
state.roles = roles
}
},
actions: {
// 登录
Login ({ commit }, userInfo) {
return new Promise((resolve, reject) => {
login(userInfo).then(response => {
const result = response.data
console.log('有东西吗', result)
storage.set(ACCESS_TOKEN, result.token, new Date().getTime() + 7 * 24 * 60 * 60 * 1000)
storage.set('USERNAME', result.username, new Date().getTime() + 7 * 24 * 60 * 60 * 1000)
commit('SET_TOKEN', result.token)
commit('SET_ISINITPWD', result.isInitPwd)
commit('SET_LOGINIP', result.loginIP)
commit('SET_LOGINTIME', result.loginTime)
commit('SET_LASTLOGINIP', result.lastLoginIP)
commit('SET_LASTLOGINTIME', result.lastLoginTime)
commit('SET_USERID', result.userID)
commit('SET_USERNAME', result.username)
const role = { id: 'admin', name: '管理员', permissions: [ { 'roleId': 'admin', 'permissionId': 'admin' } ] }
role.permissionList = ['dashboard_admin', 'manage', 'accountm', 'document', 'document_admin', 'notice', 'notice_all', 'notice_uptwo', 'share', 'share_admin', 'feedback', 'feedback_admin', 'help']
commit('SET_ROLES', result.role)
resolve()
}).catch(error => {
reject(error)
})
})
},
// 登出
Logout ({ commit, state }) {
return new Promise((resolve) => {
logout(state.token).then(() => {
commit('SET_TOKEN', '')
// commit('SET_ROLES', [])
storage.remove(ACCESS_TOKEN)
resolve()
}).catch((err) => {
console.log('logout fail:', err)
// resolve()
}).finally(() => {
})
})
}
}
}
export default user
Metadata
Metadata
Assignees
Labels
questionFurther information is requestedFurther information is requested
