index_20220908143413.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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', '/meeting/queryMeeting'] // 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. let url = window.location.hash
  18. if (url.indexOf("userNameSaaS") > 0 && url.indexOf("passWordSaaS") > 0) {
  19. if (store.getters.roles.length === 0) {
  20. let params = {
  21. username: undefined,
  22. password: undefined
  23. }
  24. let data = url.split("login?userNameSaaS=")[1].split("&passWordSaaS=")
  25. console.log(data)
  26. params.username = decrypt(decodeURIComponent(data[0]))
  27. params.password = decrypt(decodeURIComponent(data[1]))
  28. store.dispatch("Login", params).then((res) => {
  29. store.dispatch('GetInfo').then(() => {
  30. loadMenus(next, to)
  31. })
  32. }).catch(() => {
  33. return
  34. //next()
  35. })
  36. } else {
  37. next()
  38. }
  39. } else {
  40. if (getToken()) {
  41. // 已登录且要跳转的页面是登录页
  42. if (to.path === '/login') {
  43. next({ path: '/' })
  44. NProgress.done()
  45. } else {
  46. if (store.getters.roles.length === 0) { // 判断当前用户是否已拉取完user_info信息
  47. store.dispatch('GetInfo').then(() => { // 拉取user_info
  48. // 动态路由,拉取菜单
  49. loadMenus(next, to)
  50. }).catch(() => {
  51. store.dispatch('LogOut').then(() => {
  52. location.reload() // 为了重新实例化vue-router对象 避免bug
  53. })
  54. })
  55. // 登录时未拉取 菜单,在此处拉取
  56. } else if (store.getters.loadMenus) {
  57. // 修改成false,防止死循环
  58. store.dispatch('updateLoadMenus')
  59. loadMenus(next, to)
  60. } else {
  61. next()
  62. }
  63. }
  64. } else {
  65. /* has no token*/
  66. if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单,直接进入
  67. next()
  68. } else if (to.path === '/sso') {
  69. next()
  70. } else if (to.path === '/meeting/queryMeeting') {
  71. next()
  72. } else {
  73. next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
  74. NProgress.done()
  75. }
  76. }
  77. }
  78. })
  79. export const loadMenus = (next, to) => {
  80. buildMenus().then(res => {
  81. const sdata = JSON.parse(JSON.stringify(res))
  82. const rdata = JSON.parse(JSON.stringify(res))
  83. const sidebarRoutes = filterAsyncRouter(sdata)
  84. const rewriteRoutes = filterAsyncRouter(rdata, true)
  85. rewriteRoutes.push({ path: '*', redirect: '/404', hidden: true })
  86. store.dispatch('GenerateRoutes', rewriteRoutes).then(() => { // 存储路由
  87. router.addRoutes(rewriteRoutes) // 动态添加可访问路由表
  88. next({...to, replace: true })
  89. })
  90. store.dispatch('SetSidebarRouters', sidebarRoutes)
  91. })
  92. }
  93. router.afterEach(() => {
  94. NProgress.done() // finish progress bar
  95. })