import router from './router'
import store from './store'
import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar 进度条插件
import 'nprogress/nprogress.css'// progress bar style 导入样式 否则看不到效果
import { getToken } from '@/utils/auth' // getToken from cookie
NProgress.configure({ showSpinner: false })// NProgress Configuration 显示右上角螺旋加载提示
// permission judge function
function hasPermission(perms, permissions) {
console.log('权限', perms, permissions)
if (perms.indexOf('*') >= 0) return true // admin permission passed directly
if (!permissions) return true
// some遍历 返回布尔值 数组中有一个为true则返回
return perms.some(perm => permissions.indexOf(perm) >= 0)
}
// 白名单 不需要token登录的页面
const whiteList = ['/login', '/auth-redirect']// no redirect whitelist
router.beforeEach((to, from, next) => {
NProgress.start() // start progress bar 开启进度条
if (getToken()) { // determine if there has token
/* has token*/
if (to.path === '/login') {
// 有token 并且要访问的是login页面,则去首页
next({ path: '/' })
// 关闭进度条
NProgress.done() // if current page is dashboard will not trigger afterEach hook, so manually handle it
} else {
if (store.getters.perms.length === 0) { // 判断当前用户是否已拉取完user_info信息
// 没有用户信息 调取用户信息API
store.dispatch('GetUserInfo').then(res => { // 拉取user_info
const perms = res.data.data.perms // note: perms must be a array! such as: ['GET /aaa','POST /bbb']
store.dispatch('GenerateRoutes', { perms }).then(() => { // 根据perms权限生成可访问的路由表
// 动态添加更多的路由规则 参数:数组(立即访问刚刚添加的路由不能访问 可能白屏)
router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
// 确保动态添加的路由已经完全加载上去
next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
})
}).catch((err) => {
// 退出登录
store.dispatch('FedLogOut').then(() => {
Message.error(err || 'Verification failed, please login again')
next({ path: '/' })
})
})
} else {
// 没有动态改变权限的需求可直接next() 删除下方权限判断 ↓
if (hasPermission(store.getters.perms, to.meta.perms)) {
next()
} else {
next({ path: '/401', replace: true, query: { noGoBack: true }})
}
// 可删 ↑
}
}
} else {
/* has no token*/
// 没有找到则返回-1
if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单,直接进入
next()
} else {
// 需要登录的页面
next(`/login?redirect=${to.path}`) // 否则全部重定向到登录页
NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
}
}
})
router.afterEach(() => {
NProgress.done() // finish progress bar
})
import permission from './permission'
const install = function(Vue) {
Vue.directive('permission', permission)
}
if (window.Vue) {
window['permission'] = permission
Vue.use(install); // eslint-disable-line
}
permission.install = install
export default permission
import store from '@/store'
export default{
// 自定义指令
inserted(el, binding, vnode) {
const { value } = binding
const perms = store.getters && store.getters.perms // 当前用户的角色
// instanceof 运算符用来检测 constructor.prototype构造函数的原型 是否存在于参数 object 的原型链上。
if (value && value instanceof Array && value.length > 0) {
const permissions = value
var hasPermission = false
// indexOf 找到了返回该索引 找不到返回-1
if (perms.indexOf('*') >= 0) {
hasPermission = true
} else {
hasPermission = perms.some(perm => { // some 返回布尔值 判断数组中有一个元素符合要求就返回true
return permissions.includes(perm) // includes判断一个数组是否包含另一个指定的值 包含返回true 否则返回false(返回布尔值)
})
}
if (!hasPermission) { // if判断条件为true才进入
el.parentNode && el.parentNode.removeChild(el)
}
} else {
throw new Error(`need perms! Like v-permission="['GET /aaa','POST /bbb']"`)
}
}
}
main.js
vuex里面的模块