zmp-sdk
Version:
Zalo Mini App SDK
2 lines (1 loc) • 8.96 kB
JavaScript
import e from'./../external/@swc/helpers/src/_async_to_generator.mjs.js';import n from'./../external/@swc/helpers/src/_class_call_check.mjs.js';import t from'./../external/@swc/helpers/src/_create_class.mjs.js';import s from'./../external/@swc/helpers/src/_object_spread.mjs.js';import r from"../utils/lodash.js";import{COOKIE_NAME as o,APIS as i,APP_ID as c,ZAPP_ID as a,API as u,ApiCallStage as l,MINI_APP_INTERNAL_LINK as m}from"../constants.js";import{logger as p}from"./logger.js";import f from"./call/zaloNative.js";import{apiResponse as h}from"../utils/response.js";import{sendLogData as d}from"./utils.js";import{getDynamicAPIByAction as g}from"../utils/common.js";import T from"./apis/general/authorize.js";import _ from"./apis/general/getSetting.js";import{__generator as k}from'./../external/tslib/tslib.es6.js';import{JumpStatus as v}from"../types/enum.js";var A=function(){function A(){n(this,A),this._jumpStatus=void 0,this.WAITING_QUEUE=[],this.getAccessTokenCount=0,this._accessToken="",this._jsAccessToken="",this.refreshToken="",this._cookies=[],this.accessTokenExpiresIn=0,this.prevGetAccessTokenTimestamp=(new Date).getTime(),this.manualSetAccessToken=!1,this._miniProgramConfig={},this.retryLimitMap={}}var w=A.prototype;return w.initRetryLimit=function(e){var n=u[e],t={lastCall:(new Date).getTime(),limit:n&&n.limit?n.limit:3,retry:0};return this.retryLimitMap[e]=t,t},w.resetRetryLimit=function(e){this.retryLimitMap[e].lastCall=(new Date).getTime(),this.retryLimitMap[e].retry=0},w.getRetryLimit=function(e){var n=this.retryLimitMap[e];return n&&n.lastCall&&n.retry||(n=this.initRetryLimit(e)),n},w.increaseRetryLimit=function(e){this.retryLimitMap[e].retry++},w.jump=function(){var n=this;return e((function(){return k(this,(function(e){return[2,new Promise((function(e,t){n._jumpStatus?n._jumpStatus===v.DONE?e(v.DONE):e(v.DOING):(n._jumpStatus=v.DOING,p({stage:l.REQUEST,type:"log",name:"jump"}),f("JUMP_LOGIN",{},{success:function(t){var s=t.data;n._miniProgramConfig=(null==s?void 0:s.miniProgramConfig)||{};var r=(null==s?void 0:s.cookiesOAuthLogins)||[],i=r.find((function(e){return e.name===o.JS_TOKEN}));i&&(n._jsAccessToken=i.value,window.ZJSBridge.setJSToken&&window.ZJSBridge.setJSToken(i.value)),n._cookies=r;var c=n._cookies.find((function(e){return e.name===o.ZOAUTH}));(null==c?void 0:c.value)||d([{action:"action.jump.login",error:-102,message:"no zoauth",data:{}}]),e(v.DONE)},fail:function(e){d([{action:"action.jump.login",error:-101,message:e.message||"jump error",data:{}}]),t(e)}},{skipJump:!0}))}))]}))}))()},w.loginZaloV3=function(n,t,s){var r=this;return e((function(){var o;return k(this,(function(c){switch(c.label){case 0:return o="".concat(i.GET_ACCESS_TOKEN_V3,"?app_id=").concat(t,"&redirect_uri=").concat(m,"/").concat(n,"/&code=").concat(s,"&isSDK=true"),[4,fetch(o).then((a=e((function(e){var n,t,s;return k(this,(function(o){switch(o.label){case 0:return[4,e.json()];case 1:return n=o.sent(),t=null==n?void 0:n.access_token,r._accessToken=t,s=1e3*parseInt(n.expires_in),r.accessTokenExpiresIn=s,r.prevGetAccessTokenTimestamp=(new Date).getTime(),window.ZJSBridge.setZAccSession&&window.ZJSBridge.setZAccSession(r._accessToken||""),[2,t]}}))})),function(e){return a.apply(this,arguments)}))];case 1:return[2,c.sent()]}var a}))}))()},w.loginZaloV4=function(n){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"",o=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"",c=this;return e((function(){var a,u;return k(this,(function(l){switch(l.label){case 0:return(a=new URLSearchParams).append("app_id",n),a.append("code",t),a.append("code_verifier",o),r.isNull(c.refreshToken)||0===c.refreshToken.length?a.append("grant_type","authorization_code"):(a.append("grant_type","refresh_token"),a.append("refresh_token",c.refreshToken||"")),u={headers:{"Content-Type":"application/x-www-form-urlencoded"}},[4,fetch(i.GET_ACCESS_TOKEN,s({method:"POST",body:a},u)).then((m=e((function(e){var n,t,s,r;return k(this,(function(o){switch(o.label){case 0:return[4,e.json()];case 1:return n=o.sent(),t=(null==n?void 0:n.access_token)||"",s=(null==n?void 0:n.refresh_token)||"",c.refreshToken=s,c._accessToken=t,r=1e3*parseInt(null==n?void 0:n.expires_in),c.accessTokenExpiresIn=r,c.prevGetAccessTokenTimestamp=(new Date).getTime(),window.ZJSBridge.setZAccSession&&window.ZJSBridge.setZAccSession(t),[2,t]}}))})),function(e){return m.apply(this,arguments)}))];case 1:return[2,l.sent()]}var m}))}))()},w.getAccessToken=function(){var n=arguments.length>0&&void 0!==arguments[0]&&arguments[0],t=this;return e((function(){var s,r,i,u,l,m,p,f,g;function T(){return _.apply(this,arguments)}function _(){return(_=e((function(){return k(this,(function(e){switch(e.label){case 0:return[4,new Promise((function(e){var n=setInterval((function(){(r.getAccessTokenCount<=1||r._accessToken)&&(e("done"),clearInterval(n))}),200)}))];case 1:return[2,e.sent()]}}))}))).apply(this,arguments)}return k(this,(function(e){switch(e.label){case 0:return s=(new Date).getTime()-t.prevGetAccessTokenTimestamp,(r=t).getAccessTokenCount++,t.getAccessTokenCount>1?[4,T()]:[3,2];case 1:e.sent(),e.label=2;case 2:return t.manualSetAccessToken?(r.getAccessTokenCount--,[2,Promise.resolve(t._accessToken)]):n?[4,t.verifyUserAuthorized()]:[3,4];case 3:return u=e.sent(),[3,5];case 4:u=!0,e.label=5;case 5:return i=u,t._accessToken.length>0&&s<t.accessTokenExpiresIn&&i?(r.getAccessTokenCount--,[2,Promise.resolve(t._accessToken)]):(t.accessToken="",[4,t.jumpAndGetToken(o.ZOAUTH)]);case 6:return l=e.sent(),[4,t.jumpAndGetToken(o.ZOAUTH_VRF)];case 7:if(m=e.sent(),p="zmp.getaccesstoken.fail",!i)throw r.getAccessTokenCount--,d([{action:p,error:-104,message:"no user auth",data:{}}]),h.error.loginFailed("User Authentication Required");if(!l)return[3,15];e.label=8;case 8:return e.trys.push([8,13,,14]),m?[4,t.loginZaloV4(c,l,m)]:[3,10];case 9:return f=e.sent(),[3,12];case 10:return[4,t.loginZaloV3(c,a,l)];case 11:f=e.sent(),e.label=12;case 12:return[3,14];case 13:return g=e.sent(),f="",console.log("Can not get access token",g),d([{action:p,error:-103,message:String(g)||"Can not get access token",data:{}}]),[3,14];case 14:return r.getAccessTokenCount--,f?[2,Promise.resolve(t._accessToken)]:(d([{action:p,error:-101,message:"no accessToken",data:{}}]),[2,Promise.reject(h.error.loginFailed("Can't get accessToken. Please try again later."))]);case 15:throw r.getAccessTokenCount--,d([{action:p,error:-102,message:"no oauth",data:{}}]),h.error.loginFailed("Zalo app has not been activated")}}))}))()},w.verifyUserAuthorized=function(){var n=this;return e((function(){var e,t,s,r,o,i;return k(this,(function(c){switch(c.label){case 0:return t=!0,[4,n.jumpAndGetToken()];case 1:if(c.sent(),!1!==(null===(e=n._miniProgramConfig)||void 0===e?void 0:e.userAuthorized))return[3,7];c.label=2;case 2:return c.trys.push([2,6,,7]),(s=g("action.mp.user.authorize"))&&s.isSupported?[4,_()]:[3,5];case 3:return!1!==(null==(o=c.sent())||null===(r=o.authSetting)||void 0===r?void 0:r["scope.userInfo"])?[3,5]:[4,T()];case 4:i=c.sent(),t=(null==i?void 0:i["scope.userInfo"])||!1,c.label=5;case 5:return[3,7];case 6:return c.sent(),t=!1,[3,7];case 7:return[2,t]}}))}))()},w.setAccessToken=function(e){this.manualSetAccessToken=!0,this._accessToken=e,window.ZJSBridge.setZAccSession&&window.ZJSBridge.setZAccSession(e)},w.getJSAccessToken=function(){var n=this;return e((function(){var e;return k(this,(function(t){switch(t.label){case 0:return n._jsAccessToken.length>0?[2,Promise.resolve(n._jsAccessToken)]:[4,n.jumpAndGetToken(o.JS_TOKEN)];case 1:return e=t.sent(),[2,Promise.resolve(e)]}}))}))()},w.getSync=function(e){var n;return null===(n=this._cookies.find((function(n){return n.name===e})))||void 0===n?void 0:n.value},w.jumpAndGetToken=function(n){var t,s=arguments.length>1&&void 0!==arguments[1]&&arguments[1],o=this;return new Promise((t=e((function(e){var t,i,c,a;return k(this,(function(u){switch(u.label){case 0:return!s&&o._cookies.length>0?r.isString(n)?(t=o._cookies.find((function(e){return e.name===n})),[2,e(null==t?void 0:t.value)]):[2,e("")]:(s&&o._jumpStatus===v.DONE&&(o._jumpStatus=void 0),i={name:n,cb:function(n){return e(n)}},[4,o.jump()]);case 1:return(c=u.sent())===v.DONE?(o._jumpStatus=c,o.WAITING_QUEUE.length&&(o.WAITING_QUEUE.forEach((function(e){var n=o._cookies.find((function(n){return n.name===e.name}));e.cb(null==n?void 0:n.value)})),o.WAITING_QUEUE=[]),a=o._cookies.find((function(e){return e.name===i.name})),i.cb(null==a?void 0:a.value)):o.WAITING_QUEUE.push(i),[2]}}))})),function(e){return t.apply(this,arguments)}))},A.getInstance=function(){return A.instance||(A.instance=new A),A.instance},t(A,[{key:"jumpStatus",get:function(){return this._jumpStatus}},{key:"accessToken",set:function(e){this._accessToken=e,window.ZJSBridge.setZAccSession&&window.ZJSBridge.setZAccSession(e)}},{key:"miniProgramConfig",get:function(){return this._miniProgramConfig}}]),A}(),w=A.getInstance();export{w as default};