20260715221223
This commit is contained in:
1 parent
7049584d1e
commit
704dda07b5
21 files changed
+2649
-427
No files matched your search
@@ -0,0 +1,22 @@
|
||||
import { post } from "./http";
|
||||
|
||||
export const authApi = {
|
||||
login(credentials) {
|
||||
return post("/api/auth/login", credentials);
|
||||
},
|
||||
};
|
||||
|
||||
export const dataApi = {
|
||||
loadData(params) {
|
||||
return post("/api/data/loaddata", params);
|
||||
},
|
||||
loadDataBySql(sql) {
|
||||
return post("/api/data/loaddatabysql", { sql });
|
||||
},
|
||||
page(params) {
|
||||
return post("/api/data/page", params);
|
||||
},
|
||||
saveObject(tables) {
|
||||
return post("/api/data/saveobjt", { tables });
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,40 @@
|
||||
import { useAuthStore } from "../stores/auth";
|
||||
|
||||
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL ?? "";
|
||||
|
||||
export async function request(path, options = {}) {
|
||||
const authStore = useAuthStore();
|
||||
const headers = new Headers(options.headers);
|
||||
|
||||
headers.set("Accept", "application/json");
|
||||
if (options.body && !headers.has("Content-Type")) {
|
||||
headers.set("Content-Type", "application/json");
|
||||
}
|
||||
if (authStore.token) {
|
||||
headers.set("Authorization", `Bearer ${authStore.token}`);
|
||||
}
|
||||
|
||||
const response = await fetch(`${API_BASE_URL}${path}`, {
|
||||
...options,
|
||||
headers,
|
||||
});
|
||||
const payload = await response.json().catch(() => null);
|
||||
|
||||
if (!response.ok) {
|
||||
if (response.status === 401) authStore.clearSession();
|
||||
throw new Error(payload?.message ?? `请求失败 (${response.status})`);
|
||||
}
|
||||
|
||||
if (payload?.code !== undefined && payload.code !== 0) {
|
||||
throw new Error(payload.message ?? "请求失败");
|
||||
}
|
||||
|
||||
return payload?.data ?? payload;
|
||||
}
|
||||
|
||||
export function post(path, body) {
|
||||
return request(path, {
|
||||
method: "POST",
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user