mirror of
https://github.com/hpd840321/craftlabs-authorization-sdk.git
synced 2026-06-10 02:20:28 +08:00
44 lines
1.3 KiB
JavaScript
44 lines
1.3 KiB
JavaScript
import { createApp } from "vue";
|
|
import { createPinia } from "pinia";
|
|
import ElementPlus from "element-plus";
|
|
import "element-plus/dist/index.css";
|
|
import "./theme.css";
|
|
import axios from "axios";
|
|
import App from "./App.vue";
|
|
import router from "./router";
|
|
import { useAuthStore } from "./stores/auth";
|
|
import permission from "./directives/permission";
|
|
|
|
// 开发环境始终使用相对路径,以便 Vite 将 /api 代理到后端;误设 VITE_API_BASE 时否则会直连并常出现跨域或连错环境。
|
|
const apiBaseRaw =
|
|
typeof import.meta.env.VITE_API_BASE === "string" ? import.meta.env.VITE_API_BASE.trim() : "";
|
|
if (!import.meta.env.DEV && apiBaseRaw) {
|
|
axios.defaults.baseURL = apiBaseRaw.replace(/\/+$/, "");
|
|
}
|
|
|
|
const pinia = createPinia();
|
|
const app = createApp(App);
|
|
app.use(pinia);
|
|
|
|
useAuthStore(pinia).restoreAxiosAuth();
|
|
|
|
axios.interceptors.response.use(
|
|
(r) => r,
|
|
(err) => {
|
|
if (err.response?.status === 401) {
|
|
const auth = useAuthStore();
|
|
auth.logout();
|
|
if (router.currentRoute.value.name !== "login") {
|
|
router.push({
|
|
name: "login",
|
|
query: { redirect: router.currentRoute.value.fullPath },
|
|
});
|
|
}
|
|
}
|
|
return Promise.reject(err);
|
|
},
|
|
);
|
|
|
|
app.directive("permission", permission);
|
|
app.use(router).use(ElementPlus).mount("#app");
|