UNPKG

zkey.webapp.core

Version:

基于Vue的框架Webapp框架核心,控制应用架构

1 lines 5.21 kB
"use strict";function _classCallCheck(e,s){if(!(e instanceof s))throw new TypeError("Cannot call a class as a function")}var Session=function(){function e(){_classCallCheck(this,e)}return e.prototype.set=function(e,s){var t={data:s};try{sessionStorage.setItem(e,JSON.stringify(t))}catch(i){sessionStorage&&alert("系统环境异常!,请尝试关闭无痕模式!")}},e.prototype.get=function(e){var s={};return s=sessionStorage.getItem(e),null!=s?s=JSON.parse(s):(s={},s.data=null),s.data},e.prototype.remove=function(e){sessionStorage.removeItem(e)},e.prototype.clear=function(){sessionStorage.clear()},e}(),Device=function e(s){_classCallCheck(this,e);var t=navigator.userAgent.toLowerCase(),i=s.get("ZK_isInWebView");if(null==i){var r=window.location.href;r.indexOf("__userid")>0&&r.indexOf("__token")>0&&(i=!0,s.set("ZK_isInWebView",i))}else i=!1;var n={isWX:"micromessenger"==t.match(/MicroMessenger/i),isIE:t.indexOf("trident")>-1,browserVer:0,isOpera:t.indexOf("presto")>-1,isWebKit:t.indexOf("applewebkit")>-1,isFix:t.indexOf("gecko")>-1&&-1==t.indexOf("KHTML")&&t.indexOf("firefox")>-1,isMobile:!!t.match(/AppleWebKit.*Mobile.*/i),isSafari:!1,isQQ:" qq"==t.match(/\sQQ/i),isIOS:!!t.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/i),isAndroid:t.indexOf("android")>-1||t.indexOf("adr")>-1,isInWebView:i||!1};try{if(n.isIE){var o=/(msie\s|trident.*rv:)([\w.]+)/.exec(t);n.browserVer=o[2]||"0"}else if(n.isFix){var o=/(firefox)\/([\w.]+)/.exec(t);n.browserVer=o[2]||"0"}else if(n.isOpera){var o=/(opera).+version\/([\w.]+)/.exec(t);n.browserVer=o[2]||"0"}else{var o=/(chrome)\/([\w.]+)/.exec(t);null!=o&&(n.isChrome=!0,n.browserVer=o[2]||"0");var a=/version\/([\w.]+).*(safari)/.exec(t);null!=a&&(n.isSafari=!0,n.browserVer=a[1]||"0")}}catch(c){console.log("检测游览器信息失败!",c)}var u=window.document,h=u.documentElement;for(var f in n)if(n.hasOwnProperty(f)&&1==n[f]){var l=f.toLocaleLowerCase().replace("is","-");h.setAttribute("env"+l,!0)}this.isWX=n.isWX,this.isIE=n.isIE,this.browserVer=n.browserVer,this.isOpera=n.isOpera,this.isWebKit=n.isWebKit,this.isFix=n.isFix,this.isMobile=n.isMobile,this.isSafari=n.isSafari,this.isQQ=n.isQQ,this.isIOS=n.isIOS,this.isAndroid=n.isAndroid,this.isIOS=n.isIOS,this.isInWebView=n.isInWebView,this.isChrome=n.isChrome,this.isSafari=n.isSafari},General=function(){function e(){_classCallCheck(this,e),this.sessionStorage=new Session,this.device=new Device(this.sessionStorage)}return e.create=function(){return this.instance||(this.instance=new e),this.instance},e}(),Notice=function(){function e(){_classCallCheck(this,e),this.subjects={},window.onZKBack=function(){_self.publish("zkBack")}}return e.create=function(){return this.instance||(this.instance=new e),this.instance},e.prototype.addObserver=function(e,s,t){this.subjects[e]||(this.subjects[e]={observers:{},func:[]}),t&&(this.subjects[e].observers[t]||(this.subjects[e].observers[t]={}),this.subjects[e].observers[t].func||(this.subjects[e].observers[t].func=[]),this.subjects[e].observers[t].func.push(s)),this.subjects[e].func.push(s)},e.prototype.publish=function(e,s,t){var i=this;setTimeout(function(){if(!i.subjects[e])return!1;if(t)for(var r=0,n=i.subjects[e][t].func.length;n>r;r++)i.subjects[e][t].func[r](s,t);else for(var r=0,n=i.subjects[e].func.length;n>r;r++)i.subjects[e].func[r](s)},1)},e}(),Common=function(){function e(){_classCallCheck(this,e),this.code=1,this.readyState=!1}return e.create=function(){return this.instance||(this.instance=new e),this.instance},e.prototype.getCode=function(){return this.code++,this.code},e}(),general=General.create(),notice=Notice.create(),common=Common.create(),HyApp=function(){function e(){_classCallCheck(this,e),window.$HyApp||(window.$HyApp=this),this.requests={},this.timeOut=5e3}return e.create=function(){return this.instance||(this.instance=new e),this.instance},e.prototype.excute=function(e,s,t,i,r){var n=this,o="HyApp_re_"+common.getCode();this.requests[o]={className:e,methodName:s,sucessCallback:function(e){i&&i(e)},errorCallback:function(e,s){clearTimeout(n.requests[o].timer),r&&r(e,s)}},setTimeout(function(){try{if(general.device.isIOS){var i={className:e,methodName:s,id:o,params:t};window.webkit.messageHandlers.HyAppNative.postMessage(JSON.stringify(i))}else{if("object"!=typeof t)throw new Error("params 必须是对象");t=JSON.stringify(t),window.HyAppNative.excute(e,s,o,t)}}catch(r){n.requests[o].errorCallback(404,"调用原生Api失败!请求Id:"+o)}},1),this.requests[o].timer=setTimeout(function(){n.requests[o].errorCallback(504,"请求Native超时!请求Id:"+o),delete n.requests[o]},this.timeOut)},e.prototype.nativeSendResult=function(e,s){var t=this;setTimeout(function(){try{if(!t.requests.hasOwnProperty(e))throw new Error("请求不存在");if(clearTimeout(t.requests[e].timer),"string"!=typeof s)throw new Error("result 必须是JSON字符串");s=JSON.parse(s),t.requests[e].sucessCallback(s)}catch(i){t.requests[e].errorCallback(500,i)}},1)},e.prototype.publish=function(e,s){setTimeout(function(){if("string"!=typeof s)throw new Error("result 必须是JSON字符串");s=JSON.parse(s),notice.publish(e,s)},1)},e}();window.ZKWebappCore={HyApp:HyApp,Common:Common,General:General,Notice:Notice};