测试接口.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>API 测试工具</title>
  7. <style>
  8. body {
  9. font-family: Arial, sans-serif;
  10. max-width: 800px;
  11. margin: 50px auto;
  12. padding: 20px;
  13. }
  14. .section {
  15. margin-bottom: 30px;
  16. padding: 20px;
  17. border: 1px solid #ddd;
  18. border-radius: 5px;
  19. }
  20. h2 {
  21. color: #333;
  22. margin-top: 0;
  23. }
  24. input, textarea, button {
  25. width: 100%;
  26. padding: 10px;
  27. margin: 5px 0;
  28. box-sizing: border-box;
  29. }
  30. textarea {
  31. height: 150px;
  32. font-family: monospace;
  33. }
  34. button {
  35. background-color: #4CAF50;
  36. color: white;
  37. border: none;
  38. cursor: pointer;
  39. font-size: 16px;
  40. }
  41. button:hover {
  42. background-color: #45a049;
  43. }
  44. .result {
  45. margin-top: 10px;
  46. padding: 10px;
  47. background-color: #f5f5f5;
  48. border-radius: 3px;
  49. white-space: pre-wrap;
  50. word-wrap: break-word;
  51. }
  52. .success {
  53. background-color: #d4edda;
  54. color: #155724;
  55. }
  56. .error {
  57. background-color: #f8d7da;
  58. color: #721c24;
  59. }
  60. </style>
  61. </head>
  62. <body>
  63. <h1>API 测试工具</h1>
  64. <!-- 注册接口 -->
  65. <div class="section">
  66. <h2>1. 注册管理员账号</h2>
  67. <input type="text" id="regUsername" placeholder="用户名" value="admin">
  68. <input type="password" id="regPassword" placeholder="密码" value="123456">
  69. <input type="text" id="regNickname" placeholder="昵称" value="管理员">
  70. <button onclick="register()">注册</button>
  71. <div id="regResult" class="result"></div>
  72. </div>
  73. <!-- 登录接口 -->
  74. <div class="section">
  75. <h2>2. 管理员登录</h2>
  76. <input type="text" id="loginUsername" placeholder="用户名" value="fbs">
  77. <input type="password" id="loginPassword" placeholder="密码" value="123">
  78. <button onclick="login()">登录</button>
  79. <div id="loginResult" class="result"></div>
  80. </div>
  81. <!-- 生成密码 -->
  82. <div class="section">
  83. <h2>3. 生成BCrypt加密密码</h2>
  84. <input type="text" id="passwordToEncode" placeholder="要加密的密码" value="123">
  85. <button onclick="encodePassword()">生成加密密码</button>
  86. <div id="encodeResult" class="result"></div>
  87. </div>
  88. <script>
  89. const API_BASE = 'http://localhost:8080/api/admin';
  90. async function register() {
  91. const username = document.getElementById('regUsername').value;
  92. const password = document.getElementById('regPassword').value;
  93. const nickname = document.getElementById('regNickname').value;
  94. const resultDiv = document.getElementById('regResult');
  95. resultDiv.textContent = '正在注册...';
  96. resultDiv.className = 'result';
  97. try {
  98. const response = await fetch(`${API_BASE}/register`, {
  99. method: 'POST',
  100. headers: {
  101. 'Content-Type': 'application/json'
  102. },
  103. body: JSON.stringify({
  104. username: username,
  105. password: password,
  106. nickname: nickname
  107. })
  108. });
  109. const data = await response.json();
  110. resultDiv.textContent = JSON.stringify(data, null, 2);
  111. resultDiv.className = `result ${data.code === 200 ? 'success' : 'error'}`;
  112. } catch (error) {
  113. resultDiv.textContent = '错误: ' + error.message;
  114. resultDiv.className = 'result error';
  115. }
  116. }
  117. async function login() {
  118. const username = document.getElementById('loginUsername').value;
  119. const password = document.getElementById('loginPassword').value;
  120. const resultDiv = document.getElementById('loginResult');
  121. resultDiv.textContent = '正在登录...';
  122. resultDiv.className = 'result';
  123. try {
  124. const response = await fetch(`${API_BASE}/login`, {
  125. method: 'POST',
  126. headers: {
  127. 'Content-Type': 'application/json'
  128. },
  129. body: JSON.stringify({
  130. username: username,
  131. password: password
  132. })
  133. });
  134. const data = await response.json();
  135. resultDiv.textContent = JSON.stringify(data, null, 2);
  136. resultDiv.className = `result ${data.code === 200 ? 'success' : 'error'}`;
  137. if (data.code === 200 && data.data && data.data.token) {
  138. localStorage.setItem('token', data.data.token);
  139. console.log('Token已保存:', data.data.token);
  140. }
  141. } catch (error) {
  142. resultDiv.textContent = '错误: ' + error.message;
  143. resultDiv.className = 'result error';
  144. }
  145. }
  146. async function encodePassword() {
  147. const password = document.getElementById('passwordToEncode').value;
  148. const resultDiv = document.getElementById('encodeResult');
  149. resultDiv.textContent = '正在生成...';
  150. resultDiv.className = 'result';
  151. try {
  152. const response = await fetch(`${API_BASE}/password/encode?password=${encodeURIComponent(password)}`);
  153. const data = await response.json();
  154. if (data.code === 200) {
  155. resultDiv.innerHTML = `
  156. <strong>原始密码:</strong> ${data.data.original}<br>
  157. <strong>加密后:</strong> ${data.data.encoded}<br><br>
  158. <strong>SQL更新语句:</strong><br>
  159. <code>${data.data.sql}</code>
  160. `;
  161. resultDiv.className = 'result success';
  162. } else {
  163. resultDiv.textContent = JSON.stringify(data, null, 2);
  164. resultDiv.className = 'result error';
  165. }
  166. } catch (error) {
  167. resultDiv.textContent = '错误: ' + error.message + '\n\n请确保后端服务已启动!';
  168. resultDiv.className = 'result error';
  169. }
  170. }
  171. </script>
  172. </body>
  173. </html>