362 lines
13 KiB
JavaScript
362 lines
13 KiB
JavaScript
const axios = require('axios');
|
||
const colors = require('colors');
|
||
|
||
// 测试配置
|
||
const BASE_URL = 'http://localhost/dev-api';
|
||
|
||
// 获取Token的方法(从之前的登录脚本导入)
|
||
async function getToken() {
|
||
try {
|
||
const { getToken } = require('./quick-token-getter');
|
||
return await getToken();
|
||
} catch (error) {
|
||
console.log('使用模拟Token进行测试'.yellow);
|
||
return 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...'; // 模拟Token
|
||
}
|
||
}
|
||
|
||
// 通用API测试方法
|
||
async function testAPI(endpoint, method = 'GET', data = null, token = null) {
|
||
try {
|
||
const config = {
|
||
method: method.toLowerCase(),
|
||
url: `${BASE_URL}${endpoint}`,
|
||
headers: {
|
||
'Content-Type': 'application/json;charset=utf-8',
|
||
clientid: 'e5cd7e4891bf95d1d19206ce24a7b32e',
|
||
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
|
||
referer: 'http://localhost/login?redirect=%252Findex',
|
||
},
|
||
timeout: 10000,
|
||
};
|
||
|
||
if (token) {
|
||
config.headers['Authorization'] = `Bearer ${token}`;
|
||
}
|
||
|
||
if (data && (method.toUpperCase() === 'POST' || method.toUpperCase() === 'PUT')) {
|
||
config.data = data;
|
||
} else if (data && method.toUpperCase() === 'GET') {
|
||
config.params = data;
|
||
}
|
||
|
||
const response = await axios(config);
|
||
|
||
return {
|
||
success: true,
|
||
data: response.data,
|
||
status: response.status,
|
||
};
|
||
} catch (error) {
|
||
return {
|
||
success: false,
|
||
error: error.response?.data || error.message,
|
||
status: error.response?.status,
|
||
};
|
||
}
|
||
}
|
||
|
||
// 测试报修工单列表接口
|
||
async function testRepairOrderList(token) {
|
||
console.log('\n📋 测试报修工单列表接口'.blue.bold);
|
||
console.log('='.repeat(60).blue);
|
||
|
||
// 1. 测试基本列表查询
|
||
console.log('\n1. 测试基本列表查询(无参数)'.cyan);
|
||
const basicResult = await testAPI('/pm/repairOrder/list', 'GET', null, token);
|
||
|
||
if (basicResult.success) {
|
||
console.log('✅ 接口调用成功'.green);
|
||
console.log(`📊 响应状态: ${basicResult.status}`.green);
|
||
console.log(`📝 响应消息: ${basicResult.data.msg}`.green);
|
||
|
||
if (basicResult.data.data) {
|
||
if (basicResult.data.data.rows) {
|
||
// 分页格式
|
||
console.log(`📈 总记录数: ${basicResult.data.data.total || 0}`.yellow);
|
||
console.log(`📄 当前页记录数: ${basicResult.data.data.rows.length}`.yellow);
|
||
if (basicResult.data.data.rows.length > 0) {
|
||
console.log('📋 第一条数据示例:'.yellow);
|
||
console.log(
|
||
JSON.stringify(basicResult.data.data.rows[0], null, 2).substring(0, 500) +
|
||
'...',
|
||
);
|
||
} else {
|
||
console.log('⚠️ 数据库中没有记录'.yellow);
|
||
}
|
||
} else if (Array.isArray(basicResult.data.data)) {
|
||
// 直接数组格式
|
||
console.log(`📄 记录数: ${basicResult.data.data.length}`.yellow);
|
||
if (basicResult.data.data.length > 0) {
|
||
console.log('📋 第一条数据示例:'.yellow);
|
||
console.log(
|
||
JSON.stringify(basicResult.data.data[0], null, 2).substring(0, 500) + '...',
|
||
);
|
||
} else {
|
||
console.log('⚠️ 数据库中没有记录'.yellow);
|
||
}
|
||
} else {
|
||
console.log('⚠️ 数据格式异常:'.yellow, typeof basicResult.data.data);
|
||
console.log(JSON.stringify(basicResult.data.data, null, 2));
|
||
}
|
||
} else {
|
||
console.log('⚠️ 响应中没有data字段'.yellow);
|
||
}
|
||
} else {
|
||
console.log('❌ 接口调用失败'.red);
|
||
console.log(`📊 错误状态: ${basicResult.status}`.red);
|
||
console.log(`📝 错误信息: ${JSON.stringify(basicResult.error, null, 2)}`.red);
|
||
}
|
||
|
||
// 2. 测试分页查询
|
||
console.log('\n2. 测试分页查询'.cyan);
|
||
const pageResult = await testAPI(
|
||
'/pm/repairOrder/list',
|
||
'GET',
|
||
{
|
||
pageNum: 1,
|
||
pageSize: 10,
|
||
},
|
||
token,
|
||
);
|
||
|
||
if (pageResult.success) {
|
||
console.log('✅ 分页查询成功'.green);
|
||
if (pageResult.data.data && pageResult.data.data.rows) {
|
||
console.log(`📈 分页总数: ${pageResult.data.data.total}`.yellow);
|
||
console.log(`📄 当前页数据量: ${pageResult.data.data.rows.length}`.yellow);
|
||
}
|
||
} else {
|
||
console.log('❌ 分页查询失败'.red);
|
||
console.log(`📝 错误: ${JSON.stringify(pageResult.error, null, 2)}`.red);
|
||
}
|
||
|
||
// 3. 测试带条件查询
|
||
console.log('\n3. 测试带条件查询'.cyan);
|
||
const conditionResult = await testAPI(
|
||
'/pm/repairOrder/list',
|
||
'GET',
|
||
{
|
||
pageNum: 1,
|
||
pageSize: 10,
|
||
orderStatus: '1', // 查询待分派状态的工单
|
||
},
|
||
token,
|
||
);
|
||
|
||
if (conditionResult.success) {
|
||
console.log('✅ 条件查询成功'.green);
|
||
if (conditionResult.data.data && conditionResult.data.data.rows) {
|
||
console.log(`📈 条件查询结果数: ${conditionResult.data.data.total}`.yellow);
|
||
}
|
||
} else {
|
||
console.log('❌ 条件查询失败'.red);
|
||
}
|
||
|
||
return basicResult;
|
||
}
|
||
|
||
// 测试数据库连接和基础接口
|
||
async function testDatabaseConnection(token) {
|
||
console.log('\n🔗 测试数据库连接和基础接口'.blue.bold);
|
||
console.log('='.repeat(60).blue);
|
||
|
||
// 测试维修分类接口(检查数据库连接)
|
||
console.log('\n1. 测试维修分类接口'.cyan);
|
||
const categoryResult = await testAPI(
|
||
'/pm/repairCategory/list',
|
||
'GET',
|
||
{
|
||
pageNum: 1,
|
||
pageSize: 10,
|
||
},
|
||
token,
|
||
);
|
||
|
||
if (categoryResult.success) {
|
||
console.log('✅ 维修分类接口正常'.green);
|
||
if (categoryResult.data.data && categoryResult.data.data.rows) {
|
||
console.log(`📈 维修分类数量: ${categoryResult.data.data.total}`.yellow);
|
||
}
|
||
} else {
|
||
console.log('❌ 维修分类接口失败'.red);
|
||
}
|
||
|
||
// 测试用户信息接口(检查认证)
|
||
console.log('\n2. 测试用户信息接口'.cyan);
|
||
const userResult = await testAPI('/system/user/getInfo', 'GET', null, token);
|
||
|
||
if (userResult.success) {
|
||
console.log('✅ 用户认证正常'.green);
|
||
if (userResult.data.data && userResult.data.data.user) {
|
||
console.log(`👤 当前用户: ${userResult.data.data.user.userName}`.yellow);
|
||
console.log(`🔑 用户权限: ${userResult.data.data.permissions.join(', ')}`.yellow);
|
||
}
|
||
} else {
|
||
console.log('❌ 用户认证失败'.red);
|
||
}
|
||
|
||
return {
|
||
categoryResult,
|
||
userResult,
|
||
};
|
||
}
|
||
|
||
// 创建测试数据
|
||
async function createTestData(token) {
|
||
console.log('\n➕ 创建测试数据'.blue.bold);
|
||
console.log('='.repeat(60).blue);
|
||
|
||
// 首先获取维修分类
|
||
const categoryResult = await testAPI('/pm/repairCategory/options', 'GET', null, token);
|
||
let categoryId = 1; // 默认分类ID
|
||
|
||
if (categoryResult.success && categoryResult.data.data && categoryResult.data.data.length > 0) {
|
||
categoryId = categoryResult.data.data[0].value;
|
||
console.log(`✅ 获取到维修分类ID: ${categoryId}`.green);
|
||
} else {
|
||
console.log('⚠️ 使用默认分类ID: 1'.yellow);
|
||
}
|
||
|
||
// 创建测试工单数据
|
||
const testOrderData = {
|
||
title: '测试报修工单 - 办公室空调故障',
|
||
description: '办公室空调不制冷,温度显示异常,需要维修检查。',
|
||
repairLocation: '行政楼二楼201办公室',
|
||
categoryId: categoryId,
|
||
priorityLevel: 2,
|
||
reporterName: '测试用户',
|
||
reporterPhone: '13900139001',
|
||
reporterDept: '信息技术部',
|
||
remark: '紧急处理,影响办公',
|
||
};
|
||
|
||
console.log('\n1. 创建测试工单'.cyan);
|
||
const createResult = await testAPI('/pm/repairOrder', 'POST', testOrderData, token);
|
||
|
||
if (createResult.success) {
|
||
console.log('✅ 测试工单创建成功'.green);
|
||
console.log(`📝 响应: ${JSON.stringify(createResult.data, null, 2)}`.green);
|
||
return true;
|
||
} else {
|
||
console.log('❌ 测试工单创建失败'.red);
|
||
console.log(`📝 错误: ${JSON.stringify(createResult.error, null, 2)}`.red);
|
||
return false;
|
||
}
|
||
}
|
||
|
||
// 主调试函数
|
||
async function debugRepairOrderList() {
|
||
console.log('🔍 报修管理列表数据调试工具'.rainbow.bold);
|
||
console.log('='.repeat(80).rainbow);
|
||
|
||
const startTime = new Date();
|
||
|
||
try {
|
||
// 1. 获取认证Token
|
||
console.log('\n🔐 步骤1: 获取认证Token'.magenta.bold);
|
||
const token = await getToken();
|
||
if (token.includes('模拟') || token.length < 50) {
|
||
console.log('⚠️ 使用模拟Token,可能影响测试结果'.yellow);
|
||
} else {
|
||
console.log('✅ 获取到有效Token'.green);
|
||
}
|
||
|
||
// 2. 测试数据库连接和基础接口
|
||
const dbTest = await testDatabaseConnection(token);
|
||
|
||
// 3. 测试报修工单列表接口
|
||
const listTest = await testRepairOrderList(token);
|
||
|
||
// 4. 如果没有数据,尝试创建测试数据
|
||
if (listTest.success && listTest.data.data) {
|
||
let dataCount = 0;
|
||
if (listTest.data.data.rows) {
|
||
dataCount = listTest.data.data.total || 0;
|
||
} else if (Array.isArray(listTest.data.data)) {
|
||
dataCount = listTest.data.data.length;
|
||
}
|
||
|
||
if (dataCount === 0) {
|
||
console.log('\n📝 检测到数据库中没有报修工单数据,尝试创建测试数据...'.yellow.bold);
|
||
const created = await createTestData(token);
|
||
|
||
if (created) {
|
||
console.log('\n🔄 重新测试列表接口...'.cyan);
|
||
await testRepairOrderList(token);
|
||
}
|
||
}
|
||
}
|
||
|
||
// 5. 生成调试报告
|
||
const endTime = new Date();
|
||
const duration = endTime - startTime;
|
||
|
||
console.log('\n📊 调试结果汇总'.rainbow.bold);
|
||
console.log('='.repeat(80).rainbow);
|
||
|
||
const summary = {
|
||
认证状态: token.length > 50 ? '✅ 正常' : '⚠️ 模拟Token',
|
||
数据库连接: dbTest.categoryResult.success ? '✅ 正常' : '❌ 异常',
|
||
用户权限: dbTest.userResult.success ? '✅ 正常' : '❌ 异常',
|
||
列表接口: listTest.success ? '✅ 正常' : '❌ 异常',
|
||
调试耗时: `${duration}ms`,
|
||
};
|
||
|
||
for (const [key, value] of Object.entries(summary)) {
|
||
console.log(`${key}: ${value}`);
|
||
}
|
||
|
||
// 6. 问题诊断和建议
|
||
console.log('\n💡 问题诊断和建议'.yellow.bold);
|
||
console.log('='.repeat(60).yellow);
|
||
|
||
if (!listTest.success) {
|
||
console.log('🔧 接口调用失败,请检查:');
|
||
console.log(' - 后端服务是否正常启动');
|
||
console.log(' - 接口路径是否正确');
|
||
console.log(' - 权限配置是否正确');
|
||
} else if (listTest.data.data) {
|
||
let dataCount = 0;
|
||
if (listTest.data.data.rows) {
|
||
dataCount = listTest.data.data.total || 0;
|
||
} else if (Array.isArray(listTest.data.data)) {
|
||
dataCount = listTest.data.data.length;
|
||
}
|
||
|
||
if (dataCount === 0) {
|
||
console.log('🔧 数据库中没有报修工单数据,建议:');
|
||
console.log(' - 检查数据库表是否存在');
|
||
console.log(' - 手动添加一些测试数据');
|
||
console.log(' - 检查数据权限过滤是否过于严格');
|
||
} else {
|
||
console.log('✅ 接口和数据都正常,前端可能存在问题:');
|
||
console.log(' - 检查前端数据处理逻辑');
|
||
console.log(' - 检查浏览器控制台错误');
|
||
console.log(' - 验证前端API调用代码');
|
||
}
|
||
}
|
||
} catch (error) {
|
||
console.error('\n💥 调试过程出错:'.red.bold, error.message);
|
||
}
|
||
}
|
||
|
||
// 如果直接运行此脚本
|
||
if (require.main === module) {
|
||
debugRepairOrderList()
|
||
.then(() => {
|
||
console.log('\n👋 调试完成'.cyan);
|
||
})
|
||
.catch((error) => {
|
||
console.error('调试脚本执行失败:', error);
|
||
process.exit(1);
|
||
});
|
||
}
|
||
|
||
module.exports = {
|
||
debugRepairOrderList,
|
||
testRepairOrderList,
|
||
testDatabaseConnection,
|
||
createTestData,
|
||
};
|
||
|