该功能时作为H5调起小程序的微信分享
1:小程序方 在webview页面,打开分享的权限
wx.showShareMenu({ withShareTicket:true, menu:['shareTimeline','shareAppMessage'] })
2:在小程序内写 onShareAppMessage方法,用来获取在哪个页面调起的微信分享,
onShareAppMessage: function(res) { console.log(res) let nowURL = decodeURIComponent(res.webViewUrl) let id = nowURL.split('?')[1].split('&')[0].split('=')[1] let name = decodeURIComponent(nowURL.split('?')[1].split('&')[1].split('=')[1]) let data = { id: id, name: name,} let payData = JSON.stringify(data) let url = `/pages/music/music?data=${payData}` return { title: name, path: url, } },
3:在2中的这处代码,是用来劫持分享,并自定义分享的,
let payData = JSON.stringify(data) let url = `/pages/music/music?data=${payData}` return { title: name, path: url, }
4:转发出去后,通过用户的点击再次回来时,在webview页面获取是否有参
onLoad: function(e) { console.log(e) if (e.data) { console.log(e)ghu let i = JSON.parse(e.data) console.log(i) this.url = 'http://192.****8080/?hrsaasUserId=95623&id='+i.id+'&name='+ i.name console.log(this.url) } else { this.url = 'http://192.****:8080/?hrsaasUserId=95623' }
如果有参,就是通过分享页面进来的,跳入webview页面
如果无参,就认为是从首页点进来的 不做处理
5:在H5的onload处 获取参数,逻辑同4, 有参就跳入你想进的页面
if(e.data){uni.navigateTo({url:'/pages/consulting/scaleDetail?id='+this.scaleId+'&name='+encodeURIComponent(this.scaleName)+'&questionType=0&isPay=0',})
来源地址:https://blog.csdn.net/weixin_48091030/article/details/129693140