兼容微信JS SDK和WeixinJSBridge的分享API

最近微信官方推出了JS SDK,着实让开发微信APP的小伙伴们欢喜了一把,伴随着的是原来的WeixinJSBridge分享功能的丧失。那么作为一个微信开发人员,如果用户没有升级客户端,新的api无法使用,作为升级后的客户端,老的又无法使用,导致我们的代码得写两份,很尴尬。

开玩笑,做每一个页面都需要搞两份分享代码,简直不可取,于是就包装了一下,可以兼容JS SDK和WeixinJSBridge的API,调用方法如下:

兼容微信JS SDK和WeixinJSBridge的分享API

var shareData = { title: "小媒体技术乐园", desc: "在这里,你可以看到很多有用的技术文章哦", link: "http://www.xiaomeiti.com/", imgUrl: "http://www.jshacker.com/pull/4/349101198.jpg" }; var wxApi = new WeixinShare(shareData); WeixinShare代码在后面哦。。。

备注:如果要用新版本的api分享,不要忘了引入http://res.wx.qq.com/open/js/jweixin-1.0.0.js哦,至于如何接入api,可以参考: https://mp.weixin.qq.com/cgi-bin/announce?action=getannouncement&key=1420741421&version=10&lang=zh_CN

如上代码就可以实现兼容啦,但有时候你可能会说了,title、desc这些变量可能是动态的,不是写死的,那么也好办,只需要这么写即可:

var shareData = { appid: "asdasdasdasdasd", title: function(){return "小媒体技术乐园"}, desc: function(){return "在这里,你可以看到很多有用的技术文章哦"}, link: function(){return "http://www.xiaomeiti.com/"}, imgUrl: function(){return "http://www.jshacker.com/pull/4/349101198.jpg"} }; var wxApi = new WeixinShare(shareData); 在上面的function里你可以做自己的事情,只要把最终的值return出去即可。

兼容微信JS SDK和WeixinJSBridge的分享API

WeixinShare Code: /** @shareData: { appid: string/function title: string/function desc: string/function link: string/function imgUrl: string/function } **/ function WeixinShare(shareData) { this.shareData = shareData; if(wx && wx.checkJsApi) { this.shareType = "api"; this.initByAPI(); } else { this.shareType = "bridge"; this.initByBridge(); } } WeixinShare.prototype.initByAPI = function() { var me = this; wx.ready(function() { var shareData = { title: me.getParam("title"), desc: me.getParam("desc"), link: me.getParam("link"), imgUrl: me.getParam("imgUrl"), trigger: function (res) { this.title = me.getParam("title"); this.desc = me.getParam("desc"); this.link = me.getParam("link"); this.imgUrl = me.getParam("imgUrl"); } }; wx.onMenuShareAppMessage(shareData); wx.onMenuShareTimeline(shareData); }); }; WeixinShare.prototype.initByBridge = function() { var me = this; document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() { WeixinJSBridge.on('menu:share:appmessage', function (argv) { me.shareFriend()} ); WeixinJSBridge.on('menu:share:timeline', function (argv) { me.shareTimeline()} ); }, false); }; WeixinShare.prototype.getParam = function(name) { var val = this.shareData[name]; if(typeof val == "function") { return val(); } return val; }; WeixinShare.prototype.shareFriend = function() { WeixinJSBridge.invoke('sendAppMessage', { appid: this.getParam("appid"), img_url: this.getParam("imgUrl"), img_width: 120, img_height: 120, link: this.getParam("link"), title: this.getParam("title"), desc: this.getParam("desc") }, function(res) { _report('send_msg', res.err_msg); }); }; WeixinShare.prototype.shareTimeline = function() { WeixinJSBridge.invoke('shareTimeline', { appid: this.getParam("appid"), img_url: this.getParam("imgUrl"), img_width: 120, img_height: 120, link: this.getParam("link"), title: this.getParam("title"), desc: this.getParam("desc") }, function(res) { _report('timeline', res.err_msg); }); };

相关阅读