之前公司微信开发的时候 写了个微信的接口改良版,当然好多想改进的都没改。。大概是太懒了 (囧
/**
* Created by DFH on 13-12-16.
*/
/*
--html
var shareData = {
//分享展示图片地址 **必须
"imgUrl": "a.jpg",
//分享至朋友圈链接 **必须
"timeLineLink": 'asdfasdf',
//分享至朋友链接 **必须
"sendFriendLink": 'asfsafd',
//分享至微博链接
"weiboLink": 'asdfasf',
//分享至朋友圈的标题 **必须
"tTitle": 'dasdfas',
//分享至朋友圈的内容 **必须
"tContent": "asdfsaf"
};
Weixin({
shareData:shareData
}).invoke();
*/
(function(){
var root = this ;
/**
* 分享数据配置
*
* @type {{
* shareData: {}, 分享数据
* hideOptionMenu: boolean, 是否隐藏右上角按钮
* hideToolbar: boolean 是否隐藏底部按钮
* }}
*/
var WX_CONFIG = {
shareData: {
//分享展示图片地址 **必须
"imgUrl": "",
//分享至朋友圈链接 **必须
"timeLineLink": '',
//分享至朋友链接 **必须
"sendFriendLink": '',
//分享至微博链接
"weiboLink": '',
//分享至朋友圈的标题 **必须
"tTitle": '',
//分享至朋友圈的内容 **必须
"tContent": "",
//分享至朋友的标题
"fTitle": "",
//分享至朋友的内容
"fContent": "",
//分享至微博的内容
"wContent": ""
} ,
//隐藏右上角按钮,默认false
hideOptionMenu:false,
//隐藏底部栏,默认true
hideToolbar:true
};
var Weixin=function(obj){
if (obj instanceof Weixin) return obj;
if (!(this instanceof Weixin)) return new Weixin(obj);
this._wrapped = obj;
this.checkData();
};
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = Weixin;
}
exports.Weixin = Weixin;
} else {
root.Weixin = Weixin; //root为window
}
/**
* 调用开始
*/
Weixin.prototype.invoke = function(){
var _this = this ;
if(!_this.readyInvok) return;
this.setEvent();
}
Weixin.prototype.setEvent = function(){
var doc = root.document,
_this = this,
config = _this._wrapped ;
console.log(config);
doc.addEventListener('WeixinJSBridgeReady',function onBridgeReady(){
if(config.hideOptionMenu) WeixinJSBridge.call('hideOptionMenu');
if(config.hideToolbar) WeixinJSBridge.call('hideToolbar');
//分享绑定 朋友圈
WeixinJSBridge.on('menu:share:timeline', function (argv) {
WeixinJSBridge.invoke('shareTimeline', {
"img_url": config.shareData.imgUrl,
"img_width": "640",
"img_height": "640",
"link": config.shareData.timeLineLink,
"desc": config.shareData.tContent,
"title": config.shareData.tTitle
}, function (res) {
_report('timeline', res.err_msg);
});
});
// 分享绑定 发送给好友
WeixinJSBridge.on('menu:share:appmessage', function (argv) {
WeixinJSBridge.invoke('sendAppMessage', {
"img_url": config.shareData.imgUrl,
"img_width": "640",
"img_height": "640",
"link": config.shareData.sendFriendLink,
"desc": config.shareData.fContent,
"title": config.shareData.fTitle
}, function (res) {
_report('send_msg', res.err_msg);
})
});
// 分享绑定 微博
WeixinJSBridge.on('menu:share:weibo', function (argv) {
WeixinJSBridge.invoke('shareWeibo', {
"img_url": config.shareData.imgUrl,
"img_width": "640",
"img_height": "640",
"content": config.shareData.wContent+window.shareData.weiboLink,
"url": config.shareData.weiboLink
}, function (res) {
_report('weibo', res.err_msg);
});
});
}) ;
}
Weixin.prototype.checkData = function(){
var _this = this,
data = _this._wrapped;
_this.readyInvok = true;
/**
* shareData校验
*/
if(!data.shareData || !data.shareData.imgUrl || !data.shareData.timeLineLink ||!data.shareData.tTitle ||!data.shareData.tContent){
_this.readyInvok = false;
throw new Error('u6ca1u6709u5206u4eabu6570u636euff0cu8bf7u68c0u67e5uff01');
}
if(!data.shareData.sendFriendLink) data.shareData.sendFriendLink = data.shareData.timeLineLink;
if(!data.shareData.weiboLink) data.shareData.weiboLink = data.shareData.timeLineLink;
if(!data.shareData.fTitle) data.shareData.fTitle = data.shareData.tTitle;
if(!data.shareData.fContent) data.shareData.fContent = data.shareData.tContent;
if(!data.shareData.wContent) data.shareData.wContent = data.shareData.tContent;
}
})();