wechat.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * 微信网页端调用JS
  3. * @author dodge
  4. * @contact dodgepudding@gmail.com
  5. * @link http://blog.4wer.com/wechat-timeline-share
  6. * @version 1.1
  7. *
  8. * 自定义分享使用:
  9. * WeixinJS.hideOptionMenu() 隐藏右上角按钮
  10. * WeixinJS.hideToolbar() 隐藏工具栏
  11. * 自定义分享内容数据格式:
  12. * var dataForWeixin={
  13. appId:"",
  14. MsgImg:"消息图片路径",
  15. TLImg:"时间线图路径",
  16. url:"分享url路径",
  17. title:"标题",
  18. desc:"描述",
  19. fakeid:"",
  20. prepare:function(argv){
  21. if (typeof argv.shareTo!='undefined')
  22. switch(argv.shareTo) {
  23. case 'friend':
  24. //发送给朋友
  25. alert(argv.scene); //friend
  26. break;
  27. case 'timeline':
  28. //发送给朋友
  29. break;
  30. case 'weibo':
  31. //发送到微博
  32. alert(argv.url);
  33. break;
  34. case 'favorite':
  35. //收藏
  36. alert(argv.scene);//favorite
  37. break;
  38. case 'connector':
  39. //分享到第三方应用
  40. alert(argv.scene);//connector
  41. break;
  42. default:
  43. }
  44. },
  45. callback:function(res){
  46. //发送给好友或应用
  47. if (res.err_msg=='send_app_msg:confirm') {
  48. //todo:func1();
  49. alert(res.err_desc);
  50. }
  51. if (res.err_msg=='send_app_msg:cancel') {
  52. //todo:func2();
  53. alert(res.err_desc);
  54. }
  55. //分享到朋友圈
  56. if (res.err_msg=='share_timeline:confirm') {
  57. //todo:func1();
  58. alert(res.err_desc);
  59. }
  60. if (res.err_msg=='share_timeline:cancel') {
  61. //todo:func1();
  62. alert(res.err_desc);
  63. }
  64. //分享到微博
  65. if (res.err_msg=='share_weibo:confirm') {
  66. //todo:func1();
  67. alert(res.err_desc);
  68. }
  69. if (res.err_msg=='share_weibo:cancel') {
  70. //todo:func1();
  71. alert(res.err_desc);
  72. }
  73. //收藏或分享到应用
  74. if (res.err_msg=='send_app_msg:ok') {
  75. //todo:func1();
  76. alert(res.err_desc);
  77. }
  78. }
  79. };
  80. */
  81. WeixinJS = typeof WeixinJS!='undefined' || {};
  82. WeixinJS.hideOptionMenu = function() {
  83. document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
  84. if (typeof WeixinJSBridge!='undefined') WeixinJSBridge.call('hideOptionMenu');
  85. });
  86. };
  87. WeixinJS.hideToolbar = function() {
  88. document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
  89. if (typeof WeixinJSBridge!='undefined') WeixinJSBridge.call('hideToolbar');
  90. });
  91. };
  92. WeixinJS.getNetworkType = function(callback) {
  93. document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
  94. if (typeof WeixinJSBridge!='undefined') WeixinJSBridge.invoke('getNetworkType',{},
  95. function(res){
  96. //result: network_type:wifi,network_type:edge,network_type:fail,network_type:wwan
  97. callback(res.err_msg);
  98. });
  99. });
  100. };
  101. WeixinJS.closeWindow = function() {
  102. if (typeof WeixinJSBridge!='undefined') WeixinJSBridge.invoke("closeWindow", {});
  103. };
  104. WeixinJS.payCallback = function(appId,package,timeStamp,nonceStr,signType,paySign,callback){
  105. if (typeof WeixinJSBridge!='undefined')
  106. WeixinJSBridge.invoke('getBrandWCPayRequest',{
  107. "appId" : appId.toString(),
  108. "timeStamp" : timeStamp.toString(),
  109. "nonceStr" : nonceStr.toString(),
  110. "package" : package.toString(),
  111. "signType" : signType.toString(),
  112. "paySign" : paySign.toString()
  113. },function(res){
  114. // res.err_msg == "get_brand_wcpay_request:ok" return true;
  115. // res.err_msg == "get_brand_wcpay_request:cancel" return false;
  116. callback(res);
  117. });
  118. };
  119. WeixinJS.editAddress = function(appId,addrSign,timeStamp,nonceStr,callback){
  120. var postdata = {
  121. "appId" : appId.toString(),
  122. "scope" : "jsapi_address",
  123. "signType" : "sha1",
  124. "addrSign" : addrSign.toString(),
  125. "timeStamp" : timeStamp.toString(),
  126. "nonceStr" : nonceStr.toString()
  127. };
  128. if (typeof WeixinJSBridge!='undefined')
  129. WeixinJSBridge.invoke('editAddress',postdata, function(res){
  130. //return res.proviceFirstStageName,res.addressCitySecondStageName,res.addressCountiesThirdStageName,res.addressDetailInfo,res.userName,res.addressPostalCode,res.telNumber
  131. //error return res.err_msg
  132. callback(res);
  133. });
  134. };
  135. (function(){
  136. var onBridgeReady=function(){
  137. WeixinJSBridge.on('menu:share:appmessage', function(argv){
  138. (dataForWeixin.prepare)(argv);
  139. WeixinJSBridge.invoke('sendAppMessage',{
  140. "appid":dataForWeixin.appId,
  141. "img_url":dataForWeixin.MsgImg,
  142. "img_width":"120",
  143. "img_height":"120",
  144. "link":dataForWeixin.url,
  145. "desc":dataForWeixin.desc,
  146. "title":dataForWeixin.title
  147. }, function(res){(dataForWeixin.callback)(res);});
  148. });
  149. WeixinJSBridge.on('menu:share:timeline', function(argv){
  150. (dataForWeixin.prepare)(argv);
  151. WeixinJSBridge.invoke('shareTimeline',{
  152. "img_url":dataForWeixin.TLImg,
  153. "img_width":"120",
  154. "img_height":"120",
  155. "link":dataForWeixin.url,
  156. "desc":dataForWeixin.desc,
  157. "title":dataForWeixin.title
  158. }, function(res){(dataForWeixin.callback)(res);});
  159. });
  160. WeixinJSBridge.on('menu:share:weibo', function(argv){
  161. (dataForWeixin.prepare)(argv);
  162. WeixinJSBridge.invoke('shareWeibo',{
  163. "content":dataForWeixin.title,
  164. "url":dataForWeixin.url
  165. }, function(res){(dataForWeixin.callback)(res);});
  166. });
  167. WeixinJSBridge.on('menu:share:facebook', function(argv){
  168. (dataForWeixin.prepare)(argv);
  169. WeixinJSBridge.invoke('shareFB',{
  170. "img_url":dataForWeixin.TLImg,
  171. "img_width":"120",
  172. "img_height":"120",
  173. "link":dataForWeixin.url,
  174. "desc":dataForWeixin.desc,
  175. "title":dataForWeixin.title
  176. }, function(res){(dataForWeixin.callback)(res);});
  177. });
  178. };
  179. if(document.addEventListener){
  180. document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
  181. }else if(document.attachEvent){
  182. document.attachEvent('WeixinJSBridgeReady' , onBridgeReady);
  183. document.attachEvent('onWeixinJSBridgeReady' , onBridgeReady);
  184. }
  185. })();