const axios = require('axios'); const colors = require('colors'); // 获取Token async function getToken() { try { const { getToken } = require('./quick-token-getter'); return await getToken(); } catch (error) { console.log('获取Token失败'.red); return null; } } // 详细测试接口返回数据 async function testDetailedData() { console.log('🔍 报修管理列表详细数据测试'.rainbow.bold); console.log('='.repeat(80).rainbow); try { // 1. 获取Token console.log('\n🔐 获取认证Token'.magenta.bold); const token = await getToken(); if (!token) { console.log('❌ 无法获取Token,测试终止'.red); return; } console.log('✅ Token获取成功'.green); // 2. 测试接口 const config = { method: 'get', url: 'http://localhost/dev-api/pm/repairOrder/list', headers: { 'Content-Type': 'application/json;charset=utf-8', Authorization: `Bearer ${token}`, clientid: 'e5cd7e4891bf95d1d19206ce24a7b32e', 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8', referer: 'http://localhost/login?redirect=%252Findex', }, params: { pageNum: 1, pageSize: 10, }, timeout: 10000, }; console.log('\n📡 发起API请求...'.cyan); console.log('请求URL:', config.url); console.log('请求参数:', config.params); const response = await axios(config); console.log('\n📊 响应详情分析'.green.bold); console.log('='.repeat(60).green); console.log('✅ HTTP状态码:', response.status); console.log('✅ 响应头 Content-Type:', response.headers['content-type']); console.log('\n📋 完整响应体结构:'.yellow.bold); console.log(JSON.stringify(response.data, null, 2)); // 分析响应数据结构 console.log('\n🔍 数据结构分析'.blue.bold); console.log('='.repeat(60).blue); if (response.data) { console.log('🔸 响应根结构类型:', typeof response.data); console.log('🔸 响应根结构键:', Object.keys(response.data)); if (response.data.code !== undefined) { console.log('🔸 code:', response.data.code, typeof response.data.code); } if (response.data.msg !== undefined) { console.log('🔸 msg:', response.data.msg, typeof response.data.msg); } if (response.data.data !== undefined) { console.log('🔸 data字段存在,类型:', typeof response.data.data); console.log('🔸 data内容:', JSON.stringify(response.data.data, null, 2)); if (response.data.data && typeof response.data.data === 'object') { console.log('🔸 data对象的键:', Object.keys(response.data.data)); if (response.data.data.rows !== undefined) { console.log('🔸 rows字段类型:', typeof response.data.data.rows); console.log( '🔸 rows数组长度:', Array.isArray(response.data.data.rows) ? response.data.data.rows.length : 'not array', ); if ( Array.isArray(response.data.data.rows) && response.data.data.rows.length > 0 ) { console.log( '🔸 第一条记录结构:', Object.keys(response.data.data.rows[0]), ); console.log( '🔸 第一条记录示例:', JSON.stringify(response.data.data.rows[0], null, 2), ); } } if (response.data.data.total !== undefined) { console.log( '🔸 total字段:', response.data.data.total, typeof response.data.data.total, ); } } } else { console.log('❌ data字段不存在!'); console.log('可能的原因:'); console.log(' - 接口返回格式与预期不符'); console.log(' - 数据库查询结果为空'); console.log(' - 后端逻辑处理有问题'); } } // 检查数据库中是否有数据 console.log('\n🗄️ 检查数据库数据'.magenta.bold); console.log('='.repeat(60).magenta); // 测试不带分页参数的请求 const simpleConfig = { ...config, params: {}, }; console.log('测试不带分页参数的请求...'); try { const simpleResponse = await axios(simpleConfig); console.log('✅ 简单请求成功'); console.log('简单请求响应:', JSON.stringify(simpleResponse.data, null, 2)); } catch (error) { console.log('❌ 简单请求失败:', error.response?.data || error.message); } } catch (error) { console.log('\n💥 测试过程中出现错误'.red.bold); console.log('='.repeat(60).red); if (error.response) { console.log('HTTP状态码:', error.response.status); console.log('错误响应:', JSON.stringify(error.response.data, null, 2)); } else { console.log('网络错误:', error.message); } } } // 运行测试 if (require.main === module) { testDetailedData() .then(() => { console.log('\n👋 详细数据测试完成'.cyan); }) .catch((error) => { console.error('测试脚本执行失败:', error); process.exit(1); }); } module.exports = { testDetailedData };