mazey-wechat-launch-app
Version:
Launch App in WeChat/Weixin
7 lines (6 loc) • 7.99 kB
JavaScript
/*!
* mazey-wechat-launch-app v2.4.4 https://www.npmjs.com/package/mazey-wechat-launch-app
* (c) 2018-2026 Cheng
* Released under the MIT License.
*/
import"core-js/modules/es.array.concat.js";import"core-js/modules/es.array.iterator.js";import"core-js/modules/es.object.assign.js";import"core-js/modules/es.object.to-string.js";import"core-js/modules/es.promise.js";import"core-js/modules/es.string.iterator.js";import"core-js/modules/web.dom-collections.iterator.js";import{genCustomConsole as e,addStyle as n,generateRndNum as a,getQueryParam as o}from"mazey";import t from"jquery";import i from"js-sha1";var r=function(){return r=Object.assign||function(e){for(var n,a=1,o=arguments.length;a<o;a++)for(var t in n=arguments[a])Object.prototype.hasOwnProperty.call(n,t)&&(e[t]=n[t]);return e},r.apply(this,arguments)};"function"==typeof SuppressedError&&SuppressedError;var c={weixinJsSdkTicket:"",launchContainerQuery:".mazey-launch-app-selector",launchShowWeixinToBrowserImgUrl:"",canShowWeixinToBrowser:!1,extInfo:"",serviceAccountAppId:"",wexinServiceAccountAppId:"",openPlatformMobileAppId:"",genTagPrefixStr:"mazey-launch-app-btn-prefix-",launchShowWeixinToBrowserClassName:"mazey-launch-app-wx-to-browser",launchBtnClassName:"mazey-launch-app-inner-btn",launchBtnStyle:"",launchBtnText:"Launch App <br /><br /><br />",launchErrorLink:"",canContinuousUpdating:!1,onMenuShareTimelineOptions:void 0,onMenuShareAppMessageOptions:void 0,isConClosed:!0,isWxDebug:!1,openTagList:["wx-open-launch-app"],canLaunchApp:function(){return!0},canFireErrorLinkDirectly:function(){return!1},launchBtnClick:function(){},launchReady:function(){}},l=function(l){void 0===l&&(l=c);var p=Object.assign(c,l),s=p.weixinJsSdkTicket,u=p.launchContainerQuery,d=p.genTagPrefixStr,h=p.launchShowWeixinToBrowserImgUrl,g=p.canShowWeixinToBrowser,A=p.launchShowWeixinToBrowserClassName,f=p.launchBtnClassName,m=p.launchBtnStyle,w=p.launchBtnText,_=p.launchErrorLink,E=p.wexinServiceAccountAppId,P=p.openPlatformMobileAppId,v=p.canContinuousUpdating,S=p.onMenuShareTimelineOptions,x=p.onMenuShareAppMessageOptions,y=p.isConClosed,L=p.isWxDebug,k=p.openTagList,I=p.canLaunchApp,C=p.canFireErrorLinkDirectly,N=p.launchBtnClick,T=p.launchReady,b=p.serviceAccountAppId,H=p.extInfo,U=e("LaunchCon:",{isClosed:y}),O=window.wx;O||console.error("Launch App: wx is not found");var M=function(){};U.log("Launch App");var R={title:"",link:"",imgUrl:"",success:function(){},cancel:function(){}},W={title:"",desc:"",link:"",imgUrl:"",type:"",dataUrl:"",success:function(){},cancel:function(){}},B={title:document.title,link:location.href};R=r(r({},R),B),W=r(r({},W),B);var j=function(e,n,a){return void 0===e&&(e=""),void 0===n&&(n=""),void 0===a&&(a="button"),"<style>.".concat(f,"{")+"opacity: 0;width: 100%;height: 100%;backgroud: transparent;color: #300f54;border: none;box-sizing: border-box;text-align: center;vertical-align: middle;"+m+n+"}</style>"+"<".concat(a,' class="').concat(f,'"')+(e&&"button"===a?' onclick="'.concat(e,'"'):"")+(e&&"a"===a?' target="_self" href="'.concat(e,'"'):"")+">"+w+"</".concat(a,">")},z=j();function D(){n('.mazey-launch-app-mask{position:fixed;top:0;right:0;bottom:0;left:0;background-color:rgba(0, 0, 0, .5);text-align:right;font-size:0;white-space:nowrap;overflow:auto;z-index:999;}.mazey-launch-app-img{display:inline-block;vertical-align:middle;text-align:left;font-size:14px;white-space:normal;width:50vw;margin:0.5rem 0.5rem 0 0;}.mazey-launch-app-img:after{content:"";display:inline-block;height:100%;vertical-align:middle;}',{id:"mazey-launch-app-mask-style"}),window.LAUNCH_APP_HIDE_WEIXIN_BROWSER||(window.LAUNCH_APP_HIDE_WEIXIN_BROWSER=function(){t(".mazey-launch-app-mask").hide()}),0===t(".mazey-launch-app-mask").length?t("body").append("<div class='mazey-launch-app-mask' onclick='window.LAUNCH_APP_HIDE_WEIXIN_BROWSER()'><img "+"class='mazey-launch-app-img ".concat(A,"' ")+"src='".concat(h,"' ")+"alt='Launch App' /></div>"):t(".mazey-launch-app-mask").show()}function q(){U.log("renderWeixinLaunchTemplate"),!b&&E&&(console.warn("wexinServiceAccountAppId will be deprecated, use serviceAccountAppId instead."),b=E),function(e,n){void 0===e&&(e=""),void 0===n&&(n=""),U.log("renderWXOpenLaunchApp"),Promise.all([Promise.resolve(s||window.LAUNCH_APP_WEIXIN_JS_SDK_TICKET||o("weixinJsSdkTicket")||""),Promise.resolve(i)]).then(function(o){U.log("allRes",o);var i=o[0];if(i){var c=o[1];if(c){var l=a(7),p=i,s=(new Date).getTime(),h=c("jsapi_ticket="+p+"&noncestr="+l+"×tamp="+s+"&url="+location.href);return O.config({debug:L,appId:e,timestamp:s,nonceStr:l,signature:h,jsApiList:["showOptionMenu","onMenuShareTimeline","onMenuShareAppMessage"],openTagList:k}),O.ready(function(){if(x&&(U.log("onMenuShareAppMessageOptions",x),O.onMenuShareAppMessage(x)),window.LAUNCH_APP_SHARE_APP_MESSAGE=function(e){e=r(r({},W),e),U.log("window LAUNCH_APP_SHARE_APP_MESSAGE",e),O.onMenuShareAppMessage(e)},S&&(U.log("onMenuShareTimelineOptions",S),O.onMenuShareTimeline(S)),window.LAUNCH_APP_SHARE_TIMELINE=function(e){e=r(r({},R),e),U.log("window LAUNCH_APP_SHARE_TIMELINE",e),O.onMenuShareTimeline(e)},n){window.LAUNCH_APP_READY=!0,T&&T();var e=function(){var e=t(u);U.log("containers",u,e,e.length),e.length>0&&e.each(function(e,a){var o=t(a).attr("data-launch-app-key");if(!o){var i=t(a).parents("[data-launch-app-key]:eq(0)");i.length>0&&(o=i.attr("data-launch-app-key"),U.log("keyEle",i,o))}o||(o=String(e));var r="mazey-launch-app-tag-".concat(o),c=t(".".concat(r," wx-open-launch-app")),l=t(a).attr("data-launch-app-ext-info");if(!l){var p=t(a).parents("[data-launch-app-ext-info]:eq(0)");p.length>0&&(l=p.attr("data-launch-app-ext-info"),U.log("eleExtInfoEle parents",p,l))}l&&(H=l);var s=t(a).attr("data-launch-app-error-link");if(!s){var u=t(a).parents("[data-launch-app-error-link]:eq(0)");u.length>0&&(s=u.attr("data-launch-app-error-link"),U.log("eleErrorLinkEle parents",u,s))}var h=_;s&&(h=s),0===c.length&&(t(a).addClass(r),U.log("success:",r),function(e){var a=d,o=a+e,i="<wx-open-launch-app id='"+o+"' appid='"+n+"' extinfo='".concat(H)+"' style='z-index:99;position:absolute;width:100%;height:100%;opacity:1;background:transparent;overflow:hidden;left:0;'><script type='text/wxtag-template'>"+z+"<\/script></wx-open-launch-app>";t("."+e+":eq(0)").append(i);var r=document.getElementById(o);r&&(r.addEventListener("ready",function(e){U.log("ready event",e)}),r.addEventListener("launch",function(e){U.log("launch event",e),e.detail&&e.detail.extInfo&&U.log("launch extInfo",e.detail.extInfo)}),r.addEventListener("error",function(e){U.error(e.detail),t("[id^='"+a+"']").hide(),g&&D(),h&&(U.log("realErrorLink",h),location.href=h)}),r.addEventListener("click",function(e){U.log("click event",e),N&&N()}))}(r))})};e(),M=e}else U.log("openPlatformMobileAppId is required for weixin launch app")}),O.error(function(e){U.error(e)}),"success:launch_app"}U.error("sha1 is required")}else U.error("weixinJsSdkTicket is required")}).catch(function(e){U.error(e)})}(b,P)}function X(e){if(void 0===e&&(e={}),U.log("appUpdated",e),!window.LAUNCH_APP_LOAD&&I(e)&&(q(),window.LAUNCH_APP_LOAD=!0),v&&window.LAUNCH_APP_LOAD&&I(e)&&M(),C()){var n=t(u);n.length>0&&n.each(function(e,n){var a=t(n).children("."+f);if(!a||!a.length){var o=t(n).attr("data-launch-app-error-link");if(!o){var i=t(n).parents("[data-launch-app-error-link]:eq(0)");i.length>0&&(o=i.attr("data-launch-app-error-link"))}if(o&&"string"==typeof o&&o.length){var r=j(o,"position:absolute;left:0;z-index:999;","a");t(n).append(r)}}})}}function J(){U.log("appBeforeDestroy",J),window.LAUNCH_APP_LOAD=null,window.LAUNCH_APP_HIDE_WEIXIN_BROWSER=null}return window.LAUNCH_APP_UPDATE=X,window.LAUNCH_APP_BEFORE_DESTROY=J,window.LAUNCH_APP_SHOW_WEIXIN_TO_BROWSER=D,{LAUNCH_APP_UPDATE:X,LAUNCH_APP_BEFORE_DESTROY:J,LAUNCH_APP_SHOW_WEIXIN_TO_BROWSER:D,LAUNCH_APP_SHARE_TIMELINE:function(e){U.log("opt",e)},LAUNCH_APP_SHARE_APP_MESSAGE:function(e){U.log("opt",e)},start:X,update:X,destroy:J}};export{l as default};