wechat.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /**
  2. * 微信网页端调用JS(官方于微信6.0.2版本发布新版JSAPI接口,此接口文件废弃)
  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.showOptionMenu() 显示右上角按钮
  11. * WeixinJS.hideToolbar() 隐藏工具栏
  12. * WeixinJS.showToolbar() 显示工具栏
  13. * WeixinJS.getNetworkType() 获取网络状态
  14. * WeixinJS.closeWindow() 关闭窗口
  15. * WeixinJS.scanQRCode() 扫描二维码
  16. * WeixinJS.openUrlByExtBrowser(url) 使用浏览器打开网址
  17. * WeixinJS.jumpToBizProfile(username) 跳转到指定公众账号页面
  18. * WeixinJS.sendEmail(title,content) 发送邮件
  19. * WeixinJS.openProductView(latitude,longitude,name,address,scale,infoUrl) 查看地图
  20. * WeixinJS.addContact(username) 添加微信账号
  21. * WeixinJS.imagePreview(urls,current) 调出微信内图片预览
  22. * WeixinJS.payCallback(appId,package,timeStamp,nonceStr,signType,paySign,callback) 微信JsApi支付接口
  23. * WeixinJS.editAddress(appId,addrSign,timeStamp,nonceStr,callback) 微信JsApi支付接口
  24. * 自定义分享内容数据格式:
  25. * var dataForWeixin={
  26. appId:"",
  27. MsgImg:"消息图片路径",
  28. TLImg:"时间线图路径",
  29. url:"分享url路径",
  30. title:"标题",
  31. desc:"描述",
  32. fakeid:"",
  33. prepare:function(argv){
  34. if (typeof argv.shareTo!='undefined')
  35. switch(argv.shareTo) {
  36. case 'friend':
  37. //发送给朋友
  38. alert(argv.scene); //friend
  39. break;
  40. case 'timeline':
  41. //发送给朋友
  42. break;
  43. case 'weibo':
  44. //发送到微博
  45. alert(argv.url);
  46. break;
  47. case 'favorite':
  48. //收藏
  49. alert(argv.scene);//favorite
  50. break;
  51. case 'connector':
  52. //分享到第三方应用
  53. alert(argv.scene);//connector
  54. break;
  55. default:
  56. }
  57. },
  58. callback:function(res){
  59. //发送给好友或应用
  60. if (res.err_msg=='send_app_msg:confirm') {
  61. //todo:func1();
  62. alert(res.err_desc);
  63. }
  64. if (res.err_msg=='send_app_msg:cancel') {
  65. //todo:func2();
  66. alert(res.err_desc);
  67. }
  68. //分享到朋友圈
  69. if (res.err_msg=='share_timeline:ok') {
  70. //todo:func1();
  71. alert(res.err_desc);
  72. }
  73. if (res.err_msg=='share_timeline:cancel') {
  74. //todo:func1();
  75. alert(res.err_desc);
  76. }
  77. //分享到微博
  78. if (res.err_msg=='share_weibo:confirm') {
  79. //todo:func1();
  80. alert(res.err_desc);
  81. }
  82. if (res.err_msg=='share_weibo:cancel') {
  83. //todo:func1();
  84. alert(res.err_desc);
  85. }
  86. //收藏或分享到应用
  87. if (res.err_msg=='send_app_msg:ok') {
  88. //todo:func1();
  89. alert(res.err_desc);
  90. }
  91. }
  92. };
  93. */
  94. WeixinJS = typeof WeixinJS!='undefined' || {};
  95. //隐藏右上角按钮
  96. WeixinJS.hideOptionMenu = function() {
  97. document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
  98. if (typeof WeixinJSBridge!='undefined') WeixinJSBridge.call('hideOptionMenu');
  99. });
  100. };
  101. //显示右上角按钮
  102. WeixinJS.showOptionMenu = function() {
  103. document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
  104. if (typeof WeixinJSBridge!='undefined') WeixinJSBridge.call('showOptionMenu');
  105. });
  106. };
  107. //隐藏底部导航栏
  108. WeixinJS.hideToolbar = function() {
  109. document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
  110. if (typeof WeixinJSBridge!='undefined') WeixinJSBridge.call('hideToolbar');
  111. });
  112. };
  113. //显示底部导航栏
  114. WeixinJS.showToolbar = function() {
  115. document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
  116. if (typeof WeixinJSBridge!='undefined') WeixinJSBridge.call('showToolbar');
  117. });
  118. };
  119. //网页获取用户网络状态
  120. netType={"network_type:wifi":"wifi网络","network_type:edge":"非wifi,包含3G/2G","network_type:fail":"网络断开连接","network_type:wwan":"2g或者3g"};
  121. WeixinJS.getNetworkType = function(callback) {
  122. document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
  123. if (typeof WeixinJSBridge!='undefined') WeixinJSBridge.invoke('getNetworkType',{},
  124. function(res){
  125. //result: network_type:wifi,network_type:edge,network_type:fail,network_type:wwan
  126. //netType[e.err_msg]
  127. callback(res.err_msg);
  128. });
  129. });
  130. };
  131. //关闭窗口
  132. WeixinJS.closeWindow = function() {
  133. if (typeof WeixinJSBridge!='undefined') WeixinJSBridge.invoke("closeWindow", {});
  134. };
  135. //扫描二维码
  136. WeixinJS.scanQRCode = function() {
  137. if (typeof WeixinJSBridge!='undefined') WeixinJSBridge.invoke("scanQRCode", {});
  138. };
  139. //使用浏览器打开网址
  140. WeixinJS.openUrlByExtBrowser=function(url){
  141. if (typeof WeixinJSBridge!='undefined') WeixinJSBridge.invoke("openUrlByExtBrowser",{"url" : url});
  142. };
  143. //跳转到指定公众账号页面
  144. WeixinJS.jumpToBizProfile=function(username){
  145. if (typeof WeixinJSBridge!='undefined') WeixinJSBridge.invoke("jumpToBizProfile",{"tousername" : username});
  146. };
  147. //发送邮件
  148. WeixinJS.sendEmail=function(title,content){
  149. if (typeof WeixinJSBridge!='undefined') WeixinJSBridge.invoke("sendEmail",{
  150. "title" : title,
  151. "content" : content
  152. });
  153. };
  154. //查看地图
  155. WeixinJS.openProductView=function(latitude,longitude,name,address,scale,infoUrl){
  156. if (typeof WeixinJSBridge!='undefined') WeixinJSBridge.invoke("openProductView",{
  157. "latitude" : latitude, //纬度
  158. "longitude" : longitude, //经度
  159. "name" : name, //名称
  160. "address" : address, //地址
  161. "scale" : scale, //地图缩放级别
  162. "infoUrl" : infoUrl, //查看位置界面底部的超链接
  163. });
  164. };
  165. //添加微信账号
  166. WeixinJS.addContact=function weixinAddContact(username){
  167. if (typeof WeixinJSBridge!='undefined') WeixinJSBridge.invoke("addContact", {
  168. "webtype": "1",
  169. "username": username
  170. }, function(e) {
  171. WeixinJSBridge.log(e.err_msg);
  172. //e.err_msg:add_contact:added 已经添加
  173. //e.err_msg:add_contact:cancel 取消添加
  174. //e.err_msg:add_contact:ok 添加成功
  175. if(e.err_msg == 'add_contact:added' || e.err_msg == 'add_contact:ok'){
  176. //关注成功,或者已经关注过
  177. }
  178. });
  179. };
  180. /**
  181. * 调出微信内图片预览scrollview
  182. * @param array urls 图片url数组
  183. * @param string current 当前图片url
  184. */
  185. WeixinJS.imagePreview = function(urls,current) {
  186. if (typeof WeixinJSBridge!='undefined')
  187. WeixinJSBridge.invoke("imagePreview", {
  188. current: current,
  189. urls: urls
  190. });
  191. };
  192. //微信JsApi支付接口
  193. WeixinJS.payCallback = function(appId,package,timeStamp,nonceStr,signType,paySign,callback){
  194. if (typeof WeixinJSBridge!='undefined')
  195. WeixinJSBridge.invoke('getBrandWCPayRequest',{
  196. "appId" : appId.toString(),
  197. "timeStamp" : timeStamp.toString(),
  198. "nonceStr" : nonceStr.toString(),
  199. "package" : package.toString(),
  200. "signType" : signType.toString(),
  201. "paySign" : paySign.toString()
  202. },function(res){
  203. // res.err_msg == "get_brand_wcpay_request:ok" return true;
  204. // res.err_msg == "get_brand_wcpay_request:cancel" return false;
  205. callback(res);
  206. });
  207. };
  208. //编辑收货地址
  209. WeixinJS.editAddress = function(appId,addrSign,timeStamp,nonceStr,callback){
  210. var postdata = {
  211. "appId" : appId.toString(),
  212. "scope" : "jsapi_address",
  213. "signType" : "sha1",
  214. "addrSign" : addrSign.toString(),
  215. "timeStamp" : timeStamp.toString(),
  216. "nonceStr" : nonceStr.toString()
  217. };
  218. if (typeof WeixinJSBridge!='undefined')
  219. WeixinJSBridge.invoke('editAddress',postdata, function(res){
  220. //return res.proviceFirstStageName,res.addressCitySecondStageName,res.addressCountiesThirdStageName,res.addressDetailInfo,res.userName,res.addressPostalCode,res.telNumber
  221. //error return res.err_msg
  222. callback(res);
  223. });
  224. };
  225. (function(){
  226. if (typeof dataForWeixin=="undefined") return;
  227. var onBridgeReady=function(){
  228. WeixinJSBridge.on('menu:share:appmessage', function(argv){
  229. (dataForWeixin.prepare)(argv);
  230. WeixinJSBridge.invoke('sendAppMessage',{
  231. "appid":dataForWeixin.appId,
  232. "img_url":dataForWeixin.MsgImg,
  233. "img_width":"120",
  234. "img_height":"120",
  235. "link":dataForWeixin.url,
  236. "desc":dataForWeixin.desc,
  237. "title":dataForWeixin.title
  238. }, function(res){(dataForWeixin.callback)(res);});
  239. });
  240. WeixinJSBridge.on('menu:share:timeline', function(argv){
  241. (dataForWeixin.prepare)(argv);
  242. WeixinJSBridge.invoke('shareTimeline',{
  243. "img_url":dataForWeixin.TLImg,
  244. "img_width":"120",
  245. "img_height":"120",
  246. "link":dataForWeixin.url,
  247. "desc":dataForWeixin.desc,
  248. "title":dataForWeixin.title
  249. }, function(res){(dataForWeixin.callback)(res);});
  250. });
  251. WeixinJSBridge.on('menu:share:weibo', function(argv){
  252. (dataForWeixin.prepare)(argv);
  253. WeixinJSBridge.invoke('shareWeibo',{
  254. "content":dataForWeixin.title,
  255. "url":dataForWeixin.url
  256. }, function(res){(dataForWeixin.callback)(res);});
  257. });
  258. WeixinJSBridge.on('menu:share:facebook', function(argv){
  259. (dataForWeixin.prepare)(argv);
  260. WeixinJSBridge.invoke('shareFB',{
  261. "img_url":dataForWeixin.TLImg,
  262. "img_width":"120",
  263. "img_height":"120",
  264. "link":dataForWeixin.url,
  265. "desc":dataForWeixin.desc,
  266. "title":dataForWeixin.title
  267. }, function(res){(dataForWeixin.callback)(res);});
  268. });
  269. };
  270. if(document.addEventListener){
  271. document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
  272. }else if(document.attachEvent){
  273. document.attachEvent('WeixinJSBridgeReady' , onBridgeReady);
  274. document.attachEvent('onWeixinJSBridgeReady' , onBridgeReady);
  275. }
  276. })();