最近做的H5页面页面按钮触发分享操作(针对iOS操作失败)踩坑记录:
基本有三种形式:
1.在route里面处理,每次跳转location重新赋值,页面刷新
弊端:当使用vuex的时候,刷新照成vuex丢失需要处理
router.beforeEach((to,from, next) =>{
const u = navigator.userAgent
const isIOS = !!u.match(/\i[^;]+;(U;)? CPU.+Mac OS X/)//iOS终端
// 修复iOS版微信H5页面History兼容性问题
if (isIOS && to.path !== location.pathname) {
location.assign(to.fullPath)
} else {
next()
}
})
2.由于iOS微信里面跳转,采用push跳转的时候,实际路由没变,所以需要记录入口页面的location来获取signature去wx.config
坑点1:如果页面跳转一直使用的push,务必使用最外层的location
坑点2:如果页面中间跳转过程中使用了location跳转,记录距离我们页面最近的location页面的地址
//记录页面使用
sessionStorage.setItem("usePath", window.location.href+'')
//分享页面使用
const getPath = sessionStorage.getItem("usePath")
const url = getPath.split('#')[0]
// ps:使用url获取signature时,切记encodeURIComponent(url)
3.这个是2的优化版,保证最后一次进入分享页面时,使用location跳转,那样我们就可以跟安卓一样处理,直接使用当前页面window.location.href.split(’#’)[0]去获取signature即可