permission.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import Vue from 'vue'
  2. import router from './router'
  3. import store from './store'
  4. import NProgress from 'nprogress' // progress bar
  5. import 'nprogress/nprogress.css' // progress bar style
  6. import notification from 'ant-design-vue/es/notification'
  7. import { ACCESS_TOKEN } from '@/store/mutation-types'
  8. NProgress.configure({ showSpinner: false }) // NProgress Configuration
  9. const whiteList = ['login', 'register', 'registerResult'] // no redirect whitelist
  10. router.beforeEach((to, from, next) => {
  11. NProgress.start() // start progress bar
  12. if (Vue.ls.get(ACCESS_TOKEN)) {
  13. /* has token */
  14. if (to.path === '/user/login') {
  15. next({ path: '/dashboard/workplace' })
  16. NProgress.done()
  17. } else {
  18. if (store.getters.roles.length === 0) {
  19. store
  20. .dispatch('GetInfo')
  21. .then(res => {
  22. const roles = res.result && res.result.role
  23. store.dispatch('GenerateRoutes', { roles }).then(() => {
  24. // 根据roles权限生成可访问的路由表
  25. // 动态添加可访问路由表
  26. router.addRoutes(store.getters.addRouters)
  27. const redirect = decodeURIComponent(from.query.redirect || to.path)
  28. if (to.path === redirect) {
  29. // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
  30. next({ ...to, replace: true })
  31. } else {
  32. // 跳转到目的路由
  33. next({ path: redirect })
  34. }
  35. })
  36. })
  37. .catch(() => {
  38. notification.error({
  39. message: '错误',
  40. description: '请求用户信息失败,请重试'
  41. })
  42. store.dispatch('Logout').then(() => {
  43. next({ path: '/user/login', query: { redirect: to.fullPath } })
  44. })
  45. })
  46. } else {
  47. next()
  48. }
  49. }
  50. } else {
  51. if (whiteList.includes(to.name)) {
  52. // 在免登录白名单,直接进入
  53. next()
  54. } else {
  55. next({ path: '/user/login', query: { redirect: to.fullPath } })
  56. NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
  57. }
  58. }
  59. })
  60. router.afterEach(() => {
  61. NProgress.done() // finish progress bar
  62. })
  63. /**
  64. * Action 权限指令
  65. * 指令用法:
  66. * - 在需要控制 action 级别权限的组件上使用 v-action:[method] , 如下:
  67. * <i-button v-action:add >添加用户</a-button>
  68. * <a-button v-action:delete>删除用户</a-button>
  69. * <a v-action:edit @click="edit(record)">修改</a>
  70. *
  71. * - 当前用户没有权限时,组件上使用了该指令则会被隐藏
  72. * - 当后台权限跟 pro 提供的模式不同时,只需要针对这里的权限过滤进行修改即可
  73. *
  74. * @see https://github.com/sendya/ant-design-pro-vue/pull/53
  75. */
  76. const action = Vue.directive('action', {
  77. bind: function (el, binding, vnode) {
  78. const actionName = binding.arg
  79. const roles = store.getters.roles
  80. const permissionId = vnode.context.$route.meta.permission
  81. let actions = []
  82. roles.permissions.forEach(p => {
  83. if (p.permissionId !== permissionId) {
  84. return
  85. }
  86. actions = p.actionList
  87. })
  88. if (actions.indexOf(actionName) < 0) {
  89. setTimeout(() => {
  90. if (el.parentNode == null) {
  91. el.style.display = 'none'
  92. } else {
  93. el.parentNode.removeChild(el)
  94. }
  95. }, 10)
  96. }
  97. }
  98. })
  99. export {
  100. action
  101. }