refactor(backend): 重构动物相关 API 接口
- 更新了动物数据结构和相关类型定义 - 优化了动物列表、详情、创建、更新和删除接口 - 新增了更新动物状态接口 - 移除了与认领记录相关的接口 -调整了 API 响应结构
This commit is contained in:
77
test_admin_api.js
Normal file
77
test_admin_api.js
Normal file
@@ -0,0 +1,77 @@
|
||||
const axios = require('axios');
|
||||
|
||||
// 创建axios实例
|
||||
const api = axios.create({
|
||||
baseURL: 'http://localhost:3100/api/v1',
|
||||
timeout: 10000
|
||||
});
|
||||
|
||||
async function testAdminAPI() {
|
||||
console.log('🚀 开始测试管理员API接口...\n');
|
||||
|
||||
let token = null;
|
||||
|
||||
try {
|
||||
// 1. 测试管理员登录(使用新创建的测试账户)
|
||||
console.log('1. 测试管理员登录...');
|
||||
const loginResponse = await api.post('/admin/login', {
|
||||
username: 'testadmin',
|
||||
password: 'admin123'
|
||||
});
|
||||
|
||||
console.log('✅ 登录成功');
|
||||
console.log('登录响应:', loginResponse.data);
|
||||
|
||||
// 保存token用于后续测试
|
||||
token = loginResponse.data.data.token;
|
||||
api.defaults.headers.common['Authorization'] = `Bearer ${token}`;
|
||||
|
||||
} catch (error) {
|
||||
if (error.response) {
|
||||
console.log('❌ 登录失败:', error.response.data);
|
||||
} else {
|
||||
console.error('❌ 请求失败:', error.message);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// 2. 测试获取当前管理员信息
|
||||
console.log('\n2. 测试获取当前管理员信息...');
|
||||
const profileResponse = await api.get('/admin/profile');
|
||||
|
||||
console.log('✅ 获取管理员信息成功');
|
||||
console.log('管理员信息:', profileResponse.data);
|
||||
|
||||
} catch (error) {
|
||||
if (error.response) {
|
||||
console.log('❌ 获取管理员信息失败:', error.response.data);
|
||||
} else {
|
||||
console.error('❌ 请求失败:', error.message);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
// 3. 测试获取管理员列表
|
||||
console.log('\n3. 测试获取管理员列表...');
|
||||
const listResponse = await api.get('/admin');
|
||||
|
||||
console.log('✅ 获取管理员列表成功');
|
||||
console.log('管理员列表:');
|
||||
listResponse.data.data.admins.forEach((admin, index) => {
|
||||
console.log(`${index + 1}. ID: ${admin.id}, 用户名: ${admin.username}, 角色: ${admin.role}, 状态: ${admin.status}`);
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
if (error.response) {
|
||||
console.log('❌ 获取管理员列表失败:', error.response.data);
|
||||
} else {
|
||||
console.error('❌ 请求失败:', error.message);
|
||||
}
|
||||
}
|
||||
|
||||
console.log('\n✅ 管理员API测试完成');
|
||||
}
|
||||
|
||||
// 运行测试
|
||||
testAdminAPI().catch(console.error);
|
||||
Reference in New Issue
Block a user