wechat-utils-1.0.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /**
  2. * @author 何秀刚
  3. * @description wechat 工具类,依赖JQuery,如果引入此文件,自动绑定分享事件。
  4. * @version V.1.0
  5. */
  6. // 获取绑定微信JSSDK的配置参数
  7. var getConfigUrl = "/watero/config/get";
  8. // JSSDK是否绑定成功
  9. var jssdkSuccess = true;
  10. //是否隐藏全局菜单
  11. if (window.hiddenAllWechatMenu == null) {
  12. window.hiddenAllWechatMenu = false;
  13. }
  14. // 分享信息全局变量
  15. window.wxFriend = null;
  16. if (window.repleFlag) {
  17. // 自定义
  18. window.wxFriend = window.repleWxData;
  19. } else {
  20. // 使用全局默认
  21. window.wxFriend = {
  22. "appId": "",
  23. "imgUrl":'http://m.iamberry.com/common/goods/images/indexShare.jpg',
  24. "link":"http://www.iamberry.com/ratfw/index",
  25. "desc":"这里是分享描述",
  26. "title":"这里是分享标题",
  27. "netError":"您的网络异常,请刷新重试!如多次刷新无效,请向我们反应 ~",
  28. "shareTimeSuccess":"这里是分享朋友圈后调用的文字",
  29. "shareTimeCancel":"这里是取消分享给朋友圈后调用的文字",
  30. "shareAppSuccess":"这里是分享好友后调用的文字",
  31. "shareAppCancel":"这里是取消分享给好友后调用的文字"
  32. };
  33. }
  34. // 微信共享地址数据
  35. window.wechatAddr = {
  36. "status":false, // 是否调用成功,true成功,false失败
  37. "statusMsg":"失败时信息",
  38. "detailInfo":"详细地址",
  39. "postalCode":"邮编",
  40. "userName":"收货人",
  41. "userTel":"收货人电话",
  42. "nationalCode":"收货地址国家码"
  43. };
  44. /** 引入JSSDK1.0基本组件,微信共享地址智能使用1.0版本,并且需要服务器返回参数支持 */
  45. //incoldeJSFile("http://res.wx.qq.com/open/js/jweixin-1.0.0.js");
  46. /** 引入JSSDK1.1基本组件 ,支持摇一摇、微信共享地址 */
  47. incoldeJSFile("http://res.wx.qq.com/open/js/jweixin-1.1.0.js");
  48. getConfig(); // 配置JSSDK组件
  49. /** 获取服务器JSSDK参数配置 */
  50. function getConfig(){
  51. $.ajax({
  52. xhrFields: {
  53. withCredentials: true
  54. },
  55. type: 'get', url: getConfigUrl, data: {appid: "", url: location.href,"date":new Date().getTime()}, dataType: 'json', timeout: 10000, context: $('body'),
  56. success: function(data){setConfig(data);},
  57. error: function(xhr, type){getConfig();}
  58. });
  59. }
  60. /**配置JS-SDK*/
  61. function setConfig(config){
  62. wx.config({
  63. debug: false,
  64. appId: config.appId,
  65. timestamp: config.timestamp,
  66. nonceStr: config.nonceStr,
  67. signature: config.signature,
  68. jsApiList: [
  69. 'checkJsApi',
  70. 'onMenuShareTimeline',
  71. 'onMenuShareAppMessage',
  72. 'hideMenuItems',
  73. 'scanQRCode',
  74. 'showMenuItems'
  75. ]
  76. });
  77. wx.ready(function(){ //JSSDK绑定成功
  78. wxReadySuccess();
  79. });
  80. wx.error(function(res){ // JSSDK绑定失败的方法
  81. wxReadyError();
  82. });
  83. }
  84. /** JSSDK绑定成功调用事件 */
  85. function wxReadySuccess() {
  86. window.jssdkSuccess = true;
  87. shareMessage();
  88. wx.getNetworkType({success: function (res) {}});
  89. if (window.hiddenAllWechatMenu) {
  90. wx.showMenuItems({
  91. menuList: [
  92. ] // 要显示的菜单项,所有menu项见附录3
  93. });
  94. wx.hideMenuItems({
  95. menuList: [
  96. "menuItem:copyUrl",
  97. "menuItem:openWithSafari",
  98. "menuItem:originPage",
  99. "menuItem:share:email",
  100. "menuItem:dayMode",
  101. "menuItem:setFont",
  102. "menuItem:nightMode",
  103. "menuItem:share:qq",
  104. "menuItem:dayMode",
  105. "menuItem:exposeArticle",
  106. "menuItem:nightMode",
  107. "menuItem:openWithQQBrowser",
  108. "menuItem:share:QZone",
  109. "menuItem:favorite",
  110. "menuItem:share:facebook",
  111. "menuItem:share:appMessage",
  112. "menuItem:share:timeline"
  113. ] // 要隐藏的菜单项,所有menu项见附录3
  114. });
  115. } else {
  116. wx.showMenuItems({
  117. menuList: [
  118. "menuItem:share:appMessage",
  119. "menuItem:share:timeline"
  120. ] // 要显示的菜单项,所有menu项见附录3
  121. });
  122. wx.hideMenuItems({
  123. menuList: [
  124. "menuItem:copyUrl",
  125. "menuItem:openWithSafari",
  126. "menuItem:originPage",
  127. "menuItem:share:email",
  128. "menuItem:dayMode",
  129. "menuItem:setFont",
  130. "menuItem:nightMode",
  131. "menuItem:share:qq",
  132. "menuItem:dayMode",
  133. "menuItem:exposeArticle",
  134. "menuItem:nightMode",
  135. "menuItem:openWithQQBrowser",
  136. "menuItem:share:QZone",
  137. "menuItem:favorite",
  138. "menuItem:share:facebook"
  139. ] // 要隐藏的菜单项,所有menu项见附录3
  140. });
  141. }
  142. }
  143. /** JSSDK 绑定失败调用方法 */
  144. function wxReadyError() {
  145. // alert(wxFriend.netError);
  146. }
  147. /** 分享时、分享后调用方法 */
  148. function shareMessage(title, message) {
  149. title = title || wxFriend.title;
  150. message = message || wxFriend.desc;
  151. wx.onMenuShareTimeline({ //分享到朋友圈,只有标题、图标,没有内容简介
  152. title: title, link: wxFriend.link, imgUrl: wxFriend.imgUrl,
  153. success: function () {
  154. if (wxFriend.shareAppSuccess != null && wxFriend.shareAppSuccess != "") {
  155. // alert(wxFriend.shareAppSuccess);
  156. }
  157. },
  158. cancel: function () {
  159. if (wxFriend.shareTimeCancel != null && wxFriend.shareTimeCancel != "") {
  160. // alert(wxFriend.shareTimeCancel);
  161. }
  162. }
  163. });
  164. wx.onMenuShareAppMessage({ // 分享给好友,有标题、内容简介、图标
  165. title: title, desc: message, link: wxFriend.link, imgUrl: wxFriend.imgUrl,
  166. success: function () {
  167. if (wxFriend.shareAppSuccess != null && wxFriend.shareAppSuccess != "") {
  168. // alert(wxFriend.shareAppSuccess);
  169. }
  170. },
  171. cancel: function () {
  172. if (wxFriend.shareTimeCancel != null && wxFriend.shareTimeCancel != "") {
  173. // alert(wxFriend.shareTimeCancel);
  174. }
  175. }
  176. });
  177. }
  178. /**
  179. * 微信支付
  180. * str:微信支付参数,字符串
  181. * paySuccessCallBack:微信支付成功后回调方法,此方法必须为全局时,方可以回调成功
  182. * payCancelCallBack:微信支付失败后回调方法,此方法必须为全局时,方可以回调成功
  183. */
  184. function wechatPay(str, paySuccessCallBack, payCancelCallBack) {
  185. if (typeof WeixinJSBridge == "undefined"){
  186. alert("非微信浏览器!无法发起微信支付请求!");
  187. return;
  188. }
  189. var obj = JSON.parse("{" + str + "}");
  190. WeixinJSBridge.invoke('getBrandWCPayRequest', obj, function(res) {
  191. if(res.err_msg=='get_brand_wcpay_request:ok'){
  192. paySuccessCallBack
  193. }else{
  194. payCancelCallBack
  195. }
  196. });
  197. }
  198. if (typeof WeixinJSBridge == "undefined"){
  199. if( document.addEventListener ){
  200. document.addEventListener('WeixinJSBridgeReady', wechatPay, false);
  201. }else if (document.attachEvent){
  202. document.attachEvent('WeixinJSBridgeReady', wechatPay);
  203. document.attachEvent('onWeixinJSBridgeReady', wechatPay);
  204. }
  205. }
  206. /** 发起微信共享地址Version 1.0 */
  207. function editAddrV0(config) {
  208. WeixinJSBridge.invoke('editAddress', {"appId" : config.appId, "scope" : "jsapi_address", "signType" : "SHA1", "addrSign" : config.addrSign, "timeStamp" : config.timestamp, "nonceStr" : config.nonceStr},
  209. function(res) {
  210. if ("edit_address:ok" == res.err_msg) {
  211. var address = res.proviceFirstStageName + "-" + res.addressCitySecondStageName + "-" + res.addressCountiesThirdStageName + "-" + res.addressDetailInfo;
  212. wechatAddr.status = true;
  213. wechatAddr.detailInfo = address;
  214. wechatAddr.postalCode = res.addressPostalCode;
  215. wechatAddr.userName = res.userName;
  216. wechatAddr.userTel = res.telNumber;
  217. return wechatAddr;
  218. } else if ("edit_address:fail" == res.err_msg) {// 没有选择
  219. wechatAddr.statusMsg = "您没有选择收货地址!";
  220. return wechatAddr;
  221. } else { // 参数过时,请刷新
  222. wechatAddr.statusMsg = "操作过时,请刷新重试!";
  223. return wechatAddr;
  224. }
  225. }
  226. );
  227. }
  228. /**
  229. * 发起微信共享地址Version 1.1<br>
  230. * {<br>
  231. * status : 状态,true:请求成功,false:请求失败<br>
  232. * errorInfo : 请求失败时的提示信息<br>
  233. * res: { 请求成功时的地址信息,失败为空<br>
  234. * userName : 收货人姓名。<br>
  235. * postalCode : 邮编。<br>
  236. * provinceName 国标收货地址第一级地址(省)。<br>
  237. * cityName : 国标收货地址第二级地址(市)。<br>
  238. * countryName : 国标收货地址第三级地址(区)。<br>
  239. * detailInfo : 详细收货地址信息。<br>
  240. * nationalCode : 收货地址国家码。<br>
  241. * telNumber : 收货人电话<br>
  242. * }
  243. * }
  244. */
  245. function editAddrV1(successCallback, errorCallback) {
  246. if (!window.jssdkSuccess) {
  247. mui.alert("错误啦 ~~ 请刷新重试!");
  248. return;
  249. }
  250. wx.openAddress({
  251. success: successCallback,
  252. cancel: errorCallback
  253. });
  254. }
  255. /** 动态引入wechat utils JavaScript */
  256. function incoldeJSFile(jsUrl) {
  257. var typeHand = document.getElementsByTagName('head').item(0);
  258. var typeScript= document.createElement("script");
  259. typeScript.type = "text/javascript";
  260. typeScript.src=jsUrl;
  261. typeHand.appendChild(typeScript);
  262. }