333351444
This commit is contained in:
+1
-2
@@ -15,14 +15,13 @@ 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: getCurrentApiUrl(),
|
||||
baseURL: import.meta.env.VITE_APP_API_URL,
|
||||
});
|
||||
|
||||
// 退出系统
|
||||
|
||||
@@ -36,11 +36,6 @@ 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 ※ --------------------
|
||||
*
|
||||
|
||||
@@ -1,66 +0,0 @@
|
||||
/*
|
||||
* 环境检测测试文件
|
||||
* 用于验证环境检测功能是否正常工作
|
||||
*
|
||||
* @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()');
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
/*
|
||||
* 环境检测工具
|
||||
*
|
||||
* @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();
|
||||
}
|
||||
Reference in New Issue
Block a user