封装微信分享到朋友/朋友圈js
在页面引入:
<script src="/static/lib/jquery-2.2.2.min.js"></script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script src="/static/src/wx_share/index.js?1"></script>
<script>
wxShare.title='流量圈社群-你想要的合作资源在这里';
wxShare.desc='项目找流量,流量找变现,资源找互换,就到流量圈社群!';
wxShare.imgUrl='http://imgpro.lamahui.com/static/images/2017/08/15/b4485e74178d6d05d9e29ccdcd2f82712807.jpg';
wxShare.ok();
</script>
index.js
$.get('/wxShare', { url: location.href.split('#')[0] } ,function(res) { wx.config({ debug: false, appId: res.data.package.appId, timestamp: res.data.package.timestamp, nonceStr: res.data.package.nonceStr, signature: res.data.package.signature, jsApiList: [ "onMenuShareAppMessage", "onMenuShareTimeline", "chooseImage", "uploadImage", "downloadImage" ] }); }).fail(function() { console.log('微信分享获取后台参数ajax失败!'); }); var wxShare = { ok: function() { var self = this; self.title = self.title; self.desc = self.desc; self.link = self.link || location.href; self.imgUrl = self.imgUrl; wx.ready(function() { wx.onMenuShareAppMessage({ title: self.title, desc: self.desc, link: self.link, imgUrl: self.imgUrl, success: function() { if (typeof self.successCallback === 'function') { self.successCallback(); } }, cancel: function() { if (typeof self.cancelCallback === 'function') { self.cancelCallback(); } } }); wx.onMenuShareTimeline({ title: self.title2 || self.title, desc: self.desc2 || self.desc, link: self.link2 || self.link, imgUrl: self.imgUrl2 || self.imgUrl, success: function() { if (typeof self.successCallback2 === 'function') { self.successCallback2(); } else if (typeof self.successCallback === 'function') { self.successCallback(); } }, cancel: function() { if (typeof self.cancelCallback2 === 'function') { self.cancelCallback2(); } else if (typeof self.cancelCallback === 'function') { self.cancelCallback(); } } }); }); } }
转载:http://blog.csdn.net/gengxuelei/article/details/52315762