好几年没做微信分享了,都找不到之前的代码了,现在将整理清楚,供大家使用。
微信分享可分享有多种途径,其中包括微博,qq,微信好友和朋友圈等。
实现效果如下:
image.png

注意事项:

1. 图片大小要100x100px,32k以内才能显示;
2. 这个方法必须先config成功,然后再wx.ready里才能调用。直接放到$(function(){})里执行,实践证明是不行的。
3.appId是需要公众号绑定,timestamp,nonceStr,signature需要java后台提供

appId: '{$appid}', // 必填,公众号的唯一标识
        timestamp: "{$signPackage.timestamp}", // 必填,生成签名的时间戳
        nonceStr: '{$signPackage.nonceStr}', // 必填,生成签名的随机串
        signature: '{$signPackage.signature}',// 必填,签名,见附录1
function wechat_sdk() {
    var timestamp = parseInt(1603796563);
    var nonceStr = '1e7a369c-c5ce-452b-b8b7-4d5c9932365c';
    var signature = '43c432a63a4c76a41c5ac5d95983a5e237c0d1f5';
    var link = window.location.href;
    var imgurl = 'http://images0.zaijiawan.com/wechat/shareLogo.png@!orijpg';
    var shareToTimeLineTitle = '爸妈这样做,孩子品德好| 高半米育儿学校';
    var shareToMessageTitle = '爸妈这样做,孩子品德好';
    var shareToMessageDesc = '高半米育儿学校';
        wx.config({
            debug: false,
            appId: 'wxbf7da72ccbcac264',
            // appId: 'wx4e0adbbc553d3b85',
            timestamp: timestamp,
            nonceStr: nonceStr,
            signature: signature,
            jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'onMenuShareQZone', 'closeWindow']
        });

        wx.ready(function () {
            wx.onMenuShareTimeline({
                title: shareToTimeLineTitle,
                link: link,
                imgUrl: imgurl,
                success: function () {
                    aliyunPushLog(true, 'KnowledgeItemDetail', 'KnowledgeItemDetailShare', 'uid=ojpQEv7raaXn0gJslcTi2j4GPNho', 'kid=946');
                },
                cancel: function () {
                    CNZZ_PUSH(["_trackEvent", "课程详情", "取消分享", '朋友圈', 0, null]);
                }
            });
            wx.onMenuShareAppMessage({
                title: shareToMessageTitle,
                desc: shareToMessageDesc,
                link: link,
                imgUrl: imgurl,
                success: function () {
                    aliyunPushLog(true, 'KnowledgeItemDetail', 'KnowledgeItemDetailShare', 'uid=ojpQEv7raaXn0gJslcTi2j4GPNho', 'kid=946');

                },
                cancel: function () {
                    // CNZZ_PUSH(["_trackEvent", "课程详情", "取消分享", '发送给朋友', 0, null]);
                }
            });
            wx.onMenuShareQQ({
                imgUrl: imgurl,
                link: link,
                success: function () {
                    aliyunPushLog(true, 'KnowledgeItemDetail', 'KnowledgeItemDetailShare', 'uid=ojpQEv7raaXn0gJslcTi2j4GPNho', 'kid=946');
                },
                cancel: function () {
                    // CNZZ_PUSH(["_trackEvent", "课程详情", "取消分享", 'QQ', 0, null]);
                }
            });
            wx.onMenuShareWeibo({
                imgUrl: imgurl,
                link: link,
                success: function () {
                    aliyunPushLog(true, 'KnowledgeItemDetail', 'KnowledgeItemDetailShare', 'uid=ojpQEv7raaXn0gJslcTi2j4GPNho', 'kid=946');
                },
                cancel: function () {
                    CNZZ_PUSH(["_trackEvent", "课程详情", "取消分享", '微博', 0, null]);
                }
            });
            wx.onMenuShareQZone({
                imgUrl: imgurl,
                link: link,
                success: function () {
                    aliyunPushLog(true, 'KnowledgeItemDetail', 'KnowledgeItemDetailShare', 'uid=ojpQEv7raaXn0gJslcTi2j4GPNho', 'kid=946');
                },
                cancel: function () {
                    CNZZ_PUSH(["_trackEvent", "课程详情", "取消分享", 'QQ空间', 0, null]);
                }
            });
        });
        wx.error(function (res) {
            CNZZ_PUSH(["_trackEvent", "课程详情", "分享失败", null, 0, null]);
        });
    }

相关链接:
https://blog.csdn.net/xuanjie...
https://blog.csdn.net/weixin_...
https://www.cnblogs.com/txw1958/p/weixin-js.html


陌然浅笑
25 声望3 粉丝

坚持自律