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.
*/
;require("core-js/modules/es.array.concat.js"),require("core-js/modules/es.array.iterator.js"),require("core-js/modules/es.object.assign.js"),require("core-js/modules/es.object.to-string.js"),require("core-js/modules/es.promise.js"),require("core-js/modules/es.string.iterator.js"),require("core-js/modules/web.dom-collections.iterator.js");var e=require("mazey"),n=require("jquery"),a=require("js-sha1"),o=function(){return o=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},o.apply(this,arguments)};"function"==typeof SuppressedError&&SuppressedError;var t={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(){}};module.exports=function(r){void 0===r&&(r=t);var i=Object.assign(t,r),c=i.weixinJsSdkTicket,l=i.launchContainerQuery,s=i.genTagPrefixStr,p=i.launchShowWeixinToBrowserImgUrl,u=i.canShowWeixinToBrowser,d=i.launchShowWeixinToBrowserClassName,h=i.launchBtnClassName,g=i.launchBtnStyle,A=i.launchBtnText,f=i.launchErrorLink,w=i.wexinServiceAccountAppId,m=i.openPlatformMobileAppId,_=i.canContinuousUpdating,E=i.onMenuShareTimelineOptions,P=i.onMenuShareAppMessageOptions,v=i.isConClosed,S=i.isWxDebug,y=i.openTagList,x=i.canLaunchApp,L=i.canFireErrorLinkDirectly,k=i.launchBtnClick,I=i.launchReady,C=i.serviceAccountAppId,N=i.extInfo,T=e.genCustomConsole("LaunchCon:",{isClosed:v}),b=window.wx;b||console.error("Launch App: wx is not found");var H=function(){};T.log("Launch App");var U={title:"",link:"",imgUrl:"",success:function(){},cancel:function(){}},O={title:"",desc:"",link:"",imgUrl:"",type:"",dataUrl:"",success:function(){},cancel:function(){}},M={title:document.title,link:location.href};U=o(o({},U),M),O=o(o({},O),M);var R=function(e,n,a){return void 0===e&&(e=""),void 0===n&&(n=""),void 0===a&&(a="button"),"<style>.".concat(h,"{")+"opacity: 0;width: 100%;height: 100%;backgroud: transparent;color: #300f54;border: none;box-sizing: border-box;text-align: center;vertical-align: middle;"+g+n+"}</style>"+"<".concat(a,' class="').concat(h,'"')+(e&&"button"===a?' onclick="'.concat(e,'"'):"")+(e&&"a"===a?' target="_self" href="'.concat(e,'"'):"")+">"+A+"</".concat(a,">")},W=R();function B(){e.addStyle('.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(){n(".mazey-launch-app-mask").hide()}),0===n(".mazey-launch-app-mask").length?n("body").append("<div class='mazey-launch-app-mask' onclick='window.LAUNCH_APP_HIDE_WEIXIN_BROWSER()'><img "+"class='mazey-launch-app-img ".concat(d,"' ")+"src='".concat(p,"' ")+"alt='Launch App' /></div>"):n(".mazey-launch-app-mask").show()}function j(){T.log("renderWeixinLaunchTemplate"),!C&&w&&(console.warn("wexinServiceAccountAppId will be deprecated, use serviceAccountAppId instead."),C=w),function(t,r){void 0===t&&(t=""),void 0===r&&(r=""),T.log("renderWXOpenLaunchApp"),Promise.all([Promise.resolve(c||window.LAUNCH_APP_WEIXIN_JS_SDK_TICKET||e.getQueryParam("weixinJsSdkTicket")||""),Promise.resolve(a)]).then(function(a){T.log("allRes",a);var i=a[0];if(i){var c=a[1];if(c){var p=e.generateRndNum(7),d=i,h=(new Date).getTime(),g=c("jsapi_ticket="+d+"&noncestr="+p+"×tamp="+h+"&url="+location.href);return b.config({debug:S,appId:t,timestamp:h,nonceStr:p,signature:g,jsApiList:["showOptionMenu","onMenuShareTimeline","onMenuShareAppMessage"],openTagList:y}),b.ready(function(){if(P&&(T.log("onMenuShareAppMessageOptions",P),b.onMenuShareAppMessage(P)),window.LAUNCH_APP_SHARE_APP_MESSAGE=function(e){e=o(o({},O),e),T.log("window LAUNCH_APP_SHARE_APP_MESSAGE",e),b.onMenuShareAppMessage(e)},E&&(T.log("onMenuShareTimelineOptions",E),b.onMenuShareTimeline(E)),window.LAUNCH_APP_SHARE_TIMELINE=function(e){e=o(o({},U),e),T.log("window LAUNCH_APP_SHARE_TIMELINE",e),b.onMenuShareTimeline(e)},r){window.LAUNCH_APP_READY=!0,I&&I();var e=function(){var e=n(l);T.log("containers",l,e,e.length),e.length>0&&e.each(function(e,a){var o=n(a).attr("data-launch-app-key");if(!o){var t=n(a).parents("[data-launch-app-key]:eq(0)");t.length>0&&(o=t.attr("data-launch-app-key"),T.log("keyEle",t,o))}o||(o=String(e));var i="mazey-launch-app-tag-".concat(o),c=n(".".concat(i," wx-open-launch-app")),l=n(a).attr("data-launch-app-ext-info");if(!l){var p=n(a).parents("[data-launch-app-ext-info]:eq(0)");p.length>0&&(l=p.attr("data-launch-app-ext-info"),T.log("eleExtInfoEle parents",p,l))}l&&(N=l);var d=n(a).attr("data-launch-app-error-link");if(!d){var h=n(a).parents("[data-launch-app-error-link]:eq(0)");h.length>0&&(d=h.attr("data-launch-app-error-link"),T.log("eleErrorLinkEle parents",h,d))}var g=f;d&&(g=d),0===c.length&&(n(a).addClass(i),T.log("success:",i),function(e){var a=s,o=a+e,t="<wx-open-launch-app id='"+o+"' appid='"+r+"' extinfo='".concat(N)+"' style='z-index:99;position:absolute;width:100%;height:100%;opacity:1;background:transparent;overflow:hidden;left:0;'><script type='text/wxtag-template'>"+W+"<\/script></wx-open-launch-app>";n("."+e+":eq(0)").append(t);var i=document.getElementById(o);i&&(i.addEventListener("ready",function(e){T.log("ready event",e)}),i.addEventListener("launch",function(e){T.log("launch event",e),e.detail&&e.detail.extInfo&&T.log("launch extInfo",e.detail.extInfo)}),i.addEventListener("error",function(e){T.error(e.detail),n("[id^='"+a+"']").hide(),u&&B(),g&&(T.log("realErrorLink",g),location.href=g)}),i.addEventListener("click",function(e){T.log("click event",e),k&&k()}))}(i))})};e(),H=e}else T.log("openPlatformMobileAppId is required for weixin launch app")}),b.error(function(e){T.error(e)}),"success:launch_app"}T.error("sha1 is required")}else T.error("weixinJsSdkTicket is required")}).catch(function(e){T.error(e)})}(C,m)}function z(e){if(void 0===e&&(e={}),T.log("appUpdated",e),!window.LAUNCH_APP_LOAD&&x(e)&&(j(),window.LAUNCH_APP_LOAD=!0),_&&window.LAUNCH_APP_LOAD&&x(e)&&H(),L()){var a=n(l);a.length>0&&a.each(function(e,a){var o=n(a).children("."+h);if(!o||!o.length){var t=n(a).attr("data-launch-app-error-link");if(!t){var r=n(a).parents("[data-launch-app-error-link]:eq(0)");r.length>0&&(t=r.attr("data-launch-app-error-link"))}if(t&&"string"==typeof t&&t.length){var i=R(t,"position:absolute;left:0;z-index:999;","a");n(a).append(i)}}})}}function D(){T.log("appBeforeDestroy",D),window.LAUNCH_APP_LOAD=null,window.LAUNCH_APP_HIDE_WEIXIN_BROWSER=null}return window.LAUNCH_APP_UPDATE=z,window.LAUNCH_APP_BEFORE_DESTROY=D,window.LAUNCH_APP_SHOW_WEIXIN_TO_BROWSER=B,{LAUNCH_APP_UPDATE:z,LAUNCH_APP_BEFORE_DESTROY:D,LAUNCH_APP_SHOW_WEIXIN_TO_BROWSER:B,LAUNCH_APP_SHARE_TIMELINE:function(e){T.log("opt",e)},LAUNCH_APP_SHARE_APP_MESSAGE:function(e){T.log("opt",e)},start:z,update:z,destroy:D}};