11<template >
22 <a-form
3- ref =" formRef" :model =" form" :rules =" rules" :label-col-style =" { display: 'none' }"
4- :wrapper-col-style =" { flex: 1 }" size =" large" @submit =" handleLogin"
3+ ref =" formRef"
4+ :model =" form"
5+ :rules =" rules"
6+ :label-col-style =" { display: 'none' }"
7+ :wrapper-col-style =" { flex: 1 }"
8+ size =" large"
9+ @submit =" handleLogin"
510 >
611 <a-form-item field =" username" hide-label >
7- <a-input v-model =" form.username" placeholder =" 请输入用户名" allow-clear />
12+ <a-input v-model =" form.username" placeholder =" 请输入用户名" allow-clear />
813 </a-form-item >
914 <a-form-item field =" password" hide-label >
10- <a-input-password v-model =" form.password" placeholder =" 请输入密码" />
15+ <a-input-password v-model =" form.password" placeholder =" 请输入密码" />
1116 </a-form-item >
12- <a-form-item field =" captcha" hide-label v-if = " unCaptcha " >
13- <a-input v-model =" form.captcha" placeholder =" 请输入验证码" :max-length =" 4" allow-clear style =" flex : 1 1 " />
17+ <a-form-item v-if = " isCaptchaEnabled " field =" captcha" hide-label >
18+ <a-input v-model =" form.captcha" placeholder =" 请输入验证码" :max-length =" 4" allow-clear style =" flex : 1 1 " />
1419 <div class =" captcha-container" @click =" getCaptcha" >
15- <img :src =" captchaImgBase64" alt =" 验证码" class =" captcha" />
20+ <img :src =" captchaImgBase64" alt =" 验证码" class =" captcha" />
1621 <div v-if =" form.expired" class =" overlay" >
1722 <p >已过期,请刷新</p >
1823 </div >
3338</template >
3439
3540<script setup lang="ts">
36- import {type FormInstance , Message } from ' @arco-design/web-vue'
37- import {useStorage } from ' @vueuse/core'
38- import {getCaptchaConfig , getImageCaptcha } from ' @/apis/common'
39- import {useTabsStore , useUserStore } from ' @/stores'
40- import {encryptByRsa } from ' @/utils/encrypt'
41+ import { type FormInstance , Message } from ' @arco-design/web-vue'
42+ import { useStorage } from ' @vueuse/core'
43+ import { getImageCaptcha } from ' @/apis/common'
44+ import { useTabsStore , useUserStore } from ' @/stores'
45+ import { encryptByRsa } from ' @/utils/encrypt'
4146
4247const loginConfig = useStorage (' login-config' , {
4348 rememberMe: true ,
@@ -46,24 +51,23 @@ const loginConfig = useStorage('login-config', {
4651 // username: debug ? 'admin' : '', // 演示默认值
4752 // password: debug ? 'admin123' : '', // 演示默认值
4853})
49- // 是否开启验证码
50- const unCaptcha = ref (true )
54+ // 是否启用验证码
55+ const isCaptchaEnabled = ref (true )
5156// 验证码图片
5257const captchaImgBase64 = ref ()
5358
5459const formRef = ref <FormInstance >()
5560const form = reactive ({
5661 username: loginConfig .value .username ,
5762 password: loginConfig .value .password ,
58- unCaptcha: unCaptcha .value ,
5963 captcha: ' ' ,
6064 uuid: ' ' ,
6165 expired: false ,
6266})
6367const rules: FormInstance [' rules' ] = {
64- username: [{required: true , message: ' 请输入用户名' }],
65- password: [{required: true , message: ' 请输入密码' }],
66- captcha: [{required: unCaptcha .value , message: ' 请输入验证码' }],
68+ username: [{ required: true , message: ' 请输入用户名' }],
69+ password: [{ required: true , message: ' 请输入密码' }],
70+ captcha: [{ required: isCaptchaEnabled .value , message: ' 请输入验证码' }],
6771}
6872
6973// 验证码过期定时器
@@ -91,25 +95,15 @@ onBeforeUnmount(() => {
9195// 获取验证码
9296const getCaptcha = () => {
9397 getImageCaptcha ().then ((res ) => {
94- const {uuid, img, expireTime} = res .data
95- form . uuid = uuid
98+ const { uuid, img, expireTime, isEnabled } = res .data
99+ isCaptchaEnabled . value = isEnabled
96100 captchaImgBase64 .value = img
101+ form .uuid = uuid
97102 form .expired = false
98103 startTimer (expireTime )
99104 })
100105}
101106
102- const initCaptchaConfig = () => {
103- getCaptchaConfig ().then ((res ) => {
104- const result = res .data
105- if (result .NEED_CAPTCHA == 0 ) {
106- unCaptcha .value = false
107- } else {
108- getCaptcha ()
109- }
110- })
111- }
112-
113107const userStore = useUserStore ()
114108const tabsStore = useTabsStore ()
115109const router = useRouter ()
@@ -123,19 +117,18 @@ const handleLogin = async () => {
123117 await userStore .accountLogin ({
124118 username: form .username ,
125119 password: encryptByRsa (form .password ) || ' ' ,
126- unCaptcha: form .unCaptcha ,
127120 captcha: form .captcha ,
128121 uuid: form .uuid ,
129122 })
130123 tabsStore .reset ()
131- const {redirect, ... othersQuery} = router .currentRoute .value .query
124+ const { redirect, ... othersQuery } = router .currentRoute .value .query
132125 await router .push ({
133126 path: (redirect as string ) || ' /' ,
134127 query: {
135128 ... othersQuery ,
136129 },
137130 })
138- const {rememberMe} = loginConfig .value
131+ const { rememberMe } = loginConfig .value
139132 loginConfig .value .username = rememberMe ? form .username : ' '
140133 Message .success (' 欢迎使用' )
141134 } catch (error ) {
@@ -147,7 +140,7 @@ const handleLogin = async () => {
147140}
148141
149142onMounted (() => {
150- initCaptchaConfig ()
143+ getCaptcha ()
151144})
152145 </script >
153146
0 commit comments