permission.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import router from './router'
  2. import { useStore } from 'vuex';
  3. /* import NProgress from 'nprogress' // Progress 进度条
  4. import 'nprogress/nprogress.css' // Progress 进度条样式 */
  5. import { ElMessage } from 'element-plus'
  6. import Storage from '@/utils/storage'
  7. const whiteList = ['/login', '/boxed-signup','/platformLogin']
  8. let toFirstRoute = function getTopRoutePath(router, next) {
  9. const excludes = [
  10. "",
  11. "*",
  12. '/404',
  13. '/401',
  14. '/500',
  15. '/login',
  16. '/cashier/login',
  17. '/boxed-signup',
  18. '/payType'
  19. ]
  20. const paths = router.getRoutes().map(item => item.path)
  21. const path = paths.filter(item => !excludes.includes(item) && item.indexOf('/:') === -1)[0]
  22. if (typeof next === 'function') {
  23. next(path)
  24. } else {
  25. router.replace(path)
  26. }
  27. }
  28. router.beforeEach(async (to, from, next) => {
  29. const store = useStore()
  30. if (to?.meta?.layout == 'auth') {
  31. store.dispatch("setMainLayout", 'auth');
  32. } else {
  33. store.dispatch("setMainLayout", 'app');
  34. }
  35. /* next(true); */
  36. const refreshToken = Storage.getItem('admin_refresh_token')
  37. if (refreshToken) {
  38. if ((to.path === '/login' || to.path === '/boxed-signup' || to.path ==='/platformLogin') && store.getters.addRouters.length) {
  39. return next(toFirstRoute(router))
  40. /* toFirstRoute(router, next) */
  41. /* NProgress.done() */
  42. } else {
  43. if (store.getters.addRouters.length === 0) {
  44. try {
  45. await store.dispatch('GenerateRoutes')
  46. const routers = store.getters.addRouters
  47. router.addRoute(routers)
  48. if (routers.length) {
  49. const paths = router.getRoutes().map(item => item.path)
  50. if (paths.includes(to.path)) {
  51. return next({ ...to, replace: true })
  52. } else {
  53. return next(toFirstRoute(router))
  54. /* toFirstRoute(router, next) */
  55. }
  56. } else {
  57. return next('/404')
  58. }
  59. } catch {
  60. await store.dispatch('fedLogoutAction')
  61. ElMessage.error('验证失败,请重新登录')
  62. const toPath = Storage.getItem('admin_franchise') ? '/boxed-signup' : '/login'
  63. return next(toPath)
  64. /* const toPath = Storage.getItem('admin_franchise') ? '/boxed-signup' : '/login'
  65. window.location.replace(router.resolve(toPath).href) */
  66. }
  67. } else {
  68. return next()
  69. }
  70. }
  71. } else {
  72. if (whiteList.includes(to.path)) {
  73. return next()
  74. } else {
  75. const toPath = Storage.getItem('admin_franchise') ? '/boxed-signup' : '/login'
  76. return next(toPath)
  77. /* NProgress.done() */
  78. }
  79. }
  80. })
  81. router.afterEach(() => {
  82. /* NProgress.done() */
  83. })