新增空闲锁屏、修复401等问题,新增文件版本管理、共享库等占位符
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
import axios from "axios";
|
||||
import type { AxiosResponse } from "axios";
|
||||
|
||||
const authClient = axios.create({
|
||||
baseURL: "/",
|
||||
timeout: 15000,
|
||||
});
|
||||
|
||||
export type ExtendResponse = {
|
||||
accessToken: string;
|
||||
expiresAt: string;
|
||||
};
|
||||
|
||||
export type UnlockResponse = {
|
||||
accessToken: string;
|
||||
expiresAt: string;
|
||||
};
|
||||
|
||||
export const extendToken = (token: string): Promise<AxiosResponse<ExtendResponse>> =>
|
||||
authClient.post<ExtendResponse>(
|
||||
"/api/v1/auth/extend",
|
||||
{},
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
export const unlockSession = (payload: { email: string; password: string }): Promise<AxiosResponse<UnlockResponse>> =>
|
||||
authClient.post<UnlockResponse>("/api/v1/auth/unlock", payload);
|
||||
|
||||
export default authClient;
|
||||
+59
-25
@@ -2,51 +2,71 @@ import axios, { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse } f
|
||||
import { ElMessage } from "element-plus";
|
||||
import router from "../router";
|
||||
import { getToken } from "../utils/auth";
|
||||
import { useAuthStore } from "../store/auth";
|
||||
import type { ApiError } from "../types/api";
|
||||
import { useStudyStore } from "../store/study";
|
||||
import { TEXT } from "../locales";
|
||||
|
||||
let unauthorizedNotified = false;
|
||||
import { extendAccessToken, forceLogout, lockSession, markNetworkActive } from "../session/sessionManager";
|
||||
import { useSessionStore } from "../store/session";
|
||||
|
||||
const instance: AxiosInstance = axios.create({
|
||||
baseURL: "/",
|
||||
timeout: 15000,
|
||||
});
|
||||
|
||||
instance.interceptors.request.use((config: AxiosRequestConfig) => {
|
||||
export type ApiRequestConfig = AxiosRequestConfig & {
|
||||
ignoreIdle?: boolean;
|
||||
allowWhileLocked?: boolean;
|
||||
_retry?: boolean;
|
||||
};
|
||||
|
||||
class LockedError extends Error {
|
||||
constructor() {
|
||||
super("LOCKED");
|
||||
this.name = "LockedError";
|
||||
}
|
||||
}
|
||||
|
||||
instance.interceptors.request.use((config: ApiRequestConfig) => {
|
||||
const session = useSessionStore();
|
||||
const reqUrl = config.url || "";
|
||||
const allowWhileLocked =
|
||||
config.allowWhileLocked ||
|
||||
reqUrl.includes("/api/v1/auth/login") ||
|
||||
reqUrl.includes("/api/v1/auth/register") ||
|
||||
reqUrl.includes("/api/v1/auth/unlock");
|
||||
if (session.locked && !allowWhileLocked) {
|
||||
return Promise.reject(new LockedError());
|
||||
}
|
||||
const token = getToken();
|
||||
if (token) {
|
||||
config.headers = config.headers || {};
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
if (!config.ignoreIdle) {
|
||||
markNetworkActive();
|
||||
}
|
||||
return config;
|
||||
});
|
||||
|
||||
instance.interceptors.response.use(
|
||||
(response: AxiosResponse) => response,
|
||||
async (error: AxiosError<ApiError>) => {
|
||||
if (error instanceof LockedError) {
|
||||
ElMessage.warning(TEXT.common.messages.sessionLocked || "请解锁后继续操作");
|
||||
return Promise.reject(error);
|
||||
}
|
||||
const status = error.response?.status;
|
||||
const data = error.response?.data;
|
||||
const reqUrl = error.config?.url || "";
|
||||
const isAuthEndpoint = reqUrl.includes("/api/v1/auth/login") || reqUrl.includes("/api/v1/auth/register");
|
||||
const isAuthEndpoint =
|
||||
reqUrl.includes("/api/v1/auth/login") ||
|
||||
reqUrl.includes("/api/v1/auth/register") ||
|
||||
reqUrl.includes("/api/v1/auth/extend") ||
|
||||
reqUrl.includes("/api/v1/auth/unlock");
|
||||
if (isAuthEndpoint) {
|
||||
// 认证相关的错误由具体页面自行处理,避免重复提示
|
||||
return Promise.reject(error);
|
||||
}
|
||||
// 401 统一处理:同步清理本地 token,并防止重复弹窗
|
||||
const handleUnauthorized = () => {
|
||||
const auth = useAuthStore();
|
||||
auth.logout();
|
||||
if (!unauthorizedNotified) {
|
||||
unauthorizedNotified = true;
|
||||
ElMessage.error(data?.message || TEXT.common.messages.sessionExpired);
|
||||
setTimeout(() => {
|
||||
unauthorizedNotified = false;
|
||||
}, 1000);
|
||||
}
|
||||
router.push("/login");
|
||||
};
|
||||
// 如果当前项目不存在或已失效,清理项目并跳到工作台
|
||||
if (status === 404 && typeof data?.message === "string" && data.message.toLowerCase().includes("study")) {
|
||||
try {
|
||||
@@ -58,9 +78,23 @@ instance.interceptors.response.use(
|
||||
router.push("/workbench");
|
||||
}
|
||||
if (status === 401) {
|
||||
handleUnauthorized();
|
||||
} else if (data?.message) {
|
||||
ElMessage.error(data.message);
|
||||
const config = error.config as ApiRequestConfig;
|
||||
if (config && !config._retry) {
|
||||
const result = await extendAccessToken("response-401");
|
||||
if (result.token) {
|
||||
config._retry = true;
|
||||
config.headers = config.headers || {};
|
||||
config.headers.Authorization = `Bearer ${result.token}`;
|
||||
return instance(config);
|
||||
}
|
||||
if (result.authFailed) {
|
||||
lockSession("token_invalid");
|
||||
}
|
||||
}
|
||||
} else if (status === 403 && (data as any)?.detail === "账号已停用") {
|
||||
forceLogout();
|
||||
} else if (data?.message || (data as any)?.detail) {
|
||||
ElMessage.error((data as any)?.message || (data as any)?.detail);
|
||||
} else {
|
||||
ElMessage.error(TEXT.common.messages.requestFailed);
|
||||
}
|
||||
@@ -68,12 +102,12 @@ instance.interceptors.response.use(
|
||||
}
|
||||
);
|
||||
|
||||
export const apiGet = <T = unknown>(url: string, config?: AxiosRequestConfig) => instance.get<T>(url, config);
|
||||
export const apiPost = <T = unknown>(url: string, data?: unknown, config?: AxiosRequestConfig) =>
|
||||
export const apiGet = <T = unknown>(url: string, config?: ApiRequestConfig) => instance.get<T>(url, config);
|
||||
export const apiPost = <T = unknown>(url: string, data?: unknown, config?: ApiRequestConfig) =>
|
||||
instance.post<T>(url, data, config);
|
||||
export const apiPatch = <T = unknown>(url: string, data?: unknown, config?: AxiosRequestConfig) =>
|
||||
export const apiPatch = <T = unknown>(url: string, data?: unknown, config?: ApiRequestConfig) =>
|
||||
instance.patch<T>(url, data, config);
|
||||
export const apiDelete = <T = unknown>(url: string, config?: AxiosRequestConfig) =>
|
||||
export const apiDelete = <T = unknown>(url: string, config?: ApiRequestConfig) =>
|
||||
instance.delete<T>(url, config);
|
||||
|
||||
export default instance;
|
||||
|
||||
Reference in New Issue
Block a user