微信公众号的分享

1、先引入"微信JS-SDK"文件
获取"微信JS-SDK"文件
https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html
在这里插入图片描述

2、在页面引入wx_share.js
例如:防止页面缓存

<script>
    var timestamp=new Date().getTime();
    document.write("<script src='__JS__/wx_share.js?v=" + timestamp + "'><\/script>");
</script>
function shareFunc() {
    wx.ready(function() {
        updateShare()
    })
}
function updateShare() {
    wx.updateTimelineShareData({
        title: TimelineVal(),
        link: wxData.link,
        imgUrl: wxData.imgUrl,
        success: function() {
            window.hideShareLayer && window.hideShareLayer()
        },
        cancel: function() {
            window.hideShareLayer && window.hideShareLayer()
        }
    }),
    wx.onMenuShareTimeline({
        title: TimelineVal(),
        link: wxData.link,
        imgUrl: wxData.imgUrl,
        success: function() {
            window.hideShareLayer && window.hideShareLayer()
        },
        cancel: function() {
            window.hideShareLayer && window.hideShareLayer()
        }
    }),
    wx.updateAppMessageShareData({
        title: wxData.title,
        desc: getDesc(),
        link: wxData.link,
        imgUrl: wxData.imgUrl,
        success: function() {
            window.hideShareLayer && window.hideShareLayer()
        },
        cancel: function() {
            window.hideShareLayer && window.hideShareLayer()
        }
    }),
    wx.onMenuShareAppMessage({
        title: wxData.title,
        desc: getDesc(),
        link: wxData.link,
        imgUrl: wxData.imgUrl,
        type: 'link', // 分享类型,music、video或link,不填默认为link
        dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
        success: function() {
            window.hideShareLayer && window.hideShareLayer()
        },
        cancel: function() {
            window.hideShareLayer && window.hideShareLayer()
        }
    }),

    wx.onMenuShareWeibo({
        title: wxData.title,
        desc: getDesc(),
        link: wxData.link,
        imgUrl: wxData.imgUrl,
        success: function() {
            window.hideShareLayer && window.hideShareLayer()
        },
        cancel: function() {
            window.hideShareLayer && window.hideShareLayer()
        }
    })
}
function getDesc() {
    return wxData.desc2
}
function addZero(e, a) {
    return new Array(a - (e + "").length + 1).join("0") + e
}
function TimelineVal() {
    return null == wxData.desc2 || "" == wxData.desc2,
    getDesc()
}
var hrefUrl = window.location.href.split("#")[0]
  , domain = "http://www.qipai.com"
  , wxData = new Array;

wxData.link = baseurl,
wxData.imgUrl = baseurl+"static/wap/images/logo1.jpg",
wxData.title = "标题XXXXX",
wxData.desc = "描述XXXXX",
wxData.desc2 = wxData.desc,
$get('/api/weixin/getJsSign',{url: hrefUrl},function(e){
    if(e.code==1){
        // console.log(e);
        wx.config({
            debug: false,
            appId: e.data.appId,
            timestamp: e.data.timestamp,
            nonceStr: e.data.nonceStr,
            signature: e.data.signature,
            jsApiList:  ["updateTimelineShareData", "updateAppMessageShareData","onMenuShareWeibo","onMenuShareTimeline","onMenuShareAppMessage"]
        }),
        shareFunc()
    } else{
        layer.msg(res.msg);
    }
});

// $.ajax({
//     url: "接口XXX",
//     type: "get",
//     async: !0,
//     dataType: "jsonp",
//     data: {
//         url: hrefUrl
//     },
//     success: function(e) {
//         // console.log(e);
//         wx.config({
//             debug: 0,
//             appId: e.appid,
//             timestamp: e.timestamp,
//             nonceStr: e.noncestr,
//             signature: e.signature,
//             jsApiList: ["updateTimelineShareData", "updateAppMessageShareData","onMenuShareWeibo","onMenuShareTimeline","onMenuShareAppMessage"]
//         }),
//         shareFunc()
//     }
// });
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值