141 lines
3.8 KiB
JavaScript
141 lines
3.8 KiB
JavaScript
/**
|
|
* 快速测试脚本 - 验证管理后台数据
|
|
*/
|
|
|
|
const https = require('http');
|
|
|
|
console.log('🔍 开始测试管理后台...\n');
|
|
|
|
// 测试配置
|
|
const config = {
|
|
backend: 'http://localhost:3000',
|
|
admin: 'http://localhost:3001'
|
|
};
|
|
|
|
// 测试步骤
|
|
async function runTests() {
|
|
console.log('步骤 1: 测试后端健康检查...');
|
|
await testEndpoint(config.backend + '/health', 'GET');
|
|
|
|
console.log('\n步骤 2: 测试管理员登录...');
|
|
const loginData = await testLogin();
|
|
|
|
if (loginData && loginData.token) {
|
|
console.log('\n步骤 3: 测试用户列表 API...');
|
|
await testUsers(loginData.token);
|
|
|
|
console.log('\n步骤 4: 测试统计数据 API...');
|
|
await testStats(loginData.token);
|
|
}
|
|
|
|
console.log('\n✅ 测试完成!');
|
|
console.log('\n📋 下一步操作:');
|
|
console.log('1. 打开浏览器访问: http://localhost:3001');
|
|
console.log('2. 按 F12 打开开发者工具');
|
|
console.log('3. 在 Console 执行: localStorage.clear(); location.reload();');
|
|
console.log('4. 使用 admin/admin123 登录');
|
|
console.log('5. 查看用户管理和数据统计页面');
|
|
}
|
|
|
|
function testEndpoint(url, method = 'GET', data = null, token = null) {
|
|
return new Promise((resolve, reject) => {
|
|
const urlObj = new URL(url);
|
|
const options = {
|
|
hostname: urlObj.hostname,
|
|
port: urlObj.port,
|
|
path: urlObj.pathname + urlObj.search,
|
|
method: method,
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
}
|
|
};
|
|
|
|
if (token) {
|
|
options.headers['Authorization'] = `Bearer ${token}`;
|
|
}
|
|
|
|
if (data) {
|
|
const postData = JSON.stringify(data);
|
|
options.headers['Content-Length'] = Buffer.byteLength(postData);
|
|
}
|
|
|
|
const req = https.request(options, (res) => {
|
|
let body = '';
|
|
res.on('data', (chunk) => body += chunk);
|
|
res.on('end', () => {
|
|
try {
|
|
const result = JSON.parse(body);
|
|
console.log(` ✅ ${method} ${url}`);
|
|
console.log(` 状态码: ${res.statusCode}`);
|
|
resolve(result);
|
|
} catch (e) {
|
|
console.log(` ⚠️ 响应: ${body.substring(0, 100)}`);
|
|
resolve(null);
|
|
}
|
|
});
|
|
});
|
|
|
|
req.on('error', (e) => {
|
|
console.log(` ❌ 错误: ${e.message}`);
|
|
resolve(null);
|
|
});
|
|
|
|
if (data) {
|
|
req.write(JSON.stringify(data));
|
|
}
|
|
|
|
req.end();
|
|
});
|
|
}
|
|
|
|
async function testLogin() {
|
|
const result = await testEndpoint(
|
|
config.backend + '/api/v1/admin/login',
|
|
'POST',
|
|
{ username: 'admin', password: 'admin123' }
|
|
);
|
|
|
|
if (result && result.data && result.data.token) {
|
|
console.log(` 🔑 Token: ${result.data.token.substring(0, 30)}...`);
|
|
console.log(` 👤 用户: ${result.data.admin.username} (${result.data.admin.role})`);
|
|
return result.data;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
async function testUsers(token) {
|
|
const result = await testEndpoint(
|
|
config.backend + '/api/v1/admin/users?page=1&limit=20',
|
|
'GET',
|
|
null,
|
|
token
|
|
);
|
|
|
|
if (result && result.data) {
|
|
console.log(` 👥 用户总数: ${result.data.pagination.total}`);
|
|
console.log(` 📄 当前页: ${result.data.users.length} 个用户`);
|
|
if (result.data.users.length > 0) {
|
|
console.log(` 📝 示例用户: ${result.data.users[0].nickname} (${result.data.users[0].invitationCode})`);
|
|
}
|
|
}
|
|
}
|
|
|
|
async function testStats(token) {
|
|
const result = await testEndpoint(
|
|
config.backend + '/api/v1/admin/statistics/dashboard',
|
|
'GET',
|
|
null,
|
|
token
|
|
);
|
|
|
|
if (result && result.data) {
|
|
console.log(` 📊 总用户数: ${result.data.totalUsers}`);
|
|
console.log(` 📅 活跃预约: ${result.data.activeAppointments}`);
|
|
console.log(` 💰 待审核提现: ${result.data.pendingWithdrawals}`);
|
|
}
|
|
}
|
|
|
|
// 运行测试
|
|
runTests().catch(console.error);
|