Rap 原分销系统代码Web
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.4KB

  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. NProgress.configure({ showSpinner: false })// NProgress Configuration
  10. const whiteList = ['/login']// no redirect whitelist
  11. router.beforeEach((to, from, next) => {
  12. if (to.meta.title) {
  13. document.title = to.meta.title + ' - ' + Config.title
  14. }
  15. NProgress.start()
  16. if (getToken()) {
  17. // 已登录且要跳转的页面是登录页
  18. if (to.path === '/login') {
  19. next({ path: '/' })
  20. NProgress.done()
  21. } else {
  22. if (store.getters.roles.length === 0) { // 判断当前用户是否已拉取完user_info信息
  23. store.dispatch('GetInfo').then(() => { // 拉取user_info
  24. // 动态路由,拉取菜单
  25. loadMenus(next, to)
  26. }).catch(() => {
  27. store.dispatch('LogOut').then(() => {
  28. location.reload() // 为了重新实例化vue-router对象 避免bug
  29. })
  30. })
  31. // 登录时未拉取 菜单,在此处拉取
  32. } else if (store.getters.loadMenus) {
  33. // 修改成false,防止死循环
  34. store.dispatch('updateLoadMenus')
  35. loadMenus(next, to)
  36. } else {
  37. next()
  38. }
  39. }
  40. } else {
  41. /* has no token*/
  42. if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单,直接进入
  43. next()
  44. } else {
  45. next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
  46. NProgress.done()
  47. }
  48. }
  49. })
  50. export const loadMenus = (next, to) => {
  51. buildMenus().then(res => {
  52. const sdata = JSON.parse(JSON.stringify(res))
  53. const rdata = JSON.parse(JSON.stringify(res))
  54. const sidebarRoutes = filterAsyncRouter(sdata)
  55. const rewriteRoutes = filterAsyncRouter(rdata, false, true)
  56. rewriteRoutes.push({ path: '*', redirect: '/404', hidden: true })
  57. store.dispatch('GenerateRoutes', rewriteRoutes).then(() => { // 存储路由
  58. router.addRoutes(rewriteRoutes) // 动态添加可访问路由表
  59. next({ ...to, replace: true })
  60. })
  61. store.dispatch('SetSidebarRouters', sidebarRoutes)
  62. })
  63. }
  64. router.afterEach(() => {
  65. NProgress.done() // finish progress bar
  66. })