cshenfu
2023-05-25 b86a6e42f663b2fc39c7a0c21ddeaeea93b253e4
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
<template>
    <view class="captcha-box">
        <view class="captcha-img-box">
            <uni-icons class="loding" size="20px" color="#BBB" v-if="loging" type="spinner-cycle"></uni-icons>
            <image class="captcha-img" :class="{opacity:loging}" @click="getImageCaptcha" :src="captchaBase64"
                mode="widthFix"></image>
        </view>
        <input @blur="focusCaptchaInput = false" :focus="focusCaptchaInput" type="text" class="captcha"
            :inputBorder="false" maxlength="4" v-model="val" placeholder="请输入验证码">
    </view>
</template>
 
<script>
    export default {
        props: {
            modelValue:String,
            value:String,
            scene: {
                type: String,
                default () {
                    return ""
                }
            },
            focus: {
                type: Boolean,
                default () {
                    return false
                }
            }
        },
        computed:{
            val:{
                get(){
                    return this.value||this.modelValue
                },
                set(value){
                    // console.log(value);
                    // TODO 兼容 vue2
                    // #ifdef VUE2
                    this.$emit('input', value);
                    // #endif
                    
                    // TODO 兼容 vue3
                    // #ifdef VUE3
                    this.$emit('update:modelValue', value)
                    // #endif
                }
            }
        },
        data() {
            return {
                focusCaptchaInput: false,
                captchaBase64: "",
                loging: false
            };
        },
        watch: {
            scene: {
                handler(scene) {
                    if (scene) {
                        this.getImageCaptcha(this.focus)
                    } else {
                        uni.showToast({
                            title: 'scene不能为空',
                            icon: 'none'
                        });
                    }
                },
                immediate:true
            }
        },
        methods: {
            getImageCaptcha(focus = true) {
                this.loging = true
                if (focus) {
                    this.val = ''
                    this.focusCaptchaInput = true
                }
                const uniIdCo = uniCloud.importObject("uni-captcha-co", {
                    customUI: true
                })
                uniIdCo.getImageCaptcha({
                        scene: this.scene
                    }).then(result => {
                        // console.log(result);
                        this.captchaBase64 = result.captchaBase64
                    })
                    .catch(e => {
                        uni.showToast({
                            title: e.message,
                            icon: 'none'
                        });
                    }).finally(e => {
                        this.loging = false
                    })
            }
        }
    }
</script>
 
<style lang="scss" scoped>
    .captcha-box {
        /* #ifndef APP-NVUE */
        display: flex;
        /* #endif */
        flex-direction: row;
        align-items: center;
        justify-content: flex-end;
        flex: 1;
    }
 
    .captcha-img-box,
    .captcha {
        height: 44px;
        line-height: 44px;
    }
 
    .captcha-img-box {
        position: relative;
        background-color: #FEFAE7;
    }
 
    .captcha {
        background-color: #F8F8F8;
        font-size: 14px;
        flex: 1;
        padding: 0 20rpx;
        margin-left: 20rpx;
        /* #ifndef APP-NVUE */
        box-sizing: border-box;
        /* #endif */
    }
 
    .captcha-img-box,
    .captcha-img,
    .loding {
        height: 44px !important;
        width: 100px;
    }
    
    .captcha-img{
        cursor: pointer;
    }
 
    .loding {
        z-index: 9;
        color: #bbb;
        position: absolute;
        text-align: center;
        line-height: 45px;
        animation: rotate 1s linear infinite;
    }
 
    .opacity {
        opacity: 0.5;
    }
 
    @keyframes rotate {
        from {
            transform: rotate(0deg)
        }
 
        to {
            transform: rotate(360deg)
        }
    }
</style>