UNPKG

@zeppos/zml

Version:

A Mini Library of ZeppOS MiniApp

2 lines (1 loc) 2.04 kB
function e(e,n={}){return this.request({method:"http.request",params:e},n)}function n(){return i()&&s()}function t(){return i()&&o()}function s(){return"undefined"!=typeof hmApp}function o(){return"undefined"!=typeof __$$R$$__}function i(){return s()||o()}let a=null;a="undefined"!=typeof __$$R$$__?__$$R$$__:()=>({}),n()?hmApp.getPackageInfo:t()&&a("@zos/app").getPackageInfo,n()?hmUI:t()&&a("@zos/ui"),n()?hmSetting:t()&&a("@zos/settings"),n()?px:a("@zos/utils").px,n()?hmSetting.getDeviceInfo:t()&&a("@zos/device").getDeviceInfo,n()?"undefined"!=typeof __$$app$$__&&__$$app$$__:t()&&a("@zos/i18n").getText,n()?hmApp.gotoPage:t()&&a("@zos/router").push;let r=null;n()?r=hmApp:t()&&(r=a("@zos/app"));const h=Promise;function u(){let e="";for(let n=0;n<12;n++)e+=0===n?Math.floor(9*Math.random())+1:Math.floor(10*Math.random());return e}function l(n){const t=function(){const{messaging:e}=getApp()._options.globalData;return e}();return{onInit(){this.messaging=this.state.messaging=t,this._onCall=this.onCall?.bind(this),this._onRequest=this.onRequest?.bind(this),this._onBleChanged=this.onBleChanged?.bind(this),this.messaging.onCall(this._onCall).onRequest(this._onRequest).onBleChanged(this._onBleChanged)},onDestroy(){this._onCall&&this.messaging.offOnCall(this._onCall),this._onRequest&&this.messaging.offOnRequest(this._onRequest),this._onBleChanged&&this.messaging.offOnBleChanged(this._onBleChanged)},request(e,n={}){const t=function(){const e={canceled:!1};return e.promise=new h((function(n,t){e.resolve=n,e.reject=t})),e.cancel=()=>{e.canceled=!0,e.reject(new Error("Task canceled"))},e}(),s=u(),o="response:result:"+s,i="response:error:"+s;return r.onMessage(o,(function(e,...n){t.resolve(...n)})),r.onMessage(i,(function(e,...n){t.reject(...n)})),this.messaging.request(e,n).then((e=>{r.postMessage(o,e)})).catch((e=>{r.postMessage(i,e)})).finally((()=>{r.offMessage(o),r.offMessage(i)})),t.promise},call(e){return this.messaging.call(e)},httpRequest:e}}const g="3.0";export{g as API_LEVEL,u as generateRandom12,l as pagePlugin};