weixin.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /**
  2. * 微信相关功能插件
  3. * -----------------------------
  4. */
  5. // 微信相关功能插件--zpeto扩展
  6. ;(function($){
  7. $.fn.wx = function(option){
  8. // 初始化函数体
  9. var $wx = $(this);
  10. var opts = $.extend({},$.fn.wx.defaults,option); // 继承传入的值
  11. // 确定微信是否准备好
  12. document.addEventListener("WeixinJSBridgeReady", function(){
  13. window.G_WEIXIN_READY = true;
  14. }, false);
  15. // 回到函数循环执行
  16. function CallWeiXinAPI(fn, count){
  17. var total = 2000; //30s
  18. count = count || 0;
  19. if(true === window.G_WEIXIN_READY || ("WeixinJSBridge" in window)){
  20. fn.apply(null, []);
  21. }else{
  22. if(count <= total){
  23. setTimeout(function(){
  24. CallWeiXinAPI(fn, count++);
  25. }, 15);
  26. }
  27. }
  28. }
  29. var _unit = {
  30. /**
  31. * 执行回调
  32. * @param Object handler {Function callback, Array args, Object context, int delay}
  33. */
  34. execHandler : function(handler){
  35. if(handler && handler instanceof Object){
  36. var callback = handler.callback || null;
  37. var args = handler.args || [];
  38. var context = handler.context || null;
  39. var delay = handler.delay || -1;
  40. if(callback && callback instanceof Function){
  41. if(typeof(delay) == "number" && delay >= 0){
  42. setTimeout(function(){
  43. callback.apply(context, args);
  44. }, delay);
  45. }else{
  46. callback.apply(context, args);
  47. }
  48. }
  49. }
  50. },
  51. /**
  52. * 合并参数后执行回调
  53. * @param Object handler {Function callback, Array args, Object context, int delay}
  54. * @param Array args 参数
  55. */
  56. execAfterMergerHandler : function(handler, _args){
  57. if(handler && handler instanceof Object){
  58. var args = handler.args || [];
  59. handler.args = _args.concat(args);
  60. }
  61. this.execHandler(handler);
  62. }
  63. }
  64. // 微信的接口
  65. var _api = {
  66. Share : {
  67. /**
  68. * 分享到微博
  69. * @param Object options {String content, String url}
  70. * @param Object handler
  71. */
  72. "weibo" : function(options, handler){
  73. CallWeiXinAPI(function(){
  74. WeixinJSBridge.on("menu:share:weibo",function(argv){
  75. WeixinJSBridge.invoke('shareWeibo', options, function(res){
  76. _unit.execAfterMergerHandler(handler, [res]);
  77. });
  78. });
  79. });
  80. },
  81. /**
  82. * 分享到微博
  83. * @param Object options {
  84. * String img_url,
  85. * Number img_width,
  86. * Number img_height,
  87. * String link,
  88. * String desc,
  89. * String title
  90. * }
  91. * @param Object handler
  92. */
  93. "timeline" : function(options, handler){
  94. CallWeiXinAPI(function(){
  95. WeixinJSBridge.on("menu:share:timeline",function(argv){
  96. WeixinJSBridge.invoke('shareTimeline', options, function(res){
  97. _unit.execAfterMergerHandler(handler, [res]);
  98. });
  99. });
  100. });
  101. },
  102. /**
  103. * 分享到微博
  104. * @param Object options {
  105. * String appid,
  106. * String img_url,
  107. * Number img_width,
  108. * Number img_height,
  109. * String link,
  110. * String desc,
  111. * String title
  112. * }
  113. * @param Object handler
  114. */
  115. "message" : function(options, handler){
  116. CallWeiXinAPI(function(){
  117. WeixinJSBridge.on("menu:share:appmessage",function(argv){
  118. WeixinJSBridge.invoke('sendAppMessage', options, function(res){
  119. _unit.execAfterMergerHandler(handler, [res]);
  120. });
  121. });
  122. });
  123. }
  124. },
  125. /**
  126. * 设置底栏
  127. * @param boolean visible 是否显示
  128. * @param Object handler
  129. */
  130. "setToolbar" : function(visible, handler){
  131. CallWeiXinAPI(function(){
  132. if(true === visible){
  133. WeixinJSBridge.call('showToolbar');
  134. }else{
  135. WeixinJSBridge.call('hideToolbar');
  136. }
  137. _unit.execAfterMergerHandler(handler, [visible]);
  138. });
  139. },
  140. /**
  141. * 设置右上角选项菜单
  142. * @param boolean visible 是否显示
  143. * @param Object handler
  144. */
  145. "setOptionMenu" : function(visible, handler){
  146. CallWeiXinAPI(function(){
  147. if(true === visible){
  148. WeixinJSBridge.call('showOptionMenu');
  149. }else{
  150. WeixinJSBridge.call('hideOptionMenu');
  151. }
  152. _unit.execAfterMergerHandler(handler, [visible]);
  153. });
  154. },
  155. /**
  156. * 调用微信支付
  157. * @param Object data 微信支付参数
  158. * @param Object handlerMap 回调句柄 {Handler success, Handler fail, Handler cancel, Handler error}
  159. */
  160. "pay" : function(data, handlerMap){
  161. CallWeiXinAPI(function(){
  162. var requireData = {"appId":"","timeStamp":"","nonceStr":"","package":"","signType":"","paySign":""};
  163. var map = handlerMap || {};
  164. var handler = null;
  165. var args = [data];
  166. for(var key in requireData){
  167. if(requireData.hasOwnProperty(key)){
  168. requireData[key] = data[key]||"";
  169. console.info(key + " = " + requireData[key]);
  170. }
  171. }
  172. WeixinJSBridge.invoke('getBrandWCPayRequest', requireData, function(response){
  173. var key = "get_brand_wcpay_request:";
  174. switch(response.err_msg){
  175. case key + "ok":
  176. handler = map.success;
  177. break;
  178. case key + "fail":
  179. handler = map.fail || map.error;
  180. break;
  181. case key + "cancel":
  182. handler = map.cancel || map.error;
  183. break;
  184. default:
  185. handler = map.error;
  186. break;
  187. }
  188. _unit.execAfterMergerHandler(handler, args);
  189. });
  190. });
  191. }
  192. };
  193. var opt1 = {
  194. "content" : opts.con
  195. };
  196. var opt2 = {
  197. "img_url" : opts.img,
  198. "img_width" : 180,
  199. "img_height" : 180,
  200. "link" : opts.link,
  201. "desc" : opts.con,
  202. "title" : opts.title
  203. };
  204. // var opt3 = $.extend({"appid":"wx21f7e6a981efd178"}, opt2);
  205. handler = {
  206. callback : function(){
  207. }
  208. }
  209. // 执行函数
  210. _api.Share.timeline(opt2,handler);
  211. _api.Share.message(opt2,handler);
  212. _api.Share.weibo(opt1,handler);
  213. return $wx;
  214. }
  215. $.fn.wx.defaults = {
  216. title : '',
  217. con : '',
  218. link : document.URL,
  219. img : 'http://mat1.gtimg.com/news/2014/zt/mh17/you/images/share180.png'
  220. };
  221. $.fn.wx.version = '1.0.0';
  222. })(jQuery);
  223. /* |xGv00|e219e80e98159548066e1b1146634d91 */