test-api.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. const axios = require('axios');
  2. const BASE_URL = 'http://localhost:3000';
  3. async function testAPI() {
  4. console.log('=== 开始测试 API 接口 ===\n');
  5. // 1. 测试健康检查
  6. try {
  7. console.log('1️⃣ 测试健康检查...');
  8. const health = await axios.get(`${BASE_URL}/health`);
  9. console.log('✅ 健康检查:', health.data);
  10. } catch (error) {
  11. console.error('❌ 健康检查失败:', error.message);
  12. }
  13. // 2. 测试数据库连接
  14. try {
  15. console.log('\n2️⃣ 测试数据库连接...');
  16. const db = await axios.get(`${BASE_URL}/api/tts/test-db`);
  17. console.log('✅ 数据库连接:', db.data);
  18. } catch (error) {
  19. console.error('❌ 数据库连接失败:', error.message);
  20. }
  21. // 3. 测试发送验证码
  22. try {
  23. console.log('\n3️⃣ 测试发送验证码...');
  24. const sendCode = await axios.post(`${BASE_URL}/api/auth/send-code`, {
  25. phone: '13800138000'
  26. });
  27. console.log('✅ 发送验证码:', sendCode.data);
  28. } catch (error) {
  29. console.error('❌ 发送验证码失败:', error.message);
  30. }
  31. // 4. 测试登录(开发环境免密)
  32. try {
  33. console.log('\n4️⃣ 测试登录...');
  34. const login = await axios.post(`${BASE_URL}/api/auth/login`, {
  35. phone: '13800138000',
  36. code: '123456'
  37. });
  38. console.log('✅ 登录成功:', login.data);
  39. const token = login.data.data.token;
  40. const userId = login.data.data.user.id;
  41. console.log('📝 Token:', token.substring(0, 50) + '...');
  42. console.log('📝 用户ID:', userId);
  43. // 5. 测试获取用户信息
  44. try {
  45. console.log('\n5️⃣ 测试获取用户信息...');
  46. const userInfo = await axios.get(`${BASE_URL}/api/auth/user-info`, {
  47. headers: { Authorization: `Bearer ${token}` }
  48. });
  49. console.log('✅ 用户信息:', userInfo.data);
  50. } catch (error) {
  51. console.error('❌ 获取用户信息失败:', error.message);
  52. }
  53. // 6. 测试获取音色列表
  54. try {
  55. console.log('\n6️⃣ 测试获取音色列表...');
  56. const voices = await axios.get(`${BASE_URL}/api/tts/voices`);
  57. console.log('✅ 音色列表:', voices.data);
  58. } catch (error) {
  59. console.error('❌ 获取音色列表失败:', error.message);
  60. }
  61. // 7. 测试获取音频列表
  62. try {
  63. console.log('\n7️⃣ 测试获取音频列表...');
  64. const audios = await axios.get(`${BASE_URL}/api/audio/list`, {
  65. headers: { Authorization: `Bearer ${token}` }
  66. });
  67. console.log('✅ 音频列表:', audios.data);
  68. } catch (error) {
  69. console.error('❌ 获取音频列表失败:', error.message);
  70. }
  71. // 8. 测试会员状态
  72. try {
  73. console.log('\n8️⃣ 测试会员状态...');
  74. const member = await axios.get(`${BASE_URL}/api/member/status`, {
  75. headers: { Authorization: `Bearer ${token}` }
  76. });
  77. console.log('✅ 会员状态:', member.data);
  78. } catch (error) {
  79. console.error('❌ 获取会员状态失败:', error.message);
  80. }
  81. } catch (error) {
  82. console.error('❌ 登录失败:', error.message);
  83. }
  84. console.log('\n=== API 测试完成 ===');
  85. }
  86. testAPI();