Files
ai_dianshang/miniprogram/utils/env-switcher.js
2025-11-17 13:32:54 +08:00

110 lines
2.5 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'
},
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: `无法连接到后端服务\n${envInfo.apiBase}\n\n请检查:\n1. 后端服务是否启动\n2. 网络连接是否正常\n3. 环境配置是否正确`,
showCancel: false
});
}
});
}
}