97 lines
3.1 KiB
JavaScript
97 lines
3.1 KiB
JavaScript
|
|
/**
|
|||
|
|
* 测试创建项目接口
|
|||
|
|
*/
|
|||
|
|
const axios = require('axios');
|
|||
|
|
|
|||
|
|
const API_BASE_URL = 'http://localhost:5351';
|
|||
|
|
|
|||
|
|
async function testCreateProject() {
|
|||
|
|
try {
|
|||
|
|
console.log('🚀 开始测试创建项目接口...\n');
|
|||
|
|
|
|||
|
|
// 1. 先登录获取token
|
|||
|
|
console.log('1. 登录获取认证token...');
|
|||
|
|
const loginResponse = await axios.post(`${API_BASE_URL}/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('✅ 登录成功,获取到token\n');
|
|||
|
|
|
|||
|
|
// 2. 测试创建项目
|
|||
|
|
console.log('2. 测试创建新项目...');
|
|||
|
|
const newProject = {
|
|||
|
|
name: '测试项目_' + new Date().getTime(),
|
|||
|
|
status: 'supervision',
|
|||
|
|
farmName: '测试养殖场',
|
|||
|
|
supervisionObject: '牛',
|
|||
|
|
supervisionQuantity: 100,
|
|||
|
|
supervisionPeriod: '12个月',
|
|||
|
|
supervisionAmount: 500000.00,
|
|||
|
|
startTime: '2024-01-01',
|
|||
|
|
endTime: '2024-12-31',
|
|||
|
|
earTag: 50,
|
|||
|
|
collar: 30,
|
|||
|
|
host: 20,
|
|||
|
|
loanOfficer: '张专员',
|
|||
|
|
description: '这是一个测试项目,用于验证创建接口功能'
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
const createResponse = await axios.post(`${API_BASE_URL}/api/projects`, newProject, {
|
|||
|
|
headers: {
|
|||
|
|
'Authorization': `Bearer ${token}`,
|
|||
|
|
'Content-Type': 'application/json'
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
if (createResponse.data.success) {
|
|||
|
|
console.log('✅ 项目创建成功!');
|
|||
|
|
console.log('📋 创建的项目信息:');
|
|||
|
|
console.log(` - 项目ID: ${createResponse.data.data.id}`);
|
|||
|
|
console.log(` - 项目名称: ${createResponse.data.data.name}`);
|
|||
|
|
console.log(` - 养殖场: ${createResponse.data.data.farmName}`);
|
|||
|
|
console.log(` - 监管对象: ${createResponse.data.data.supervisionObject}`);
|
|||
|
|
console.log(` - 监管数量: ${createResponse.data.data.supervisionQuantity}`);
|
|||
|
|
console.log(` - 监管金额: ${createResponse.data.data.supervisionAmount}元`);
|
|||
|
|
console.log(` - 贷款专员: ${createResponse.data.data.loanOfficer}`);
|
|||
|
|
console.log(` - 创建时间: ${createResponse.data.data.createdAt}`);
|
|||
|
|
} else {
|
|||
|
|
console.log('❌ 项目创建失败:', createResponse.data.message);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 3. 验证项目是否在列表中
|
|||
|
|
console.log('\n3. 验证项目是否在列表中...');
|
|||
|
|
const listResponse = await axios.get(`${API_BASE_URL}/api/projects`, {
|
|||
|
|
headers: {
|
|||
|
|
'Authorization': `Bearer ${token}`
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
if (listResponse.data.success) {
|
|||
|
|
const projects = listResponse.data.data.projects;
|
|||
|
|
const createdProject = projects.find(p => p.name === newProject.name);
|
|||
|
|
|
|||
|
|
if (createdProject) {
|
|||
|
|
console.log('✅ 项目已成功添加到列表中');
|
|||
|
|
console.log(`📊 当前总项目数: ${projects.length}`);
|
|||
|
|
} else {
|
|||
|
|
console.log('❌ 项目未在列表中找到');
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('❌ 测试失败:', error.message);
|
|||
|
|
if (error.response) {
|
|||
|
|
console.error('响应数据:', error.response.data);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 运行测试
|
|||
|
|
testCreateProject();
|