import { aw as defineStore, r as ref, c as computed, v as defineComponent, l as onMounted, B as createElementBlock, C as createVNode, ak as resolveComponent, y as openBlock, ax as createRouter, ay as createWebHistory, at as createApp, az as createPinia } from "./vue-dev.7a51ac2d.js";
import { E as ElementPlusIconsVue, i as installer } from "./element-dev.26a170ea.js";
import { a as axios } from "./utils-dev.be08a1a2.js";
(function polyfill() {
  const relList = document.createElement("link").relList;
  if (relList && relList.supports && relList.supports("modulepreload")) {
    return;
  }
  for (const link of document.querySelectorAll('link[rel="modulepreload"]')) {
    processPreload(link);
  }
  new MutationObserver((mutations) => {
    for (const mutation of mutations) {
      if (mutation.type !== "childList") {
        continue;
      }
      for (const node of mutation.addedNodes) {
        if (node.tagName === "LINK" && node.rel === "modulepreload")
          processPreload(node);
      }
    }
  }).observe(document, { childList: true, subtree: true });
  function getFetchOpts(link) {
    const fetchOpts = {};
    if (link.integrity)
      fetchOpts.integrity = link.integrity;
    if (link.referrerPolicy)
      fetchOpts.referrerPolicy = link.referrerPolicy;
    if (link.crossOrigin === "use-credentials")
      fetchOpts.credentials = "include";
    else if (link.crossOrigin === "anonymous")
      fetchOpts.credentials = "omit";
    else
      fetchOpts.credentials = "same-origin";
    return fetchOpts;
  }
  function processPreload(link) {
    if (link.ep)
      return;
    link.ep = true;
    const fetchOpts = getFetchOpts(link);
    fetch(link.href, fetchOpts);
  }
})();
const index = "";
const useAuthStore = defineStore("auth", () => {
  const user = ref(null);
  const token = ref(localStorage.getItem("access_token"));
  const loading = ref(false);
  const isAuthenticated = computed(() => !!token.value && !!user.value);
  const setToken = (newToken) => {
    token.value = newToken;
    localStorage.setItem("access_token", newToken);
    axios.defaults.headers.common["Authorization"] = `Bearer ${newToken}`;
  };
  const clearToken = () => {
    token.value = null;
    user.value = null;
    localStorage.removeItem("access_token");
    delete axios.defaults.headers.common["Authorization"];
  };
  const fetchUserInfo = async () => {
    if (!token.value)
      return;
    try {
      const response = await axios.get("/api/user/profile");
      if (response.data.code === 0) {
        user.value = response.data.data;
      }
    } catch (error) {
      console.error("获取用户信息失败:", error);
      clearToken();
      throw error;
    }
  };
  const checkAuth = async () => {
    if (token.value && !user.value) {
      try {
        await fetchUserInfo();
      } catch (error) {
        clearToken();
      }
    }
    if (token.value) {
      axios.defaults.headers.common["Authorization"] = `Bearer ${token.value}`;
    }
  };
  const logout = async () => {
    try {
      if (token.value) {
        await axios.get("/auth/logout");
      }
    } catch (error) {
      console.error("登出失败:", error);
    } finally {
      clearToken();
    }
  };
  return {
    user,
    token,
    loading,
    isAuthenticated,
    setToken,
    clearToken,
    fetchUserInfo,
    checkAuth,
    logout
  };
});
const _hoisted_1 = { id: "app" };
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "App",
  setup(__props) {
    const authStore = useAuthStore();
    onMounted(() => {
      authStore.checkAuth();
    });
    return (_ctx, _cache) => {
      const _component_router_view = resolveComponent("router-view");
      return openBlock(), createElementBlock("div", _hoisted_1, [
        createVNode(_component_router_view)
      ]);
    };
  }
});
const App_vue_vue_type_style_index_0_lang = "";
const scriptRel = "modulepreload";
const assetsURL = function(dep) {
  return "/" + dep;
};
const seen = {};
const __vitePreload = function preload(baseModule, deps, importerUrl) {
  if (!deps || deps.length === 0) {
    return baseModule();
  }
  const links = document.getElementsByTagName("link");
  return Promise.all(deps.map((dep) => {
    dep = assetsURL(dep);
    if (dep in seen)
      return;
    seen[dep] = true;
    const isCss = dep.endsWith(".css");
    const cssSelector = isCss ? '[rel="stylesheet"]' : "";
    const isBaseRelative = !!importerUrl;
    if (isBaseRelative) {
      for (let i = links.length - 1; i >= 0; i--) {
        const link2 = links[i];
        if (link2.href === dep && (!isCss || link2.rel === "stylesheet")) {
          return;
        }
      }
    } else if (document.querySelector(`link[href="${dep}"]${cssSelector}`)) {
      return;
    }
    const link = document.createElement("link");
    link.rel = isCss ? "stylesheet" : scriptRel;
    if (!isCss) {
      link.as = "script";
      link.crossOrigin = "";
    }
    link.href = dep;
    document.head.appendChild(link);
    if (isCss) {
      return new Promise((res, rej) => {
        link.addEventListener("load", res);
        link.addEventListener("error", () => rej(new Error(`Unable to preload CSS for ${dep}`)));
      });
    }
  })).then(() => baseModule()).catch((err) => {
    const e = new Event("vite:preloadError", { cancelable: true });
    e.payload = err;
    window.dispatchEvent(e);
    if (!e.defaultPrevented) {
      throw err;
    }
  });
};
const routes = [
  {
    path: "/",
    redirect: "/dashboard"
  },
  {
    path: "/login",
    name: "Login",
    component: () => __vitePreload(() => import("./Login-dev.6b0c82e3.js"), true ? ["assets/Login-dev.6b0c82e3.js","assets/vue-dev.7a51ac2d.js","assets/_plugin-vue_export-helper-dev.cc2b3d55.js","assets/element-dev.26a170ea.js","assets/utils-dev.be08a1a2.js","assets/Login-dev.e3300704.css"] : void 0),
    meta: { requiresGuest: true }
  },
  {
    path: "/auth/callback",
    name: "AuthCallback",
    component: () => __vitePreload(() => import("./AuthCallback-dev.4480f579.js"), true ? ["assets/AuthCallback-dev.4480f579.js","assets/vue-dev.7a51ac2d.js","assets/element-dev.26a170ea.js","assets/_plugin-vue_export-helper-dev.cc2b3d55.js","assets/utils-dev.be08a1a2.js","assets/AuthCallback-dev.1bb355ff.css"] : void 0)
  },
  {
    path: "/dashboard",
    name: "Dashboard",
    component: () => __vitePreload(() => import("./Dashboard-dev.cae2ce38.js"), true ? ["assets/Dashboard-dev.cae2ce38.js","assets/vue-dev.7a51ac2d.js","assets/element-dev.26a170ea.js","assets/_plugin-vue_export-helper-dev.cc2b3d55.js","assets/utils-dev.be08a1a2.js","assets/Dashboard-dev.865428fa.css"] : void 0),
    meta: { requiresAuth: true }
  },
  {
    path: "/products",
    name: "Products",
    component: () => __vitePreload(() => import("./Products-dev.3852cc44.js"), true ? ["assets/Products-dev.3852cc44.js","assets/utils-dev.be08a1a2.js","assets/element-dev.26a170ea.js","assets/vue-dev.7a51ac2d.js","assets/_plugin-vue_export-helper-dev.cc2b3d55.js","assets/Products-dev.7e43ba28.css"] : void 0),
    meta: { requiresAuth: true }
  },
  {
    path: "/orders",
    name: "Orders",
    component: () => __vitePreload(() => import("./Orders-dev.5c571709.js"), true ? ["assets/Orders-dev.5c571709.js","assets/utils-dev.be08a1a2.js","assets/element-dev.26a170ea.js","assets/vue-dev.7a51ac2d.js","assets/_plugin-vue_export-helper-dev.cc2b3d55.js","assets/Orders-dev.8d664eac.css"] : void 0),
    meta: { requiresAuth: true }
  }
];
const router = createRouter({
  history: createWebHistory(),
  routes
});
router.beforeEach(async (to, from, next) => {
  const authStore = useAuthStore();
  if (to.meta.requiresAuth) {
    if (!authStore.isAuthenticated) {
      next({ name: "Login" });
      return;
    }
  }
  if (to.meta.requiresGuest && authStore.isAuthenticated) {
    next({ name: "Dashboard" });
    return;
  }
  next();
});
const app = createApp(_sfc_main);
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component);
}
app.use(createPinia());
app.use(router);
app.use(installer);
app.mount("#app");
export {
  useAuthStore as u
};
//# sourceMappingURL=index-dev.bb5c7162.js.map
