77 lines
2.2 KiB
JavaScript
77 lines
2.2 KiB
JavaScript
|
|
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);
|