Files
ai_dianshang/miniprogram/utils/env-switcher.js
2025-11-28 15:18:10 +08:00

137 lines
3.1 KiB
JavaScript

/**
* 环境切换辅助工具
* 用于在开发过程中快速切换不同的后端环境
*/
// 环境配置映射
const ENV_CONFIG = {
development: {
name: '开发环境',
description: '本地后端服务 (localhost:8080)',
apiBase: 'http://localhost:8080/api/v1',
useMock: false,
color: '#52c41a'
},
production: {
name: '生产环境',
description: '线上正式服务器(中国区)',
apiBase: 'https://tral.cc/api/v1',
useMock: false,
color: '#f5222d'
},
'production-cn': {
name: '生产环境-中国区',
description: '中国区生产服务器',
apiBase: 'https://api-cn.your-domain.com/api/v1',
useMock: false,
color: '#f5222d'
},
'production-us': {
name: '生产环境-美国区',
description: '美国区生产服务器',
apiBase: 'https://api-us.your-domain.com/api/v1',
useMock: false,
color: '#fa8c16'
},
'production-eu': {
name: '生产环境-欧洲区',
description: '欧洲区生产服务器',
apiBase: 'https://api-eu.your-domain.com/api/v1',
useMock: false,
color: '#13c2c2'
},
test: {
name: '测试环境',
description: '测试服务器',
apiBase: 'https://tral.cc/api/v1',
useMock: false,
color: '#1890ff'
},
mock: {
name: 'Mock模式',
description: '使用本地模拟数据',
apiBase: 'http://localhost:8080/api/v1',
useMock: true,
color: '#722ed1'
}
};
/**
* 获取当前环境信息
*/
export function getCurrentEnv() {
const { config } = require('../config/index');
return {
current: config.environment,
config: ENV_CONFIG[config.environment],
apiBase: config.apiBase,
useMock: config.useMock
};
}
/**
* 获取所有可用环境
*/
export function getAllEnvs() {
return ENV_CONFIG;
}
/**
* 显示当前环境信息(已禁用)
*/
export function showCurrentEnv() {
const envInfo = getCurrentEnv();
// 环境提示已禁用,仅返回环境信息
return envInfo;
}
/**
* 环境启动提示(已禁用)
*/
export function envStartupTip() {
// 环境提示已禁用
return;
}
/**
* 检查网络连接
*/
export function checkNetworkConnection() {
const envInfo = getCurrentEnv();
if (typeof wx !== 'undefined') {
wx.request({
url: `${envInfo.apiBase}/health`,
method: 'GET',
success: (res) => {
if (res.statusCode === 200) {
console.log('✅ 后端服务连接正常');
wx.showToast({
title: '服务连接正常',
icon: 'success'
});
} else {
console.warn('⚠️ 后端服务响应异常:', res.statusCode);
wx.showToast({
title: '服务响应异常',
icon: 'none'
});
}
},
fail: (error) => {
console.error('❌ 后端服务连接失败:', error);
wx.showModal({
title: '连接失败',
content: `无法连接到后端服务
${envInfo.apiBase}
请检查:
1. 后端服务是否启动
2. 网络连接是否正常
3. 环境配置是否正确`,
showCancel: false
});
}
});
}
}