index.vue
7.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<template>
<div class="login-container">
<div class="login-form-container">
<!-- Logo区域 -->
<div class="login-header">
<img src="/logo.png" alt="Logo" class="login-logo" />
<h1 class="login-title">Apple经销商ERP系统</h1>
<p class="login-subtitle">欢迎登录</p>
</div>
<!-- 登录表单 -->
<el-form
ref="loginFormRef"
:model="loginForm"
:rules="loginRules"
class="login-form"
@keyup.enter="handleLogin"
>
<el-form-item prop="username">
<el-input
v-model="loginForm.username"
placeholder="请输入用户名"
size="large"
prefix-icon="User"
clearable
/>
</el-form-item>
<el-form-item prop="password">
<el-input
v-model="loginForm.password"
type="password"
placeholder="请输入密码"
size="large"
prefix-icon="Lock"
show-password
clearable
/>
</el-form-item>
<el-form-item prop="captcha" v-if="showCaptcha">
<div class="captcha-container">
<el-input
v-model="loginForm.captcha"
placeholder="请输入验证码"
size="large"
prefix-icon="Picture"
clearable
/>
<img
:src="captchaImage"
alt="验证码"
class="captcha-image"
@click="refreshCaptcha"
/>
</div>
</el-form-item>
<el-form-item>
<div class="login-options">
<el-checkbox v-model="loginForm.rememberMe">
记住我
</el-checkbox>
<el-link type="primary" class="forgot-password">
忘记密码?
</el-link>
</div>
</el-form-item>
<el-form-item>
<el-button
type="primary"
size="large"
class="login-btn"
:loading="loading"
@click="handleLogin"
>
{{ loading ? '登录中...' : '登录' }}
</el-button>
</el-form-item>
</el-form>
</div>
<!-- 背景装饰 -->
<div class="login-bg">
<div class="bg-circle bg-circle-1"></div>
<div class="bg-circle bg-circle-2"></div>
<div class="bg-circle bg-circle-3"></div>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import { useUserStore } from '@/stores/modules/user'
import type { LoginForm } from '@/types'
const router = useRouter()
const route = useRoute()
const userStore = useUserStore()
// 表单引用
const loginFormRef = ref<FormInstance>()
// 加载状态
const loading = ref(false)
// 验证码相关
const showCaptcha = ref(false)
const captchaImage = ref('')
// 登录表单数据
const loginForm = reactive<LoginForm>({
username: '',
password: '',
captcha: '',
rememberMe: false
})
// 表单验证规则
const loginRules: FormRules = {
username: [
{ required: true, message: '请输入用户名', trigger: 'blur' },
{ min: 2, max: 20, message: '用户名长度在 2 到 20 个字符', trigger: 'blur' }
],
password: [
{ required: true, message: '请输入密码', trigger: 'blur' },
{ min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
],
captcha: [
{ required: true, message: '请输入验证码', trigger: 'blur' }
]
}
// 处理登录
const handleLogin = async () => {
if (!loginFormRef.value) return
try {
const valid = await loginFormRef.value.validate()
if (!valid) return
loading.value = true
await userStore.login(loginForm)
// 登录成功,跳转到目标页面或首页
const redirect = route.query.redirect as string
router.push(redirect || '/')
} catch (error) {
console.error('登录失败:', error)
// 显示验证码
showCaptcha.value = true
await refreshCaptcha()
} finally {
loading.value = false
}
}
// 刷新验证码
const refreshCaptcha = async () => {
try {
// 这里应该调用获取验证码的API
// const response = await getCaptchaApi()
// captchaImage.value = response.captchaImage
captchaImage.value = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg=='
} catch (error) {
console.error('获取验证码失败:', error)
}
}
// 组件挂载时初始化
onMounted(() => {
// 如果已经登录,直接跳转到首页
if (userStore.isLoggedIn) {
router.push('/')
}
})
</script>
<style lang="scss" scoped>
.login-container {
position: relative;
width: 100%;
height: 100vh;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
display: flex;
align-items: center;
justify-content: center;
overflow: hidden;
}
.login-form-container {
position: relative;
z-index: 2;
width: 400px;
padding: 40px;
background: rgba(255, 255, 255, 0.95);
border-radius: 12px;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
backdrop-filter: blur(10px);
}
.login-header {
text-align: center;
margin-bottom: 32px;
.login-logo {
width: 64px;
height: 64px;
margin-bottom: 16px;
}
.login-title {
margin: 0 0 8px 0;
font-size: 24px;
font-weight: 600;
color: var(--el-text-color-primary);
}
.login-subtitle {
margin: 0;
font-size: 14px;
color: var(--el-text-color-secondary);
}
}
.login-form {
.captcha-container {
display: flex;
align-items: center;
gap: 12px;
.captcha-image {
width: 100px;
height: 40px;
border-radius: 4px;
cursor: pointer;
border: 1px solid var(--el-border-color);
}
}
.login-options {
display: flex;
align-items: center;
justify-content: space-between;
width: 100%;
.forgot-password {
font-size: 14px;
}
}
.login-btn {
width: 100%;
height: 44px;
font-size: 16px;
font-weight: 500;
}
}
.login-bg {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1;
.bg-circle {
position: absolute;
border-radius: 50%;
background: rgba(255, 255, 255, 0.1);
animation: float 6s ease-in-out infinite;
&.bg-circle-1 {
width: 200px;
height: 200px;
top: 10%;
left: 10%;
animation-delay: 0s;
}
&.bg-circle-2 {
width: 150px;
height: 150px;
top: 60%;
right: 10%;
animation-delay: 2s;
}
&.bg-circle-3 {
width: 100px;
height: 100px;
bottom: 20%;
left: 20%;
animation-delay: 4s;
}
}
}
@keyframes float {
0%, 100% {
transform: translateY(0px);
}
50% {
transform: translateY(-20px);
}
}
// 响应式设计
@media (max-width: 768px) {
.login-form-container {
width: 90%;
padding: 24px;
}
.login-header {
.login-title {
font-size: 20px;
}
}
}
</style>