permission.js 3.1 KB

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