login-form.vue
6.95 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
<template>
<div class="login-form-wrapper">
<slot name="title">
<div class="login-form-title">欢迎使用{{ title }}管理</div>
<div class="login-form-sub-title">{{ title }}管理系统</div>
</slot>
<slot name="error-msg">
<div class="login-form-error-msg">{{ errorMessage }}</div>
</slot>
<a-form ref="loginForm" :model="userInfo" class="login-form" layout="vertical" @submit="handleSubmit">
<a-form-item field="username" :rules="[{ required: true, message: $t('login.form.userName.errMsg') }]"
:validate-trigger="['change', 'blur']" hide-label>
<a-input v-model="userInfo.username" :placeholder="$t('login.form.userName.placeholder')">
<template #prefix>
<icon-user/>
</template>
</a-input>
</a-form-item>
<a-form-item field="password" :rules="[{ required: true, message: $t('login.form.password.errMsg') }]"
:validate-trigger="['change', 'blur']" hide-label>
<a-input-password v-model="userInfo.password" :placeholder="$t('login.form.password.placeholder')" allow-clear>
<template #prefix>
<icon-lock/>
</template>
</a-input-password>
</a-form-item>
<a-row :gutter="5">
<a-col :span="12">
<a-form-item field="code" :rules="[
{ required: true, message: $t('login.form.verificationCode.errMsg') },
]" :validate-trigger="['change', 'blur']" hide-label>
<a-input v-model="userInfo.code" :placeholder="$t('login.form.password.verificationCode.placeholder')"
:max-length="4" style="width: 200px" class="code">
<template #prefix>
<icon-safe/>
</template>
</a-input>
</a-form-item>
</a-col>
<a-col :span="8">
<div class="login-code" @click="captcha">
<img :src=codeData alt="" class="login-img"/>
</div>
</a-col>
<a-col :span="4">
<span class="login-img-text" @click="captcha">换一张</span>
</a-col>
</a-row>
<a-space :size="16" direction="vertical">
<div class="login-form-password-actions">
<a-checkbox checked="rememberPassword" :model-value="loginConfig.rememberPassword"
@change="setRememberPassword as any">
{{ $t('login.form.rememberPassword') }}
</a-checkbox>
</div>
<a-button type="primary" html-type="submit" long :loading="loading">
{{ $t('login.form.login') }}
</a-button>
<!-- <a-button type="text" long class="login-form-register-btn">
{{ $t('login.form.register') }}
<a-link>{{ $t('login.form.forgetPassword') }}</a-link>
</a-button> -->
</a-space>
</a-form>
</div>
</template>
<script lang="ts" setup>
import type {LoginData} from '@/api/user'
import {getCaptcha, getMenuList} from '@/api/user'
import useLoading from '@/hooks/loading'
import {useUserStore} from '@/store'
import {Message} from '@arco-design/web-vue'
import {ValidatedError} from '@arco-design/web-vue/es/form/interface'
import {useStorage} from '@vueuse/core'
import {onMounted, reactive, ref} from 'vue'
import {useI18n} from 'vue-i18n'
import {useRouter} from 'vue-router'
import CryptoJS from "crypto-js";
const router = useRouter()
const env = import.meta.env.MODE;
if (env == "demo") {
const currentRoute = router.currentRoute.value
router.replace({
name: "loginDemo",
query: {
...router.currentRoute.value.query,
},
})
}
const {t} = useI18n()
const errorMessage = ref('')
const {loading, setLoading} = useLoading()
const userStore = useUserStore()
const title = import.meta.env.VITE_APP_TITLE;
const loginConfig = useStorage('login-config', {
rememberPassword: true,
username: '', // 演示默认值
password: '', // demo default value
})
const decryptText = (password: string) => {
const decrypted = CryptoJS.AES.decrypt(password, code);
return decrypted.toString(CryptoJS.enc.Utf8);
}
const encryptText = (password: string) => {
return CryptoJS.AES.encrypt(password, code).toString();
}
const code = 'ylt-cloud';
const userInfo = reactive({
username: loginConfig.value.username,
password: loginConfig.value.password ? decryptText(loginConfig.value.password) : '',
code: "",
uuid: ""
})
const codeData: any = ref();
const captcha = () => {
getCaptcha({randomStr: Date.now()}).then((res: any) => {
codeData.value = 'data:image/png;base64,' + res.img;
userInfo.uuid = res.uuid;
})
};
const handleSubmit = async ({
errors,
values,
}: {
errors: Record<string, ValidatedError> | undefined
values: Record<string, any>
}) => {
if (loading.value) return
if (!errors) {
setLoading(true)
try {
values.browserFlag = Math.random().toString(32).substring(4).toUpperCase();
await userStore.login(values as LoginData)
//读取权限菜单
const {data} = await getMenuList();
if (!data || data.length <= 0) {
captcha();
errorMessage.value = "登录失败";
Message.error("暂无菜单权限,请联系管理员");
return;
}
localStorage.setItem('initPassword', "1");
Message.success(t('login.form.login.success'))
const {rememberPassword} = loginConfig.value
const {username, password} = values
// 实际生产环境需要进行加密存储。
// The actual production environment requires encrypted storage.
loginConfig.value.username = rememberPassword ? username : ''
loginConfig.value.password = rememberPassword ? encryptText(password) : ''
const {redirect, ...othersQuery} = router.currentRoute.value.query
router.push({
path: redirect || "/",
query: {
...othersQuery,
}
})
} catch (err) {
captcha();
errorMessage.value = (err as Error).message;
userInfo.code = '';
} finally {
setLoading(false);
}
}
}
const setRememberPassword = (value: boolean) => {
loginConfig.value.rememberPassword = value
}
onMounted(() => {
captcha();
})
</script>
<style lang="less" scoped>
.login-form {
&-wrapper {
width: 320px;
}
&-title {
color: var(--color-text-1);
font-weight: 500;
font-size: 24px;
line-height: 32px;
}
&-sub-title {
color: var(--color-text-3);
font-size: 16px;
line-height: 24px;
}
&-error-msg {
height: 32px;
color: rgb(var(--red-6));
line-height: 32px;
}
&-password-actions {
display: flex;
justify-content: space-between;
}
&-register-btn {
color: var(--color-text-3) !important;
}
.login-code {
width: 100%;
height: 32px;
img {
width: 100%;
height: 100%;
}
}
.login-img-text {
line-height: 32px;
cursor: pointer;
color: var(--color-text-1);
}
.login-img-text:hover {
text-decoration-line: underline;
}
}
</style>