⼩程序⼆维码⽣成乱码问题处理
应⽤场景:在⼩程序内展⽰⼩程序⼆维码,此⼆维码可以进⼊⽬标⼩程序的特定页⾯或者携带参数
⼀、获取access_token
代码转换如在⼩程序A内,或B内本⾝要展⽰B的⼆维码,需要使⽤⼩程序B的appid与密钥,通过⼩城程官⽅接⼝换取B⼩程序的access_token, 换取⼆维码的时候会⽤到;
wxml:
<button bindtap='getqrcode'>获取⼆维码</button>
<image class='qrcode' src='data:image/png;base64,{{imgUrls}}'></image>
此处image需要使⽤base64格式图⽚,接⼝返回来的数据原本为图⽚,但接收数据显⽰为乱码,因为默认接收格式responseType为text,解析图⽚会出现错误,需要转换为base64码
js:
getqrcode:function(){
let that = this;
let appid = '想要展⽰⼆维码⼩程序的appid';
let sec = '想要展⽰⼆维码⼩程序的密钥';
//换取access_token 接⼝
url: 'api.weixin.qq/cgi-bin/token?grant_type=client_credential&appid=' + appid + '&secret=' + sec,
header:{
'content-type':'application/json'
},
success:function(res){
console.log(res);
quest({
//获得⼆维码数据接⼝
url: 'api.weixin.qq/wxa/getwxacodeunlimit?access_token=' + res.data.access_token,
data: {
'scene': 111,//必填 scene 携带参数,可以在onload页⾯⾥⾯query⾥⾯解析出来,详情百度
'page': 'pages/login/login',//必填具体的⼩程序页⾯,但不能携带参数,参数在scene中携带
'line_color': { 'r': 30, 'g': 144, 'b': 255 },//⼩程序⼆维码线条颜⾊
'is_hyaline':true//图⽚底⾊是否为透明
},
header: {
'content-type':'application/json'
},
method: 'POST',
// dataType: 'json',
responseType: 'arraybuffer',//将原本按⽂本解析修改为arraybuffer
success: function(res) {
that.setData({
imgUrls: wx.arrayBufferToBase64(res.data)
})
}
})
}
})
},
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论