authGuard.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { Router, useRoute } from 'vue-router'
  2. import useUserInfo from "@/stores/modules/user";
  3. import tokenInfo from "@/stores/modules/token";
  4. const route = useRoute()
  5. import pinia from "@/stores/index";
  6. /**
  7. * 除了注册页,当没有 token 则跳转至注册页
  8. * @param router
  9. */
  10. export function authGuard(router: Router) {
  11. router.beforeEach(async (to, from, next) => {
  12. /* 跳过路由守卫 */
  13. const useUserInfoStore = useUserInfo();
  14. const tokenInfoStore = tokenInfo();
  15. const appConfig = pinia.state.value.config?.appConfig;
  16. if(!appConfig) return next()
  17. if (tokenInfoStore.getToken /* 已登录 */) {
  18. if(!useUserInfoStore.userInfo.id){
  19. await useUserInfoStore.getInfo()
  20. }
  21. // 检查企业是否过期,如果过期跳转到到期页面
  22. if (useUserInfoStore.isExpired && to.path !== '/photography/expired') {
  23. return next('/photography/expired')
  24. }
  25. return next()
  26. } else {
  27. if(to.meta.noAuth) return next()
  28. useUserInfoStore.updateLoginShow(true)
  29. }
  30. return next()
  31. })
  32. }