test_frontend.html 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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: 1200px;
  11. margin: 0 auto;
  12. padding: 20px;
  13. background: #f5f5f5;
  14. }
  15. .container {
  16. background: white;
  17. padding: 20px;
  18. border-radius: 8px;
  19. box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  20. margin-bottom: 20px;
  21. }
  22. h1 {
  23. color: #333;
  24. border-bottom: 2px solid #409eff;
  25. padding-bottom: 10px;
  26. }
  27. h2 {
  28. color: #666;
  29. margin-top: 20px;
  30. }
  31. button {
  32. background: #409eff;
  33. color: white;
  34. border: none;
  35. padding: 10px 20px;
  36. border-radius: 4px;
  37. cursor: pointer;
  38. margin: 5px;
  39. }
  40. button:hover {
  41. background: #66b1ff;
  42. }
  43. .success {
  44. background: #d4edda;
  45. border-left: 4px solid #28a745;
  46. padding: 15px;
  47. margin: 10px 0;
  48. }
  49. .error {
  50. background: #f8d7da;
  51. border-left: 4px solid #dc3545;
  52. padding: 15px;
  53. margin: 10px 0;
  54. }
  55. #output {
  56. background: #f9f9f9;
  57. padding: 15px;
  58. border-radius: 4px;
  59. min-height: 100px;
  60. white-space: pre-wrap;
  61. font-family: monospace;
  62. max-height: 600px;
  63. overflow-y: auto;
  64. }
  65. table {
  66. width: 100%;
  67. border-collapse: collapse;
  68. margin-top: 10px;
  69. }
  70. th, td {
  71. border: 1px solid #ddd;
  72. padding: 8px;
  73. text-align: left;
  74. }
  75. th {
  76. background-color: #409eff;
  77. color: white;
  78. }
  79. tr:nth-child(even) {
  80. background-color: #f2f2f2;
  81. }
  82. </style>
  83. </head>
  84. <body>
  85. <div class="container">
  86. <h1>🔍 前端API测试工具</h1>
  87. <h2>步骤 1: 登录</h2>
  88. <div>
  89. <input type="text" id="username" placeholder="用户名" value="admin" style="padding: 8px; margin-right: 10px;">
  90. <input type="password" id="password" placeholder="密码" value="admin123" style="padding: 8px; margin-right: 10px;">
  91. <button onclick="testLogin()">登录</button>
  92. </div>
  93. <h2>步骤 2: 测试API</h2>
  94. <div>
  95. <button onclick="testGetUsers()">获取用户列表</button>
  96. <button onclick="testGetMenus()">获取菜单</button>
  97. <button onclick="testGetRoles()">获取角色列表</button>
  98. </div>
  99. <h2>输出:</h2>
  100. <div id="output">等待操作...</div>
  101. <div id="userTable"></div>
  102. </div>
  103. <script>
  104. let accessToken = localStorage.getItem('access_token');
  105. function log(message, type = 'info') {
  106. const output = document.getElementById('output');
  107. const timestamp = new Date().toLocaleTimeString();
  108. const prefix = type === 'error' ? '❌' : type === 'success' ? '✅' : 'ℹ️';
  109. output.textContent += `[${timestamp}] ${prefix} ${message}\n`;
  110. output.scrollTop = output.scrollHeight;
  111. }
  112. function clearOutput() {
  113. document.getElementById('output').textContent = '';
  114. document.getElementById('userTable').innerHTML = '';
  115. }
  116. async function testLogin() {
  117. clearOutput();
  118. log('开始登录测试...');
  119. const username = document.getElementById('username').value;
  120. const password = document.getElementById('password').value;
  121. try {
  122. const response = await fetch('http://localhost:8000/api/v1/auth/login', {
  123. method: 'POST',
  124. headers: {
  125. 'Content-Type': 'application/json'
  126. },
  127. body: JSON.stringify({ username, password })
  128. });
  129. log(`状态码: ${response.status}`);
  130. const data = await response.json();
  131. log(`响应: ${JSON.stringify(data, null, 2)}`);
  132. if (data.code === 0) {
  133. accessToken = data.data.access_token;
  134. localStorage.setItem('access_token', accessToken);
  135. log('登录成功!Token已保存', 'success');
  136. log(`Token: ${accessToken.substring(0, 50)}...`);
  137. } else {
  138. log(`登录失败: ${data.message}`, 'error');
  139. }
  140. } catch (error) {
  141. log(`请求失败: ${error.message}`, 'error');
  142. }
  143. }
  144. async function testGetUsers() {
  145. if (!accessToken) {
  146. log('请先登录!', 'error');
  147. return;
  148. }
  149. clearOutput();
  150. log('开始获取用户列表...');
  151. try {
  152. const response = await fetch('http://localhost:8000/api/v1/system/admin/users?page=1&page_size=20', {
  153. method: 'GET',
  154. headers: {
  155. 'Authorization': `Bearer ${accessToken}`,
  156. 'Content-Type': 'application/json'
  157. }
  158. });
  159. log(`状态码: ${response.status}`);
  160. const data = await response.json();
  161. if (data.code === 0) {
  162. log(`获取成功!总数: ${data.data.total}`, 'success');
  163. log(`用户数据: ${JSON.stringify(data.data, null, 2)}`);
  164. // 显示用户表格
  165. displayUserTable(data.data.items);
  166. } else {
  167. log(`获取失败: ${data.message}`, 'error');
  168. }
  169. } catch (error) {
  170. log(`请求失败: ${error.message}`, 'error');
  171. }
  172. }
  173. async function testGetMenus() {
  174. if (!accessToken) {
  175. log('请先登录!', 'error');
  176. return;
  177. }
  178. clearOutput();
  179. log('开始获取菜单...');
  180. try {
  181. const response = await fetch('http://localhost:8000/api/v1/system/user/menus', {
  182. method: 'GET',
  183. headers: {
  184. 'Authorization': `Bearer ${accessToken}`,
  185. 'Content-Type': 'application/json'
  186. }
  187. });
  188. log(`状态码: ${response.status}`);
  189. const data = await response.json();
  190. if (data.code === 0) {
  191. log(`获取成功!菜单数: ${data.data.length}`, 'success');
  192. log(`菜单数据: ${JSON.stringify(data.data, null, 2)}`);
  193. } else {
  194. log(`获取失败: ${data.message}`, 'error');
  195. }
  196. } catch (error) {
  197. log(`请求失败: ${error.message}`, 'error');
  198. }
  199. }
  200. async function testGetRoles() {
  201. if (!accessToken) {
  202. log('请先登录!', 'error');
  203. return;
  204. }
  205. clearOutput();
  206. log('开始获取角色列表...');
  207. try {
  208. const response = await fetch('http://localhost:8000/api/v1/system/roles/all', {
  209. method: 'GET',
  210. headers: {
  211. 'Authorization': `Bearer ${accessToken}`,
  212. 'Content-Type': 'application/json'
  213. }
  214. });
  215. log(`状态码: ${response.status}`);
  216. const data = await response.json();
  217. if (data.code === 0) {
  218. log(`获取成功!角色数: ${data.data.length}`, 'success');
  219. log(`角色数据: ${JSON.stringify(data.data, null, 2)}`);
  220. } else {
  221. log(`获取失败: ${data.message}`, 'error');
  222. }
  223. } catch (error) {
  224. log(`请求失败: ${error.message}`, 'error');
  225. }
  226. }
  227. function displayUserTable(users) {
  228. const tableDiv = document.getElementById('userTable');
  229. let html = '<h2>用户列表</h2><table>';
  230. html += '<tr><th>用户名</th><th>邮箱</th><th>手机</th><th>角色</th><th>状态</th><th>管理员</th></tr>';
  231. users.forEach(user => {
  232. html += '<tr>';
  233. html += `<td>${user.username}</td>`;
  234. html += `<td>${user.email}</td>`;
  235. html += `<td>${user.phone || '-'}</td>`;
  236. html += `<td>${user.roles || '未分配'}</td>`;
  237. html += `<td>${user.is_active ? '激活' : '禁用'}</td>`;
  238. html += `<td>${user.is_superuser ? '是' : '否'}</td>`;
  239. html += '</tr>';
  240. });
  241. html += '</table>';
  242. tableDiv.innerHTML = html;
  243. }
  244. // 页面加载时检查token
  245. window.onload = function() {
  246. if (accessToken) {
  247. log('检测到已保存的Token', 'success');
  248. log(`Token: ${accessToken.substring(0, 50)}...`);
  249. } else {
  250. log('未检测到Token,请先登录');
  251. }
  252. };
  253. </script>
  254. </body>
  255. </html>