"use strict";vare,n=(e=require("crypto"))&&"object"==typeofe&&"default"ine?e.default:e;constt={TOKEN_EXPIRED:"uni-id-token-expired",CHECK_TOKEN_FAILED:"uni-id-check-token-failed",PARAM_REQUIRED:"uni-id-param-required",ACCOUNT_EXISTS:"uni-id-account-exists",ACCOUNT_NOT_EXISTS:"uni-id-account-not-exists",ACCOUNT_CONFLICT:"uni-id-account-conflict",ACCOUNT_BANNED:"uni-id-account-banned",ACCOUNT_AUDITING:"uni-id-account-auditing",ACCOUNT_AUDIT_FAILED:"uni-id-account-audit-failed",ACCOUNT_CLOSED:"uni-id-account-closed"};functioni(e){return!!e&&("object"==typeofe||"function"==typeofe)&&"function"==typeofe.then}constr={"uni-id-token-expired":30203,"uni-id-check-token-failed":30202};functiono(e){const{errCode:n,errMsgValue:t}=e;e.errMsg=this._t(n,t),ninr&&(e.code=r[n])}functionc(e){return"object"===(i=e,Object.prototype.toString.call(i).slice(8,-1).toLowerCase())&&e.errCode&&(n=e.errCode,Object.values(t).includes(n))&&!!e.errCode;varn,i}lets={"zh-Hans":{"uni-id-token-expired":"登陆状态失效,token已过期","uni-id-check-token-failed":"token校验未通过","uni-id-param-required":"缺少参数: {param}","uni-id-account-exists":"此账号已注册","uni-id-account-not-exists":"此账号未注册","uni-id-account-conflict":"用户账号冲突","uni-id-account-banned":"从账号已封禁","uni-id-account-auditing":"此账号正在审核中","uni-id-account-audit-failed":"此账号审核失败","uni-id-account-closed":"此账号已注销"},en:{"uni-id-token-expired":"The login status is invalid, token has expired","uni-id-check-token-failed":"Check token failed","uni-id-param-required":"Parameter required: {param}","uni-id-account-exists":"Account exists","uni-id-account-not-exists":"Account does not exists","uni-id-account-conflict":"User account conflict","uni-id-account-banned":"Account has been banned","uni-id-account-auditing":"Account audit in progress","uni-id-account-audit-failed":"Account audit failed","uni-id-account-closed":"Account has been closed"}};try{conste=require.resolve("uni-config-center/uni-id/lang/index.js");s=function(e,n){constt=Object.keys(e);t.push(...Object.keys(n));consti={};for(letr=0;r<t.length;r++){consto=t[r];i[o]=Object.assign({},e[o],n[o])}returni}(s,require(e))}catch(e){}vara=s;functionu(e){returne.replace(/=/g,"").replace(/\+/g,"-").replace(/\//g,"_")}functiond(e){returnJSON.parse((n=function(e){varn=4-(e=e.toString()).length%4;if(4!==n)for(vart=0;t<n;++t)e+="=";returne.replace(/-/g,"+").replace(/_/g,"/")}(e),Buffer.from(n,"base64").toString("utf-8")));varn}functionl(e){returnu((n=JSON.stringify(e),Buffer.from(n,"utf-8").toString("base64")));varn}functionf(e,t){returnu(n.createHmac("sha256",t).update(e).digest("base64"))}constp=function(e,n){if("string"!=typeofe)thrownewError("Invalid token");constt=e.split(".");if(3!==t.length)thrownewError("Invalid token");const[i,r,o]=t;if(f(i+"."+r,n)!==o)thrownewError("Invalid token");constc=d(i);if("HS256"!==c.alg||"JWT"!==c.typ)thrownewError("Invalid token");consts=d(r);if(1e3*s.exp<Date.now()){conste=newError("Token expired");throwe.name="TokenExpiredError",e}returns},h=function(e,n,t={}){const{expiresIn:i}=t;if(!i)thrownewError("expiresIn is required");constr=parseInt(Date.now()/1e3),o={...e,iat:r,exp:r+t.expiresIn},c=l({alg:"HS256",typ:"JWT"})+"."+l(o);returnc+"."+f(c,n)},g=uniCloud.database(),C=g.command,E=g.collection("uni-id-users"),I=g.collection("uni-id-roles");asyncfunction_(e){const{tokenSecret:n,tokenExpiresIn:i,tokenExpiresThreshold:r}=this._getConfig();if(r>i)thrownewError("Config error, tokenExpiresThreshold should be less than tokenExpiresIn");leto;try{o=p(e,n)}catch(e){if("TokenExpiredError"===e.name)throw{errCode:t.TOKEN_EXPIRED};throw{errCode:t.CHECK_TOKEN_FAILED}}returno}asyncfunctionk(e){constn=e.uid,i=(awaitE.doc(n).get()).data[0];if(!i)throw{errCode:t.CHECK_TOKEN_FAILED};if(i.valid_token_date&&i.valid_token_date>1e3*e.iat)throw{errCode:t.TOKEN_EXPIRED};const{role:r,permission:o}=awaitw(i);returnT.call(this,{uid:n,role:r,permission:o})}asyncfunctionw(e){if(!e)throw{errCode:t.ACCOUNT_NOT_EXISTS};!function(e){switch(e.status){casevoid0:case0:break;case1:throw{errCode:t.ACCOUNT_BANNED};case2:throw{errCode:t.ACCOUNT_AUDITING};case3:throw{errCode:t.ACCOUNT_AUDIT_FAILED};case4:throw{errCode:t.ACCOUNT_CLOSED}}}(e);constn=e.role||[];if(0===n.length)return{role:[],permission:[]};if(n.includes("admin"))return{role:["admin"],permission:[]};consti=awaitI.where({role_id:C.in(n)});varr;return{role:n,permission:(r=i.data.reduce((e,n)=>{n.permission&&e.push(...n.permission)},[]),Array.from(newSet(r)))}}asyncfunctionm({uid:e,signContent:n,tokenSecret:t,tokenExpiresIn:i}={}){constr=Date.now(),o=h(n,t,{expiresIn:i});returnawaitE.doc(e).update({last_login_ip:this._clientInfo.clientIP,last_login_date:r}),{token:o,tokenExpired:r+1e3*i}}asyncfunctionT({uid:e,role:n,permission:i}={}){const{tokenSecret:r,tokenExpiresIn:o,tokenExpiresThreshold:c}=this._getConfig();if(!e)throw{errCode:t.PARAM_REQUIRED,errMsgValue:{param:"uid"}};if(c>o)thrownewError("Config error, tokenExpiresThreshold should be less than tokenExpiresIn");if(!n||!i){constt=(awaitE.doc(e).get()).data[0],r=awaitw(t);n=r.role,i=r.permission}if(!this.interceptorMap.has("customToken"))returnm.call(this,{uid:e,signContent:{uid:e,role:n,permission:i},tokenSecret:r,tokenExpiresIn:o});consts=this.interceptorMap.get("customToken");if("function"==typeofs)thrownewError("Invalid custom token file");consta=awaits({uid:e,role:n,permission:i});returnm.call(this,{uid:e,signContent:a,tokenSecret:r,tokenExpiresIn:o})}varx=Object.freeze({__proto__:null,checkToken:asyncfunction(e,{autoRefresh:n=!0}={}){constt=await_.call(this,e),{tokenExpiresThreshold:i}=this._getConfig();if(!i||!n)return{code:0,errCode:0,...t};constr=Date.now();leto={};return1e3*t.exp-r<1e3*i&&(o=awaitk.call(this,t)),{code:0,errCode:0,...t,...o}},createToken:asyncfunction({uid:e,role:n,permission:t}={}){returnT.call(this,{uid:e,role:n,permission:t})},refreshToken:asyncfunction({token:e}={}){constn=await_.call(this,e);return{errCode:0,...awaitk.call(this,n)}}});constA=require("uni-config-center")({pluginId:"uni-id"});classO{constructor({context:e,clientInfo:n,config:t}={}){this._clientInfo=e?function(e){return{appId:e.APPID,platform:e.PLATFORM,locale:e.LOCALE,clientIP:e.CLIENTIP,deviceId:e.DEVICEID}}(e):n,this.config=t||this._getOriginConfig(),this.interceptorMap=newMap,A.hasFile("custom-token.js")&&this.setInterceptor("customToken",require(A.resolve("custom-token.js"))),this._i18n=uniCloud.initI18n({locale:this._clientInfo.locale,fallbackLocale:"zh-Hans",messages:a})}_t(...e){returnthis._i18n.t(...e)}_getOriginConfig(){if(A.hasFile("config.json")){lete;try{e=A.config()}catch(e){thrownewError("Invalid uni-id config file\n"+e.messages)}returnArray.isArray(e)?e:e[0]?Object.values(e):e}thrownewError("Invalid uni-id config file")}_getAppConfig(){conste=this._getOriginConfig();returnArray.isArray(e)?e.find(e=>e.dcloudAppid===this.clientInfo.appId)||e.find(e=>e.isDefaultConfig):e}_getPlatformConfig(){conste=this._getAppConfig();"app-plus"===this._clientInfo.platform&&(this._clientInfo.platform="app"),"h5"===this._clientInfo.platform&&(this._clientInfo.platform="web");constn=Object.assign({tokenExpiresIn:7200,tokenExpiresThreshold:1200,passwordErrorLimit:6,passwordErrorRetryTime:3600},e,e[this._clientInfo.platform]);return["tokenSecret","tokenExpiresIn"].forEach(e=>{if(!n||!n[e])thrownewError(`Config parameter missing, ${e} is required`)}),n}_getConfig(){returnthis._getPlatformConfig()}}for(consteinx)O.prototype[e]=x[e];functiony(e){constn=newO(e);returnnewProxy(n,{get(e,n){if(nine&&0!==n.indexOf("_")){if("function"==typeofe[n])return(t=e[n],function(){lete;try{e=t.apply(this,arguments)}catch(e){if(c(e))returno.call(this,e),e;throwe}returni(e)?e.then(e=>(c(e)&&o.call(this,e),e),e=>{if(c(e))returno.call(this,e),e;throwe}):(c(e)&&o.call(this,e),e)}).bind(e);if("context"!==n&&"config"!==n)returne[n]}vart}})}O.prototype.createInstance=y;constN={createInstance:y};module.exports=N;
"use strict";vare,n=(e=require("crypto"))&&"object"==typeofe&&"default"ine?e.default:e;constt={TOKEN_EXPIRED:"uni-id-token-expired",CHECK_TOKEN_FAILED:"uni-id-check-token-failed",PARAM_REQUIRED:"uni-id-param-required",ACCOUNT_EXISTS:"uni-id-account-exists",ACCOUNT_NOT_EXISTS:"uni-id-account-not-exists",ACCOUNT_CONFLICT:"uni-id-account-conflict",ACCOUNT_BANNED:"uni-id-account-banned",ACCOUNT_AUDITING:"uni-id-account-auditing",ACCOUNT_AUDIT_FAILED:"uni-id-account-audit-failed",ACCOUNT_CLOSED:"uni-id-account-closed"};functioni(e){return!!e&&("object"==typeofe||"function"==typeofe)&&"function"==typeofe.then}constr={"uni-id-token-expired":30203,"uni-id-check-token-failed":30202};functiono(e){const{errCode:n,errMsgValue:t}=e;e.errMsg=this._t(n,t),ninr&&(e.code=r[n])}functionc(e){return"object"===(i=e,Object.prototype.toString.call(i).slice(8,-1).toLowerCase())&&e.errCode&&(n=e.errCode,Object.values(t).includes(n))&&!!e.errCode;varn,i}lets={"zh-Hans":{"uni-id-token-expired":"登录状态失效,token已过期","uni-id-check-token-failed":"token校验未通过","uni-id-param-required":"缺少参数: {param}","uni-id-account-exists":"此账号已注册","uni-id-account-not-exists":"此账号未注册","uni-id-account-conflict":"用户账号冲突","uni-id-account-banned":"从账号已封禁","uni-id-account-auditing":"此账号正在审核中","uni-id-account-audit-failed":"此账号审核失败","uni-id-account-closed":"此账号已注销"},en:{"uni-id-token-expired":"The login status is invalid, token has expired","uni-id-check-token-failed":"Check token failed","uni-id-param-required":"Parameter required: {param}","uni-id-account-exists":"Account exists","uni-id-account-not-exists":"Account does not exists","uni-id-account-conflict":"User account conflict","uni-id-account-banned":"Account has been banned","uni-id-account-auditing":"Account audit in progress","uni-id-account-audit-failed":"Account audit failed","uni-id-account-closed":"Account has been closed"}};try{conste=require.resolve("uni-config-center/uni-id/lang/index.js");s=function(e,n){constt=Object.keys(e);t.push(...Object.keys(n));consti={};for(letr=0;r<t.length;r++){consto=t[r];i[o]=Object.assign({},e[o],n[o])}returni}(s,require(e))}catch(e){}vara=s;functionu(e){returne.replace(/=/g,"").replace(/\+/g,"-").replace(/\//g,"_")}functiond(e){returnJSON.parse((n=function(e){varn=4-(e=e.toString()).length%4;if(4!==n)for(vart=0;t<n;++t)e+="=";returne.replace(/-/g,"+").replace(/_/g,"/")}(e),Buffer.from(n,"base64").toString("utf-8")));varn}functionl(e){returnu((n=JSON.stringify(e),Buffer.from(n,"utf-8").toString("base64")));varn}functionf(e,t){returnu(n.createHmac("sha256",t).update(e).digest("base64"))}constp=function(e,n){if("string"!=typeofe)thrownewError("Invalid token");constt=e.split(".");if(3!==t.length)thrownewError("Invalid token");const[i,r,o]=t;if(f(i+"."+r,n)!==o)thrownewError("Invalid token");constc=d(i);if("HS256"!==c.alg||"JWT"!==c.typ)thrownewError("Invalid token");consts=d(r);if(1e3*s.exp<Date.now()){conste=newError("Token expired");throwe.name="TokenExpiredError",e}returns},h=function(e,n,t={}){const{expiresIn:i}=t;if(!i)thrownewError("expiresIn is required");constr=parseInt(Date.now()/1e3),o={...e,iat:r,exp:r+t.expiresIn},c=l({alg:"HS256",typ:"JWT"})+"."+l(o);returnc+"."+f(c,n)},g=uniCloud.database(),C=g.command,E=g.collection("uni-id-users"),I=g.collection("uni-id-roles");asyncfunction_(e){const{tokenSecret:n,tokenExpiresIn:i,tokenExpiresThreshold:r}=this._getConfig();if(r>i)thrownewError("Config error, tokenExpiresThreshold should be less than tokenExpiresIn");leto;try{o=p(e,n)}catch(e){if("TokenExpiredError"===e.name)throw{errCode:t.TOKEN_EXPIRED};throw{errCode:t.CHECK_TOKEN_FAILED}}returno}asyncfunctionk(e){constn=e.uid,i=(awaitE.doc(n).get()).data[0];if(!i)throw{errCode:t.CHECK_TOKEN_FAILED};if(i.valid_token_date&&i.valid_token_date>1e3*e.iat)throw{errCode:t.TOKEN_EXPIRED};const{role:r,permission:o}=awaitw(i);returnT.call(this,{uid:n,role:r,permission:o})}asyncfunctionw(e){if(!e)throw{errCode:t.ACCOUNT_NOT_EXISTS};!function(e){switch(e.status){casevoid0:case0:break;case1:throw{errCode:t.ACCOUNT_BANNED};case2:throw{errCode:t.ACCOUNT_AUDITING};case3:throw{errCode:t.ACCOUNT_AUDIT_FAILED};case4:throw{errCode:t.ACCOUNT_CLOSED}}}(e);constn=e.role||[];if(0===n.length)return{role:[],permission:[]};if(n.includes("admin"))return{role:["admin"],permission:[]};consti=awaitI.where({role_id:C.in(n)});varr;return{role:n,permission:(r=i.data.reduce((e,n)=>{n.permission&&e.push(...n.permission)},[]),Array.from(newSet(r)))}}asyncfunctionm({uid:e,signContent:n,tokenSecret:t,tokenExpiresIn:i}={}){constr=Date.now(),o=h(n,t,{expiresIn:i});returnawaitE.doc(e).update({last_login_ip:this._clientInfo.clientIP,last_login_date:r}),{token:o,tokenExpired:r+1e3*i}}asyncfunctionT({uid:e,role:n,permission:i}={}){const{tokenSecret:r,tokenExpiresIn:o,tokenExpiresThreshold:c}=this._getConfig();if(!e)throw{errCode:t.PARAM_REQUIRED,errMsgValue:{param:"uid"}};if(c>o)thrownewError("Config error, tokenExpiresThreshold should be less than tokenExpiresIn");if(!n||!i){constt=(awaitE.doc(e).get()).data[0],r=awaitw(t);n=r.role,i=r.permission}if(!this.interceptorMap.has("customToken"))returnm.call(this,{uid:e,signContent:{uid:e,role:n,permission:i},tokenSecret:r,tokenExpiresIn:o});consts=this.interceptorMap.get("customToken");if("function"==typeofs)thrownewError("Invalid custom token file");consta=awaits({uid:e,role:n,permission:i});returnm.call(this,{uid:e,signContent:a,tokenSecret:r,tokenExpiresIn:o})}varx=Object.freeze({__proto__:null,checkToken:asyncfunction(e,{autoRefresh:n=!0}={}){constt=await_.call(this,e),{tokenExpiresThreshold:i}=this._getConfig();if(!i||!n)return{code:0,errCode:0,...t};constr=Date.now();leto={};return1e3*t.exp-r<1e3*i&&(o=awaitk.call(this,t)),{code:0,errCode:0,...t,...o}},createToken:asyncfunction({uid:e,role:n,permission:t}={}){returnT.call(this,{uid:e,role:n,permission:t})},refreshToken:asyncfunction({token:e}={}){constn=await_.call(this,e);return{errCode:0,...awaitk.call(this,n)}}});constA=require("uni-config-center")({pluginId:"uni-id"});classO{constructor({context:e,clientInfo:n,config:t}={}){this._clientInfo=e?function(e){return{appId:e.APPID,platform:e.PLATFORM,locale:e.LOCALE,clientIP:e.CLIENTIP,deviceId:e.DEVICEID}}(e):n,this.config=t||this._getOriginConfig(),this.interceptorMap=newMap,A.hasFile("custom-token.js")&&this.setInterceptor("customToken",require(A.resolve("custom-token.js"))),this._i18n=uniCloud.initI18n({locale:this._clientInfo.locale,fallbackLocale:"zh-Hans",messages:a})}_t(...e){returnthis._i18n.t(...e)}_getOriginConfig(){if(A.hasFile("config.json")){lete;try{e=A.config()}catch(e){thrownewError("Invalid uni-id config file\n"+e.messages)}returnArray.isArray(e)?e:e[0]?Object.values(e):e}thrownewError("Invalid uni-id config file")}_getAppConfig(){conste=this._getOriginConfig();returnArray.isArray(e)?e.find(e=>e.dcloudAppid===this.clientInfo.appId)||e.find(e=>e.isDefaultConfig):e}_getPlatformConfig(){conste=this._getAppConfig();"app-plus"===this._clientInfo.platform&&(this._clientInfo.platform="app"),"h5"===this._clientInfo.platform&&(this._clientInfo.platform="web");constn=Object.assign({tokenExpiresIn:7200,tokenExpiresThreshold:1200,passwordErrorLimit:6,passwordErrorRetryTime:3600},e,e[this._clientInfo.platform]);return["tokenSecret","tokenExpiresIn"].forEach(e=>{if(!n||!n[e])thrownewError(`Config parameter missing, ${e} is required`)}),n}_getConfig(){returnthis._getPlatformConfig()}}for(consteinx)O.prototype[e]=x[e];functiony(e){constn=newO(e);returnnewProxy(n,{get(e,n){if(nine&&0!==n.indexOf("_")){if("function"==typeofe[n])return(t=e[n],function(){lete;try{e=t.apply(this,arguments)}catch(e){if(c(e))returno.call(this,e),e;throwe}returni(e)?e.then(e=>(c(e)&&o.call(this,e),e),e=>{if(c(e))returno.call(this,e),e;throwe}):(c(e)&&o.call(this,e),e)}).bind(e);if("context"!==n&&"config"!==n)returne[n]}vart}})}O.prototype.createInstance=y;constN={createInstance:y};module.exports=N;