const axios = require('axios'); // 测试最终修复效果 async function testFinalFix() { console.log('🔍 测试最终修复效果...\n'); const testCases = [ { name: '部门列表接口', url: 'https://ad.ningmuyun.com/api/government/departments', expected: '应该返回200状态码和部门数据' }, { name: '数据中心接口', url: 'https://ad.ningmuyun.com/api/government/data-center', expected: '应该返回200状态码和统计数据' }, { name: '市场价格接口', url: 'https://ad.ningmuyun.com/api/government/market-price?type=beef', expected: '应该返回200状态码和价格数据' }, { name: '岗位列表接口', url: 'https://ad.ningmuyun.com/api/government/positions', expected: '应该返回200状态码和岗位数据' }, { name: '养殖户列表接口', url: 'https://ad.ningmuyun.com/api/government/farmers', expected: '应该返回200状态码和养殖户数据' }, { name: '养殖类型接口', url: 'https://ad.ningmuyun.com/api/government/farm-types', expected: '应该返回200状态码和养殖类型数据' }, { name: '登录接口', url: 'https://ad.ningmuyun.com/api/government/auth/login', method: 'POST', data: { username: 'admin', password: 'admin123' }, expected: '应该返回401认证错误(接口正常)' } ]; for (const test of testCases) { console.log(`📋 ${test.name}`); console.log(` URL: ${test.url}`); console.log(` 预期: ${test.expected}`); try { const config = { method: test.method || 'GET', url: test.url, headers: { 'Content-Type': 'application/json' }, timeout: 10000, validateStatus: function (status) { return status < 500; // 接受所有小于500的状态码 } }; if (test.data) { config.data = test.data; } const response = await axios(config); console.log(` ✅ 状态码: ${response.status}`); if (response.status === 200) { console.log(` ✅ 成功: 接口正常工作`); if (Array.isArray(response.data)) { console.log(` 📊 数据量: ${response.data.length} 条记录`); } else if (response.data && typeof response.data === 'object') { console.log(` 📊 数据类型: 对象,包含 ${Object.keys(response.data).length} 个字段`); } } else if (response.status === 401) { console.log(` ✅ 正常: 需要认证(接口路径正确)`); } else if (response.status === 404) { console.log(` ❌ 错误: 接口不存在`); } } catch (error) { if (error.response) { console.log(` ❌ 状态码: ${error.response.status}`); console.log(` ❌ 错误: ${error.response.statusText}`); if (error.response.status === 404) { console.log(` 🔍 分析: nginx配置可能还有问题`); } } else if (error.request) { console.log(` ❌ 网络错误: ${error.message}`); } else { console.log(` ❌ 请求配置错误: ${error.message}`); } } console.log(' ' + '─'.repeat(60)); console.log(''); } console.log('🎯 测试完成!'); console.log(''); console.log('📝 最终nginx配置:'); console.log(' location ^~ /api/government/auth/ {'); console.log(' proxy_pass http://localhost:5352/api/auth/;'); console.log(' }'); console.log(' location ^~ /api/government/ {'); console.log(' proxy_pass http://localhost:5352/api/government/;'); console.log(' }'); console.log(''); console.log('🔄 路径映射:'); console.log(' /api/government/departments → http://localhost:5352/api/government/departments'); console.log(' /api/government/data-center → http://localhost:5352/api/government/data-center'); console.log(' /api/government/market-price → http://localhost:5352/api/government/market-price'); console.log(''); console.log('🔄 现在需要重启nginx使配置生效:'); console.log(' sudo nginx -t # 检查配置语法'); console.log(' sudo systemctl reload nginx # 重新加载配置'); } // 运行测试 testFinalFix().catch(console.error);