This commit is contained in:
2025-08-11 11:41:22 +08:00
parent 3dc28bf225
commit e576fb8c4c
16 changed files with 802 additions and 11 deletions
+2 -1
View File
@@ -15,13 +15,14 @@ import { decryptData, encryptData } from './encrypt';
import { DATA_TYPE_ENUM } from '../constants/common-const';
import _ from 'lodash';
import LocalStorageKeyConst from '/@/constants/local-storage-key-const.js';
import { getCurrentApiUrl } from '/@/utils/env-util';
// token的消息头
const TOKEN_HEADER = 'Authorization';
// 创建axios对象
const smartAxios = axios.create({
baseURL: import.meta.env.VITE_APP_API_URL,
baseURL: getCurrentApiUrl(),
});
// 退出系统
+5
View File
@@ -36,6 +36,11 @@ import '/@/utils/ployfill';
import { useDictStore } from '/@/store/modules/system/dict.js';
import { dictApi } from '/@/api/support/dict-api.js';
// 开发环境下引入环境检测测试
if (import.meta.env.DEV) {
import('/@/utils/env-test.js');
}
/*
* -------------------- ※ 着重 解释说明下main.js的初始化逻辑 begin ※ --------------------
*
+66
View File
@@ -0,0 +1,66 @@
/*
* 环境检测测试文件
* 用于验证环境检测功能是否正常工作
*
* @Author: xing
* @Date: 2025-01-27
* @Copyright gdxz
*/
import { detectEnvironment, getCurrentApiUrl } from './env-util';
/**
* 测试环境检测功能
*/
export function testEnvironmentDetection() {
console.log('=== 环境检测测试 ===');
const env = detectEnvironment();
console.log('环境检测结果:');
console.log('- isLocal:', env.isLocal);
console.log('- isDevServer:', env.isDevServer);
console.log('- 当前主机名:', typeof window !== 'undefined' ? window.location.hostname : 'Node.js环境');
console.log('- 当前API地址:', getCurrentApiUrl());
console.log('- 环境变量NODE_ENV:', import.meta.env.NODE_ENV);
console.log('- 环境变量VITE_APP_API_URL:', import.meta.env.VITE_APP_API_URL);
// 测试不同环境下的API地址
console.log('\n=== 不同环境下的API地址测试 ===');
// 模拟本地环境
if (typeof window !== 'undefined') {
const originalHostname = window.location.hostname;
// 测试本地环境
Object.defineProperty(window.location, 'hostname', {
value: 'localhost',
writable: true
});
console.log('模拟localhost环境:', getCurrentApiUrl());
// 测试开发服务器环境
Object.defineProperty(window.location, 'hostname', {
value: 'dev-casedata.igandan.com',
writable: true
});
console.log('模拟dev-casedata.igandan.com环境:', getCurrentApiUrl());
// 恢复原始值
Object.defineProperty(window.location, 'hostname', {
value: originalHostname,
writable: true
});
}
console.log('\n=== 测试完成 ===');
}
/**
* 在浏览器控制台中运行测试
*/
if (typeof window !== 'undefined') {
// 将测试函数挂载到全局,方便在控制台调用
window.testEnv = testEnvironmentDetection;
console.log('环境检测测试已准备就绪,请在控制台运行: testEnv()');
}
+73
View File
@@ -0,0 +1,73 @@
/*
* 环境检测工具
*
* @Author: xing
* @Date: 2025-01-27
* @Copyright gdxz
*/
/**
* 检测当前环境
* @returns {Object} 包含环境信息的对象
*/
export function detectEnvironment() {
// 检测是否为本地环境
const isLocal = () => {
// 检查主机名
if (typeof window !== 'undefined') {
const hostname = window.location.hostname;
return hostname === 'localhost' ||
hostname === '127.0.0.1' ||
hostname === '0.0.0.0' ||
hostname.includes('192.168.') ||
hostname.includes('10.') ||
hostname.includes('172.');
}
// 检查环境变量
if (import.meta.env.DEV) {
return true;
}
return false;
};
// 检测是否为开发服务器环境
const isDevServer = () => {
if (typeof window !== 'undefined') {
const hostname = window.location.hostname;
return hostname === 'dev-casedata.igandan.com';
}
return false;
};
const local = isLocal();
const devServer = isDevServer();
return {
isLocal,
isDevServer,
isLocal: local,
isDevServer: devServer,
// 根据环境返回对应的API地址
getApiUrl: () => {
if (local) {
return 'http://127.0.0.1:5480';
} else if (devServer) {
return 'http://dev-casedata.igandan.com:5480';
} else {
// 其他环境使用默认配置
return import.meta.env.VITE_APP_API_URL;
}
}
};
}
/**
* 获取当前环境的API基础URL
* @returns {string} API基础URL
*/
export function getCurrentApiUrl() {
const env = detectEnvironment();
return env.getApiUrl();
}