import { createRouter, createWebHashHistory } from 'vue-router'
import { useUserStore } from "@/stores/user.js";
import { routes } from "./routes.js"
const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 路由守卫 - 登录验证
router.beforeEach((to, from, next) => {
  const userStore = useUserStore();
  let token = userStore.authToken || sessionStorage.getItem("authToken") || "";
  if (!token) {
    if (to.path === '/login' || to.meta.nopermission) {
      next();
    } else {
      next('/login')
    }
  } else {
    if (to.path === "/login") {
      next("/");
    } else {
      next();
    }
  }
})

export default router
