uniApp h5分享实现

前端需要传页面的url给后端,其中不包含#之后的内容,后端会返回timestamp、nonceStr、signature
that.$http.post(that.$api.getH5Sign, {
    url: window.location.href.split('#')[0]
}).then(res => {
    wxj.config({
        debug: true, // 开启调试模式
        appId: that.$config.weixinAppId, // 必填,公众号的唯一标识
        timestamp: res.data.timestamp, // 必填,生成签名的时间戳
        nonceStr: res.data.noncestr, // 必填,生成签名的随机串
        signature: res.data.sgture, // 必填,签名
        jsApiList: ["updateAppMessageShareData", "updateTimelineShareData"] // 必填,需要使用的JS接口列表
    });
    wxj.ready(function () { //需在用户可能点击分享按钮前就先调用              
         wxj.updateAppMessageShareData({
            title:that.detail.routeName, // 分享标题
            desc: that.detail.routeFeatures, // 分享描述
            link: window.location.href, // 分享链接,即要进入的页面的链接(其中的域名)必须与当前页面对应的公众号JS安全域名一致
            imgUrl: imgUrl, // 分享图标
        });
        wxj.updateTimelineShareData({
           title: that.detail.routeName, // 分享标题
           desc: that.detail.routeFeatures, // 分享描述
           link: window.location.href, //分享链接,即要进入的页面的链接(其中的域名)必须与当前页面对应的公众号JS安全域名一致
       imgUrl: imgUrl, // 分享图标
})
});
})
原文地址:https://www.cnblogs.com/gao-xiaomeng/p/14298335.html