1
1
import Loading from "../assets/loading.svg"
2
- import { useState } from "preact/hooks"
2
+ import { useState , useRef } from "preact/hooks"
3
3
4
4
export default function Component ( ) {
5
5
const [ loading , setLoading ] = useState ( false )
6
+ const usernameRef = useRef ( null )
7
+ const passwordRef = useRef ( null )
8
+ const [ errorMsg , setErrorMsg ] = useState ( '' )
9
+ const [ loggedIn , setLoggedIn ] = useState ( false )
10
+
11
+ async function submit ( e ) {
12
+ e . preventDefault ( )
13
+ setLoading ( true )
14
+ try {
15
+ const response = await fetch ( '/login' , {
16
+ method : 'POST' ,
17
+ headers : {
18
+ 'Content-Type' : 'application/x-www-form-urlencoded'
19
+ } ,
20
+ body : `username=${ encodeURIComponent ( usernameRef . current . value ) } &password=${ encodeURIComponent ( passwordRef . current . value ) } `
21
+ } )
22
+ const result = await response . json ( )
23
+ if ( result . code ) {
24
+ setErrorMsg ( result . message )
25
+ } else {
26
+ setLoggedIn ( true )
27
+ }
28
+ } catch ( error ) {
29
+ console . error ( error )
30
+ setErrorMsg ( '连接失败: ' + error . message )
31
+ } finally {
32
+ setLoading ( false )
33
+ }
34
+ }
35
+
36
+ if ( loggedIn ) {
37
+ return (
38
+ < div className = "min-h-[75vh] flex flex-col items-center justify-center px-4 py-12" >
39
+ < div className = "w-full max-w-md space-y-8" >
40
+ < div className = "text-center" >
41
+ < svg
42
+ className = "mx-auto h-24 w-24 text-green-500"
43
+ fill = "none"
44
+ stroke = "currentColor"
45
+ viewBox = "0 0 24 24"
46
+ xmlns = "http://www.w3.org/2000/svg"
47
+ >
48
+ < path
49
+ strokeLinecap = "round"
50
+ strokeLinejoin = "round"
51
+ strokeWidth = "2"
52
+ d = "M5 13l4 4L19 7"
53
+ > </ path >
54
+ </ svg >
55
+ < h1 className = "text-2xl font-bold tracking-tight text-gray-900 dark:text-gray-50" >
56
+ 登录成功
57
+ </ h1 >
58
+ < p className = "mt-2 text-xs text-gray-600 dark:text-gray-400" >
59
+ 现在您可以断开 BYR-pet Wi-Fi 连接
60
+ </ p >
61
+ </ div >
62
+ </ div >
63
+ </ div >
64
+ )
65
+ }
66
+
6
67
return (
7
68
< >
8
69
< div className = "min-h-[75vh] flex flex-col items-center justify-center px-4 py-12" >
@@ -15,8 +76,11 @@ export default function Component() {
15
76
输入学号和校园网密码以连接 BUPT-portal
16
77
</ p >
17
78
</ div >
18
- < form className = "space-y-6" action = "/login" method = "post ">
79
+ < div className = "space-y-6" >
19
80
< div >
81
+ { errorMsg && ( < div className = "text-red-500 dark:text-red-400 text-center text-sm mb-4" >
82
+ { errorMsg }
83
+ </ div > ) }
20
84
< label
21
85
htmlFor = "username"
22
86
className = "block text-sm font-medium text-gray-700 dark:text-gray-300"
@@ -31,6 +95,7 @@ export default function Component() {
31
95
className = "block w-full appearance-none rounded-md border border-gray-300 px-3 py-2 placeholder-gray-400 shadow-sm focus:border-indigo-500 focus:outline-none focus:ring-indigo-500 dark:border-gray-700 dark:bg-gray-800 dark:text-gray-50 dark:placeholder-gray-500"
32
96
type = "text"
33
97
name = "username"
98
+ ref = { usernameRef }
34
99
/>
35
100
</ div >
36
101
</ div >
@@ -49,24 +114,24 @@ export default function Component() {
49
114
className = "block w-full appearance-none rounded-md border border-gray-300 px-3 py-2 placeholder-gray-400 shadow-sm focus:border-indigo-500 focus:outline-none focus:ring-indigo-500 dark:border-gray-700 dark:bg-gray-800 dark:text-gray-50 dark:placeholder-gray-500"
50
115
type = "password"
51
116
name = "password"
117
+ ref = { passwordRef }
52
118
/>
53
119
</ div >
54
120
</ div >
55
121
< div >
56
122
< button
57
123
type = "submit"
58
124
disabled = { loading }
125
+ onClick = { submit }
59
126
className = "flex w-full justify-center rounded-md border border-transparent bg-indigo-600 py-2 px-4 text-sm font-medium text-white shadow-sm hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 dark:bg-indigo-500 dark:hover:bg-indigo-600 dark:focus:ring-indigo-600 disabled:opacity-50 disabled:cursor-not-allowed"
60
127
>
61
- { loading && < img src = { Loading } className = "w-5 h-5 mr-2 animate-spin" alt = "loading" /> }
62
- { loading ? "登录中..." : " 连接 BUPT-portal" }
128
+ { loading && < img src = { Loading } className = "w-5 h-5 mr-2 animate-spin" alt = "loading" /> }
129
+ { loading ? "登录中..." : errorMsg ? "重试" : " 连接 BUPT-portal"}
63
130
</ button >
64
131
</ div >
65
- </ form >
132
+ </ div >
66
133
</ div >
67
134
</ div >
68
135
</ >
69
-
70
-
71
136
)
72
137
}
0 commit comments