Start.vue 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <template>
  2. <router-view></router-view>
  3. </template>
  4. <script lang="ts">
  5. import { onBeforeRouteUpdate } from "vue-router";
  6. import type { RouteLocationNormalized, NavigationGuardNext } from "vue-router";
  7. import { useMainStore } from "@/stores/main";
  8. import { storeToRefs } from "pinia";
  9. export default {
  10. setup() {
  11. onBeforeRouteUpdate((to, from, next) => {
  12. startRouteGuard(to, from, next);
  13. });
  14. },
  15. beforeRouteEnter(to, from, next) {
  16. startRouteGuard(to, from, next);
  17. },
  18. };
  19. //function
  20. const startRouteGuard = async (
  21. to: RouteLocationNormalized,
  22. from: RouteLocationNormalized,
  23. next: NavigationGuardNext
  24. ) => {
  25. const mainStore = useMainStore();
  26. const mainStoreRef = storeToRefs(mainStore);
  27. if (to.path === "/qrcode" || to.path === "/yt-views") {
  28. next();
  29. mainStore.qrCheckLoggedIn();
  30. } else {
  31. mainStore.checkLoggedIn();
  32. }
  33. if (mainStoreRef.readIsLoggedIn.value) {
  34. if (to.path === "/login" || to.path === "/") {
  35. next("/main/dashboard");
  36. } else {
  37. next();
  38. }
  39. } else if (mainStoreRef.readIsLoggedIn.value === false) {
  40. if (to.path === "/" || (to.path as string).startsWith("/main")) {
  41. next("/login");
  42. } else {
  43. next();
  44. }
  45. }
  46. };
  47. </script>