wangzhibo
2026-07-16 b57020623cf0c946706573bf102e548c3a544423
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
<template>
    <div class="pic-captcha" @click="refresh">
        <div v-if="svg" class="svg" v-html="svg" />
        <img v-else-if="base64" class="base64" :src="base64" alt="" />
 
        <template v-else>
            <el-icon class="is-loading" :size="18">
                <loading />
            </el-icon>
        </template>
    </div>
</template>
 
<script lang="ts" setup>
defineOptions({
    name: 'pic-captcha'
});
 
import { onMounted, ref } from 'vue';
import { ElMessageBox } from 'element-plus';
import { Loading } from '@element-plus/icons-vue';
import { useCool } from '/@/cool';
import { useI18n } from 'vue-i18n';
 
const emit = defineEmits(['update:modelValue', 'change']);
 
const { service } = useCool();
const { t } = useI18n();
 
// base64
const base64 = ref('');
 
// svg
const svg = ref('');
 
// 刷新
async function refresh() {
    svg.value = '';
    base64.value = '';
 
    await service.base.open
        .captcha({
            height: 45,
            width: 150,
            color: '#2c3142'
        })
        .then(({ captchaId, data }) => {
            if (data) {
                if (data.includes(';base64,')) {
                    base64.value = data;
                } else {
                    svg.value = data;
                }
 
                emit('update:modelValue', captchaId);
                emit('change', {
                    base64,
                    svg,
                    captchaId
                });
            } else {
                ElMessageBox.alert(t('验证码获取失败'), {
                    title: t('提示'),
                    type: 'error'
                });
            }
        })
        .catch(err => {
            ElMessageBox.alert(err.message, {
                title: t('提示'),
                type: 'error'
            });
        });
}
 
onMounted(() => {
    refresh();
});
 
defineExpose({
    refresh
});
</script>
 
<style lang="scss" scoped>
.pic-captcha {
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    height: 45px;
    width: 150px;
    position: relative;
    user-select: none;
 
    .svg {
        height: 100%;
        position: relative;
    }
 
    .base64 {
        height: 100%;
    }
 
    .is-loading {
        position: absolute;
        right: 15px;
    }
}
</style>