1. 1\导入核心文件
    2. http://res.wx.qq.com/open/js/jweixin-1.0.0.js
    3. 调用方式
    4. <script type="text/javascript">
    5. $.getScript('http://res.wx.qq.com/open/js/jweixin-1.0.0.js', function(){
    6. $.getScript('http://m.to8to.com/js/wx.js?20150401', function(){
    7. /*********** 微信分享 ************/
    8. try {
    9. var shareConfig = {
    10. title: '【号外】七夕赶上了七周年,土巴兔怒砸百万,还不快约!',
    11. desc: '老板疯了,土巴兔APP七夕派大奖,敢来就敢送,就怕你不来~',
    12. link: 'http://mobileapi.to8to.com/index.php?module=lottery&action=index&version=2.5',// 分享链接
    13. imgUrl: 'http://img.to8to.com/front_end/pic/app_share_weixin_love.jpg' // 分享图标
    14. };
    15. to8toWx && to8toWx.init({
    16. onReady: function() {
    17. // 分享到朋友圈
    18. to8toWx.exec('onMenuShareTimeline', shareConfig);
    19. // 发送给朋友
    20. to8toWx.exec('onMenuShareAppMessage', shareConfig);
    21. // 分享到QQ
    22. to8toWx.exec('onMenuShareQQ', shareConfig);
    23. }
    24. });
    25. } catch (e) {}
    26. /*********** 微信分享 ************/
    27. });
    28. });
    29. </script>
    30. ////////////////////wx.js/////////////////////
    31. /**
    32. * Created by carl.wu on 15-2-7.
    33. */
    34. var to8toWx = to8toWx || {};
    35. // 微信API 列表
    36. to8toWx.apiList = [
    37. 'onMenuShareTimeline',
    38. 'onMenuShareAppMessage',
    39. 'onMenuShareQQ',
    40. 'onMenuShareWeibo',
    41. 'startRecord',
    42. 'stopRecord',
    43. 'onVoiceRecordEnd',
    44. 'playVoice',
    45. 'pauseVoice',
    46. 'stopVoice',
    47. 'onVoicePlayEnd',
    48. 'uploadVoice',
    49. 'downloadVoice',
    50. 'chooseImage',
    51. 'previewImage',
    52. 'uploadImage',
    53. 'downloadImage',
    54. 'translateVoice',
    55. 'getNetworkType',
    56. 'openLocation',
    57. 'getLocation',
    58. 'hideOptionMenu',
    59. 'showOptionMenu',
    60. 'hideMenuItems',
    61. 'showMenuItems',
    62. 'hideAllNonBaseMenuItem',
    63. 'showAllNonBaseMenuItem',
    64. 'closeWindow',
    65. 'scanQRCode',
    66. 'chooseWXPay',
    67. 'openProductSpecificView',
    68. 'addCard',
    69. 'chooseCard',
    70. 'openCard'
    71. ];
    72. // 异步获取签名参数url
    73. to8toWx.ajaxUrl = 'http://m.to8to.com/hfive/getsignstr/';
    74. /**
    75. * 初始化微信JS
    76. * @param opts
    77. * @returns {to8toWx}
    78. */
    79. to8toWx.init = function(opts) {
    80. var _this = this;
    81. opts = $.extend({}, opts);
    82. // 获取签名串
    83. getSignature(function(configs) {
    84. // 初始化微信配置
    85. var wxConfig = {
    86. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
    87. appId: configs.appId, // 必填,公众号的唯一标识
    88. timestamp: configs.timestamp, // 必填,生成签名的时间戳
    89. nonceStr: configs.nonceStr, // 必填,生成签名的随机串
    90. signature: configs.signature,// 必填,签名,见附录1
    91. jsApiList: to8toWx.apiList // 必填,需要使用的JS接口列表
    92. };
    93. // 设置微信配置
    94. wx.config(wxConfig);
    95. // 微信初始化callback
    96. wx.ready(function(){
    97. if (typeof opts.onReady === 'function') {
    98. opts.onReady.call(_this);
    99. }
    100. });
    101. });
    102. return this;
    103. }
    104. /**
    105. * 执行WX api列表中的方法
    106. * 有些方法必须在wx.ready中执行,有些不受限制
    107. *
    108. * @param apiName
    109. * @param opt
    110. * @returns {to8toWx}
    111. */
    112. to8toWx.exec = function(apiName, opt) {
    113. try {
    114. wx[apiName](opt);
    115. } catch (e) {
    116. throw new Error('方法'+apiName+'不存在!');
    117. }
    118. return this;
    119. }
    120. /**
    121. * 获取签名串
    122. * 支持同步和异步
    123. * @param callback
    124. */
    125. function getSignature(callback) {
    126. if (typeof to8toWxsignPackage !== 'undefined') {
    127. if (typeof callback === 'function') {
    128. callback(to8toWxsignPackage);
    129. } else {
    130. return to8toWxsignPackage;
    131. }
    132. } else {
    133. // 修改为jsonp方式获取数据
    134. $.ajax({
    135. url: to8toWx.ajaxUrl,
    136. 'type': 'GET',
    137. 'dataType': 'jsonp',
    138. 'data': {"url": encodeURIComponent(location.href.split('#')[0])},
    139. 'success': function(configs) {
    140. if (typeof callback === 'function') {
    141. callback(configs);
    142. }
    143. }
    144. });
    145. //$.post(to8toWx.ajaxUrl, {
    146. // url: encodeURIComponent(location.href.split('#')[0])
    147. //},function(configs) {
    148. // if (typeof callback === 'function') {
    149. // callback(configs);
    150. // }
    151. //}, 'json');
    152. }
    153. }