feat: login

This commit is contained in:
2025-09-10 15:20:45 +08:00
parent f33f597a9a
commit 55cd1f349d
21 changed files with 285 additions and 1157 deletions

View File

@@ -16,6 +16,7 @@ import type { UserVO, TokenType } from "@/services/login/types";
import defaultSettings from "../config/defaultSettings";
import { errorConfig } from "./requestErrorConfig";
import "@ant-design/v5-patch-for-react-19";
import { getAccessToken, getRefreshToken, getTenantId } from "./utils/auth";
const isDev = process.env.NODE_ENV === "development";
const isDevOrTest = isDev || process.env.CI;
@@ -33,6 +34,10 @@ export async function getInitialState(): Promise<{
const fetchUserInfo = async () => {
// history.push(loginPath);
try {
const token = getAccessToken();
if (!token) {
throw new Error("No token found");
}
const msg = await getInfo();
return msg.data;
} catch (_error) {
@@ -84,6 +89,7 @@ export const layout: RunTimeLayoutConfig = ({
onPageChange: () => {
const { location } = history;
// 如果没有登录,重定向到 login
console.log(initialState);
if (!initialState?.currentUser && location.pathname !== loginPath) {
history.push(loginPath);
}
@@ -153,4 +159,28 @@ export const layout: RunTimeLayoutConfig = ({
export const request: RequestConfig = {
baseURL: isDev ? "" : "https://proapi.azurewebsites.net",
...errorConfig,
// 添加请求拦截器
requestInterceptors: [
(url, options) => {
// 为所有请求添加 API 前缀
if (url && !url.startsWith(process.env.API_PREFIX || "/admin-api")) {
url = (process.env.API_PREFIX || "/admin-api") + url;
}
// 获取存储在本地的 token 和 tenantId
const token = getAccessToken();
const tenantId = getTenantId(); // 默认租户ID为1
console.log(tenantId);
// 设置统一的请求头
const headers: Record<string, string> = {
...options.headers,
Accept: "*",
"tenant-id": tenantId,
};
// 如果有token则添加Authorization头
if (token) {
headers["Authorization"] = `Bearer ${getAccessToken()}`;
}
return { url, options: { ...options, headers } };
},
],
};