95 lines
2.7 KiB
JavaScript
95 lines
2.7 KiB
JavaScript
|
|
// 测试无害化场所API
|
|||
|
|
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;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 测试无害化场所列表API
|
|||
|
|
async function testHarmlessPlaceList(token) {
|
|||
|
|
try {
|
|||
|
|
console.log('\n测试无害化场所列表API...');
|
|||
|
|
const response = await axios.get(`${BASE_URL}/harmless-place/list`, {
|
|||
|
|
headers: {
|
|||
|
|
'Authorization': `Bearer ${token}`
|
|||
|
|
},
|
|||
|
|
params: {
|
|||
|
|
page: 1,
|
|||
|
|
pageSize: 10
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
console.log('API调用成功,状态码:', response.status);
|
|||
|
|
console.log('返回数据结构:', Object.keys(response.data));
|
|||
|
|
console.log('无害化场所总数:', response.data.total || '未知');
|
|||
|
|
|
|||
|
|
if (response.data.data && Array.isArray(response.data.data)) {
|
|||
|
|
console.log('返回的无害化场所列表长度:', response.data.data.length);
|
|||
|
|
if (response.data.data.length > 0) {
|
|||
|
|
console.log('第一条无害化场所数据:');
|
|||
|
|
console.log(response.data.data[0]);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return response.data;
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('无害化场所列表API调用失败:', error.message);
|
|||
|
|
if (error.response) {
|
|||
|
|
console.error('错误状态码:', error.response.status);
|
|||
|
|
console.error('错误数据:', error.response.data);
|
|||
|
|
}
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 主测试函数
|
|||
|
|
const runTests = async () => {
|
|||
|
|
console.log('开始测试无害化场所管理API...');
|
|||
|
|
try {
|
|||
|
|
// 1. 登录获取token
|
|||
|
|
const token = await login();
|
|||
|
|
|
|||
|
|
if (!token) {
|
|||
|
|
console.error('无法继续测试,因为未获取到有效的token');
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 2. 测试获取无害化场所列表
|
|||
|
|
await testHarmlessPlaceList(token);
|
|||
|
|
|
|||
|
|
console.log('\n所有测试完成!');
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('测试过程中发生错误:', error);
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
// 运行测试
|
|||
|
|
runTests();
|