需求说明
兔展作品在微信环境下每次分享出来的链接都会跟上次有所不同, 原因是:
- 埋点数据: 渠道信息来源, 分享层级等参数更改
- 业务逻辑: 助力插件逻辑需要分享时带上当前用户的助力 id
问题说明
小程序预览兔展作品页面在分享时不能拿到作品处理后的分享链接
解决方案
在小程序加载兔展作品的时候, H5 会在不同的时机发送处理好的分享链接给小程序, 见微信文档
数据格式如下:
{ "type" : "EXT_APP_H5_SHARE_LINK" ,
"payload" : {
"shareLink" : "" ,
"triggerShareLink" : "" ,
"imgUrl" : "" ,
"desc" : "" ,
"title" : "" ,
}
}
|
对接方案
1. 从页面参数中拿到链接并展示
2. 分享前获取 H5 发来的处理好的分享链接(请做好接收不到信息的兼容逻辑)
3. 分享时把分享链接放到页面参数中
示例如下:
< web-view src = "{{link}}" bindmessage = "handleMessage" />
|
let nextLink = ''
let triggerShareLink = ''
Page({
data: {
link: "" ,
nextLink: ""
},
onLoad(options) {
wx.showShareMenu();
const currentLink = options.link && decodeURIComponent(options.link);
console.error( '1. 从页面参数中拿到链接并展示' , currentLink)
nextLink = currentLink
triggerShareLink = currentLink;
this .setData({
link: currentLink
})
},
handleMessage: (event) => {
console.log( 'onMessage' , event)
event.detail.data.forEach((current) => {
if (current.type === "EXT_APP_H5_SHARE_LINK" ) {
nextLink = current.payload.shareLink
triggerShareLink = current.payload.triggerShareLink
}
})
console.error( '2. 分享前获取 H5 发来的处理好的分享链接' , {
nextLink,
triggerShareLink
})
},
onShareAppMessage() {
const path = `/pages/index/index?link=${encodeURIComponent(nextLink)}`
console.error( '3. 分享时把分享链接放到页面参数中' , path)
console.error( '4. 触发 H5 分享埋点上报' , triggerShareLink)
this .setData({
link: triggerShareLink
})
return {
path
}
}
})
|
完整示例附件
