Skip to content

Instantly share code, notes, and snippets.

@guo-yu
Created March 28, 2014 08:16
Show Gist options
  • Save guo-yu/9827760 to your computer and use it in GitHub Desktop.
Save guo-yu/9827760 to your computer and use it in GitHub Desktop.
/**!
* 微信内置浏览器的Javascript API,功能包括:
*
* 1、分享到微信朋友圈
* 2、分享给微信好友
* 3、分享到腾讯微博
* 4、隐藏/显示右上角的菜单入口
* 5、隐藏/显示底部浏览器工具栏
* 6、获取当前的网络状态
*
* @author zhaoxianlie(http://www.baidufe.com)
*/
var WeixinApi = (function () {
/**
* 分享到微信朋友圈
* @param {Object} data 待分享的信息
* @p-config {String} appId 公众平台的appId(服务号可用)
* @p-config {String} imageUrl 图片地址
* @p-config {String} link 链接地址
* @p-config {String} desc 描述
* @p-config {String} title 分享的标题
*
* @param {Object} callbacks 相关回调方法
* @p-config {Boolean} async ready方法是否需要异步执行,默认false
* @p-config {Function} ready(argv) 就绪状态
* @p-config {Function} dataLoaded(data) 数据加载完成后调用,async为true时有用,也可以为空
* @p-config {Function} cancel(resp) 取消
* @p-config {Function} fail(resp) 失败
* @p-config {Function} confirm(resp) 成功
* @p-config {Function} all(resp) 无论成功失败都会执行的回调
*/
function weixinShareTimeline(data, callbacks) {
callbacks = callbacks || {};
var shareTimeline = function (theData) {
WeixinJSBridge.invoke('shareTimeline', {
"appid":theData.appId ? theData.appId : '',
"img_url":theData.imgUrl,
"link":theData.link,
"desc":theData.title,
"title":theData.desc, // 注意这里要分享出去的内容是desc
"img_width":"120",
"img_height":"120"
}, function (resp) {
switch (resp.err_msg) {
// share_timeline:cancel 用户取消
case 'share_timeline:cancel':
callbacks.cancel && callbacks.cancel(resp);
break;
// share_timeline:fail 发送失败
case 'share_timeline:fail':
callbacks.fail && callbacks.fail(resp);
break;
// share_timeline:confirm 发送成功
case 'share_timeline:confirm':
callbacks.confirm && callbacks.confirm(resp);
break;
}
// 无论成功失败都会执行的回调
callbacks.all && callbacks.all(resp);
});
};
WeixinJSBridge.on('menu:share:timeline', function (argv) {
if (callbacks.async && callbacks.ready) {
if(!callbacks.__dataLoadedFuncInited) {
var loadedCb = callbacks.dataLoaded || new Function();
callbacks.dataLoaded = function (newData) {
loadedCb(newData);
shareTimeline(newData);
};
callbacks.__dataLoadedFuncInited = true;
}
// 然后就绪
callbacks.ready && callbacks.ready(argv);
} else {
// 就绪状态
callbacks.ready && callbacks.ready(argv);
shareTimeline(data);
}
});
}
/**
* 发送给微信上的好友
* @param {Object} data 待分享的信息
* @p-config {String} appId 公众平台的appId(服务号可用)
* @p-config {String} imageUrl 图片地址
* @p-config {String} link 链接地址
* @p-config {String} desc 描述
* @p-config {String} title 分享的标题
*
* @param {Object} callbacks 相关回调方法
* @p-config {Boolean} async ready方法是否需要异步执行,默认false
* @p-config {Function} ready(argv) 就绪状态
* @p-config {Function} dataLoaded(data) 数据加载完成后调用,async为true时有用,也可以为空
* @p-config {Function} cancel(resp) 取消
* @p-config {Function} fail(resp) 失败
* @p-config {Function} confirm(resp) 成功
* @p-config {Function} all(resp) 无论成功失败都会执行的回调
*/
function weixinSendAppMessage(data, callbacks) {
callbacks = callbacks || {};
var sendAppMessage = function (theData) {
WeixinJSBridge.invoke('sendAppMessage', {
"appid":theData.appId ? theData.appId : '',
"img_url":theData.imgUrl,
"link":theData.link,
"desc":theData.desc,
"title":theData.title,
"img_width":"120",
"img_height":"120"
}, function (resp) {
switch (resp.err_msg) {
// send_app_msg:cancel 用户取消
case 'send_app_msg:cancel':
callbacks.cancel && callbacks.cancel(resp);
break;
// send_app_msg:fail 发送失败
case 'send_app_msg:fail':
callbacks.fail && callbacks.fail(resp);
break;
// send_app_msg:confirm 发送成功
case 'send_app_msg:confirm':
callbacks.confirm && callbacks.confirm(resp);
break;
}
// 无论成功失败都会执行的回调
callbacks.all && callbacks.all(resp);
});
};
WeixinJSBridge.on('menu:share:appmessage', function (argv) {
if (callbacks.async && callbacks.ready) {
if(!callbacks.__dataLoadedFuncInited) {
var loadedCb = callbacks.dataLoaded || new Function();
callbacks.dataLoaded = function (newData) {
loadedCb(newData);
sendAppMessage(newData);
};
callbacks.__dataLoadedFuncInited = true;
}
// 然后就绪
callbacks.ready && callbacks.ready(argv);
} else {
// 就绪状态
callbacks.ready && callbacks.ready(argv);
sendAppMessage(data);
}
});
}
/**
* 分享到腾讯微博
* @param {Object} data 待分享的信息
* @p-config {String} imageUrl 图片地址
* @p-config {String} link 链接地址
* @p-config {String} desc 描述
* @p-config {String} title 分享的标题
*
* @param {Object} callbacks 相关回调方法
* @p-config {Boolean} async ready方法是否需要异步执行,默认false
* @p-config {Function} ready(argv) 就绪状态
* @p-config {Function} dataLoaded(data) 数据加载完成后调用,async为true时有用,也可以为空
* @p-config {Function} cancel(resp) 取消
* @p-config {Function} fail(resp) 失败
* @p-config {Function} confirm(resp) 成功
* @p-config {Function} all(resp) 无论成功失败都会执行的回调
*/
function weixinShareWeibo(data, callbacks) {
callbacks = callbacks || {};
var shareWeibo = function (theData) {
WeixinJSBridge.invoke('shareWeibo', {
"content":theData.desc,
"link":theData.link,
"img_url":theData.imgUrl,
"title":theData.title,
"img_width":"120",
"img_height":"120"
}, function (resp) {
switch (resp.err_msg) {
// share_weibo:cancel 用户取消
case 'share_weibo:cancel':
callbacks.cancel && callbacks.cancel(resp);
break;
// share_weibo:fail 发送失败
case 'share_weibo:fail':
callbacks.fail && callbacks.fail(resp);
break;
// share_weibo:confirm 发送成功
case 'share_weibo:confirm':
callbacks.confirm && callbacks.confirm(resp);
break;
}
// 无论成功失败都会执行的回调
callbacks.all && callbacks.all(resp);
});
};
WeixinJSBridge.on('menu:share:weibo', function (argv) {
if (callbacks.async && callbacks.ready) {
if(!callbacks.__dataLoadedFuncInited) {
var loadedCb = callbacks.dataLoaded || new Function();
callbacks.dataLoaded = function (newData) {
loadedCb(newData);
shareWeibo(newData);
};
callbacks.__dataLoadedFuncInited = true;
}
// 然后就绪
callbacks.ready && callbacks.ready(argv);
} else {
// 就绪状态
callbacks.ready && callbacks.ready(argv);
shareWeibo(data);
}
});
}
/**
* 显示网页右上角的按钮
*/
function showOptionMenu() {
WeixinJSBridge.call('showOptionMenu');
}
/**
* 隐藏网页右上角的按钮
*/
function hideOptionMenu() {
WeixinJSBridge.call('hideOptionMenu');
}
/**
* 显示底部工具栏
*/
function showToolbar() {
WeixinJSBridge.call('showToolbar');
}
/**
* 隐藏底部工具栏
*/
function hideToolbar() {
WeixinJSBridge.call('hideToolbar');
}
/**
* 返回如下几种类型:
*
* network_type:wifi wifi网络
* network_type:edge 非wifi,包含3G/2G
* network_type:fail 网络断开连接
* network_type:wwan 2g或者3g
*
* 使用方法:
* WeixinApi.getNetworkType(function(networkType){
*
* });
*
* @param callback
*/
function getNetworkType(callback) {
if (callback && typeof callback == 'function') {
WeixinJSBridge.invoke('getNetworkType', {}, function (e) {
// 在这里拿到e.err_msg,这里面就包含了所有的网络类型
callback(e.err_msg);
});
}
}
/**
* 当页面加载完毕后执行,使用方法:
* WeixinApi.ready(function(Api){
* // 从这里只用Api即是WeixinApi
* });
* @param readyCallback
*/
function wxJsBridgeReady(readyCallback) {
if (readyCallback && typeof readyCallback == 'function') {
var Api = this;
var wxReadyFunc = function () {
readyCallback(Api);
};
if (document.addEventListener) {
document.addEventListener('WeixinJSBridgeReady', wxReadyFunc, false);
} else if (document.attachEvent) {
document.attachEvent('WeixinJSBridgeReady', wxReadyFunc);
document.attachEvent('onWeixinJSBridgeReady', wxReadyFunc);
}
}
}
return {
version :"1.1",
ready :wxJsBridgeReady,
shareToTimeline :weixinShareTimeline,
shareToWeibo :weixinShareWeibo,
shareToFriend :weixinSendAppMessage,
showOptionMenu :showOptionMenu,
hideOptionMenu :hideOptionMenu,
showToolbar :showToolbar,
hideToolbar :hideToolbar,
getNetworkType :getNetworkType
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment