forked from staticfile/static
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request staticfile#199 from Gerhut/master
增加 wechat.js 库
- Loading branch information
Showing
4 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/*! CopyRight: sofish http://github.com/sofish/wechat.js, Licensed under: MIT */ | ||
;(function(global, doc) { | ||
|
||
var noop = function() {}; | ||
|
||
// map 掉恶心的不统一的 api | ||
var Wechat = function() { | ||
this.calls = []; | ||
|
||
this.map = { | ||
events: { | ||
friend: 'menu:share:appmessage', | ||
timeline: 'menu:share:timeline', | ||
weibo: 'menu:share:weibo', | ||
email: 'email' // 分享到邮件 | ||
}, | ||
actions: { | ||
friend: 'sendAppMessage', | ||
timeline: 'shareTimeline', | ||
weibo: 'shareWeibo', | ||
email: 'email' | ||
}, | ||
direct: { | ||
network: 'getNetworkType', | ||
hideToolbar: 'hideToolbar', | ||
hideOptionMenu: 'hideOptionMenu', | ||
showOptionMenu: 'showOptionMenu', | ||
closeWebView: 'closeWindow', // 关闭webview | ||
scanQRCode: 'scanQRCode', //跳转到扫码页面 | ||
imagePreview: 'imagePreview' //图片预览/查看大图 | ||
} | ||
}; | ||
}; | ||
|
||
// 有些 data 是延时获取的,这时候应该支持传入 callback | ||
Wechat.prototype._data = function(data) { | ||
var tmp = {}; | ||
|
||
for(var p in data) { | ||
if(!data.hasOwnProperty(p)) return; | ||
tmp[p] = (typeof data[p] === 'function') ? data[p]() : data[p]; | ||
} | ||
|
||
// 接口命名统一 | ||
tmp.appid = tmp.app; | ||
tmp.img_url = tmp.img; | ||
|
||
delete tmp.app; | ||
delete tmp.img; | ||
|
||
return tmp; | ||
}; | ||
|
||
|
||
// 处理数据接入 | ||
Wechat.prototype._make = function(obj) { | ||
if(typeof WeixinJSBridge === 'undefined') return this.calls.push(obj); | ||
|
||
var name = obj.name | ||
, direct = this.map.direct[name] | ||
, data = obj.data | ||
, callback = obj.callback; | ||
|
||
// 直接获取的情况 | ||
if(direct) { | ||
// 获取用户网络状态的返回值如下: | ||
// network_type:wifi wifi网络 | ||
// network_type:edge 非wifi,包含3G/2G | ||
// network_type:fail 网络断开连接 | ||
// network_type:wwan(2g或者3g) | ||
if(name === 'network') { | ||
return WeixinJSBridge.invoke(direct, {}, callback); | ||
// 图片预览/查看大图 | ||
} else if(name === 'imagePreview') { | ||
return WeixinJSBridge.invoke(direct, data, callback); | ||
} | ||
|
||
return WeixinJSBridge.call(direct, callback); | ||
} | ||
|
||
// 分享到微博的接口不同 | ||
if(name === 'weibo') { | ||
data.content = data.desc; | ||
data.url = data.link; | ||
|
||
// 朋友圈的 title 是不显示的,直接拼接 | ||
} else if(name === 'timeline') { | ||
data.title = data.title + ' - ' + data.desc; | ||
|
||
// Android 下有时候会需要 desc (*-.-) | ||
data.desc = data.title; | ||
} else if(name === 'email') { | ||
data.content = data.desc + ' ' + data.link; | ||
return WeixinJSBridge.invoke('sendEmail', data, callback); | ||
} | ||
|
||
var that = this; | ||
|
||
// 当 WeixinJSBridge 存在则直接绑定事件 | ||
WeixinJSBridge.on(this.map.events[name], function() { | ||
WeixinJSBridge.invoke(that.map.actions[name], data, callback); | ||
}); | ||
}; | ||
|
||
// 添加监听 | ||
Wechat.prototype.on = function(name, data, callback) { | ||
if(!name) return; | ||
if(typeof data === 'function') { | ||
callback = data; | ||
data = null; | ||
} | ||
|
||
this._make({ | ||
name: name, | ||
data: data ? this._data(data) : {}, | ||
callback: callback || noop | ||
}); | ||
|
||
// 返回本身,支持链式 | ||
return this; | ||
}; | ||
|
||
// WeixingJSBridgeReady 后执行绑定的队列 | ||
var ready = function() { | ||
for(var i = 0, len = wx.calls.length; i < len; i++) { | ||
wx._make(wx.calls[i]); | ||
} | ||
}; | ||
|
||
// 对外只分享一个接口,不过会返回本身,可以有备用 | ||
var wx = new Wechat(); | ||
|
||
// 创建唯一实例 | ||
var entry = function() { | ||
return wx.on.apply(wx, arguments); | ||
}; | ||
|
||
//spm3 和 cortex 6.x 已经支持自动构建成module | ||
if (typeof exports !== 'undefined' && module.exports) { | ||
module.exports = exports = entry; | ||
} else if (typeof define === 'function') { | ||
define(function(require, exports, module) { | ||
module.exports = exports = entry; | ||
}); | ||
} else { | ||
//浏览器端直接运行 | ||
global.wechat = global.wechat || entry; | ||
} | ||
|
||
if(typeof WeixinJSBridge === 'undefined'){ | ||
if(doc.addEventListener) { | ||
doc.addEventListener('WeixinJSBridgeReady', ready, false); | ||
} else { | ||
doc.attachEvent('WeixinJSBridgeReady', ready); | ||
doc.attachEvent('onWeixinJSBridgeReady', ready); | ||
} | ||
} else { | ||
ready(); | ||
} | ||
})(window, document); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "wechat.js", | ||
"filename": "wechat.js", | ||
"version": "0.1.1", | ||
"description": "微信相关的 js 操作:分享、网络、菜单", | ||
"homepage": "http://sofish.github.io/wechat.js/", | ||
"keywords": [ | ||
"wechat", | ||
"wechat.js", | ||
"sns", | ||
"sharing" | ||
], | ||
"maintainers": [ | ||
{ | ||
"name": "Sofish Lin", | ||
"web": "http://sofish.de/" | ||
} | ||
], | ||
"repositories": [ | ||
{ | ||
"type": "git", | ||
"url": "https://github.com/sofish/wechat.js" | ||
} | ||
] | ||
} | ||
|