20260717170047

This commit is contained in:
oneao committed 2026-07-17 17:00:48 +08:00
1 parent 49dafe524c
commit 35aa108a34
40 files changed
+6218 -413

No files matched your search

+3 -3
View File
@@ -5,7 +5,7 @@ import { post } from "./http";
* @param {string} orgId 机构码。
* @param {string} userId 用户 ID。
* @param {string} password 密码。
* @returns {Promise<{token: string, orgid: string, user: {id: string, name: string}}>} 登录会话。
* @returns {Promise<{success: boolean, code: number, message: string, data: {token: string, orgid: string, user: {id: string, name: string}}}>} 登录响应。
*/
export function loginApi(orgId, userId, password) {
return post("/auth/login", {
@@ -21,7 +21,7 @@ export function loginApi(orgId, userId, password) {
* @param {string} [searchCondition] 查询条件,不包含 WHERE。
* @param {string} [orderField] 排序字段,可带 ASC 或 DESC。
* @param {string[]} [searchColumns] 需要返回的字段列表,省略时返回全部字段。
* @returns {Promise<object[]>} 查询结果行。
* @returns {Promise<{success: boolean, code: number, message: string, data: object[]}>} 查询响应。
*/
export function loadDataApi(viewName, searchCondition, orderField, searchColumns) {
return post("/data/loaddata", {
@@ -35,7 +35,7 @@ export function loadDataApi(viewName, searchCondition, orderField, searchColumns
/**
* 执行只读 SQL 查询。
* @param {string} sql 完整的 SELECT 或 WITH 查询语句。
* @returns {Promise<object[]>} 查询结果行。
* @returns {Promise<{success: boolean, code: number, message: string, data: object[]}>} 查询响应。
*/
export function loadDataBySqlApi(sql) {
return post("/data/loaddatabysql", { sql });
+27 -9
View File
@@ -1,6 +1,9 @@
import axios from "axios";
import { useAuthStore } from "../stores/auth";
const AUTH_ERROR_CODE = 401;
const SESSION_REPLACED_CODE = 40101;
const http = axios.create({
baseURL: import.meta.env.VITE_API_BASE_URL || "/api",
timeout: 15000,
@@ -12,19 +15,25 @@ const http = axios.create({
let redirectingToLogin = false;
function redirectToLogin() {
function redirectToLogin(reason) {
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)}`);
const query = new URLSearchParams({ redirect });
if (reason) query.set("reason", reason);
window.location.assign(`/login?${query.toString()}`);
}
function handleUnauthorized() {
function isAuthenticationError(code) {
return code === AUTH_ERROR_CODE || code === SESSION_REPLACED_CODE;
}
function handleUnauthorized(code) {
const authStore = useAuthStore();
authStore.clearSession();
redirectToLogin();
redirectToLogin(code === SESSION_REPLACED_CODE ? "session-replaced" : undefined);
}
http.interceptors.request.use((config) => {
@@ -42,25 +51,34 @@ http.interceptors.response.use(
const payload = response.data;
if (payload?.code === undefined) return payload;
if (payload.code === 0) return payload.data;
const result = {
...payload,
success: payload.code === 0,
};
if (result.success) return result;
if (payload.code === 401) handleUnauthorized();
if (isAuthenticationError(result.code)) handleUnauthorized(result.code);
const error = new Error(payload.message || "请求失败");
error.code = payload.code;
const error = new Error(result.message || "请求失败");
error.code = result.code;
error.data = result.data;
error.success = result.success;
throw error;
},
(error) => {
const status = error.response?.status;
const payload = error.response?.data;
if (status === 401 || payload?.code === 401) handleUnauthorized();
const authenticationCode = payload?.code ?? status;
if (isAuthenticationError(authenticationCode)) handleUnauthorized(authenticationCode);
const requestError = new Error(
payload?.message ||
(error.code === "ECONNABORTED" ? "请求超时,请稍后重试" : "网络异常,请检查服务是否正常"),
);
requestError.code = payload?.code ?? status;
requestError.data = payload?.data;
requestError.success = false;
throw requestError;
},
);