permission.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import router from './router'
  2. import store from './store'
  3. import { MessageBox, Message } from 'element-ui'
  4. import NProgress from 'nprogress'
  5. import 'nprogress/nprogress.css'
  6. import methods from '@/utils/methodsTool';
  7. import { getToken } from '@/utils/auth'
  8. import $api from "@/api/api"
  9. NProgress.configure({ showSpinner: false })
  10. const whiteList = ['/login', '/auth-redirect', '/bind', '/register']
  11. router.beforeEach(async (to, from, next) => {
  12. if (to.path !== from.path) {
  13. NProgress.start();
  14. }
  15. if (store.state.user.TENANT_NANE === null) {
  16. await store.dispatch('findTenantId')
  17. }
  18. if (store.state.user.TENANT_NANE === '' && methods.getQueryVariable('TenantId')) {
  19. sessionStorage.TenantId = methods.getQueryVariable('TenantId')
  20. }
  21. if (store.state.user.TENANT_NANE || sessionStorage.TenantId) {
  22. if (!store.state.user.companyName) {
  23. store.dispatch('footerData')
  24. }
  25. }
  26. if (getToken()) {
  27. to.meta.title && store.dispatch('settings/setTitle', to.meta.title)
  28. /* has token*/
  29. if (to.path === '/login') {
  30. next({ path: '/' })
  31. NProgress.done()
  32. } else {
  33. if (store.getters.roles.length === 0) {
  34. // 判断当前用户是否已拉取完user_info信息
  35. store.dispatch('GetInfo').then(() => {
  36. store.dispatch('GenerateRoutes').then(accessRoutes => {
  37. // 根据roles权限生成可访问的路由表
  38. router.addRoutes(accessRoutes) // 动态添加可访问路由表
  39. next({ ...to, replace: true }) // hack方法 确保addRoutes已完成
  40. })
  41. }).catch(err => {
  42. store.dispatch('LogOut').then(() => {
  43. Message.error(err)
  44. next({ path: '/' })
  45. })
  46. })
  47. } else {
  48. if (!store.state.user.TENANT_NANE && !to.query.TenantId) {
  49. to.query.TenantId = sessionStorage.TenantId
  50. next(to)
  51. } else {
  52. if (!to.path.includes('/user/profile')) {
  53. checkFunc()
  54. }
  55. console.log("to:", to)
  56. next()
  57. }
  58. }
  59. }
  60. } else {
  61. // 没有token
  62. if (whiteList.indexOf(to.path) !== -1) {
  63. // 在免登录白名单,直接进入
  64. next()
  65. } else {
  66. if (store.state.user.TENANT_NANE || sessionStorage.TenantId == undefined) {
  67. next(`/login`) // 否则全部重定向到登录页
  68. } else {
  69. next(`/login?redirect=${to.fullPath}&TenantId=${sessionStorage.TenantId}`) // 否则全部重定向到登录页
  70. }
  71. NProgress.done()
  72. }
  73. }
  74. })
  75. function checkFunc() {
  76. $api.inquireusercheckPwdTime().then(res => {
  77. if (res.data) {
  78. MessageBox.confirm('您已90天为修改密码,请前往修改密码', '系统提示', {
  79. confirmButtonText: '前往修改',
  80. showCancelButton: false,
  81. closeOnClickModal: false,
  82. closeOnPressEscape: false,
  83. showClose: false,
  84. type: 'warning'
  85. }
  86. ).then(() => {
  87. location.href = '/user/profile';
  88. }).catch(() => { });
  89. }
  90. })
  91. }
  92. router.afterEach(() => {
  93. NProgress.done()
  94. })