验证码封装原创
<template>
<el-form-item class="login-container" prop="verifyCode" label="验证码">
<el-input
v-model="captchaData.verifyCode"
auto-complete="off"
placeholder="请输入右侧的计算结果"
clearable
class="input"
size="large"
@change="verifyCaptcha"
@input="setVerifyCode"
>
<template #prefix>
<svg-icon icon-class="verify_code"/>
</template>
</el-input>
<div class="captcha">
<img :src="captchaBase64" @click="getCaptcha"/>
</div>
</el-form-item>
</template>
<script lang="ts" setup>
import SvgIcon from "@/components/SvgIcon/index.vue";
import { getCaptchaApi } from "@/api/auth";
import { CaptchaCheck, CaptchaControllerService } from "@/generated";
const props = defineProps({
verifyCode: {
type: String,
required: true
},
rules: {
type: Object,
required: true
}
});
const emit = defineEmits(['update:verifyCode']);
const setVerifyCode = (value: string) => {
emit('update:verifyCode', value); // 发送自定义事件更新父组件的 verifyCode 值
};
watch(() => props.verifyCode, (newValue) => {
captchaData.value.verifyCode= newValue; // 当从父组件传来的 verifyCode 值变化时,更新本地的 verifyCode 值
});
/**
* 验证码图片Base64字符串
*/
const captchaBase64 = ref();
const captchaData = ref<CaptchaCheck>({
verifyCode: props.verifyCode,
verifyCodeKey: "",
});
let selfRules = ref({})
const veriflCode = [{
validator: verifyCaptcha,
trigger: 'blur'
}]
/**
* 获取验证码
*/
function getCaptcha() {
getCaptchaApi().then(({ data }) => {
const { verifyCodeBase64, verifyCodeKey } = data;
captchaData.value.verifyCodeKey = verifyCodeKey;
captchaBase64.value = verifyCodeBase64;
});
}
/**
* 校验
*/
async function verifyCaptcha (rule: any, value: any, callback: any) {
if (value){
return new Promise((resolve, reject) => {
CaptchaControllerService.checkCaptcha(captchaData.value)
.then((res) => {
const code = res.data
if (code !== value){
callback(new Error('验证码错误!'))
} else if(!code){
callback(new Error('验证码超时!'))
}else {
callback()
}
})
.catch((error: any) => {
reject(error);
getCaptcha();
});
})
} else {
callback(new Error('验证码不能为空!'))
}
}
onBeforeMount(() => {
selfRules.value = props.rules
selfRules.value.verifyCode = veriflCode
})
onMounted(() => {
getCaptcha();
});
</script>
<style lang="scss" scoped>
:deep(.el-form-item__content){
display: flex;
flex-grow: 1;
flex-wrap: nowrap;
align-items: center;
justify-content: left;
}
:deep(.el-form-item__label){
height: 48px;
line-height: 48px;
}
.login-container {
width: 500px;
//padding: 20px 35px 0;
display: flex;
white-space: nowrap;
.input {
background: transparent;
height: 48px;
flex-grow: 1;
// 子组件 scoped 无效,使用 :deep
//:deep(.el-input__wrapper) {
// padding: 0;
// background: transparent;
// box-shadow: none;
//
// .el-input__inner {
// color: #fff;
// background: transparent;
// border: 0;
// border-radius: 0;
// caret-color: #fff;
//
// &:-webkit-autofill {
// box-shadow: 0 0 0 1000px transparent inset !important;
// -webkit-text-fill-color: #fff !important;
// }
//
// // 设置输入框自动填充的延迟属性
// &:-webkit-autofill,
// &:-webkit-autofill:hover,
// &:-webkit-autofill:focus,
// &:-webkit-autofill:active {
// transition: color 99999s ease-out, background-color 99999s ease-out;
// transition-delay: 99999s;
// }
// }
//}
}
.captcha {
margin-left: 1px;
img {
width: 120px;
height: 48px;
cursor: pointer;
display: flex;
align-items: center;
}
}
}
</style>
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
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
上次更新: 2024/01/14 16:27:31
- 01
- element-plus多文件手动上传 原创11-03
- 02
- TrueLicense 创建及安装证书 原创10-25
- 03
- 手动修改迅捷配置 原创09-03
- 04
- 安装 acme.sh 原创08-29
- 05
- zabbix部署 原创08-20