20260716215550
This commit is contained in:
1 parent
5cd4e1e1d2
commit
49dafe524c
21 files changed
+1218
-272
No files matched your search
@@ -1,22 +1,42 @@
|
||||
import { post } from "./http";
|
||||
|
||||
export const authApi = {
|
||||
login(credentials) {
|
||||
return post("/api/auth/login", credentials);
|
||||
},
|
||||
};
|
||||
/**
|
||||
* 机构用户登录。
|
||||
* @param {string} orgId 机构码。
|
||||
* @param {string} userId 用户 ID。
|
||||
* @param {string} password 密码。
|
||||
* @returns {Promise<{token: string, orgid: string, user: {id: string, name: string}}>} 登录会话。
|
||||
*/
|
||||
export function loginApi(orgId, userId, password) {
|
||||
return post("/auth/login", {
|
||||
orgid: orgId,
|
||||
userid: userId,
|
||||
password,
|
||||
});
|
||||
}
|
||||
|
||||
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 });
|
||||
},
|
||||
};
|
||||
/**
|
||||
* 按表或视图加载数据。
|
||||
* @param {string} viewName 表名或视图名,必填。
|
||||
* @param {string} [searchCondition] 查询条件,不包含 WHERE。
|
||||
* @param {string} [orderField] 排序字段,可带 ASC 或 DESC。
|
||||
* @param {string[]} [searchColumns] 需要返回的字段列表,省略时返回全部字段。
|
||||
* @returns {Promise<object[]>} 查询结果行。
|
||||
*/
|
||||
export function loadDataApi(viewName, searchCondition, orderField, searchColumns) {
|
||||
return post("/data/loaddata", {
|
||||
view_name: viewName,
|
||||
search_condition: searchCondition,
|
||||
order_field: orderField,
|
||||
search_columns: searchColumns,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行只读 SQL 查询。
|
||||
* @param {string} sql 完整的 SELECT 或 WITH 查询语句。
|
||||
* @returns {Promise<object[]>} 查询结果行。
|
||||
*/
|
||||
export function loadDataBySqlApi(sql) {
|
||||
return post("/data/loaddatabysql", { sql });
|
||||
}
|
||||
@@ -1,40 +1,72 @@
|
||||
import axios from "axios";
|
||||
import { useAuthStore } from "../stores/auth";
|
||||
|
||||
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL ?? "";
|
||||
const http = axios.create({
|
||||
baseURL: import.meta.env.VITE_API_BASE_URL || "/api",
|
||||
timeout: 15000,
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
export async function request(path, options = {}) {
|
||||
let redirectingToLogin = false;
|
||||
|
||||
function redirectToLogin() {
|
||||
const currentPath = window.location.pathname.replace(/\/+$/, "") || "/";
|
||||
if (currentPath === "/login" || redirectingToLogin) return;
|
||||
|
||||
redirectingToLogin = true;
|
||||
const redirect = `${window.location.pathname}${window.location.search}${window.location.hash}`;
|
||||
window.location.assign(`/login?redirect=${encodeURIComponent(redirect)}`);
|
||||
}
|
||||
|
||||
function handleUnauthorized() {
|
||||
const authStore = useAuthStore();
|
||||
authStore.clearSession();
|
||||
redirectToLogin();
|
||||
}
|
||||
|
||||
http.interceptors.request.use((config) => {
|
||||
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}`);
|
||||
config.headers.Authorization = `Bearer ${authStore.token}`;
|
||||
}
|
||||
|
||||
const response = await fetch(`${API_BASE_URL}${path}`, {
|
||||
...options,
|
||||
headers,
|
||||
});
|
||||
const payload = await response.json().catch(() => null);
|
||||
return config;
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
if (response.status === 401) authStore.clearSession();
|
||||
throw new Error(payload?.message ?? `请求失败 (${response.status})`);
|
||||
}
|
||||
http.interceptors.response.use(
|
||||
(response) => {
|
||||
const payload = response.data;
|
||||
|
||||
if (payload?.code !== undefined && payload.code !== 0) {
|
||||
throw new Error(payload.message ?? "请求失败");
|
||||
}
|
||||
if (payload?.code === undefined) return payload;
|
||||
if (payload.code === 0) return payload.data;
|
||||
|
||||
return payload?.data ?? payload;
|
||||
if (payload.code === 401) handleUnauthorized();
|
||||
|
||||
const error = new Error(payload.message || "请求失败");
|
||||
error.code = payload.code;
|
||||
throw error;
|
||||
},
|
||||
(error) => {
|
||||
const status = error.response?.status;
|
||||
const payload = error.response?.data;
|
||||
|
||||
if (status === 401 || payload?.code === 401) handleUnauthorized();
|
||||
|
||||
const requestError = new Error(
|
||||
payload?.message ||
|
||||
(error.code === "ECONNABORTED" ? "请求超时,请稍后重试" : "网络异常,请检查服务是否正常"),
|
||||
);
|
||||
requestError.code = payload?.code ?? status;
|
||||
throw requestError;
|
||||
},
|
||||
);
|
||||
|
||||
export function post(path, data, config) {
|
||||
return http.post(path, data, config);
|
||||
}
|
||||
|
||||
export function post(path, body) {
|
||||
return request(path, {
|
||||
method: "POST",
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
}
|
||||
export default http;
|
||||
Reference in new issue
Block a user