Files
emall-api/test_api.js
2025-10-18 16:18:20 +08:00

53 lines
1.8 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const axios = require('axios');
const API_BASE_URL = 'http://localhost:5003/api/v1';
async function testAPI() {
try {
// 1. 注册用户
console.log('1. 注册用户...');
const registerResponse = await axios.post(`${API_BASE_URL}/auth/register`, {
username: 'testuser',
email: 'test@example.com',
password: 'password123'
});
const token = registerResponse.data.data.token;
console.log('注册成功获取到token:', token);
// 2. 创建邮件
console.log('\n2. 创建邮件...');
const mailData = {
title: '测试邮件',
content: '这是一封测试邮件内容',
recipientType: 'SELF',
sendTime: new Date(Date.now() + 24 * 60 * 60 * 1000).toISOString(), // 明天这个时候
triggerType: 'TIME',
triggerCondition: {},
attachments: [],
isEncrypted: false,
capsuleStyle: 'default'
};
const createMailResponse = await axios.post(`${API_BASE_URL}/mails/create`, mailData, {
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
}
});
console.log('创建邮件成功:', createMailResponse.data);
// 3. 获取邮件列表
console.log('\n3. 获取邮件列表...');
const mailsResponse = await axios.get(`${API_BASE_URL}/mails?type=SENT&page=1&size=20`, {
headers: {
'Authorization': `Bearer ${token}`
}
});
console.log('获取邮件列表成功:', mailsResponse.data);
} catch (error) {
console.error('测试失败:', error.response?.data || error.message);
}
}
testAPI();