index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import router from './routers'
  2. import store from '@/store'
  3. import Config from '@/settings'
  4. import NProgress from 'nprogress' // progress bar
  5. import 'nprogress/nprogress.css' // progress bar style
  6. import { getToken } from '@/utils/auth' // getToken from cookie
  7. import { buildMenus } from '@/api/system/menu'
  8. import { filterAsyncRouter } from '@/store/modules/permission'
  9. import { decrypt } from '@/utils/jsencrypt'
  10. NProgress.configure({ showSpinner: false }) // NProgress Configuration
  11. const whiteList = ['/login', '/sso'] // no redirect whitelist
  12. router.beforeEach((to, from, next) => {
  13. if (to.meta.title) {
  14. document.title = to.meta.title + ' - ' + Config.title
  15. }
  16. NProgress.start()
  17. const url = window.location.hash
  18. if (url.indexOf('userNameSaaS') > 0 && url.indexOf('passWordSaaS') > 0) {
  19. if (store.getters.roles.length === 0) {
  20. const params = {
  21. username: undefined,
  22. password: undefined
  23. }
  24. const data = url.split('login?userNameSaaS=')[1].split('&passWordSaaS=')
  25. params.username = decrypt(decodeURIComponent(data[0]))
  26. params.password = decrypt(decodeURIComponent(data[1]))
  27. store.dispatch('Login', params).then((res) => {
  28. store.dispatch('GetInfo').then(() => {
  29. loadMenus(next, to)
  30. })
  31. }).catch(() => {
  32. next()
  33. })
  34. } else {
  35. next()
  36. }
  37. } else {
  38. if (getToken()) {
  39. // 已登录且要跳转的页面是登录页
  40. if (to.path === '/login') {
  41. next({ path: '/' })
  42. NProgress.done()
  43. } else {
  44. if (store.getters.roles.length === 0) { // 判断当前用户是否已拉取完user_info信息
  45. store.dispatch('GetInfo').then(() => { // 拉取user_info
  46. // 动态路由,拉取菜单
  47. loadMenus(next, to)
  48. }).catch(() => {
  49. store.dispatch('LogOut').then(() => {
  50. location.reload() // 为了重新实例化vue-router对象 避免bug
  51. })
  52. })
  53. // 登录时未拉取 菜单,在此处拉取
  54. } else if (store.getters.loadMenus) {
  55. // 修改成false,防止死循环
  56. store.dispatch('updateLoadMenus')
  57. loadMenus(next, to)
  58. } else {
  59. next()
  60. }
  61. }
  62. } else {
  63. /* has no token*/
  64. if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单,直接进入
  65. next()
  66. } else if (to.path === '/sso') {
  67. next()
  68. } else {
  69. next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
  70. NProgress.done()
  71. }
  72. }
  73. }
  74. })
  75. export const loadMenus = (next, to) => {
  76. buildMenus().then(res => {
  77. const sdata = JSON.parse(JSON.stringify(res))
  78. const rdata = JSON.parse(JSON.stringify(res))
  79. const sidebarRoutes = filterAsyncRouter(sdata)
  80. const rewriteRoutes = filterAsyncRouter(rdata, true)
  81. rewriteRoutes.push({ path: '*', redirect: '/404', hidden: true })
  82. store.dispatch('GenerateRoutes', rewriteRoutes).then(() => { // 存储路由
  83. router.addRoutes(rewriteRoutes) // 动态添加可访问路由表
  84. next({ ...to, replace: true })
  85. })
  86. store.dispatch('SetSidebarRouters', sidebarRoutes)
  87. })
  88. }
  89. router.afterEach(() => {
  90. NProgress.done() // finish progress bar
  91. })