router.guards.js 828 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import { fn_logout } from '@/utils'
  2. const paths = ['/login']
  3. export function createRouterGuards(router, systemStore) {
  4. const routes = router.getRoutes()
  5. // 前置
  6. router.beforeEach(async (to, from, next) => {
  7. // 如果用户进入了登录页面就相当于退出
  8. if (paths.includes(to.path)) {
  9. fn_logout()
  10. return next()
  11. }
  12. // 验证菜单
  13. const menus = systemStore.getMenus
  14. if(menus.length == 0) return next('/login')
  15. if (to.matched.length === 0) {
  16. return next('/errors')
  17. }
  18. next()
  19. })
  20. router.afterEach((to, _, next) => {
  21. // const Loading = window['$loading']
  22. // setTitle(to.meta.title as string | undefined)
  23. // Loading && Loading.submit-success()
  24. })
  25. // 错误
  26. router.onError(error => {
  27. console.log(error, '路由错误')
  28. })
  29. }