项目中有一个微信分享功能, 需要用到微信SDK,初始化代码如下
//初始化sdk配置
initJssdkShare: function(callback, url) {
postRequest(global_.API_URLS.getJsConfig, {curUrl: url}).then((res) => {
if (res.resultCode == "1") {
jweixin.config({
debug: false,
appId: res.object.appId,
timestamp: res.object.timestamp,
nonceStr: res.object.nonceStr,
signature: res.object.signature,
jsApiList: [
'checkJsApi',
'onMenuShareTimeline',
'onMenuShareAppMessage',
'getLocation',
'closeWindow',
'showMenuItems',
'showOptionMenu',
'hideMenuItems'
]
});
jweixin.ready(() => {
console.log('wx ready')
if (callback) {
console.log("配置完成,执行功能")
callback(res.data);
}
});
}
});
真机操作中发现, 安卓端可以正常初始化成功, IOS服务端一直出现签名认证失败,最后发现是IOS授权的URL导致的。
解决方案: 前端项目路由时 记录当前URL至缓存中, IOS只保留首次进入页面的URL,安卓端实时刷新,微信SDK初始化时, 认证URL从缓存中获取。
// 路由全局拦截器 在这里处理登录、授权等相关操作
router.beforeEach(function(to, from, next) {
console.log('前置守卫')
//1. 判断是否为刷新页面
if(to.page){
//2. 判断是否为ios 如果为ios且有缓存url 则不做处理
var isIOS = /(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent);
//var isIOS = true
console.log("当前系统: "+navigator.userAgent+", isIOS: "+isIOS)
if(isIOS){
var syncUrl = uni.getStorageSync("current_url")
console.log("当前缓存链接:"+syncUrl)
if(!syncUrl){
console.log(global_.H5_URL+"/wx"+to.page)
uni.setStorageSync("current_url",global_.H5_URL+"/wx"+to.page)
}
}else{
console.log(global_.H5_URL+"/wx"+to.page)
uni.setStorageSync("current_url",global_.H5_URL+"/wx"+to.page)
}
}else{
//页面刷新 清除当前缓存url
console.log("首次进入: " + location.href.split('#')[0])
uni.setStorageSync("current_url",location.href.split('#')[0])
}
next()
})