cellar-js-sdk
Version:
晒啦第三方授权工具
2 lines • 11.4 kB
JavaScript
!function(t){var e={};function i(s){if(e[s])return e[s].exports;var n=e[s]={i:s,l:!1,exports:{}};return t[s].call(n.exports,n,n.exports,i),n.l=!0,n.exports}i.m=t,i.c=e,i.d=function(t,e,s){i.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:s})},i.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},i.t=function(t,e){if(1&e&&(t=i(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var s=Object.create(null);if(i.r(s),Object.defineProperty(s,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var n in t)i.d(s,n,function(e){return t[e]}.bind(null,n));return s},i.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return i.d(e,"a",e),e},i.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},i.p="",i(i.s=1)}([function(t){t.exports=JSON.parse('{"name":"cellar-js-sdk","version":"1.2.44","description":"晒啦第三方授权工具","main":"/dist/index.js","origins":{"dev":{"origin":"http://192.168.50.89:8079","path":"","target":"http://192.168.50.18:8080","pathRewrite":"/api"},"pre":{"origin":"https://wallet-pre.maytek.cn","path":"/jssdk/index.html","target":"","pathRewrite":""},"pro":{"origin":"https://wallet.metacellar.art","path":"/jssdk/index.html","target":"","pathRewrite":""}},"scripts":{"test":"echo \\"Error: no test specified\\" && exit 1","dev":"cross-env NODE_ENV=development webpack-dev-server --config build/webpack.config.js","build":"cross-env NODE_ENV=production webpack --config build/webpack.config.js","ts":"cross-env NODE_ENV=production tsc"},"keywords":["cellar"],"author":"farkas","license":"ISC","devDependencies":{"awesome-typescript-loader":"^5.2.1","clean-webpack-plugin":"^4.0.0","cross-env":"^7.0.3","html-webpack-plugin":"^4.0.0","source-map-loader":"^4.0.1","ts-loader":"^4.4.2","typescript":"^4.9.4","webpack":"^4.41.5","webpack-cli":"^3.3.10","webpack-dev-server":"^3.10.2"},"files":["/dist","README.md"],"dependencies":{"tsconfig-paths-webpack-plugin":"^4.0.0"}}')},function(t,e,i){"use strict";var s;i.r(e),function(t){t.DEV="dev",t.PRE="pre",t.PRO="pro"}(s||(s={}));class n{constructor(t,e,i,s){this.hideStyle="display: none",this.showStyle="",this.isInit=!1,this.appId=t;let n=document.getElementById("cellar_mask_"+this.getId());n&&document.body.removeChild(n);let r=document.createElement("div");r.id="cellar_mask_"+this.getId();let o=document.createElement("div");o.id="cellar_context_"+this.getId();let a=document.createElement("iframe");a.id="cellar_iframe_"+this.getId();let l=document.createElement("style");l.innerHTML=this.initStyle(),this.env=e,this.style=l,this.mask=r,this.container=o,this.iframe=a,this.origin=i,a.setAttribute("src",s),a.setAttribute("frameborder","0"),a.setAttribute("scrolling","no"),this.hideWindow(),this.hideIframe(),o.insertBefore(l,o.firstElementChild),o.insertBefore(a,o.firstElementChild),r.insertBefore(o,r.firstElementChild),document.body.insertBefore(r,document.body.firstElementChild),window.addEventListener("message",this.messageListener(),!1)}log(...t){this.env!==s.PRE&&this.env!==s.DEV||console.log(t)}initStyle(){return`\n #cellar_iframe_${this.getId()}::-webkit-scrollbar {\n visibility: hidden;\n }\n \n #cellar_mask_${this.getId()} {\n position: fixed;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n background-color: rgba(247, 247, 247, 0.8);\n z-index: 99999;\n }\n \n {\n #cellar_mask_${this.getId()} #cellar_context_${this.getId()} {\n position: fixed;\n top: 50%;\n left: 50%;\n width: 375px;\n height: 667px;\n margin-top: -334.5px;\n margin-left: -187.5px;\n }\n \n #cellar_mask_${this.getId()} #cellar_context_${this.getId()} #cellar_iframe_${this.getId()} {\n width: 100%;\n height: 100%;\n overflow: hidden;\n border-radius: 20px;\n box-shadow: 0 0 1px 1px #cdcdcd;\n }\n }\n \n {\n #cellar_mask_${this.getId()} #cellar_context_${this.getId()} {\n position: fixed;\n width: 100%;\n height: 100%;\n }\n \n #cellar_mask_${this.getId()} #cellar_context_${this.getId()} #cellar_iframe_${this.getId()} {\n width: 100%;\n height: 100%;\n overflow: hidden;\n }\n }\n `}getId(){return this.appId}getMask(){return this.mask}getIframe(){return this.iframe}getOrigin(){return this.origin}styleHide(t){t.setAttribute("style",this.hideStyle)}styleShow(t){t.setAttribute("style",this.showStyle)}showWindow(t,e){var i;this.callback=e,this.getInitState()&&(null===(i=this.getIframe().contentWindow)||void 0===i||i.postMessage(t.data,t.origin)),this.styleShow(this.getMask())}showIframe(){this.styleShow(this.getIframe())}hideIframe(){this.styleHide(this.getIframe())}hideWindow(){this.callback=void 0,this.styleHide(this.getMask())}initDone(){this.isInit=!0}getInitState(){return this.isInit}messageListener(){let t=this,e=t.getOrigin();return function(i){let s=t.getIframe();if(s){let n=s.contentWindow;if(n&&i.origin===e&&i.data){let e={};"string"==typeof i.data&&0===i.data.indexOf("{")&&i.data.lastIndexOf("}")===i.data.length-1?e=JSON.parse(i.data):"object"==typeof i.data&&(e=i.data),e.method&&("appInit"===e.method?t.initDone():"show"===e.method?t.showIframe():"close"===e.method?t.hideWindow():"messageBack"===e.method&&e&&t.log("iframe收到消息",e),t.callback&&t.callback(n,e))}else t.log("messageOrigin",i.origin),t.log("contextOrigin",e)}}}}var r=i(0),o=function(t,e,i,s){return new(i||(i=Promise))((function(n,r){function o(t){try{l(s.next(t))}catch(t){r(t)}}function a(t){try{l(s.throw(t))}catch(t){r(t)}}function l(t){var e;t.done?n(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(o,a)}l((s=s.apply(t,e||[])).next())}))};class a{constructor({appId:t,env:e,walletList:i=!1}){this.isLogin=!1,this.appId=t,this.walletList=i,e?e===s.DEV&&(e=s.PRE):e=s.PRO,this.env=e;let o=r.origins[this.env];this.origin=o.origin,this.url=this.origin+o.path+"?v="+(new Date).getTime()+"/#/?env="+this.getEnv(),this.cellarWindow=new n(this.appId,this.getEnv(),this.origin,this.url),this.isLogin="1"===this.getLocalStorage("isLogin"),this.isLogin&&(this.userCode=this.getLocalStorage("userCode"),this.userToken=this.getLocalStorage("userToken"),this.userWallet=this.getLocalStorage("userWallet"),this.authorityCode=this.getLocalStorage("authorityCode"),this.phone=this.getLocalStorage("phone"))}log(...t){this.getEnv()!==s.PRE&&this.getEnv()!==s.DEV||console.log(t)}localStorageKey(t){return this.getAppid()+"_"+t}getLocalStorage(t){return localStorage.getItem(this.localStorageKey(t))}setLocalStorage(t,e){localStorage.setItem(this.localStorageKey(t),e)}getAppid(){return this.appId}getWalletList(){return this.walletList}getUserCode(){return this.userCode}getUserToken(){return this.userToken}getUserWallet(){return this.userWallet}getAuthorityCode(){return this.authorityCode}getLoginStatus(){return this.isLogin}getPhone(){return this.phone}getCallbackUserInfo(){return{userCode:this.getUserCode(),userWallet:this.getUserWallet(),authorityCode:this.getAuthorityCode(),phone:this.getPhone(),userToken:this.getUserToken()}}getOrigin(){return this.origin}getWindow(){return this.cellarWindow}getEnv(){return this.env}setUserInfo(t,e,i,s,n){this.userCode=t,this.userToken=e,this.userWallet=i,this.authorityCode=s,this.phone=n,this.userCode&&this.userToken&&this.userWallet&&this.authorityCode&&this.phone?(this.isLogin=!0,this.setLocalStorage("userCode",this.userCode),this.setLocalStorage("userToken",this.userToken),this.setLocalStorage("userWallet",this.userWallet),this.setLocalStorage("authorityCode",this.authorityCode),this.setLocalStorage("phone",this.phone)):this.isLogin=!1,this.setLocalStorage("isLogin",this.isLogin?"1":"")}clearUserInfo(){this.isLogin=!1,this.userCode="",this.userToken="",this.userWallet="",this.authorityCode="",this.phone="",this.setLocalStorage("isLogin",""),this.setLocalStorage("userCode",""),this.setLocalStorage("userToken",""),this.setLocalStorage("userWallet",""),this.setLocalStorage("authorityCode",""),this.setLocalStorage("phone","")}cfx_accounts(){return o(this,void 0,void 0,(function*(){return yield new Promise((t,e)=>{if(this.getLoginStatus())return void t(this.getCallbackUserInfo());let i={data:JSON.stringify({method:"initAppInfo",data:{appId:this.getAppid(),env:this.getEnv(),walletList:this.getWalletList(),type:"auth",pathName:"index"}}),origin:this.getOrigin()};this.getWindow().showWindow(i,(s,n)=>{let r=n.data;"done"===n.method?r?(this.setUserInfo(r.userCode,r.userToken,r.userWallet,r.authorityCode,r.phone),this.getLoginStatus()?t(this.getCallbackUserInfo()):e("登录失败")):e("登录失败"):"appInit"===n.method?s.postMessage(i.data,i.origin):"error"===n.method&&r&&e(r.msg)})})}))}cfx_sendTransaction(t){return o(this,void 0,void 0,(function*(){return yield new Promise((e,i)=>{if(!this.getLoginStatus())return void i("无登录信息,请登录");let s;if(s=t instanceof Array&&t.length>0?t[0]:t,!s)return void i("参数错误");if(!s.data)return void i("参数错误");s.from||(s.from=this.getUserWallet());let n={data:JSON.stringify({method:"initAppInfo",data:{appId:this.getAppid(),userCode:this.getUserCode(),userToken:this.getUserToken(),authorityCode:this.getAuthorityCode(),phone:this.getPhone(),env:this.getEnv(),walletList:this.getWalletList(),type:"call",pathName:"operAuth",transactionParams:{data:s.data,from:s.from,to:s.to}}}),origin:this.getOrigin()};this.getWindow().showWindow(n,(t,s)=>{let r=s.data;"done"===s.method?e(null==r?void 0:r.hash):"appInit"===s.method?t.postMessage(n.data,n.origin):"error"===s.method&&r&&i(r.msg)})})}))}cellar_loginState(t){return o(this,void 0,void 0,(function*(){return yield new Promise((e,i)=>{t?e(this.getCallbackUserInfo()):i("登录信息不存在,请重新授权登录")})}))}cellar_loginOut(){return o(this,void 0,void 0,(function*(){return yield new Promise(t=>{this.clearUserInfo(),t(void 0)})}))}cellar_version(){return o(this,void 0,void 0,(function*(){return yield new Promise(t=>{t(r.version+"."+this.getEnv().toUpperCase())})}))}request({method:t,params:e}){if(!this.getAppid())throw new Error("初始化失败, 请检查参数是否正确");switch(t){case"cellar_loginState":return this.cellar_loginState(this.getLoginStatus());case"cfx_accounts":return this.cfx_accounts();case"cfx_sendTransaction":return this.cfx_sendTransaction(e);case"cellar_version":return this.cellar_version();case"cellar_loginOut":return this.cellar_loginOut();default:throw new Error("无效的函数")}}}i.d(e,"Cellar",(function(){return a})),i.d(e,"CellarEnv",(function(){return s})),window.Cellar=a,window.CellarEnv=s}]);
//# sourceMappingURL=cellar-js-sdk.umd.min.js.map