test_frontend_api.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. console.log('=== 模型列表接口测试 ===');
  2. // 获取 CSRF token(如果需要)
  3. function getCsrfToken() {
  4. const meta = document.querySelector('meta[name="csrf-token"]');
  5. return meta ? meta.content : '';
  6. }
  7. // 测试获取模型列表
  8. async function testGetModels() {
  9. try {
  10. console.log('发送请求获取模型列表...');
  11. const response = await fetch('/watershed/model/list?page=1&size=10', {
  12. method: 'GET',
  13. credentials: 'include',
  14. headers: {
  15. 'Content-Type': 'application/json',
  16. 'Accept': 'application/json',
  17. 'Authorization': 'Bearer ' + localStorage.getItem('token') || ''
  18. }
  19. });
  20. console.log('响应状态:', response.status, response.statusText);
  21. if (response.ok) {
  22. const data = await response.json();
  23. console.log('响应数据:', JSON.stringify(data, null, 2));
  24. if (data.code === 200 && data.rows) {
  25. console.log('成功获取到模型列表');
  26. console.log('模型数量:', data.rows.length);
  27. if (data.rows.length > 0) {
  28. const firstModel = data.rows[0];
  29. console.log('第一个模型的所有字段:', Object.keys(firstModel));
  30. if (firstModel.created_at) {
  31. console.log('✅ created_at 字段存在:', firstModel.created_at);
  32. } else if (firstModel.createTime) {
  33. console.log('✅ createTime 字段存在:', firstModel.createTime);
  34. } else {
  35. console.log('❌ 未找到创建时间字段');
  36. }
  37. }
  38. } else {
  39. console.log('❌ 请求失败:', data.msg);
  40. }
  41. } else {
  42. console.log('❌ 请求失败:', response.status);
  43. }
  44. } catch (error) {
  45. console.error('❌ 请求异常:', error);
  46. }
  47. }
  48. // 获取当前用户信息
  49. async function testUserInfo() {
  50. try {
  51. console.log('\n=== 获取当前用户信息 ===');
  52. const response = await fetch('/getInfo', {
  53. method: 'GET',
  54. credentials: 'include',
  55. headers: {
  56. 'Content-Type': 'application/json',
  57. 'Accept': 'application/json'
  58. }
  59. });
  60. const data = await response.json();
  61. console.log('用户信息:', JSON.stringify(data, null, 2));
  62. } catch (error) {
  63. console.error('获取用户信息失败:', error);
  64. }
  65. }
  66. // 执行测试
  67. console.log('=== 开始测试 ===');
  68. // 检查是否已登录
  69. if (!localStorage.getItem('token')) {
  70. console.log('⚠️ 未检测到登录 token,请先登录');
  71. // 尝试获取用户信息(可能会重定向到登录页)
  72. testUserInfo();
  73. } else {
  74. console.log('✅ 已检测到登录 token');
  75. testGetModels();
  76. testUserInfo();
  77. }