71 lines
1.7 KiB
JavaScript
71 lines
1.7 KiB
JavaScript
const http = require('http');
|
||
|
||
async function testAccountsAPI() {
|
||
try {
|
||
// 先登录获取token
|
||
const loginResult = await makeRequest({
|
||
hostname: 'localhost',
|
||
port: 5351,
|
||
path: '/api/auth/login',
|
||
method: 'POST',
|
||
headers: { 'Content-Type': 'application/json' }
|
||
}, {
|
||
username: 'admin',
|
||
password: 'Admin123456'
|
||
});
|
||
|
||
if (!loginResult.data.success) {
|
||
console.log('登录失败:', loginResult.data.message);
|
||
return;
|
||
}
|
||
|
||
const token = loginResult.data.data.token;
|
||
console.log('登录成功,token:', token.substring(0, 20) + '...');
|
||
|
||
// 测试账户API
|
||
const accountsResult = await makeRequest({
|
||
hostname: 'localhost',
|
||
port: 5351,
|
||
path: '/api/accounts?page=1&pageSize=10',
|
||
method: 'GET',
|
||
headers: { 'Authorization': `Bearer ${token}` }
|
||
});
|
||
|
||
console.log('账户API响应状态:', accountsResult.status);
|
||
console.log('账户API响应数据:', JSON.stringify(accountsResult.data, null, 2));
|
||
|
||
} catch (error) {
|
||
console.error('测试失败:', error);
|
||
}
|
||
}
|
||
|
||
function makeRequest(options, data = null) {
|
||
return new Promise((resolve, reject) => {
|
||
const req = http.request(options, (res) => {
|
||
let body = '';
|
||
res.on('data', (chunk) => {
|
||
body += chunk;
|
||
});
|
||
res.on('end', () => {
|
||
try {
|
||
const result = JSON.parse(body);
|
||
resolve({ status: res.statusCode, data: result });
|
||
} catch (error) {
|
||
resolve({ status: res.statusCode, data: body });
|
||
}
|
||
});
|
||
});
|
||
|
||
req.on('error', (error) => {
|
||
reject(error);
|
||
});
|
||
|
||
if (data) {
|
||
req.write(JSON.stringify(data));
|
||
}
|
||
req.end();
|
||
});
|
||
}
|
||
|
||
testAccountsAPI();
|