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+ </ head >
8+ < body >
9+ < h1 > 管理员API测试</ h1 >
10+
11+ < div >
12+ < h2 > 1. 登录测试</ h2 >
13+ < button onclick ="testLogin() "> 测试登录</ button >
14+ < div id ="login-result "> </ div >
15+ </ div >
16+
17+ < div >
18+ < h2 > 2. 仪表板API测试</ h2 >
19+ < button onclick ="testDashboard() "> 测试仪表板</ button >
20+ < div id ="dashboard-result "> </ div >
21+ </ div >
22+
23+ < div >
24+ < h2 > 3. 用户列表API测试</ h2 >
25+ < button onclick ="testUsers() "> 测试用户列表</ button >
26+ < div id ="users-result "> </ div >
27+ </ div >
28+
29+ < script >
30+ let authToken = null ;
31+
32+ async function testLogin ( ) {
33+ try {
34+ const response = await fetch ( '/admin/login' , {
35+ method : 'POST' ,
36+ headers : {
37+ 'Content-Type' : 'application/json'
38+ } ,
39+ body : JSON . stringify ( {
40+ username : 'testadmin' ,
41+ password : 'admin123'
42+ } )
43+ } ) ;
44+
45+ const result = await response . json ( ) ;
46+
47+ if ( result . code === 200 ) {
48+ authToken = result . data . token ;
49+ localStorage . setItem ( 'user_token' , authToken ) ;
50+ document . getElementById ( 'login-result' ) . innerHTML =
51+ '<span style="color: green;">登录成功!Token: ' + authToken . substring ( 0 , 50 ) + '...</span>' ;
52+ } else {
53+ document . getElementById ( 'login-result' ) . innerHTML =
54+ '<span style="color: red;">登录失败: ' + result . message + '</span>' ;
55+ }
56+ } catch ( error ) {
57+ document . getElementById ( 'login-result' ) . innerHTML =
58+ '<span style="color: red;">请求失败: ' + error . message + '</span>' ;
59+ }
60+ }
61+
62+ async function testDashboard ( ) {
63+ if ( ! authToken ) {
64+ authToken = localStorage . getItem ( 'user_token' ) ;
65+ }
66+
67+ if ( ! authToken ) {
68+ document . getElementById ( 'dashboard-result' ) . innerHTML =
69+ '<span style="color: red;">请先登录</span>' ;
70+ return ;
71+ }
72+
73+ try {
74+ const response = await fetch ( '/admin/dashboard' , {
75+ headers : {
76+ 'Authorization' : 'Bearer ' + authToken
77+ }
78+ } ) ;
79+
80+ const result = await response . json ( ) ;
81+
82+ document . getElementById ( 'dashboard-result' ) . innerHTML =
83+ '<pre>' + JSON . stringify ( result , null , 2 ) + '</pre>' ;
84+ } catch ( error ) {
85+ document . getElementById ( 'dashboard-result' ) . innerHTML =
86+ '<span style="color: red;">请求失败: ' + error . message + '</span>' ;
87+ }
88+ }
89+
90+ async function testUsers ( ) {
91+ if ( ! authToken ) {
92+ authToken = localStorage . getItem ( 'user_token' ) ;
93+ }
94+
95+ if ( ! authToken ) {
96+ document . getElementById ( 'users-result' ) . innerHTML =
97+ '<span style="color: red;">请先登录</span>' ;
98+ return ;
99+ }
100+
101+ try {
102+ const response = await fetch ( '/admin/users' , {
103+ headers : {
104+ 'Authorization' : 'Bearer ' + authToken
105+ }
106+ } ) ;
107+
108+ const result = await response . json ( ) ;
109+
110+ document . getElementById ( 'users-result' ) . innerHTML =
111+ '<pre>' + JSON . stringify ( result , null , 2 ) + '</pre>' ;
112+ } catch ( error ) {
113+ document . getElementById ( 'users-result' ) . innerHTML =
114+ '<span style="color: red;">请求失败: ' + error . message + '</span>' ;
115+ }
116+ }
117+ </ script >
118+ </ body >
119+ </ html >
0 commit comments