| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- const axios = require('axios');
- const BASE_URL = 'http://localhost:3000';
- async function testAPI() {
- console.log('=== 开始测试 API 接口 ===\n');
- // 1. 测试健康检查
- try {
- console.log('1️⃣ 测试健康检查...');
- const health = await axios.get(`${BASE_URL}/health`);
- console.log('✅ 健康检查:', health.data);
- } catch (error) {
- console.error('❌ 健康检查失败:', error.message);
- }
- // 2. 测试数据库连接
- try {
- console.log('\n2️⃣ 测试数据库连接...');
- const db = await axios.get(`${BASE_URL}/api/tts/test-db`);
- console.log('✅ 数据库连接:', db.data);
- } catch (error) {
- console.error('❌ 数据库连接失败:', error.message);
- }
- // 3. 测试发送验证码
- try {
- console.log('\n3️⃣ 测试发送验证码...');
- const sendCode = await axios.post(`${BASE_URL}/api/auth/send-code`, {
- phone: '13800138000'
- });
- console.log('✅ 发送验证码:', sendCode.data);
- } catch (error) {
- console.error('❌ 发送验证码失败:', error.message);
- }
- // 4. 测试登录(开发环境免密)
- try {
- console.log('\n4️⃣ 测试登录...');
- const login = await axios.post(`${BASE_URL}/api/auth/login`, {
- phone: '13800138000',
- code: '123456'
- });
- console.log('✅ 登录成功:', login.data);
-
- const token = login.data.data.token;
- const userId = login.data.data.user.id;
- console.log('📝 Token:', token.substring(0, 50) + '...');
- console.log('📝 用户ID:', userId);
- // 5. 测试获取用户信息
- try {
- console.log('\n5️⃣ 测试获取用户信息...');
- const userInfo = await axios.get(`${BASE_URL}/api/auth/user-info`, {
- headers: { Authorization: `Bearer ${token}` }
- });
- console.log('✅ 用户信息:', userInfo.data);
- } catch (error) {
- console.error('❌ 获取用户信息失败:', error.message);
- }
- // 6. 测试获取音色列表
- try {
- console.log('\n6️⃣ 测试获取音色列表...');
- const voices = await axios.get(`${BASE_URL}/api/tts/voices`);
- console.log('✅ 音色列表:', voices.data);
- } catch (error) {
- console.error('❌ 获取音色列表失败:', error.message);
- }
- // 7. 测试获取音频列表
- try {
- console.log('\n7️⃣ 测试获取音频列表...');
- const audios = await axios.get(`${BASE_URL}/api/audio/list`, {
- headers: { Authorization: `Bearer ${token}` }
- });
- console.log('✅ 音频列表:', audios.data);
- } catch (error) {
- console.error('❌ 获取音频列表失败:', error.message);
- }
- // 8. 测试会员状态
- try {
- console.log('\n8️⃣ 测试会员状态...');
- const member = await axios.get(`${BASE_URL}/api/member/status`, {
- headers: { Authorization: `Bearer ${token}` }
- });
- console.log('✅ 会员状态:', member.data);
- } catch (error) {
- console.error('❌ 获取会员状态失败:', error.message);
- }
- } catch (error) {
- console.error('❌ 登录失败:', error.message);
- }
- console.log('\n=== API 测试完成 ===');
- }
- testAPI();
|