1
1
<script setup lang="ts">
2
+ import type { FormError , FormSubmitEvent } from ' #ui/types'
2
3
import { appName } from ' ~/constants'
3
4
4
5
interface ModelType {
@@ -15,7 +16,18 @@ const loginForm = reactive<ModelType>({
15
16
password: ' ' ,
16
17
})
17
18
18
- const handleSubmitClick = async () => {
19
+ const validate = (state : any ): FormError [] => {
20
+ const errors = []
21
+ if (! state .username ) {
22
+ errors .push ({ path: ' username' , message: ' 用户名必填!' })
23
+ }
24
+ if (! state .password ) {
25
+ errors .push ({ path: ' password' , message: ' 密码必填!' })
26
+ }
27
+ return errors
28
+ }
29
+
30
+ async function handleSubmitClick(event : FormSubmitEvent <any >) {
19
31
loading .value = true
20
32
try {
21
33
const { token, tokenName } = await $fetch (' /api/login' , {
@@ -27,84 +39,68 @@ const handleSubmitClick = async () => {
27
39
router .push (' /admin' )
28
40
if (token ) {
29
41
toast .add ({ title: ' 登录成功!' , timeout: 2000 })
30
- } else {
42
+ }
43
+ else {
31
44
toast .add ({ title: ' 登录失败!' , timeout: 2000 , color: ' red' })
32
45
}
33
- } catch (e ) {
46
+ }
47
+ catch (e ) {
34
48
toast .add ({ title: ' 登录失败!' , timeout: 2000 , color: ' red' })
35
- } finally {
36
- loading .value = false
37
49
}
38
- }
39
-
40
- const keyDown = (e ) => {
41
- if (e .keyCode === 13 || e .keyCode === 100 ) {
42
- handleSubmitClick ()
50
+ finally {
51
+ loading .value = false
43
52
}
44
53
}
45
54
46
- onMounted (() => {
47
- window .addEventListener (' keydown' , keyDown )
48
- })
49
-
50
- onUnmounted (() => {
51
- window .removeEventListener (' keydown' , keyDown , false )
52
- })
53
-
54
55
definePageMeta ({
55
56
layout: ' none' ,
56
57
})
57
58
</script >
58
59
59
60
<template >
60
- <div bg-white dark:bg-gray-900 w-full >
61
- <div flex justify-center h-screen >
62
- <div hidden bg-cover lg:block class =" lg:w-2/3" style =" background-image : url (' /fufu.jpg' )" >
63
- <div flex items-center h-full px-20 bg-gray-900 bg-opacity-40 >
64
- <div >
65
- <h2 text-4xl text-white >旅行足迹</h2 >
66
-
67
- <p text-sm text-gray-300 mt-4 >
68
- 鹤鸣工作室出品,一款基于 Nuxt3 构建的⌈相片集⌋。
69
- </p >
61
+ <div flex items-center justify-center w-full h-full style =" background-image : url (' /fufu.jpg' ); background-size : cover ;"
62
+ md:grid md:grid-cols-10 md:gap-4
63
+ >
64
+ <div md:col-span-5 ></div >
65
+ <div
66
+ h-108 w-full mx-2 md:max-w-xl bg-white dark:bg-black rounded-md shadow-md md:col-span-4
67
+ >
68
+ <div h-full flex flex-col items-center >
69
+ <div flex items-center justify-center justify-between w-full px-4 mt-4 >
70
+ <div text-left >
71
+ <div i-carbon-arrow-left cursor-pointer @click =" router.push('/')" />
70
72
</div >
73
+ <ClientOnly >
74
+ <div text-right >
75
+ <DarkToggle />
76
+ </div >
77
+ </ClientOnly >
71
78
</div >
72
- </div >
73
-
74
- <div flex items-center w-full max-w-md px-6 mx-auto class =" lg:w-2/6" >
75
- <div flex-1 >
76
- <div text-center >
77
- <h2 text-4xl text-center text-gray-700 dark:text-white >{{ appName || '旅行足迹' }}</h2 >
78
79
79
- <p mt-3 text-gray-500 dark:text-gray-300 >登录你的帐号</p >
80
- </div >
80
+ <h2 text-center text-3xl mt-4 >
81
+ {{ appName || '旅行足迹' }}
82
+ </h2 >
81
83
82
- <div mt-8 >
83
- <div >
84
- <label for =" account" block mb-2 text-sm text-gray-600 dark:text-gray-200 >帐号</label >
85
- <input type =" account" v-model =" loginForm.username" name =" account" id =" account" block w-full px-4 py-2 mt-2 text-gray-700 placeholder-gray-400 bg-white border border-gray-200 rounded-md dark:placeholder-gray-600 dark:bg-gray-900 dark:text-gray-300 dark:border-gray-700 focus:border-blue-400 dark:focus:border-blue-400 focus:ring-blue-400 focus:outline-none focus:ring focus:ring-opacity-40 />
86
- </div >
84
+ <UForm :validate =" validate" :state =" loginForm" class =" space-y-6 w-2/3 mt-6" @submit =" handleSubmitClick" >
85
+ <UFormGroup label =" 用户名" name =" username" >
86
+ <UInput v-model =" loginForm.username" />
87
+ </UFormGroup >
87
88
88
- <div mt-6 >
89
- <div flex justify-between mb-2 >
90
- <label for =" password" font-ark text-sm text-gray-600 dark:text-gray-200 >密码</label >
91
- </div >
89
+ <UFormGroup label =" 密码" name =" password" >
90
+ <UInput v-model =" loginForm.password" type =" password" />
91
+ </UFormGroup >
92
92
93
- <input type =" password" v-model =" loginForm.password" name =" password" id =" password" block w-full px-4 py-2 mt-2 text-gray-700 placeholder-gray-400 bg-white border border-gray-200 rounded-md dark:placeholder-gray-600 dark:bg-gray-900 dark:text-gray-300 dark:border-gray-700 focus:border-blue-400 dark:focus:border-blue-400 focus:ring-blue-400 focus:outline-none focus:ring focus:ring-opacity-40 />
94
- </div >
93
+ <UButton type =" submit" color =" white" :loading =" loading" >
94
+ 登录
95
+ </UButton >
96
+ </UForm >
95
97
96
- <div mt-6 >
97
- <el-button
98
- :loading =" loading"
99
- @click =" handleSubmitClick"
100
- w-full px-4 py-2 tracking-wide text-white transition-colors duration-200 transform bg-blue-500 rounded-md hover:bg-blue-400 focus:outline-none focus:bg-blue-400 focus:ring focus:ring-blue-300 focus:ring-opacity-50
101
- type =" primary"
102
- >登录</el-button >
103
- </div >
104
- </div >
105
- </div >
98
+ <p text-sm text-gray-400 mt-4 >
99
+ 鹤鸣工作室出品,一款基于 Nuxt3 构建的⌈相片集⌋。
100
+ </p >
106
101
</div >
107
102
</div >
103
+ <div md:col-span-1 ></div >
108
104
</div >
109
105
</template >
110
106
0 commit comments