由于在ios和android中,location.href在spa页面的机制不同(不同在于ios是只要不刷新页面,href就不会改变,在vue项目中就会出现类型的问题),所以我们需要借助路由钩子函数,手动改变其页面的url地址,这样ios系统的手机才能正常调起微信扫一扫。
if (to.path=='/scan'&&to.path != location.pathname) {
location.assign(to.fullPath);
} else {
next()
}
用了上面的方式,ios 第一次进入还是不行,再次点击才可以,所以在mounted 时先调用一次
mounted(){
let u = navigator.userAgent;
let isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
if(isiOS){
this.first()
}
},
// ios 首次进入先触发一次
first(){
let href = encodeURIComponent(location.href.split('#')[0])
let that=this
let tiurl={
url:href
}
getJsapiTicket(tiurl).then(res=>{
if (res.code == 0) {
wx.config({
// debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId:appid, // 必填,公众号的唯一标识
timestamp: res.data.timestamp, // 必填,生成签名的时间戳
nonceStr: res.data.nonceStr, // 必填,生成签名的随机串
signature: res.data.signature,// 必填,签名
jsApiList: ['checkJsApi', 'scanQRCode'] // 必填,需要使用的JS接口列表
});
wx.ready(function(){
wx.checkJsApi({
jsApiList: ['scanQRCode'],
success: function (res) {
}
})
});
}
}
)
},