UNPKG

@honeysyt/h5-open-app

Version:
2 lines (1 loc) 16 kB
function e(e,n){for(var t=0;t<n.length;t++){var i=n[t];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}function n(e,n,t){return n in e?Object.defineProperty(e,n,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[n]=t,e}var t=function e(n,t){if(n&&!n.jWeixin){var i,o={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"},r=function(){var e={};for(var n in o)e[o[n]]=n;return e}(),a=n.document,c=a.title,s=navigator.userAgent.toLowerCase(),d=navigator.platform.toLowerCase(),u=!(!d.match("mac")&&!d.match("win")),l=-1!=s.indexOf("wxdebugger"),p=-1!=s.indexOf("micromessenger"),f=-1!=s.indexOf("android"),g=-1!=s.indexOf("iphone")||-1!=s.indexOf("ipad"),m=(i=s.match(/micromessenger\/(\d+\.\d+\.\d+)/)||s.match(/micromessenger\/(\d+\.\d+)/))?i[1]:"",h={initStartTime:V(),initEndTime:0,preVerifyStartTime:0,preVerifyEndTime:0},v={version:1,appId:"",initTime:0,preVerifyTime:0,networkType:"",isPreVerifyOk:1,systemType:g?1:f?2:-1,clientVersion:m,url:encodeURIComponent(location.href)},y={},S={_completes:[]},T={state:0,data:{}};O((function(){h.initEndTime=V()}));var w=!1,I=[],_={config:function(e){E("config",y=e);var t=!1!==y.check;O((function(){if(t)x(o.config,{verifyJsApiList:L(y.jsApiList),verifyOpenTagList:L(y.openTagList)},function(){S._complete=function(e){h.preVerifyEndTime=V(),T.state=1,T.data=e},S.success=function(e){v.isPreVerifyOk=0},S.fail=function(e){S._fail?S._fail(e):T.state=-1};var e=S._completes;return e.push((function(){!function(){if(!(u||l||y.debug||m<"6.0.2"||v.systemType<0)){var e=new Image;v.appId=y.appId,v.initTime=h.initEndTime-h.initStartTime,v.preVerifyTime=h.preVerifyEndTime-h.preVerifyStartTime,_.getNetworkType({isInnerInvoke:!0,success:function(n){v.networkType=n.networkType;var t="https://open.weixin.qq.com/sdk/report?v="+v.version+"&o="+v.isPreVerifyOk+"&s="+v.systemType+"&c="+v.clientVersion+"&a="+v.appId+"&n="+v.networkType+"&i="+v.initTime+"&p="+v.preVerifyTime+"&u="+v.url;e.src=t}})}}()})),S.complete=function(n){for(var t=0,i=e.length;t<i;++t)e[t]();S._completes=[]},S}()),h.preVerifyStartTime=V();else{T.state=1;for(var e=S._completes,n=0,i=e.length;n<i;++n)e[n]();S._completes=[]}})),_.invoke||(_.invoke=function(e,t,i){n.WeixinJSBridge&&WeixinJSBridge.invoke(e,C(t),i)},_.on=function(e,t){n.WeixinJSBridge&&WeixinJSBridge.on(e,t)})},ready:function(e){0!=T.state?e():(S._completes.push(e),!p&&y.debug&&e())},error:function(e){m<"6.0.2"||(-1==T.state?e(T.data):S._fail=e)},checkJsApi:function(e){x("checkJsApi",{jsApiList:L(e.jsApiList)},(e._complete=function(e){if(f){var n=e.checkResult;n&&(e.checkResult=JSON.parse(n))}e=function(e){var n=e.checkResult;for(var t in n){var i=r[t];i&&(n[i]=n[t],delete n[t])}return e}(e)},e))},onMenuShareTimeline:function(e){A(o.onMenuShareTimeline,{complete:function(){x("shareTimeline",{title:e.title||c,desc:e.title||c,img_url:e.imgUrl||"",link:e.link||location.href,type:e.type||"link",data_url:e.dataUrl||""},e)}},e)},onMenuShareAppMessage:function(e){A(o.onMenuShareAppMessage,{complete:function(n){"favorite"===n.scene?x("sendAppMessage",{title:e.title||c,desc:e.desc||"",link:e.link||location.href,img_url:e.imgUrl||"",type:e.type||"link",data_url:e.dataUrl||""}):x("sendAppMessage",{title:e.title||c,desc:e.desc||"",link:e.link||location.href,img_url:e.imgUrl||"",type:e.type||"link",data_url:e.dataUrl||""},e)}},e)},onMenuShareQQ:function(e){A(o.onMenuShareQQ,{complete:function(){x("shareQQ",{title:e.title||c,desc:e.desc||"",img_url:e.imgUrl||"",link:e.link||location.href},e)}},e)},onMenuShareWeibo:function(e){A(o.onMenuShareWeibo,{complete:function(){x("shareWeiboApp",{title:e.title||c,desc:e.desc||"",img_url:e.imgUrl||"",link:e.link||location.href},e)}},e)},onMenuShareQZone:function(e){A(o.onMenuShareQZone,{complete:function(){x("shareQZone",{title:e.title||c,desc:e.desc||"",img_url:e.imgUrl||"",link:e.link||location.href},e)}},e)},updateTimelineShareData:function(e){x("updateTimelineShareData",{title:e.title,link:e.link,imgUrl:e.imgUrl},e)},updateAppMessageShareData:function(e){x("updateAppMessageShareData",{title:e.title,desc:e.desc,link:e.link,imgUrl:e.imgUrl},e)},startRecord:function(e){x("startRecord",{},e)},stopRecord:function(e){x("stopRecord",{},e)},onVoiceRecordEnd:function(e){A("onVoiceRecordEnd",e)},playVoice:function(e){x("playVoice",{localId:e.localId},e)},pauseVoice:function(e){x("pauseVoice",{localId:e.localId},e)},stopVoice:function(e){x("stopVoice",{localId:e.localId},e)},onVoicePlayEnd:function(e){A("onVoicePlayEnd",e)},uploadVoice:function(e){x("uploadVoice",{localId:e.localId,isShowProgressTips:0==e.isShowProgressTips?0:1},e)},downloadVoice:function(e){x("downloadVoice",{serverId:e.serverId,isShowProgressTips:0==e.isShowProgressTips?0:1},e)},translateVoice:function(e){x("translateVoice",{localId:e.localId,isShowProgressTips:0==e.isShowProgressTips?0:1},e)},chooseImage:function(e){x("chooseImage",{scene:"1|2",count:e.count||9,sizeType:e.sizeType||["original","compressed"],sourceType:e.sourceType||["album","camera"]},(e._complete=function(e){if(f){var n=e.localIds;try{n&&(e.localIds=JSON.parse(n))}catch(e){}}},e))},getLocation:function(e){},previewImage:function(e){x(o.previewImage,{current:e.current,urls:e.urls},e)},uploadImage:function(e){x("uploadImage",{localId:e.localId,isShowProgressTips:0==e.isShowProgressTips?0:1},e)},downloadImage:function(e){x("downloadImage",{serverId:e.serverId,isShowProgressTips:0==e.isShowProgressTips?0:1},e)},getLocalImgData:function(n){!1===w?(w=!0,x("getLocalImgData",{localId:n.localId},(n._complete=function(n){if(w=!1,0<I.length){var t=I.shift();e.getLocalImgData(t)}},n))):I.push(n)},getNetworkType:function(e){x("getNetworkType",{},(e._complete=function(e){e=function(e){var n=e.errMsg;e.errMsg="getNetworkType:ok";var t=e.subtype;if(delete e.subtype,t)e.networkType=t;else{var i=n.indexOf(":"),o=n.substring(i+1);switch(o){case"wifi":case"edge":case"wwan":e.networkType=o;break;default:e.errMsg="getNetworkType:fail"}}return e}(e)},e))},openLocation:function(e){x("openLocation",{latitude:e.latitude,longitude:e.longitude,name:e.name||"",address:e.address||"",scale:e.scale||28,infoUrl:e.infoUrl||""},e)},getLocation:function(e){x(o.getLocation,{type:(e=e||{}).type||"wgs84"},(e._complete=function(e){delete e.type},e))},hideOptionMenu:function(e){x("hideOptionMenu",{},e)},showOptionMenu:function(e){x("showOptionMenu",{},e)},closeWindow:function(e){x("closeWindow",{},e=e||{})},hideMenuItems:function(e){x("hideMenuItems",{menuList:e.menuList},e)},showMenuItems:function(e){x("showMenuItems",{menuList:e.menuList},e)},hideAllNonBaseMenuItem:function(e){x("hideAllNonBaseMenuItem",{},e)},showAllNonBaseMenuItem:function(e){x("showAllNonBaseMenuItem",{},e)},scanQRCode:function(e){x("scanQRCode",{needResult:(e=e||{}).needResult||0,scanType:e.scanType||["qrCode","barCode"]},(e._complete=function(e){if(g){var n=e.resultStr;if(n){var t=JSON.parse(n);e.resultStr=t&&t.scan_code&&t.scan_code.scan_result}}},e))},openAddress:function(e){x(o.openAddress,{},(e._complete=function(e){e=function(e){return e.postalCode=e.addressPostalCode,delete e.addressPostalCode,e.provinceName=e.proviceFirstStageName,delete e.proviceFirstStageName,e.cityName=e.addressCitySecondStageName,delete e.addressCitySecondStageName,e.countryName=e.addressCountiesThirdStageName,delete e.addressCountiesThirdStageName,e.detailInfo=e.addressDetailInfo,delete e.addressDetailInfo,e}(e)},e))},openProductSpecificView:function(e){x(o.openProductSpecificView,{pid:e.productId,view_type:e.viewType||0,ext_info:e.extInfo},e)},addCard:function(e){for(var n=e.cardList,t=[],i=0,r=n.length;i<r;++i){var a=n[i],c={card_id:a.cardId,card_ext:a.cardExt};t.push(c)}x(o.addCard,{card_list:t},(e._complete=function(e){var n=e.card_list;if(n){for(var t=0,i=(n=JSON.parse(n)).length;t<i;++t){var o=n[t];o.cardId=o.card_id,o.cardExt=o.card_ext,o.isSuccess=!!o.is_succ,delete o.card_id,delete o.card_ext,delete o.is_succ}e.cardList=n,delete e.card_list}},e))},chooseCard:function(e){x("chooseCard",{app_id:y.appId,location_id:e.shopId||"",sign_type:e.signType||"SHA1",card_id:e.cardId||"",card_type:e.cardType||"",card_sign:e.cardSign,time_stamp:e.timestamp+"",nonce_str:e.nonceStr},(e._complete=function(e){e.cardList=e.choose_card_info,delete e.choose_card_info},e))},openCard:function(e){for(var n=e.cardList,t=[],i=0,r=n.length;i<r;++i){var a=n[i],c={card_id:a.cardId,code:a.code};t.push(c)}x(o.openCard,{card_list:t},e)},consumeAndShareCard:function(e){x(o.consumeAndShareCard,{consumedCardId:e.cardId,consumedCode:e.code},e)},chooseWXPay:function(e){x(o.chooseWXPay,M(e),e)},openEnterpriseRedPacket:function(e){x(o.openEnterpriseRedPacket,M(e),e)},startSearchBeacons:function(e){x(o.startSearchBeacons,{ticket:e.ticket},e)},stopSearchBeacons:function(e){x(o.stopSearchBeacons,{},e)},onSearchBeacons:function(e){A(o.onSearchBeacons,e)},openEnterpriseChat:function(e){x("openEnterpriseChat",{useridlist:e.userIds,chatname:e.groupName},e)},launchMiniProgram:function(e){x("launchMiniProgram",{targetAppId:e.targetAppId,path:function(e){if("string"==typeof e&&0<e.length){var n=e.split("?")[0],t=e.split("?")[1];return n+=".html",void 0!==t?n+"?"+t:n}}(e.path),envVersion:e.envVersion},e)},openBusinessView:function(e){x("openBusinessView",{businessType:e.businessType,queryString:e.queryString||"",envVersion:e.envVersion},(e._complete=function(e){if(f){var n=e.extraData;if(n)try{e.extraData=JSON.parse(n)}catch(n){e.extraData={}}}},e))},miniProgram:{navigateBack:function(e){e=e||{},O((function(){x("invokeMiniProgramAPI",{name:"navigateBack",arg:{delta:e.delta||1}},e)}))},navigateTo:function(e){O((function(){x("invokeMiniProgramAPI",{name:"navigateTo",arg:{url:e.url}},e)}))},redirectTo:function(e){O((function(){x("invokeMiniProgramAPI",{name:"redirectTo",arg:{url:e.url}},e)}))},switchTab:function(e){O((function(){x("invokeMiniProgramAPI",{name:"switchTab",arg:{url:e.url}},e)}))},reLaunch:function(e){O((function(){x("invokeMiniProgramAPI",{name:"reLaunch",arg:{url:e.url}},e)}))},postMessage:function(e){O((function(){x("invokeMiniProgramAPI",{name:"postMessage",arg:e.data||{}},e)}))},getEnv:function(e){O((function(){e({miniprogram:"miniprogram"===n.__wxjs_environment})}))}}},k=1,b={};return a.addEventListener("error",(function(n){if(!f){var t=n.target,i=t.tagName,o=t.src;if(("IMG"==i||"VIDEO"==i||"AUDIO"==i||"SOURCE"==i)&&-1!=o.indexOf("wxlocalresource://")){n.preventDefault(),n.stopPropagation();var r=t["wx-id"];if(r||(r=k++,t["wx-id"]=r),b[r])return;b[r]=!0,e.ready((function(){e.getLocalImgData({localId:o,success:function(e){t.src=e.localData}})}))}}}),!0),a.addEventListener("load",(function(e){if(!f){var n=e.target,t=n.tagName;if(n.src,"IMG"==t||"VIDEO"==t||"AUDIO"==t||"SOURCE"==t){var i=n["wx-id"];i&&(b[i]=!1)}}}),!0),t&&(n.wx=n.jWeixin=_),_}function x(e,t,i){n.WeixinJSBridge?WeixinJSBridge.invoke(e,C(t),(function(n){P(e,n,i)})):E(e,i)}function A(e,t,i){n.WeixinJSBridge?WeixinJSBridge.on(e,(function(n){i&&i.trigger&&i.trigger(n),P(e,n,t)})):E(e,i||t)}function C(e){return(e=e||{}).appId=y.appId,e.verifyAppId=y.appId,e.verifySignType="sha1",e.verifyTimestamp=y.timestamp+"",e.verifyNonceStr=y.nonceStr,e.verifySignature=y.signature,e}function M(e){return{timeStamp:e.timestamp+"",nonceStr:e.nonceStr,package:e.package,paySign:e.paySign,signType:e.signType||"SHA1"}}function P(e,n,t){"openEnterpriseChat"!=e&&"openBusinessView"!==e||(n.errCode=n.err_code),delete n.err_code,delete n.err_desc,delete n.err_detail;var i=n.errMsg;i||(i=n.err_msg,delete n.err_msg,i=function(e,n){var t=e,i=r[t];i&&(t=i);var o="ok";if(n){var a=n.indexOf(":");"confirm"==(o=n.substring(a+1))&&(o="ok"),"failed"==o&&(o="fail"),-1!=o.indexOf("failed_")&&(o=o.substring(7)),-1!=o.indexOf("fail_")&&(o=o.substring(5)),"access denied"!=(o=(o=o.replace(/_/g," ")).toLowerCase())&&"no permission to execute"!=o||(o="permission denied"),"config"==t&&"function not exist"==o&&(o="ok"),""==o&&(o="fail")}return t+":"+o}(e,i),n.errMsg=i),(t=t||{})._complete&&(t._complete(n),delete t._complete),i=n.errMsg||"",y.debug&&!t.isInnerInvoke&&alert(JSON.stringify(n));var o=i.indexOf(":");switch(i.substring(o+1)){case"ok":t.success&&t.success(n);break;case"cancel":t.cancel&&t.cancel(n);break;default:t.fail&&t.fail(n)}t.complete&&t.complete(n)}function L(e){if(e){for(var n=0,t=e.length;n<t;++n){var i=e[n],r=o[i];r&&(e[n]=r)}return e}}function E(e,n){if(!(!y.debug||n&&n.isInnerInvoke)){var t=r[e];t&&(e=t),n&&n._complete&&delete n._complete,console.log('"'+e+'",',n||"")}}function V(){return(new Date).getTime()}function O(e){p&&(n.WeixinJSBridge?e():a.addEventListener&&a.addEventListener("WeixinJSBridgeReady",e,!1))}}(window),i=function(){function i(e){!function(e,n){if(!(e instanceof n))throw new TypeError("Cannot call a class as a function")}(this,i),n(this,"DEFAULT_BTN_STYLE","display: flex;justify-content: center;align-items: center;width: 100%;height: 100%;color: #252525;outline: none;background: none;box-sizing: border-box;border-color: transparent;border: none;"),n(this,"DEFAULT_CONTAINER_STYLE","position: absolute;top: 0;left: 0;bottom: 0;right: 0;width: 100%;height: 100%;");var t=e.wechatConfig,o=e.openTagConfig,r=e.btnContainerStyle,a=e.btnStyle,c=e.text,s=void 0===c?"打开APP":c,d=e.config;this.wxDebugger=!!d.debug,this.wechatConfig=t;var u=o.appid,l=o.extinfo;this.openTagConfig={appid:u,extinfo:l},this.btnStyle=this.DEFAULT_BTN_STYLE+a,this.btnContainerStyle=this.DEFAULT_CONTAINER_STYLE+r,this.text=s,this.openApp=null,this.isCallError=!1}var o,r,a;return o=i,r=[{key:"createDom",value:function(){this.openApp=document.createElement("wx-open-launch-app"),this.openApp.setAttribute("style",this.btnContainerStyle),this.openApp.setAttribute("appid",this.openTagConfig.appid),this.openApp.setAttribute("extinfo",this.openTagConfig.extinfo),this.openApp.setAttribute("id",this.genTagId());var e=document.createElement("script");e.setAttribute("type","text/wxtag-template");var n=document.createElement("button");n.setAttribute("style",this.btnStyle),n.innerHTML=this.text,n.classList.add("open-btn"),e.appendChild(n),this.openApp.appendChild(e)}},{key:"verify",value:function(){var e=this,n=this.wechatConfig,i=n.appId,o=n.timestamp,r=n.nonceStr,a=n.signature;t.config({debug:this.wxDebugger,appId:i,timestamp:o,nonceStr:r,signature:a,jsApiList:["onMenuShareTimeline"],openTagList:["wx-open-launch-app"]}),t.ready((function(){e.isCallError||t.onMenuShareTimeline({title:"xxx",link:"",imgUrl:"",success:function(){console.log("share success")},cancel:function(){}})})),t.error((function(n){throw e.isCallError=!0,new Error("config error.".concat(JSON.stringify(n,Object.getOwnPropertyNames(n))))}))}},{key:"mount",value:function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:this.openApp;e.appendChild(n)}},{key:"titleFormat",value:function(e){return e.slice(0,1).toUpperCase()+e.slice(1).toLowerCase()}},{key:"genTagId",value:function(){return"lanunch-btn-".concat((new Date).getTime())}}],r&&e(o.prototype,r),a&&e(o,a),Object.defineProperty(o,"prototype",{writable:!1}),i}(),o={H5_APP:function(e){return new Promise((function(n,t){try{var o=e.wechatConfig,r=e.openTagConfig,a=e.btnContainerStyle,c=e.btnStyle,s=e.text,d=e.config,u=new i({wechatConfig:o,openTagConfig:r,btnContainerStyle:a,btnStyle:c,text:s,config:void 0===d?{}:d});return u.createDom(),u.verify(),n(u)}catch(e){return t(-1)}}))}};export{o as default};