101 lines
2.7 KiB
JavaScript
101 lines
2.7 KiB
JavaScript
// 测试修复后的epidemic agencies接口
|
||
const axios = require('axios');
|
||
|
||
// 政府后端服务地址
|
||
const BASE_URL = 'http://localhost:5352/api';
|
||
|
||
// 登录获取token
|
||
async function login() {
|
||
try {
|
||
console.log('开始登录...');
|
||
const response = await axios.post(`${BASE_URL}/auth/login`, {
|
||
username: 'admin',
|
||
password: '123456'
|
||
});
|
||
|
||
console.log('登录响应:', response.data);
|
||
|
||
if (response.data.code === 200 && response.data.data && response.data.data.token) {
|
||
console.log('登录成功,获取到token');
|
||
return response.data.data.token;
|
||
} else {
|
||
console.log('登录失败,未获取到token');
|
||
console.log('错误信息:', response.data.message || '未知错误');
|
||
return null;
|
||
}
|
||
} catch (error) {
|
||
console.error('登录请求失败:', error.message);
|
||
if (error.response) {
|
||
console.error('错误状态码:', error.response.status);
|
||
console.error('错误数据:', error.response.data);
|
||
}
|
||
return null;
|
||
}
|
||
}
|
||
|
||
// 测试新增防疫机构
|
||
async function testCreateAgency(token) {
|
||
try {
|
||
console.log('\n开始测试新增防疫机构...');
|
||
const testData = {
|
||
name: '测试防疫机构' + Date.now(),
|
||
code: 'TEST-' + Date.now(),
|
||
type: 'station',
|
||
level: 'county',
|
||
manager: '测试负责人',
|
||
phone: '13800138999',
|
||
address: '测试地址',
|
||
epidemicScope: '测试防疫范围',
|
||
remarks: '测试备注',
|
||
email: 'test@example.com',
|
||
status: 'active',
|
||
establishmentDate: '2024-01-01'
|
||
};
|
||
|
||
console.log('提交的数据:', testData);
|
||
|
||
const response = await axios.post(`${BASE_URL}/epidemic/agencies`, testData, {
|
||
headers: {
|
||
'Authorization': `Bearer ${token}`
|
||
}
|
||
});
|
||
|
||
console.log('新增防疫机构成功');
|
||
console.log(`- 状态码: ${response.status}`);
|
||
console.log(`- 返回数据:`, response.data);
|
||
|
||
return response.data;
|
||
} catch (error) {
|
||
console.error('测试新增防疫机构失败:', error.message);
|
||
if (error.response) {
|
||
console.error('错误状态码:', error.response.status);
|
||
console.error('错误数据:', error.response.data);
|
||
}
|
||
return null;
|
||
}
|
||
}
|
||
|
||
// 执行测试
|
||
async function runTests() {
|
||
try {
|
||
console.log('开始测试修复后的epidemic agencies接口...');
|
||
|
||
// 1. 登录获取token
|
||
const token = await login();
|
||
if (!token) {
|
||
console.log('未获取到token,测试终止');
|
||
return;
|
||
}
|
||
|
||
// 2. 测试新增防疫机构
|
||
const createResult = await testCreateAgency(token);
|
||
|
||
console.log('\n测试完成!');
|
||
|
||
} catch (error) {
|
||
console.error('测试过程中发生错误:', error);
|
||
}
|
||
}
|
||
|
||
// 执行测试
|
||
runTests(); |