231 lines
7.9 KiB
JavaScript
231 lines
7.9 KiB
JavaScript
const axios = require('axios');
|
|
const colors = require('colors');
|
|
|
|
// 测试配置
|
|
const BASE_URL = 'http://localhost/dev-api';
|
|
|
|
// 从test/custom-login-script.js中导入登录方法(如果需要的话)
|
|
async function getTestToken() {
|
|
// 这里可以调用登录获取Token,暂时使用模拟Token进行测试
|
|
// 实际使用时需要先登录获取真实Token
|
|
return 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...'; // 模拟Token
|
|
}
|
|
|
|
// 通用API测试方法
|
|
async function testAPI(endpoint, method = 'GET', data = null, token = null) {
|
|
try {
|
|
console.log(`\n🧪 测试接口: ${method} ${BASE_URL}${endpoint}`.cyan);
|
|
|
|
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);
|
|
|
|
console.log(`✅ 状态码: ${response.status}`.green);
|
|
console.log(`📊 响应数据: ${JSON.stringify(response.data, null, 2)}`.white);
|
|
|
|
return {
|
|
success: true,
|
|
data: response.data,
|
|
status: response.status,
|
|
};
|
|
} catch (error) {
|
|
console.log(`❌ 测试失败:`.red);
|
|
if (error.response) {
|
|
console.log(` 状态码: ${error.response.status}`.red);
|
|
console.log(` 错误信息: ${JSON.stringify(error.response.data, null, 2)}`.red);
|
|
} else {
|
|
console.log(` 错误信息: ${error.message}`.red);
|
|
}
|
|
|
|
return {
|
|
success: false,
|
|
error: error.response?.data || error.message,
|
|
status: error.response?.status,
|
|
};
|
|
}
|
|
}
|
|
|
|
// 测试维修分类选项接口
|
|
async function testCategoryOptions() {
|
|
console.log('\n🔧 测试维修分类选项接口'.blue.bold);
|
|
console.log('='.repeat(50).blue);
|
|
|
|
const result = await testAPI('/pm/repairCategory/options');
|
|
|
|
if (result.success) {
|
|
console.log('\n📝 分析结果:'.yellow);
|
|
if (result.data && result.data.data) {
|
|
console.log(` - 分类数量: ${result.data.data.length}`.yellow);
|
|
console.log(` - 数据格式: ${typeof result.data.data}`.yellow);
|
|
if (result.data.data.length > 0) {
|
|
console.log(` - 首项示例: ${JSON.stringify(result.data.data[0])}`.yellow);
|
|
}
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
// 测试统计数据接口
|
|
async function testStatistics() {
|
|
console.log('\n📊 测试报修统计数据接口'.blue.bold);
|
|
console.log('='.repeat(50).blue);
|
|
|
|
const result = await testAPI('/pm/repairOrder/statistics');
|
|
|
|
if (result.success) {
|
|
console.log('\n📝 分析结果:'.yellow);
|
|
if (result.data && result.data.data) {
|
|
const stats = result.data.data;
|
|
console.log(` - 总工单数: ${stats.totalOrders}`.yellow);
|
|
console.log(` - 待分派: ${stats.pendingAssign}`.yellow);
|
|
console.log(` - 进行中: ${stats.inProgress}`.yellow);
|
|
console.log(` - 已完成: ${stats.completed}`.yellow);
|
|
console.log(` - 平均处理时间: ${stats.avgProcessTime}小时`.yellow);
|
|
console.log(` - 平均评价得分: ${stats.avgEvaluationScore}分`.yellow);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
// 测试维修分类树形接口(对比测试)
|
|
async function testCategoryTree() {
|
|
console.log('\n🌳 测试维修分类树形接口(对比)'.blue.bold);
|
|
console.log('='.repeat(50).blue);
|
|
|
|
const result = await testAPI('/pm/repairCategory/tree');
|
|
|
|
if (result.success) {
|
|
console.log('\n📝 分析结果:'.yellow);
|
|
if (result.data && result.data.data) {
|
|
console.log(` - 根分类数量: ${result.data.data.length}`.yellow);
|
|
console.log(` - 数据结构: 树形结构`.yellow);
|
|
if (result.data.data.length > 0) {
|
|
const firstCategory = result.data.data[0];
|
|
console.log(` - 首项示例: ${firstCategory.categoryName}`.yellow);
|
|
if (firstCategory.children && firstCategory.children.length > 0) {
|
|
console.log(` - 子分类数量: ${firstCategory.children.length}`.yellow);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
// 测试报修工单列表接口(对比测试)
|
|
async function testRepairOrderList() {
|
|
console.log('\n📋 测试报修工单列表接口(对比)'.blue.bold);
|
|
console.log('='.repeat(50).blue);
|
|
|
|
const result = await testAPI('/pm/repairOrder/list', 'GET', {
|
|
pageNum: 1,
|
|
pageSize: 10,
|
|
});
|
|
|
|
if (result.success) {
|
|
console.log('\n📝 分析结果:'.yellow);
|
|
if (result.data) {
|
|
console.log(` - 响应格式: ${result.data.msg}`.yellow);
|
|
console.log(` - 总数: ${result.data.total || 0}`.yellow);
|
|
console.log(
|
|
` - 当前页数据: ${result.data.rows ? result.data.rows.length : 0}`.yellow,
|
|
);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
// 主测试函数
|
|
async function runTests() {
|
|
console.log('🚀 开始测试报修管理相关API接口'.rainbow.bold);
|
|
console.log('=' * 60);
|
|
|
|
const startTime = new Date();
|
|
|
|
try {
|
|
// 测试各个接口
|
|
const categoryOptionsResult = await testCategoryOptions();
|
|
const statisticsResult = await testStatistics();
|
|
const categoryTreeResult = await testCategoryTree();
|
|
const orderListResult = await testRepairOrderList();
|
|
|
|
// 汇总测试结果
|
|
console.log('\n📊 测试结果汇总'.rainbow.bold);
|
|
console.log('='.repeat(60).rainbow);
|
|
|
|
const tests = [
|
|
{ name: '维修分类选项接口', result: categoryOptionsResult },
|
|
{ name: '统计数据接口', result: statisticsResult },
|
|
{ name: '维修分类树形接口', result: categoryTreeResult },
|
|
{ name: '报修工单列表接口', result: orderListResult },
|
|
];
|
|
|
|
let successCount = 0;
|
|
tests.forEach((test) => {
|
|
const status = test.result.success ? '✅ 通过' : '❌ 失败';
|
|
const statusColor = test.result.success ? 'green' : 'red';
|
|
console.log(`${test.name}: ${colors[statusColor](status)}`);
|
|
if (test.result.success) successCount++;
|
|
});
|
|
|
|
const endTime = new Date();
|
|
const duration = endTime - startTime;
|
|
|
|
console.log(`\n🎯 测试完成!`.bold);
|
|
console.log(` - 成功: ${successCount}/${tests.length}`.green);
|
|
console.log(` - 耗时: ${duration}ms`.cyan);
|
|
|
|
if (successCount === tests.length) {
|
|
console.log('\n🎉 所有测试通过!API接口工作正常。'.green.bold);
|
|
} else {
|
|
console.log('\n⚠️ 部分测试失败,请检查后端服务状态。'.yellow.bold);
|
|
}
|
|
} catch (error) {
|
|
console.error('\n💥 测试执行出错:'.red.bold, error.message);
|
|
}
|
|
}
|
|
|
|
// 如果直接运行此脚本
|
|
if (require.main === module) {
|
|
runTests()
|
|
.then(() => {
|
|
console.log('\n👋 测试脚本执行完毕'.cyan);
|
|
})
|
|
.catch((error) => {
|
|
console.error('脚本执行失败:', error);
|
|
process.exit(1);
|
|
});
|
|
}
|
|
|
|
module.exports = {
|
|
testAPI,
|
|
testCategoryOptions,
|
|
testStatistics,
|
|
testCategoryTree,
|
|
testRepairOrderList,
|
|
runTests,
|
|
};
|
|
|