uni-app 获取图片验证码

 

 

<image :src="imgVerificationCode" @click="getVerificationCode" class="imageCode"></image>

arrayBufferToBase64(buffer) {
var binary = '';
var bytes = new Uint8Array(buffer);
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return window.btoa(binary);
},

getVerificationCode() {
                uni.request({
                    url: 'http://127.0.0.1:9090/code/image',
                    header: {
                        'content-type': 'application/x-www-form-urlencoded',
                        'deviceId':'xrl1334'
                    },
                    responseType:'arraybuffer',
                    method: 'GET',
                    data: {
                        "width": 60
                    },
                    success: (res) => {
                        console.log("imageCodeUrl",res)
                        var url= this.arrayBufferToBase64(res.data);
                        console.log('url',url)
                        this.imgVerificationCode ='data:image/jpeg;base64,'+url;
                    }}
                )
            },

 

posted @ 2022-09-21 22:42  三瑞  阅读(706)  评论(0编辑  收藏  举报