captcha.vue
1.76 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
<template>
<div v-if="captchaEnabled" style="width: 100%">
<el-input
v-model.trim="verCode"
size="large"
clearable
placeholder="图形验证码"
>
<template #prefix>
<svg-icon
icon-class="validCode"
class="el-input__icon input-icon"
/>
</template>
<template #append>
<div style="height: 98%" @click="getCode">
<el-image :src="codeUrl" class="login-code-img" />
</div>
</template>
</el-input>
</div>
</template>
<script setup>
import { computed, onMounted, ref } from 'vue'
import { getCodeImg } from '@/api/login'
const emit = defineEmits(['update:modelValue', 'update:uuid'])
// 验证码开关
const captchaEnabled = ref(true)
const codeUrl = ref('')
const props = defineProps({
modelValue: {
required: true,
type: String,
default: ''
},
uuid: {
required: true,
type: String,
default: ''
},
autoLoad: {
type: Boolean,
default: true
}
})
const verCode = computed({
get: () => props.modelValue,
set: (val) => {
emit('update:modelValue', val)
}
})
onMounted(() => {
if (props.autoLoad) { getCode() }
})
function getCode() {
if (captchaEnabled.value) {
getCodeImg().then((res) => {
captchaEnabled.value = res.data.captchaEnabled === undefined ? true : res.data.captchaEnabled
if (captchaEnabled.value) {
codeUrl.value = 'data:image/gif;base64,' + res.data.img
emit('update:uuid', res.data.uuid)
}
})
}
}
defineExpose({
getCode
})
</script>
<style lang="scss" scoped>
:deep(.el-input-group__append) {
padding: 0;
}
.input-icon {
height: 39px;
width: 14px;
margin-left: 0px;
}
.login-code-img {
height: 100%;
width: 100px;
cursor: pointer;
}
</style>