zhaoxing
2022-08-06 abcb0e14feb3591ed2648a08dac298cb856c0a86
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
<template>
    <div class="login-captcha" @click="refresh">
        <div v-if="svg" class="svg" v-html="svg"></div>
        <img v-else class="base64" :src="base64" alt="" />
    </div>
</template>
 
<script lang="ts">
import { defineComponent, ref } from "vue";
import { ElMessage } from "element-plus";
import { useCool } from "/@/cool";
 
export default defineComponent({
    emits: ["update:modelValue", "change"],
 
    setup(_, { emit }) {
        const { service } = useCool();
        const base64 = ref("");
        const svg = ref("");
 
        const refresh = () => {
            service.base.open
                .captcha({
                    height: 36,
                    width: 110
                })
                .then(({ captchaId, data }: any) => {
                    if (data.includes(";base64,")) {
                        base64.value = data;
                    } else {
                        svg.value = data;
                    }
 
                    emit("update:modelValue", captchaId);
                    emit("change", {
                        base64,
                        svg,
                        captchaId
                    });
                })
                .catch((err: string) => {
                    ElMessage.error(err);
                });
        };
 
        refresh();
 
        return {
            base64,
            svg,
            refresh
        };
    }
});
</script>
 
<style lang="scss" scoped>
.login-captcha {
    height: 36px;
    cursor: pointer;
 
    .svg {
        height: 100%;
    }
 
    .base64 {
        height: 100%;
    }
}
</style>