feat: 新增多环境配置支持
- 新增 dev/test/prod 三种环境配置 - 支持通过 CURRENT_ENV 常量快速切换环境 - 重构配置获取逻辑,使用环境映射表 - 新增 getCurrentEnv() 方法获取当前环境
This commit is contained in:
@ -1,9 +1,15 @@
|
|||||||
/**
|
/**
|
||||||
* 配置管理
|
* 配置管理
|
||||||
* 后端地址已预配置,用户无需手动设置
|
* 支持 dev(本地开发)和 test(测试服务器)两种环境
|
||||||
*/
|
*/
|
||||||
import * as vscode from "vscode";
|
import * as vscode from "vscode";
|
||||||
|
|
||||||
|
/** 环境类型 */
|
||||||
|
type Environment = "dev" | "test" | "prod";
|
||||||
|
|
||||||
|
/** 当前环境 - 修改这里切换环境 */
|
||||||
|
const CURRENT_ENV: Environment = "dev";
|
||||||
|
|
||||||
/** 配置项接口 */
|
/** 配置项接口 */
|
||||||
export interface IccoderConfig {
|
export interface IccoderConfig {
|
||||||
/** 后端服务地址 */
|
/** 后端服务地址 */
|
||||||
@ -14,19 +20,40 @@ export interface IccoderConfig {
|
|||||||
userId: string;
|
userId: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 默认配置(预配置,不暴露给用户) */
|
/** 环境配置 */
|
||||||
const DEFAULT_CONFIG: IccoderConfig = {
|
const ENV_CONFIG: Record<Environment, IccoderConfig> = {
|
||||||
backendUrl: "http://192.168.1.108:2233",
|
/** 本地开发环境 */
|
||||||
timeout: 60000,
|
dev: {
|
||||||
userId: "default-user",
|
backendUrl: "http://localhost:2233",
|
||||||
|
timeout: 60000,
|
||||||
|
userId: "default-user",
|
||||||
|
},
|
||||||
|
/** 测试服务器环境 */
|
||||||
|
test: {
|
||||||
|
backendUrl: "http://192.168.1.108:2233",
|
||||||
|
timeout: 60000,
|
||||||
|
userId: "default-user",
|
||||||
|
},
|
||||||
|
/** 生产环境 */
|
||||||
|
prod: {
|
||||||
|
backendUrl: "https://api.iccoder.com", // TODO: 替换为实际生产地址
|
||||||
|
timeout: 60000,
|
||||||
|
userId: "default-user",
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前环境
|
||||||
|
*/
|
||||||
|
export function getCurrentEnv(): Environment {
|
||||||
|
return CURRENT_ENV;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取配置项
|
* 获取配置项
|
||||||
* 直接返回预配置的值,用户无需手动配置
|
|
||||||
*/
|
*/
|
||||||
export function getConfig(): IccoderConfig {
|
export function getConfig(): IccoderConfig {
|
||||||
return { ...DEFAULT_CONFIG };
|
return { ...ENV_CONFIG[CURRENT_ENV] };
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -34,7 +61,6 @@ export function getConfig(): IccoderConfig {
|
|||||||
*/
|
*/
|
||||||
export function getApiUrl(path: string): string {
|
export function getApiUrl(path: string): string {
|
||||||
const { backendUrl } = getConfig();
|
const { backendUrl } = getConfig();
|
||||||
// 确保 URL 格式正确
|
|
||||||
const baseUrl = backendUrl.endsWith("/")
|
const baseUrl = backendUrl.endsWith("/")
|
||||||
? backendUrl.slice(0, -1)
|
? backendUrl.slice(0, -1)
|
||||||
: backendUrl;
|
: backendUrl;
|
||||||
|
|||||||
Reference in New Issue
Block a user