版本比较

标识

  • 该行被添加。
  • 该行被删除。
  • 格式已经改变。

...

<!--index.wxml-->
<web-view src="{{link}}" bindmessage="handleMessage"/>


// index.js

let nextLink = ''
let triggerShareLink = ''
Page({
  data: {
    link: "",
    nextLink: ""
  },
  onLoad(options) {
    wx.showShareMenu();
    const currentLink = options.link && decodeURIComponent(options.link);
    console.error('1. 从页面参数中拿到链接并展示', currentLink)
    // 如果没有接收到 H5 发来的信息的话, 默认使用当前的链接
    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
    }
  }
})

...