wxApi.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. !function(a,b){"function"==typeof define&&(define.amd||define.cmd)?define(function(){return b(a)}):b(a,!0)}(this,function(a,b){function c(b,c,d){a.WeixinJSBridge?WeixinJSBridge.invoke(b,e(c),function(a){h(b,a,d)}):k(b,d)}function d(b,c,d){a.WeixinJSBridge?WeixinJSBridge.on(b,function(a){d&&d.trigger&&d.trigger(a),h(b,a,c)}):d?k(b,d):k(b,c)}function e(a){return a=a||{},a.appId=D.appId,a.verifyAppId=D.appId,a.verifySignType="sha1",a.verifyTimestamp=D.timestamp+"",a.verifyNonceStr=D.nonceStr,a.verifySignature=D.signature,a}function f(a){return{timeStamp:a.timestamp+"",nonceStr:a.nonceStr,"package":a["package"],paySign:a.paySign,signType:a.signType||"SHA1"}}function g(a){return a.postalCode=a.addressPostalCode,delete a.addressPostalCode,a.provinceName=a.proviceFirstStageName,delete a.proviceFirstStageName,a.cityName=a.addressCitySecondStageName,delete a.addressCitySecondStageName,a.countryName=a.addressCountiesThirdStageName,delete a.addressCountiesThirdStageName,a.detailInfo=a.addressDetailInfo,delete a.addressDetailInfo,a}function h(a,b,c){"openEnterpriseChat"==a&&(b.errCode=b.err_code),delete b.err_code,delete b.err_desc,delete b.err_detail;var d=b.errMsg;d||(d=b.err_msg,delete b.err_msg,d=i(a,d),b.errMsg=d),c=c||{},c._complete&&(c._complete(b),delete c._complete),d=b.errMsg||"",D.debug&&!c.isInnerInvoke&&alert(JSON.stringify(b));var e=d.indexOf(":"),f=d.substring(e+1);switch(f){case"ok":c.success&&c.success(b);break;case"cancel":c.cancel&&c.cancel(b);break;default:c.fail&&c.fail(b)}c.complete&&c.complete(b)}function i(a,b){var c=a,d=q[c];d&&(c=d);var e="ok";if(b){var f=b.indexOf(":");e=b.substring(f+1),"confirm"==e&&(e="ok"),"failed"==e&&(e="fail"),-1!=e.indexOf("failed_")&&(e=e.substring(7)),-1!=e.indexOf("fail_")&&(e=e.substring(5)),e=e.replace(/_/g," "),e=e.toLowerCase(),("access denied"==e||"no permission to execute"==e)&&(e="permission denied"),"config"==c&&"function not exist"==e&&(e="ok"),""==e&&(e="fail")}return b=c+":"+e}function j(a){if(a){for(var b=0,c=a.length;c>b;++b){var d=a[b],e=p[d];e&&(a[b]=e)}return a}}function k(a,b){if(!(!D.debug||b&&b.isInnerInvoke)){var c=q[a];c&&(a=c),b&&b._complete&&delete b._complete,console.log('"'+a+'",',b||"")}}function l(a){if(!(v||w||D.debug||"6.0.2">A||C.systemType<0)){var b=new Image;C.appId=D.appId,C.initTime=B.initEndTime-B.initStartTime,C.preVerifyTime=B.preVerifyEndTime-B.preVerifyStartTime,I.getNetworkType({isInnerInvoke:!0,success:function(a){C.networkType=a.networkType;var c="https://open.weixin.qq.com/sdk/report?v="+C.version+"&o="+C.isPreVerifyOk+"&s="+C.systemType+"&c="+C.clientVersion+"&a="+C.appId+"&n="+C.networkType+"&i="+C.initTime+"&p="+C.preVerifyTime+"&u="+C.url;b.src=c}})}}function m(){return(new Date).getTime()}function n(b){x&&(a.WeixinJSBridge?b():r.addEventListener&&r.addEventListener("WeixinJSBridgeReady",b,!1))}function o(){I.invoke||(I.invoke=function(b,c,d){a.WeixinJSBridge&&WeixinJSBridge.invoke(b,e(c),d)},I.on=function(b,c){a.WeixinJSBridge&&WeixinJSBridge.on(b,c)})}if(!a.jWeixin){var p={config:"preVerifyJSAPI",onMenuShareTimeline:"menu:share:timeline",onMenuShareAppMessage:"menu:share:appmessage",onMenuShareQQ:"menu:share:qq",onMenuShareWeibo:"menu:share:weiboApp",onMenuShareQZone:"menu:share:QZone",previewImage:"imagePreview",getLocation:"geoLocation",openProductSpecificView:"openProductViewWithPid",addCard:"batchAddCard",openCard:"batchViewCard",chooseWXPay:"getBrandWCPayRequest",openEnterpriseRedPacket:"getRecevieBizHongBaoRequest",startSearchBeacons:"startMonitoringBeacons",stopSearchBeacons:"stopMonitoringBeacons",onSearchBeacons:"onBeaconsInRange",consumeAndShareCard:"consumedShareCard",openAddress:"editAddress"},q=function(){var a={};for(var b in p)a[p[b]]=b;return a}(),r=a.document,s=r.title,t=navigator.userAgent.toLowerCase(),u=navigator.platform.toLowerCase(),v=!(!u.match("mac")&&!u.match("win")),w=-1!=t.indexOf("wxdebugger"),x=-1!=t.indexOf("micromessenger"),y=-1!=t.indexOf("android"),z=-1!=t.indexOf("iphone")||-1!=t.indexOf("ipad"),A=function(){var a=t.match(/micromessenger\/(\d+\.\d+\.\d+)/)||t.match(/micromessenger\/(\d+\.\d+)/);return a?a[1]:""}(),B={initStartTime:m(),initEndTime:0,preVerifyStartTime:0,preVerifyEndTime:0},C={version:1,appId:"",initTime:0,preVerifyTime:0,networkType:"",isPreVerifyOk:1,systemType:z?1:y?2:-1,clientVersion:A,url:encodeURIComponent(location.href)},D={},E={_completes:[]},F={state:0,data:{}};n(function(){B.initEndTime=m()});var G=!1,H=[],I={config:function(a){D=a,k("config",a);var b=D.check===!1?!1:!0;n(function(){if(b)c(p.config,{verifyJsApiList:j(D.jsApiList)},function(){E._complete=function(a){B.preVerifyEndTime=m(),F.state=1,F.data=a},E.success=function(a){C.isPreVerifyOk=0},E.fail=function(a){E._fail?E._fail(a):F.state=-1};var a=E._completes;return a.push(function(){l()}),E.complete=function(b){for(var c=0,d=a.length;d>c;++c)a[c]();E._completes=[]},E}()),B.preVerifyStartTime=m();else{F.state=1;for(var a=E._completes,d=0,e=a.length;e>d;++d)a[d]();E._completes=[]}}),D.beta&&o()},ready:function(a){0!=F.state?a():(E._completes.push(a),!x&&D.debug&&a())},error:function(a){"6.0.2">A||(-1==F.state?a(F.data):E._fail=a)},checkJsApi:function(a){var b=function(a){var b=a.checkResult;for(var c in b){var d=q[c];d&&(b[d]=b[c],delete b[c])}return a};c("checkJsApi",{jsApiList:j(a.jsApiList)},function(){return a._complete=function(a){if(y){var c=a.checkResult;c&&(a.checkResult=JSON.parse(c))}a=b(a)},a}())},onMenuShareTimeline:function(a){d(p.onMenuShareTimeline,{complete:function(){c("shareTimeline",{title:a.title||s,desc:a.title||s,img_url:a.imgUrl||"",link:a.link||location.href,type:a.type||"link",data_url:a.dataUrl||""},a)}},a)},onMenuShareAppMessage:function(a){d(p.onMenuShareAppMessage,{complete:function(){c("sendAppMessage",{title:a.title||s,desc:a.desc||"",link:a.link||location.href,img_url:a.imgUrl||"",type:a.type||"link",data_url:a.dataUrl||""},a)}},a)},onMenuShareQQ:function(a){d(p.onMenuShareQQ,{complete:function(){c("shareQQ",{title:a.title||s,desc:a.desc||"",img_url:a.imgUrl||"",link:a.link||location.href},a)}},a)},onMenuShareWeibo:function(a){d(p.onMenuShareWeibo,{complete:function(){c("shareWeiboApp",{title:a.title||s,desc:a.desc||"",img_url:a.imgUrl||"",link:a.link||location.href},a)}},a)},onMenuShareQZone:function(a){d(p.onMenuShareQZone,{complete:function(){c("shareQZone",{title:a.title||s,desc:a.desc||"",img_url:a.imgUrl||"",link:a.link||location.href},a)}},a)},startRecord:function(a){c("startRecord",{},a)},stopRecord:function(a){c("stopRecord",{},a)},onVoiceRecordEnd:function(a){d("onVoiceRecordEnd",a)},playVoice:function(a){c("playVoice",{localId:a.localId},a)},pauseVoice:function(a){c("pauseVoice",{localId:a.localId},a)},stopVoice:function(a){c("stopVoice",{localId:a.localId},a)},onVoicePlayEnd:function(a){d("onVoicePlayEnd",a)},uploadVoice:function(a){c("uploadVoice",{localId:a.localId,isShowProgressTips:0==a.isShowProgressTips?0:1},a)},downloadVoice:function(a){c("downloadVoice",{serverId:a.serverId,isShowProgressTips:0==a.isShowProgressTips?0:1},a)},translateVoice:function(a){c("translateVoice",{localId:a.localId,isShowProgressTips:0==a.isShowProgressTips?0:1},a)},chooseImage:function(a){c("chooseImage",{scene:"1|2",count:a.count||9,sizeType:a.sizeType||["original","compressed"],sourceType:a.sourceType||["album","camera"]},function(){return a._complete=function(a){if(y){var b=a.localIds;b&&(a.localIds=JSON.parse(b))}},a}())},getLocation:function(a){},previewImage:function(a){c(p.previewImage,{current:a.current,urls:a.urls},a)},uploadImage:function(a){c("uploadImage",{localId:a.localId,isShowProgressTips:0==a.isShowProgressTips?0:1},a)},downloadImage:function(a){c("downloadImage",{serverId:a.serverId,isShowProgressTips:0==a.isShowProgressTips?0:1},a)},getLocalImgData:function(a){G===!1?(G=!0,c("getLocalImgData",{localId:a.localId},function(){return a._complete=function(a){if(G=!1,H.length>0){var b=H.shift();wx.getLocalImgData(b)}},a}())):H.push(a)},getNetworkType:function(a){var b=function(a){var b=a.errMsg;a.errMsg="getNetworkType:ok";var c=a.subtype;if(delete a.subtype,c)a.networkType=c;else{var d=b.indexOf(":"),e=b.substring(d+1);switch(e){case"wifi":case"edge":case"wwan":a.networkType=e;break;default:a.errMsg="getNetworkType:fail"}}return a};c("getNetworkType",{},function(){return a._complete=function(a){a=b(a)},a}())},openLocation:function(a){c("openLocation",{latitude:a.latitude,longitude:a.longitude,name:a.name||"",address:a.address||"",scale:a.scale||28,infoUrl:a.infoUrl||""},a)},getLocation:function(a){a=a||{},c(p.getLocation,{type:a.type||"wgs84"},function(){return a._complete=function(a){delete a.type},a}())},hideOptionMenu:function(a){c("hideOptionMenu",{},a)},showOptionMenu:function(a){c("showOptionMenu",{},a)},closeWindow:function(a){a=a||{},c("closeWindow",{},a)},hideMenuItems:function(a){c("hideMenuItems",{menuList:a.menuList},a)},showMenuItems:function(a){c("showMenuItems",{menuList:a.menuList},a)},hideAllNonBaseMenuItem:function(a){c("hideAllNonBaseMenuItem",{},a)},showAllNonBaseMenuItem:function(a){c("showAllNonBaseMenuItem",{},a)},scanQRCode:function(a){a=a||{},c("scanQRCode",{needResult:a.needResult||0,scanType:a.scanType||["qrCode","barCode"]},function(){return a._complete=function(a){if(z){var b=a.resultStr;if(b){var c=JSON.parse(b);a.resultStr=c&&c.scan_code&&c.scan_code.scan_result}}},a}())},openAddress:function(a){c(p.openAddress,{},function(){return a._complete=function(a){a=g(a)},a}())},openProductSpecificView:function(a){c(p.openProductSpecificView,{pid:a.productId,view_type:a.viewType||0,ext_info:a.extInfo},a)},addCard:function(a){for(var b=a.cardList,d=[],e=0,f=b.length;f>e;++e){var g=b[e],h={card_id:g.cardId,card_ext:g.cardExt};d.push(h)}c(p.addCard,{card_list:d},function(){return a._complete=function(a){var b=a.card_list;if(b){b=JSON.parse(b);for(var c=0,d=b.length;d>c;++c){var e=b[c];e.cardId=e.card_id,e.cardExt=e.card_ext,e.isSuccess=e.is_succ?!0:!1,delete e.card_id,delete e.card_ext,delete e.is_succ}a.cardList=b,delete a.card_list}},a}())},chooseCard:function(a){c("chooseCard",{app_id:D.appId,location_id:a.shopId||"",sign_type:a.signType||"SHA1",card_id:a.cardId||"",card_type:a.cardType||"",card_sign:a.cardSign,time_stamp:a.timestamp+"",nonce_str:a.nonceStr},function(){return a._complete=function(a){a.cardList=a.choose_card_info,delete a.choose_card_info},a}())},openCard:function(a){for(var b=a.cardList,d=[],e=0,f=b.length;f>e;++e){var g=b[e],h={card_id:g.cardId,code:g.code};d.push(h)}c(p.openCard,{card_list:d},a)},consumeAndShareCard:function(a){c(p.consumeAndShareCard,{consumedCardId:a.cardId,consumedCode:a.code},a)},chooseWXPay:function(a){c(p.chooseWXPay,f(a),a)},openEnterpriseRedPacket:function(a){c(p.openEnterpriseRedPacket,f(a),a)},startSearchBeacons:function(a){c(p.startSearchBeacons,{ticket:a.ticket},a)},stopSearchBeacons:function(a){c(p.stopSearchBeacons,{},a)},onSearchBeacons:function(a){d(p.onSearchBeacons,a)},openEnterpriseChat:function(a){c("openEnterpriseChat",{useridlist:a.userIds,chatname:a.groupName},a)}},J=1,K={};return r.addEventListener("error",function(a){if(!y){var b=a.target,c=b.tagName,d=b.src;if("IMG"==c||"VIDEO"==c||"AUDIO"==c||"SOURCE"==c){var e=-1!=d.indexOf("wxlocalresource://");if(e){a.preventDefault(),a.stopPropagation();var f=b["wx-id"];if(f||(f=J++,b["wx-id"]=f),K[f])return;K[f]=!0,wx.ready(function(){wx.getLocalImgData({localId:d,success:function(a){b.src=a.localData}})})}}}},!0),r.addEventListener("load",function(a){if(!y){var b=a.target,c=b.tagName;b.src;if("IMG"==c||"VIDEO"==c||"AUDIO"==c||"SOURCE"==c){var d=b["wx-id"];d&&(K[d]=!1)}}},!0),b&&(a.wx=a.jWeixin=I),I}});
  2. (function (global) {
  3. global.mapleWx = mapleWx(global.wx);
  4. var margin = function(o,n){
  5. for (var p in n){
  6. if(n.hasOwnProperty(p))
  7. o[p]=n[p];
  8. }
  9. return o;
  10. };
  11. function mapleWx(wx) {
  12. 'use strict';
  13. var mapleApi = new _mapleApi();
  14. var jsApiList = ['onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'onMenuShareQZone', 'startRecord', 'stopRecord', 'onVoiceRecordEnd', 'playVoice', 'pauseVoice', 'stopVoice', 'onVoicePlayEnd', 'uploadVoice', 'downloadVoice', 'chooseImage', 'previewImage', 'uploadImage', 'downloadImage', 'translateVoice', 'getNetworkType', 'openLocation', 'getLocation', 'hideOptionMenu', 'showOptionMenu', 'hideMenuItems', 'showMenuItems', 'hideAllNonBaseMenuItem', 'showAllNonBaseMenuItem', 'closeWindow', 'scanQRCode', 'chooseWXPay', 'openProductSpecificView', 'addCard', 'chooseCard', 'openCard'];
  15. function _mapleApi() {
  16. var that = this;
  17. //微信接口初始化
  18. this.init = function (config, readFn, errorFn) {
  19. mapleApi.option.config = config;
  20. mapleApi.option.wx = wx;
  21. wx.config({
  22. debug: config.debug || false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  23. appId: config.appId, // 必填,公众号的唯一标识
  24. timestamp: config.timestamp, // 必填,生成签名的时间戳
  25. nonceStr: config.nonceStr, // 必填,生成签名的随机串
  26. signature: config.signature,// 必填,签名,见附录1
  27. jsApiList: config.jsApiList || jsApiList // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
  28. });
  29. wx.ready(function () {
  30. readFn && readFn.call(mapleApi);
  31. });
  32. wx.error(function (error) {
  33. errorFn && errorFn.call(mapleApi, error);
  34. });
  35. return mapleApi;
  36. };
  37. //隐藏不安全接口
  38. that.hideNonSafetyMenuItem = function () {
  39. var list = ['menuItem:copyUrl', 'menuItem:delete', '', 'menuItem:originPage', 'menuItem:openWithQQBrowser', 'menuItem:openWithSafari', 'menuItem:share:email', 'menuItem:share:brand', 'menuItem:delete', 'menuItem:editTag'];
  40. that.hideMenuItems(list);
  41. };
  42. //一键配置所有分享
  43. that.onMenuShareAll = function(options,successFn,closeFn){
  44. that.onMenuShareAppMessage(options,function(){
  45. successFn && successFn('AppMessage');
  46. },function(){
  47. closeFn && closeFn('AppMessage');
  48. });
  49. that.onMenuShareQQ(options,function(){
  50. successFn && successFn('QQ');
  51. },function(){
  52. closeFn && closeFn('QQ');
  53. });
  54. that.onMenuShareQZone(options,function(){
  55. successFn && successFn('QZone');
  56. },function(){
  57. closeFn && closeFn('QZone');
  58. });
  59. that.onMenuShareTimeline(options,function(){
  60. successFn && successFn('Timeline');
  61. },function(){
  62. closeFn && closeFn('Timeline');
  63. });
  64. that.onMenuShareWeibo(options,function(){
  65. successFn && successFn('Weibo');
  66. },function(){
  67. closeFn && closeFn('Weibo');
  68. });
  69. };
  70. };
  71. //拍照或从手机相册中选图接口
  72. _mapleApi.prototype.chooseImage = function (options, successFn) {
  73. options || (options = {});
  74. if (typeof(options) == 'function') {
  75. successFn = options;
  76. options = {};
  77. }
  78. wx.chooseImage({
  79. count: options.count || 1, // 默认9
  80. sizeType: options.sizeType || ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
  81. sourceType: options.sourceType || ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
  82. success: function (res) {
  83. var localIds = res.localIds; // 返回选定照片的本地ID列表,localId可以作为img标签的src属性显示图片
  84. successFn && successFn.call(mapleApi, localIds, res);
  85. },
  86. fail:function(err){
  87. }
  88. });
  89. };
  90. //预览图片接口
  91. _mapleApi.prototype.previewImage = function (current, urls) {
  92. wx.previewImage({
  93. current: current, // 当前显示图片的http链接
  94. urls: urls || [] // 需要预览的图片http链接列表
  95. });
  96. };
  97. //获取本地图片接口
  98. _mapleApi.prototype.getLocalImgData = function (localId, successFn) {
  99. wx.getLocalImgData({
  100. localId: localId, // 图片的localID
  101. success: function (res) {
  102. var localData = res.localData; // localData是图片的base64数据,可以用img标签显示
  103. successFn && successFn.call(mapleApi, localIds, res);
  104. }
  105. });
  106. };
  107. //上传图片接口
  108. _mapleApi.prototype.uploadImageOne = function (localId, successFn, isShowProgressTips) {
  109. wx.uploadImage({
  110. localId: localId, // 需要上传的图片的本地ID,由chooseImage接口获得
  111. isShowProgressTips: isShowProgressTips || 1, // 默认为1,显示进度提示
  112. success: function (res) {
  113. var serverId = res.serverId; // 返回图片的服务器端ID
  114. successFn && successFn.call(mapleApi, serverId, res);
  115. }
  116. });
  117. };
  118. //上传多张图片接口
  119. _mapleApi.prototype.uploadImage = function (localIds, successFn, errorFn) {
  120. // var _this = this,allFn=[];
  121. // localIds.forEach(function(localId,k){
  122. // allFn.push(new Promise(function(resolve){
  123. // _this.uploadImageOne(localId,function(serverId){
  124. // return resolve(serverId);
  125. // })
  126. // }));
  127. // });
  128. // Promise.all(allFn).then(function(){
  129. // var i = arguments.length,serverIdList = new Array(i);
  130. // while(i--){serverIdList[i] = arguments[i];}
  131. // successFn && successFn.call(mapleApi,serverIdList[0]);
  132. // }).catch(function(err){
  133. // errorFn && errorFn.call(mapleApi,err,localIds);
  134. // });
  135. var serverIdList = [], length = localIds.length, _this = this;
  136. var _upload = function () {
  137. var localId = localIds[--length];
  138. if (!localId) return errorFn && errorFn.call(mapleApi, localIds, serverIdList);
  139. _this.uploadImageOne(localId, function (serverId) {
  140. serverIdList.push(serverId);
  141. length==0 ? successFn.call(mapleApi, serverIdList) : _upload();
  142. })
  143. };
  144. _upload();
  145. };
  146. //下载图片接口
  147. _mapleApi.prototype.downloadImage = function (serverId, successFn, isShowProgressTips) {
  148. wx.downloadImage({
  149. serverId: serverId, // 需要下载的图片的服务器端ID,由uploadImage接口获得
  150. isShowProgressTips: isShowProgressTips || 1, // 默认为1,显示进度提示
  151. success: function (res) {
  152. var localId = res.localId; // 返回图片下载后的本地ID
  153. successFn && successFn.call(mapleApi, localId);
  154. }
  155. });
  156. };
  157. //开始录音接口
  158. _mapleApi.prototype.startRecord = function () {
  159. wx.startRecord.call(mapleApi);
  160. };
  161. //停止录音接口
  162. _mapleApi.prototype.stopRecord = function (successFn) {
  163. wx.stopRecord({
  164. success: function (res) {
  165. var localId = res.localId;
  166. successFn && successFn.call(mapleApi, localId, res);
  167. }
  168. });
  169. };
  170. //监听录音自动停止接口
  171. _mapleApi.prototype.onVoiceRecordEnd = function (completeFn) {
  172. wx.onVoiceRecordEnd({
  173. // 录音时间超过一分钟没有停止的时候会执行 complete 回调
  174. complete: function (res) {
  175. var localId = res.localId;
  176. completeFn && completeFn.call(mapleApi, localId, res);
  177. }
  178. });
  179. };
  180. //播放语音接口
  181. _mapleApi.prototype.playVoice = function (localId) {
  182. wx.playVoice({
  183. localId: localId // 需要播放的音频的本地ID,由stopRecord接口获得
  184. });
  185. };
  186. //暂停播放接口
  187. _mapleApi.prototype.pauseVoice = function (localId) {
  188. wx.pauseVoice({
  189. localId: localId // 需要暂停的音频的本地ID,由stopRecord接口获得
  190. });
  191. };
  192. //停止播放接口
  193. _mapleApi.prototype.stopVoice = function (localId) {
  194. wx.stopVoice({
  195. localId: localId // 需要停止的音频的本地ID,由stopRecord接口获得
  196. });
  197. };
  198. //监听语音播放完毕接口
  199. _mapleApi.prototype.onVoicePlayEnd = function (successFn) {
  200. wx.onVoicePlayEnd({
  201. success: function (res) {
  202. var localId = res.localId; // 返回音频的本地ID
  203. successFn && successFn.call(mapleApi, localId, res);
  204. }
  205. });
  206. };
  207. //上传语音接口
  208. _mapleApi.prototype.uploadVoice = function (localId, successFn, isShowProgressTips) {
  209. wx.uploadVoice({
  210. localId: localId, // 需要上传的音频的本地ID,由stopRecord接口获得
  211. isShowProgressTips: isShowProgressTips || 1, // 默认为1,显示进度提示
  212. success: function (res) {
  213. var serverId = res.serverId; // 返回音频的服务器端ID
  214. successFn && successFn.call(mapleApi, serverId, res);
  215. }
  216. });
  217. };
  218. //下载语音接口
  219. _mapleApi.prototype.downloadVoice = function (serverId, successFn, isShowProgressTips) {
  220. wx.downloadVoice({
  221. serverId: serverId, // 需要下载的音频的服务器端ID,由uploadVoice接口获得
  222. isShowProgressTips: isShowProgressTips || 1, // 默认为1,显示进度提示
  223. success: function (res) {
  224. var localId = res.localId; // 返回音频的本地ID
  225. successFn && successFn.call(mapleApi, localId, res);
  226. }
  227. });
  228. };
  229. //识别音频并返回识别结果接口
  230. _mapleApi.prototype.translateVoice = function (localId, successFn, isShowProgressTips) {
  231. wx.translateVoice({
  232. localId: localId, // 需要识别的音频的本地Id,由录音相关接口获得
  233. isShowProgressTips: isShowProgressTips || 1, // 默认为1,显示进度提示
  234. success: function (res) {
  235. successFn && successFn.call(mapleApi, res.translateResult, res);
  236. }
  237. });
  238. };
  239. //获取网络状态接口
  240. _mapleApi.prototype.getNetworkType = function (successFn) {
  241. wx.getNetworkType({
  242. success: function (res) {
  243. successFn && successFn.call(mapleApi, res.networkType, res);
  244. }
  245. });
  246. };
  247. //使用微信内置地图查看位置接口
  248. _mapleApi.prototype.openLocation = function (options) {
  249. wx.openLocation({
  250. latitude: options.latitude || 0, // 纬度,浮点数,范围为90 ~ -90
  251. longitude: options.longitude || 0, // 经度,浮点数,范围为180 ~ -180。
  252. name: options.name || '', // 位置名
  253. address: options.address || '', // 地址详情说明
  254. scale: options.scale || 14, // 地图缩放级别,整形值,范围从1~28。默认为最大
  255. infoUrl: options.infoUrl || '' // 在查看位置界面底部显示的超链接,可点击跳转
  256. });
  257. };
  258. //获取地理位置接口
  259. _mapleApi.prototype.getLocation = function (successFn, type) {
  260. wx.getLocation({
  261. type: type || 'wgs84', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
  262. success: function (res) {
  263. var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90
  264. var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。
  265. // var speed = res.speed; // 速度,以米/每秒计
  266. // var accuracy = res.accuracy; // 位置精度
  267. successFn && successFn.call(mapleApi, latitude, longitude, res);
  268. }
  269. });
  270. };
  271. //开启查找周边ibeacon设备接口
  272. _mapleApi.prototype.startSearchBeacons = function (completeFn, ticket) {
  273. wx.startSearchBeacons({
  274. ticket: ticket || "", //摇周边的业务ticket, 系统自动添加在摇出来的页面链接后面
  275. complete: function (argv) {
  276. //开启查找完成后的回调函数
  277. completeFn && completeFn.call(mapleApi, argv);
  278. }
  279. });
  280. };
  281. //关闭查找周边ibeacon设备接口
  282. _mapleApi.prototype.stopSearchBeacons = function (completeFn) {
  283. wx.stopSearchBeacons({
  284. complete: function (res) {
  285. //关闭查找完成后的回调函数
  286. completeFn && completeFn.call(mapleApi, res);
  287. }
  288. });
  289. };
  290. //监听周边ibeacon设备接口
  291. _mapleApi.prototype.onSearchBeacons = function (completeFn) {
  292. wx.onSearchBeacons({
  293. complete: function (argv) {
  294. //回调函数,可以数组形式取得该商家注册的在周边的相关设备列表
  295. completeFn && completeFn.call(mapleApi, argv);
  296. }
  297. });
  298. };
  299. //关闭当前网页窗口接口
  300. _mapleApi.prototype.closeWindow = function () {
  301. wx.closeWindow();
  302. };
  303. //批量隐藏功能按钮接口
  304. _mapleApi.prototype.hideMenuItems = function (menuList) {
  305. wx.hideMenuItems({
  306. menuList: menuList || [] // 要隐藏的菜单项,只能隐藏“传播类”和“保护类”按钮,所有menu项见附录3
  307. });
  308. };
  309. //批量显示功能按钮接口
  310. _mapleApi.prototype.showMenuItems = function (menuList) {
  311. wx.showMenuItems({
  312. menuList: menuList || [] // 要显示的菜单项,所有menu项见附录3
  313. });
  314. };
  315. //隐藏所有非基础按钮接口
  316. _mapleApi.prototype.hideAllNonBaseMenuItem = function () {
  317. wx.hideAllNonBaseMenuItem();
  318. };
  319. //显示所有功能按钮接口
  320. _mapleApi.prototype.showAllNonBaseMenuItem = function () {
  321. wx.showAllNonBaseMenuItem();
  322. };
  323. //调起微信扫一扫接口
  324. _mapleApi.prototype.scanQRCode = function (options, successFn) {
  325. options || (options = {});
  326. if (typeof(options) == 'function') {
  327. successFn = options;
  328. options = {};
  329. }
  330. wx.scanQRCode({
  331. needResult: options.needResult || 0, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
  332. scanType: options.scanType || ["qrCode", "barCode"], // 可以指定扫二维码还是一维码,默认二者都有
  333. success: function (res) {
  334. var result = res.resultStr; // 当needResult 为 1 时,扫码返回的结果
  335. successFn && successFn.call(mapleApi, result, res);
  336. }
  337. });
  338. };
  339. //跳转微信商品页接口
  340. _mapleApi.prototype.openProductSpecificView = function (productId, viewType) {
  341. wx.openProductSpecificView({
  342. productId: productId, // 商品id
  343. viewType: viewType || 0 // 0.默认值,普通商品详情页1.扫一扫商品详情页2.小店商品详情页
  344. });
  345. };
  346. //拉取适用卡券列表并获取用户选择信息
  347. _mapleApi.prototype.chooseCard = function (options, successFn) {
  348. wx.chooseCard({
  349. shopId: options.shopId, // 门店Id
  350. cardType: options.cardType, // 卡券类型
  351. cardId: options.cardId, // 卡券Id
  352. timestamp: options.timestamp, // 卡券签名时间戳
  353. nonceStr: options.nonceStr, // 卡券签名随机串
  354. signType: options.signType || 'SHA1', // 签名方式,默认'SHA1'
  355. cardSign: options.cardSign, // 卡券签名
  356. success: function (res) {
  357. var cardList = res.cardList; // 用户选中的卡券列表信息
  358. successFn && successFn.call(mapleApi, cardList, res);
  359. }
  360. });
  361. };
  362. //批量添加卡券接口
  363. _mapleApi.prototype.addCard = function (cardList, successFn) {
  364. wx.addCard({
  365. cardList: cardList, // 需要添加的卡券列表
  366. success: function (res) {
  367. var cardList = res.cardList; // 添加的卡券列表信息
  368. successFn && successFn.call(mapleApi, cardList, res);
  369. }
  370. });
  371. };
  372. //查看微信卡包中的卡券接口
  373. _mapleApi.prototype.openCard = function (cardList) {
  374. wx.openCard({
  375. cardList: cardList// 需要打开的卡券列表
  376. });
  377. };
  378. //发起一个微信支付请求
  379. _mapleApi.prototype.chooseWXPay = function (config, successFn,groupFn) {
  380. groupFn || (groupFn = {});
  381. margin(groupFn,{
  382. timestamp: parseInt(config.timestamp), // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
  383. nonceStr: config.nonceStr, // 支付签名随机串,不长于 32 位
  384. package: config.package, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=***)
  385. signType: config.signType || 'SHA1', // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
  386. paySign: config.paySign, // 支付签名
  387. success: function (res) {
  388. // 支付成功后的回调函数
  389. successFn && successFn.call(mapleApi, res);
  390. }
  391. });
  392. wx.chooseWXPay(groupFn);
  393. };
  394. //获取“分享到朋友圈”按钮点击状态及自定义分享内容接口
  395. _mapleApi.prototype.onMenuShareTimeline = function (options, successFn, cancelFn) {
  396. options || (options = {});
  397. wx.onMenuShareTimeline({
  398. title: options.title || '', // 分享标题
  399. link: options.link || location.href, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  400. imgUrl: options.imgUrl || '', // 分享图标
  401. success: function () {
  402. // 用户确认分享后执行的回调函数
  403. successFn && successFn.call(mapleApi);
  404. },
  405. cancel: function () {
  406. // 用户取消分享后执行的回调函数
  407. cancelFn && cancelFn.call(mapleApi);
  408. }
  409. });
  410. };
  411. //获取“分享给朋友”按钮点击状态及自定义分享内容接口
  412. _mapleApi.prototype.onMenuShareAppMessage = function (options, successFn, cancelFn) {
  413. options || (options = {});
  414. wx.onMenuShareAppMessage({
  415. title: options.title || '', // 分享标题
  416. desc: options.desc || '', // 分享描述
  417. imgUrl: options.imgUrl || '', // 分享图标
  418. link: options.link || location.href, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  419. type: options.type || 'link', // 分享类型,music、video或link,不填默认为link
  420. dataUrl: options.dataUrl || '', // 如果type是music或video,则要提供数据链接,默认为空
  421. success: function () {
  422. // 用户确认分享后执行的回调函数
  423. successFn && successFn.call(mapleApi);
  424. },
  425. cancel: function () {
  426. // 用户取消分享后执行的回调函数
  427. cancelFn && cancelFn.call(mapleApi);
  428. }
  429. });
  430. };
  431. //获取“分享到QQ”按钮点击状态及自定义分享内容接口
  432. _mapleApi.prototype.onMenuShareQQ = function (options, successFn, cancelFn) {
  433. options || (options = {});
  434. wx.onMenuShareQQ({
  435. title: options.title || '', // 分享标题
  436. desc: options.desc || '', // 分享描述
  437. link: options.link || location.href, // 分享链接
  438. imgUrl: options.imgUrl || '', // 分享图标
  439. success: function () {
  440. // 用户确认分享后执行的回调函数
  441. successFn && successFn.call(mapleApi);
  442. },
  443. cancel: function () {
  444. // 用户取消分享后执行的回调函数
  445. cancelFn && cancelFn.call(mapleApi);
  446. }
  447. });
  448. };
  449. //获取“分享到腾讯微博”按钮点击状态及自定义分享内容接口
  450. _mapleApi.prototype.onMenuShareWeibo = function (options, successFn, cancelFn) {
  451. options || (options = {});
  452. wx.onMenuShareWeibo({
  453. title: options.title || '', // 分享标题
  454. desc: options.imgUrl || '', // 分享描述
  455. link: options.imgUrl || location.href, // 分享链接
  456. imgUrl: options.imgUrl || '', // 分享图标
  457. success: function () {
  458. // 用户确认分享后执行的回调函数
  459. successFn && successFn.call(mapleApi);
  460. },
  461. cancel: function () {
  462. // 用户取消分享后执行的回调函数
  463. cancelFn && cancelFn.call(mapleApi);
  464. }
  465. });
  466. };
  467. //获取“分享到QQ空间”按钮点击状态及自定义分享内容接口
  468. _mapleApi.prototype.onMenuShareQZone = function (options, successFn, cancelFn) {
  469. options || (options = {});
  470. wx.onMenuShareQZone({
  471. title: options.title || '', // 分享标题
  472. desc: options.desc || '', // 分享描述
  473. link: options.link || location.href, // 分享链接
  474. imgUrl: options.imgUrl || '', // 分享图标
  475. success: function () {
  476. // 用户确认分享后执行的回调函数
  477. successFn && successFn.call(mapleApi);
  478. },
  479. cancel: function () {
  480. // 用户取消分享后执行的回调函数
  481. cancelFn && cancelFn.call(mapleApi);
  482. }
  483. });
  484. };
  485. _mapleApi.prototype.option = {};
  486. return mapleApi.init;
  487. }
  488. }(this));