Skip to content

Commit 51a2168

Browse files
committed
refactor: 优化登录验证码开关代码
1 parent 4cd892e commit 51a2168

File tree

8 files changed

+114
-123
lines changed

8 files changed

+114
-123
lines changed

src/apis/auth/type.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ export interface RouteItem {
4545
export interface AccountLoginReq {
4646
username: string
4747
password: string
48-
unCaptcha: boolean
4948
captcha: string
5049
uuid: string
5150
}

src/apis/common/captcha.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ export type * from './type'
55

66
const BASE_URL = '/captcha'
77

8-
/** @desc 获取图片验证码 */
9-
export function getCaptchaConfig() {
10-
return http.get<boolean>(`${BASE_URL}/config`)
11-
}
12-
138
/** @desc 获取图片验证码 */
149
export function getImageCaptcha() {
1510
return http.get<T.ImageCaptchaResp>(`${BASE_URL}/image`)

src/apis/common/type.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export interface ImageCaptchaResp {
33
uuid: string
44
img: string
55
expireTime: number
6+
isEnabled: boolean
67
}
78

89
/** 仪表盘公告类型 */

src/apis/system/type.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -293,17 +293,6 @@ export interface SiteConfig {
293293
SITE_BEIAN: OptionResp
294294
}
295295

296-
/** 邮箱配置类型 */
297-
export interface MailConfig {
298-
MAIL_PROTOCOL: OptionResp
299-
MAIL_HOST: OptionResp
300-
MAIL_PORT: OptionResp
301-
MAIL_USERNAME: OptionResp
302-
MAIL_PASSWORD: OptionResp
303-
MAIL_SSL_ENABLED: OptionResp
304-
MAIL_SSL_PORT: OptionResp
305-
}
306-
307296
/** 安全配置类型 */
308297
export interface SecurityConfig {
309298
PASSWORD_ERROR_LOCK_COUNT: OptionResp
@@ -316,9 +305,20 @@ export interface SecurityConfig {
316305
PASSWORD_REQUIRE_SYMBOLS: OptionResp
317306
}
318307

319-
/** 安全配置类型 */
320-
export interface CaptchaSetting {
321-
NEED_CAPTCHA: OptionResp
308+
/** 邮箱配置类型 */
309+
export interface MailConfig {
310+
MAIL_PROTOCOL: OptionResp
311+
MAIL_HOST: OptionResp
312+
MAIL_PORT: OptionResp
313+
MAIL_USERNAME: OptionResp
314+
MAIL_PASSWORD: OptionResp
315+
MAIL_SSL_ENABLED: OptionResp
316+
MAIL_SSL_PORT: OptionResp
317+
}
318+
319+
/** 登录配置类型 */
320+
export interface LoginConfig {
321+
LOGIN_CAPTCHA_ENABLED: OptionResp
322322
}
323323

324324
/** 绑定三方账号信息 */

src/views/login/components/account/index.vue

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
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>
@@ -33,11 +38,11 @@
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
4247
const 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
// 验证码图片
5257
const captchaImgBase64 = ref()
5358
5459
const formRef = ref<FormInstance>()
5560
const form = reactive({
5661
username: loginConfig.value.username,
5762
password: loginConfig.value.password,
58-
unCaptcha: unCaptcha.value,
5963
captcha: '',
6064
uuid: '',
6165
expired: false,
6266
})
6367
const 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
// 获取验证码
9296
const 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-
113107
const userStore = useUserStore()
114108
const tabsStore = useTabsStore()
115109
const 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
149142
onMounted(() => {
150-
initCaptchaConfig()
143+
getCaptcha()
151144
})
152145
</script>
153146

src/views/system/config/components/CaptchaSetting.vue renamed to src/views/system/config/components/LoginSetting.vue

Lines changed: 52 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,76 @@
11
<template>
22
<a-spin :loading="loading">
33
<a-form
4-
ref="formRef"
5-
:model="form"
6-
auto-label-width
7-
label-align="left"
8-
:layout="width >= 500 ? 'horizontal' : 'vertical'"
9-
:disabled="!isUpdate"
10-
scroll-to-first-error>
4+
ref="formRef"
5+
:model="form"
6+
:rules="rules"
7+
auto-label-width
8+
label-align="left"
9+
:layout="width >= 500 ? 'horizontal' : 'vertical'"
10+
:disabled="!isUpdate"
11+
scroll-to-first-error
12+
>
1113
<a-form-item
12-
field="NEED_CAPTCHA"
13-
:label="captchaSetting.NEED_CAPTCHA.name"
14+
field="LOGIN_CAPTCHA_ENABLED"
15+
:label="loginConfig.LOGIN_CAPTCHA_ENABLED.name"
1416
>
15-
<a-switch v-model="form.NEED_CAPTCHA" type="round" :checked-value="1" :unchecked-value="0">
17+
<a-switch
18+
v-model="form.LOGIN_CAPTCHA_ENABLED"
19+
type="round"
20+
:checked-value="1"
21+
:unchecked-value="0"
22+
>
1623
<template #checked>是</template>
1724
<template #unchecked>否</template>
1825
</a-switch>
1926
</a-form-item>
20-
2127
<a-space style="margin-bottom: 16px">
2228
<a-button v-if="!isUpdate" v-permission="['system:config:update']" type="primary" @click="onUpdate">
23-
<template #icon>
24-
<icon-edit/>
25-
</template>
26-
修改
29+
<template #icon><icon-edit /></template>修改
2730
</a-button>
2831
<a-button v-if="!isUpdate" v-permission="['system:config:reset']" @click="onResetValue">
29-
<template #icon>
30-
<icon-undo/>
31-
</template>
32-
恢复默认
32+
<template #icon><icon-undo /></template>恢复默认
3333
</a-button>
3434
<a-button v-if="isUpdate" type="primary" @click="handleSave">
35-
<template #icon>
36-
<icon-save/>
37-
</template>
38-
保存
35+
<template #icon><icon-save /></template>保存
3936
</a-button>
4037
<a-button v-if="isUpdate" @click="reset">
41-
<template #icon>
42-
<icon-refresh/>
43-
</template>
44-
重置
38+
<template #icon><icon-refresh /></template>重置
4539
</a-button>
4640
<a-button v-if="isUpdate" @click="handleCancel">
47-
<template #icon>
48-
<icon-undo/>
49-
</template>
50-
取消
41+
<template #icon><icon-undo /></template>取消
5142
</a-button>
5243
</a-space>
5344
</a-form>
5445
</a-spin>
5546
</template>
5647

5748
<script setup lang="ts">
58-
import {useWindowSize} from '@vueuse/core'
59-
import {type FormInstance, Message, Modal} from '@arco-design/web-vue'
60-
import {type CaptchaSetting, listOption, type OptionResp, resetOptionValue, updateOption} from '@/apis/system'
61-
import {useResetReactive} from '@/hooks'
49+
import { useWindowSize } from '@vueuse/core'
50+
import { type FormInstance, Message, Modal } from '@arco-design/web-vue'
51+
import { type LoginConfig, type OptionResp, listOption, resetOptionValue, updateOption } from '@/apis/system'
52+
import { useResetReactive } from '@/hooks'
6253
63-
defineOptions({name: 'CaptchaSetting'})
64-
const {width} = useWindowSize()
54+
defineOptions({ name: 'LoginSetting' })
55+
const { width } = useWindowSize()
6556
6657
const loading = ref<boolean>(false)
6758
const formRef = ref<FormInstance>()
6859
const [form] = useResetReactive({
69-
NEED_CAPTCHA: 1,
60+
LOGIN_CAPTCHA_ENABLED: 1,
7061
})
62+
const rules: FormInstance['rules'] = {
63+
LOGIN_CAPTCHA_ENABLED: [{ required: true, message: '请选择' }],
64+
}
7165
72-
const captchaSetting = ref<CaptchaSetting>({
73-
NEED_CAPTCHA: {},
66+
const loginConfig = ref<LoginConfig>({
67+
LOGIN_CAPTCHA_ENABLED: {},
7468
})
69+
7570
// 重置
7671
const reset = () => {
7772
formRef.value?.resetFields()
78-
form.NEED_CAPTCHA = captchaSetting.value.NEED_CAPTCHA.value
73+
form.LOGIN_CAPTCHA_ENABLED = loginConfig.value.LOGIN_CAPTCHA_ENABLED.value || 0
7974
}
8075
8176
const isUpdate = ref(false)
@@ -91,29 +86,31 @@ const handleCancel = () => {
9186
}
9287
9388
const queryForm = {
94-
category: 'CAPTCHA',
89+
category: 'LOGIN',
9590
}
9691
// 查询列表数据
9792
const getDataList = async () => {
98-
loading.value = true
99-
const {data} = await listOption(queryForm)
100-
captchaSetting.value = data.reduce((obj: CaptchaSetting, option: OptionResp) => {
101-
obj[option.code] = {...option, value: Number.parseInt(option.value)}
102-
return obj
103-
}, {})
104-
105-
handleCancel()
106-
loading.value = false
93+
try {
94+
loading.value = true
95+
const { data } = await listOption(queryForm)
96+
loginConfig.value = data.reduce((obj: LoginConfig, option: OptionResp) => {
97+
obj[option.code] = { ...option, value: Number.parseInt(option.value) }
98+
return obj
99+
}, {})
100+
handleCancel()
101+
} finally {
102+
loading.value = false
103+
}
107104
}
108105
109106
// 保存
110107
const handleSave = async () => {
111108
const isInvalid = await formRef.value?.validate()
112109
if (isInvalid) return false
113110
await updateOption(
114-
Object.entries(form).map(([key, value]) => {
115-
return {id: captchaSetting.value[key].id, code: key, value}
116-
}),
111+
Object.entries(form).map(([key, value]) => {
112+
return { id: loginConfig.value[key].id, code: key, value }
113+
}),
117114
)
118115
await getDataList()
119116
Message.success('保存成功')
@@ -128,7 +125,7 @@ const handleResetValue = async () => {
128125
const onResetValue = () => {
129126
Modal.warning({
130127
title: '警告',
131-
content: '确认恢复安全配置为默认值吗',
128+
content: '确认恢复登录配置为默认值吗',
132129
hideCancel: false,
133130
maskClosable: false,
134131
onOk: handleResetValue,

0 commit comments

Comments
 (0)