微信公众号h5的分享功能

676 阅读1分钟

微信公众号h5的分享功能配置如下:

微信配置

wx.config({
    debug,
    appId,
    timestamp,
    nonceStr,
    signature,
    jsApiList: [
      'onMenuShareAppMessage',
      'updateAppMessageShareData'
    ]
  });

配置成功后,在wx.ready里设置分享参数

  wx.ready(function () {
    setWxShareApi({
      title: 'xxxxxx', // 分享标题
      desc: window.location.href.replace(/\?(.*?)$/g, '#'), // 分享描述
      link: window.location.href.replace(/\?(.*?)$/g, '#'), // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
      imgUrl: '1', // 分享图标
    })
  });

/**
 * 设置微信分享给好友和朋友圈接口
 * @param {分享参数} opt 
 */
function setWxShareApi(opt) {
  // 安卓分享设置
  wx.onMenuShareAppMessage(Object.assign(opt, {
    success: function () {
      // 用户点击了分享后执行的回调函数
      console.log('分享设置成功', window.location.href.replace(/\?(.*?)#/g, '#'), '---------', window.location.href)
    }
  }))
  // ios设置分享
  wx.updateAppMessageShareData(Object.assign(opt, {
    success: function () {
      // 用户点击了分享后执行的回调函数
      console.log('分享设置成功', window.location.href.replace(/\?(.*?)#/g, '#'), '---------', window.location.href)
    }
  }))
}```