fix:修复所有的包导入错误问题
This commit is contained in:
@@ -1,69 +0,0 @@
|
||||
/**
|
||||
* API 配置管理
|
||||
*
|
||||
* 通过环境变量配置后端服务地址与超时时间。
|
||||
* 原始 Dart: lib/core/net/service_manager.dart
|
||||
*/
|
||||
|
||||
export type AppEnv = "development" | "test" | "production";
|
||||
|
||||
export interface ApiConfig {
|
||||
/** 后端 API 基础 URL(含协议) */
|
||||
baseUrl: string;
|
||||
/** WebSocket 基础 URL */
|
||||
wsUrl: string;
|
||||
/** 连接超时(ms) */
|
||||
connectTimeout: number;
|
||||
/** 接收超时(ms) */
|
||||
receiveTimeout: number;
|
||||
/** 发送超时(ms) */
|
||||
sendTimeout: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* 超时常量
|
||||
*/
|
||||
export class TimeoutConstants {
|
||||
private constructor() {}
|
||||
static readonly shortTimeout = 30000;
|
||||
static readonly longTimeout = 60000;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析当前环境
|
||||
*/
|
||||
export function getAppEnv(): AppEnv {
|
||||
const env = process.env.NEXT_PUBLIC_APP_ENV;
|
||||
if (env === "production" || env === "test") return env;
|
||||
return "development";
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前 API 配置
|
||||
*/
|
||||
export function getApiConfig(): ApiConfig {
|
||||
const baseUrl =
|
||||
process.env.NEXT_PUBLIC_API_BASE_URL ?? "http://172.16.48.49:3002";
|
||||
const wsUrl =
|
||||
process.env.NEXT_PUBLIC_WS_BASE_URL ?? "ws://172.16.48.49:3002";
|
||||
const connectTimeout = Number.parseInt(
|
||||
process.env.NEXT_PUBLIC_API_CONNECT_TIMEOUT ?? "30000",
|
||||
10
|
||||
);
|
||||
const receiveTimeout = Number.parseInt(
|
||||
process.env.NEXT_PUBLIC_API_RECEIVE_TIMEOUT ?? "60000",
|
||||
10
|
||||
);
|
||||
const sendTimeout = Number.parseInt(
|
||||
process.env.NEXT_PUBLIC_API_SEND_TIMEOUT ?? "30000",
|
||||
10
|
||||
);
|
||||
|
||||
return {
|
||||
baseUrl,
|
||||
wsUrl,
|
||||
connectTimeout,
|
||||
receiveTimeout,
|
||||
sendTimeout,
|
||||
};
|
||||
}
|
||||
@@ -6,7 +6,7 @@
|
||||
*/
|
||||
import { ofetch, type $Fetch, type FetchHook } from "ofetch";
|
||||
import { ApiError, ErrorCode } from "./api_result";
|
||||
import { getApiConfig } from "./config/api_config";
|
||||
import { getApiConfig } from "../../../core/net/config/api_config";
|
||||
import { authRefreshInterceptor } from "./interceptor/auth_refresh_interceptor";
|
||||
import { loggingInterceptor } from "./interceptor/logging_interceptor";
|
||||
import { tokenInterceptor } from "./interceptor/token_interceptor";
|
||||
|
||||
@@ -10,7 +10,7 @@ export * from "./http_client";
|
||||
export * from "./metrics_api";
|
||||
export * from "./response_helper";
|
||||
export * from "./user_api";
|
||||
export * from "./config/api_config";
|
||||
export * from "../../../core/net/config/api_config";
|
||||
export * from "./interceptor/auth_refresh_interceptor";
|
||||
export * from "./interceptor/logging_interceptor";
|
||||
export * from "./interceptor/token_interceptor";
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
*/
|
||||
import type { FetchHook } from "ofetch";
|
||||
import { ApiPath } from "../api_path";
|
||||
import { authStorage } from "../../storage/auth_storage";
|
||||
import { AuthStorage } from "../../../storage/auth/auth_storage";
|
||||
|
||||
/**
|
||||
* 不需要 token 的路径
|
||||
@@ -40,11 +40,27 @@ function needsToken(path: string): boolean {
|
||||
return !NO_TOKEN_PATHS.some((noTokenPath) => path.includes(noTokenPath));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前可用的 auth token(登录 token 优先,其次游客 token)
|
||||
*/
|
||||
async function getAuthToken(): Promise<string | null> {
|
||||
const storage = AuthStorage.getInstance();
|
||||
const loginR = await storage.getLoginToken();
|
||||
if (loginR.success && loginR.data && loginR.data.length > 0) {
|
||||
return loginR.data;
|
||||
}
|
||||
const guestR = await storage.getGuestToken();
|
||||
if (guestR.success && guestR.data && guestR.data.length > 0) {
|
||||
return guestR.data;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export const onRequestToken: FetchHook = async (ctx) => {
|
||||
const path = getRequestPath(ctx.request);
|
||||
if (!needsToken(path)) return;
|
||||
|
||||
const token = authStorage.getAuthToken();
|
||||
const token = await getAuthToken();
|
||||
if (token && ctx.request instanceof Request) {
|
||||
ctx.request.headers.set("Authorization", `Bearer ${token}`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user