Files
nxxmdata/bank-backend/test-loan-applications-api.js
2025-09-24 17:49:32 +08:00

118 lines
4.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 贷款申请API测试
* @file test-loan-applications-api.js
*/
const axios = require('axios');
async function testLoanApplicationsAPI() {
try {
console.log('🔍 测试贷款申请API...');
// 1. 登录获取token
console.log('\n1. 登录测试...');
const loginResponse = await axios.post('http://localhost:5351/api/auth/login', {
username: 'admin',
password: 'Admin123456'
});
if (!loginResponse.data.success) {
throw new Error('登录失败: ' + loginResponse.data.message);
}
const token = loginResponse.data.data.token;
console.log('✅ 登录成功');
// 设置授权头
const authHeaders = {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
};
// 2. 获取贷款申请列表
console.log('\n2. 获取申请列表...');
const listResponse = await axios.get('http://localhost:5351/api/loan-applications', {
headers: authHeaders
});
if (!listResponse.data.success) {
throw new Error('获取列表失败: ' + listResponse.data.message);
}
console.log('✅ 获取申请列表成功');
console.log(`📊 申请数量: ${listResponse.data.data.applications.length}`);
console.log(`📊 总数: ${listResponse.data.data.pagination.total}`);
if (listResponse.data.data.applications.length > 0) {
const firstApp = listResponse.data.data.applications[0];
console.log(`📋 第一个申请: ${firstApp.applicationNumber} - ${firstApp.productName} - ${firstApp.status}`);
// 3. 获取申请详情
console.log('\n3. 获取申请详情...');
const detailResponse = await axios.get(`http://localhost:5351/api/loan-applications/${firstApp.id}`, {
headers: authHeaders
});
if (!detailResponse.data.success) {
throw new Error('获取详情失败: ' + detailResponse.data.message);
}
console.log('✅ 获取申请详情成功');
console.log(`📋 申请详情: ${detailResponse.data.data.applicationNumber}`);
console.log(`📋 审核记录数: ${detailResponse.data.data.auditRecords.length}`);
// 4. 测试审核功能(仅对待审核的申请)
if (firstApp.status === 'pending_review') {
console.log('\n4. 测试审核功能...');
const auditResponse = await axios.post(`http://localhost:5351/api/loan-applications/${firstApp.id}/audit`, {
action: 'approve',
comment: 'API测试审核通过'
}, {
headers: authHeaders
});
if (!auditResponse.data.success) {
throw new Error('审核失败: ' + auditResponse.data.message);
}
console.log('✅ 审核功能测试成功');
console.log(`📋 审核结果: ${auditResponse.data.message}`);
}
}
// 5. 获取统计信息
console.log('\n5. 获取统计信息...');
const statsResponse = await axios.get('http://localhost:5351/api/loan-applications/stats', {
headers: authHeaders
});
if (!statsResponse.data.success) {
throw new Error('获取统计失败: ' + statsResponse.data.message);
}
console.log('✅ 获取统计信息成功');
console.log(`📊 总申请数: ${statsResponse.data.data.total.applications}`);
console.log(`📊 总金额: ${statsResponse.data.data.total.amount.toFixed(2)}`);
console.log('📊 按状态统计:');
Object.entries(statsResponse.data.data.byStatus.counts).forEach(([status, count]) => {
if (count > 0) {
console.log(` - ${status}: ${count}个申请`);
}
});
console.log('\n🎉 所有API测试完成');
} catch (error) {
console.error('\n❌ API测试失败:', error.message);
if (error.response) {
console.error('响应状态:', error.response.status);
console.error('响应数据:', error.response.data);
} else if (error.code) {
console.error('错误代码:', error.code);
}
console.error('完整错误:', error);
}
}
// 运行测试
testLoanApplicationsAPI();