diff --git a/uni_modules/uni-id-common/changelog.md b/uni_modules/uni-id-common/changelog.md index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..399d5a4dc478527e7b5eea5dfb09f945b9469510 100644 --- a/uni_modules/uni-id-common/changelog.md +++ b/uni_modules/uni-id-common/changelog.md @@ -0,0 +1,6 @@ +## 1.0.2(2022-06-23) +- 对齐旧版本uni-id默认配置 +## 1.0.1(2022-06-22) +- 补充对uni-config-center的依赖 +## 1.0.0(2022-06-21) +- 提供uni-id token创建、校验、刷新接口,简化旧版uni-id公共模块 diff --git a/uni_modules/uni-id-common/package.json b/uni_modules/uni-id-common/package.json index 15b726fdd4d2e78a16d9b8bd5e2e8a4705fd6dc8..4c918deec92c701c13f04b86a554637d887bda5e 100644 --- a/uni_modules/uni-id-common/package.json +++ b/uni_modules/uni-id-common/package.json @@ -1,88 +1,88 @@ { - "id": "uni-id-common", - "displayName": "uni-id-common", - "version": "1.0.0", - "description": "uni-id公共模块,仅包含公用基础能力,建议搭配uni-id-pages使用", - "keywords": [ - "uni-id-common", - "uni-id", - "uniCloud用户中心", - "token生成/校验" -], - "repository": "", - "engines": { - "HBuilderX": "^3.1.0" - }, - "dcloudext": { - "category": [ + "id": "uni-id-common", + "displayName": "uni-id-common", + "version": "1.0.2", + "description": "包含uni-id token生成、校验、刷新功能的云函数公共模块", + "keywords": [ + "uni-id-common", "uniCloud", - "云函数模板" + "token", + "权限" ], - "sale": { - "regular": { - "price": "0.00" - }, - "sourcecode": { - "price": "0.00" - } - }, - "contact": { - "qq": "" - }, - "declaration": { - "ads": "", - "data": "", - "permissions": "" - }, - "npmurl": "" - }, - "uni_modules": { - "dependencies": [], - "encrypt": [], - "platforms": { - "cloud": { - "tcb": "u", - "aliyun": "u" - }, - "client": { - "Vue": { - "vue2": "u", - "vue3": "u" - }, - "App": { - "app-vue": "u", - "app-nvue": "u" - }, - "H5-mobile": { - "Safari": "u", - "Android Browser": "u", - "微信浏览器(Android)": "u", - "QQ浏览器(Android)": "u" - }, - "H5-pc": { - "Chrome": "u", - "IE": "u", - "Edge": "u", - "Firefox": "u", - "Safari": "u" - }, - "小程序": { - "微信": "u", - "阿里": "u", - "百度": "u", - "字节跳动": "u", - "QQ": "u", - "钉钉": "u", - "快手": "u", - "飞书": "u", - "京东": "u", - "小红书": "u" - }, - "快应用": { - "华为": "u", - "联盟": "u" - } - } - } - } -} \ No newline at end of file + "repository": "https://gitcode.net/dcloud/uni-id-common", + "engines": { + "HBuilderX": "^3.1.0" + }, + "dcloudext": { + "category": [ + "uniCloud", + "云函数模板" + ], + "sale": { + "regular": { + "price": "0.00" + }, + "sourcecode": { + "price": "0.00" + } + }, + "contact": { + "qq": "" + }, + "declaration": { + "ads": "无", + "data": "无", + "permissions": "无" + }, + "npmurl": "" + }, + "uni_modules": { + "dependencies": ["uni-config-center"], + "encrypt": [], + "platforms": { + "cloud": { + "tcb": "y", + "aliyun": "y" + }, + "client": { + "Vue": { + "vue2": "u", + "vue3": "u" + }, + "App": { + "app-vue": "u", + "app-nvue": "u" + }, + "H5-mobile": { + "Safari": "u", + "Android Browser": "u", + "微信浏览器(Android)": "u", + "QQ浏览器(Android)": "u" + }, + "H5-pc": { + "Chrome": "u", + "IE": "u", + "Edge": "u", + "Firefox": "u", + "Safari": "u" + }, + "小程序": { + "微信": "u", + "阿里": "u", + "百度": "u", + "字节跳动": "u", + "QQ": "u", + "钉钉": "u", + "快手": "u", + "飞书": "u", + "京东": "u", + "小红书": "u" + }, + "快应用": { + "华为": "u", + "联盟": "u" + } + } + } + } +} diff --git a/uni_modules/uni-id-common/readme.md b/uni_modules/uni-id-common/readme.md index 7c5d05b252d9dceba42fef0ef21336530e91a983..5f6a37a336965c3e3d6495b431eafe67df93d44c 100644 --- a/uni_modules/uni-id-common/readme.md +++ b/uni_modules/uni-id-common/readme.md @@ -1 +1,3 @@ -# uni-id-common \ No newline at end of file +# uni-id-common + +文档请参考:[uni-id-common](https://uniapp.dcloud.net.cn/uniCloud/uni-id-common.html) \ No newline at end of file diff --git a/uni_modules/uni-id-common/uniCloud/cloudfunctions/common/uni-id-common/index.js b/uni_modules/uni-id-common/uniCloud/cloudfunctions/common/uni-id-common/index.js index 9e046929f93dff673b2a44de0a8968ec4a11376a..cc7f29f468df26d7465a107808840079b32af144 100644 --- a/uni_modules/uni-id-common/uniCloud/cloudfunctions/common/uni-id-common/index.js +++ b/uni_modules/uni-id-common/uniCloud/cloudfunctions/common/uni-id-common/index.js @@ -1 +1 @@ -"use strict";var e,n=(e=require("crypto"))&&"object"==typeof e&&"default"in e?e.default:e;const t={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"};function i(e){return!!e&&("object"==typeof e||"function"==typeof e)&&"function"==typeof e.then}const o={"uni-id-token-expired":30203,"uni-id-check-token-failed":30202};function r(e){const{errCode:n,errMsgValue:t}=e;e.errMsg=this._t(n,t),n in o&&(e.code=o[n])}function c(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;var n,i}let s={"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{const e=require.resolve("uni-config-center/uni-id/lang/index.js");s=function(e,n){const t=Object.keys(e);t.push(...Object.keys(n));const i={};for(let o=0;oi)throw new Error("Config error, tokenExpiresThreshold should be less than tokenExpiresIn");let r;try{r=p(e,n)}catch(e){if("TokenExpiredError"===e.name)throw{errCode:t.TOKEN_EXPIRED};throw{errCode:t.CHECK_TOKEN_FAILED}}return r}async function k(e){const n=e.uid,i=(await E.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:o,permission:r}=await w(i);return T.call(this,{uid:n,role:o,permission:r})}async function w(e){if(!e)throw{errCode:t.ACCOUNT_NOT_EXISTS};!function(e){switch(e.status){case void 0:case 0:break;case 1:throw{errCode:t.ACCOUNT_BANNED};case 2:throw{errCode:t.ACCOUNT_AUDITING};case 3:throw{errCode:t.ACCOUNT_AUDIT_FAILED};case 4:throw{errCode:t.ACCOUNT_CLOSED}}}(e);const n=e.role||[];if(0===n.length)return{role:[],permission:[]};if(n.includes("admin"))return{role:["admin"],permission:[]};const i=await _.where({role_id:C.in(n)});var o;return{role:n,permission:(o=i.data.reduce((e,n)=>{n.permission&&e.push(...n.permission)},[]),Array.from(new Set(o)))}}async function m({uid:e,signContent:n,tokenSecret:t,tokenExpiresIn:i}={}){const o=Date.now(),r=h(n,t,{expiresIn:i});return await E.doc(e).update({last_login_ip:this._clientInfo.clientIP,last_login_date:o}),{token:r,tokenExpired:o+1e3*i}}async function T({uid:e,role:n,permission:i}={}){const{tokenSecret:o,tokenExpiresIn:r,tokenExpiresThreshold:c}=this._getConfig();if(!e)throw{errCode:t.PARAM_REQUIRED,errMsgValue:{param:"uid"}};if(c>r)throw new Error("Config error, tokenExpiresThreshold should be less than tokenExpiresIn");if(!n||!i){const t=(await E.doc(e).get()).data[0],o=await w(t);n=o.role,i=o.permission}if(!this.interceptorMap.has("customToken"))return m.call(this,{uid:e,signContent:{uid:e,role:n,permission:i},tokenSecret:o,tokenExpiresIn:r});const s=this.interceptorMap.get("customToken");if("function"==typeof s)throw new Error("Invalid custom token file");const a=await s({uid:e,role:n,permission:i});return m.call(this,{uid:e,signContent:a,tokenSecret:o,tokenExpiresIn:r})}var A=Object.freeze({__proto__:null,checkToken:async function(e,{autoRefresh:n=!0}={}){const t=await I.call(this,e),{tokenExpiresThreshold:i}=this._getConfig();if(!i||!n)return{code:0,errCode:0,...t};const o=Date.now();let r={};return 1e3*t.exp-o<1e3*i&&(r=await k.call(this,t)),{code:0,errCode:0,...t,...r}},createToken:async function({uid:e,role:n,permission:t}={}){return T.call(this,{uid:e,role:n,permission:t})},refreshToken:async function({token:e}={}){const n=await I.call(this,e);return{errCode:0,...await k.call(this,n)}}});const x=require("uni-config-center")({pluginId:"uni-id"});class O{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=new Map,x.hasFile("custom-token.js")&&this.setInterceptor("customToken",require(x.resolve("custom-token.js"))),this._i18n=uniCloud.initI18n({locale:this._clientInfo.locale,fallbackLocale:"zh-Hans",messages:a})}_t(...e){return this._i18n.t(...e)}_getOriginConfig(){if(x.hasFile("config.json")){let e;try{e=x.config()}catch(e){throw new Error("Invalid uni-id config file\n"+e.messages)}return Array.isArray(e)?e:e[0]?Object.values(e):e}throw new Error("Invalid uni-id config file")}_getAppConfig(){const e=this._getOriginConfig();return Array.isArray(e)?e.find(e=>e.dcloudAppid===this.clientInfo.appId)||e.find(e=>e.isDefaultConfig):e}_getPlatformConfig(){const e=this._getAppConfig();"app-plus"===this._clientInfo.platform&&(this._clientInfo.platform="app"),"h5"===this._clientInfo.platform&&(this._clientInfo.platform="web");const n=Object.assign({},e,e[this._clientInfo.platform]);return["tokenSecret","tokenExpiresIn"].forEach(e=>{if(!n||!n[e])throw new Error(`Config parameter missing, ${e} is required`)}),n}_getConfig(){return this._getPlatformConfig()}}for(const e in A)O.prototype[e]=A[e];function y(e){const n=new O(e);return new Proxy(n,{get(e,n){if(n in e&&0!==n.indexOf("_")){if("function"==typeof e[n])return(t=e[n],function(){let e;try{e=t.apply(this,arguments)}catch(e){if(c(e))return r.call(this,e),e;throw e}return i(e)?e.then(e=>(c(e)&&r.call(this,e),e),e=>{if(c(e))return r.call(this,e),e;throw e}):(c(e)&&r.call(this,e),e)}).bind(e);if("context"!==n&&"config"!==n)return e[n]}var t}})}O.prototype.createInstance=y;const N={createInstance:y};module.exports=N; +"use strict";var e,n=(e=require("crypto"))&&"object"==typeof e&&"default"in e?e.default:e;const t={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"};function i(e){return!!e&&("object"==typeof e||"function"==typeof e)&&"function"==typeof e.then}const r={"uni-id-token-expired":30203,"uni-id-check-token-failed":30202};function o(e){const{errCode:n,errMsgValue:t}=e;e.errMsg=this._t(n,t),n in r&&(e.code=r[n])}function c(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;var n,i}let s={"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{const e=require.resolve("uni-config-center/uni-id/lang/index.js");s=function(e,n){const t=Object.keys(e);t.push(...Object.keys(n));const i={};for(let r=0;ri)throw new Error("Config error, tokenExpiresThreshold should be less than tokenExpiresIn");let o;try{o=p(e,n)}catch(e){if("TokenExpiredError"===e.name)throw{errCode:t.TOKEN_EXPIRED};throw{errCode:t.CHECK_TOKEN_FAILED}}return o}async function k(e){const n=e.uid,i=(await E.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}=await w(i);return T.call(this,{uid:n,role:r,permission:o})}async function w(e){if(!e)throw{errCode:t.ACCOUNT_NOT_EXISTS};!function(e){switch(e.status){case void 0:case 0:break;case 1:throw{errCode:t.ACCOUNT_BANNED};case 2:throw{errCode:t.ACCOUNT_AUDITING};case 3:throw{errCode:t.ACCOUNT_AUDIT_FAILED};case 4:throw{errCode:t.ACCOUNT_CLOSED}}}(e);const n=e.role||[];if(0===n.length)return{role:[],permission:[]};if(n.includes("admin"))return{role:["admin"],permission:[]};const i=await I.where({role_id:C.in(n)});var r;return{role:n,permission:(r=i.data.reduce((e,n)=>{n.permission&&e.push(...n.permission)},[]),Array.from(new Set(r)))}}async function m({uid:e,signContent:n,tokenSecret:t,tokenExpiresIn:i}={}){const r=Date.now(),o=h(n,t,{expiresIn:i});return await E.doc(e).update({last_login_ip:this._clientInfo.clientIP,last_login_date:r}),{token:o,tokenExpired:r+1e3*i}}async function T({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)throw new Error("Config error, tokenExpiresThreshold should be less than tokenExpiresIn");if(!n||!i){const t=(await E.doc(e).get()).data[0],r=await w(t);n=r.role,i=r.permission}if(!this.interceptorMap.has("customToken"))return m.call(this,{uid:e,signContent:{uid:e,role:n,permission:i},tokenSecret:r,tokenExpiresIn:o});const s=this.interceptorMap.get("customToken");if("function"==typeof s)throw new Error("Invalid custom token file");const a=await s({uid:e,role:n,permission:i});return m.call(this,{uid:e,signContent:a,tokenSecret:r,tokenExpiresIn:o})}var x=Object.freeze({__proto__:null,checkToken:async function(e,{autoRefresh:n=!0}={}){const t=await _.call(this,e),{tokenExpiresThreshold:i}=this._getConfig();if(!i||!n)return{code:0,errCode:0,...t};const r=Date.now();let o={};return 1e3*t.exp-r<1e3*i&&(o=await k.call(this,t)),{code:0,errCode:0,...t,...o}},createToken:async function({uid:e,role:n,permission:t}={}){return T.call(this,{uid:e,role:n,permission:t})},refreshToken:async function({token:e}={}){const n=await _.call(this,e);return{errCode:0,...await k.call(this,n)}}});const A=require("uni-config-center")({pluginId:"uni-id"});class O{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=new Map,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){return this._i18n.t(...e)}_getOriginConfig(){if(A.hasFile("config.json")){let e;try{e=A.config()}catch(e){throw new Error("Invalid uni-id config file\n"+e.messages)}return Array.isArray(e)?e:e[0]?Object.values(e):e}throw new Error("Invalid uni-id config file")}_getAppConfig(){const e=this._getOriginConfig();return Array.isArray(e)?e.find(e=>e.dcloudAppid===this.clientInfo.appId)||e.find(e=>e.isDefaultConfig):e}_getPlatformConfig(){const e=this._getAppConfig();"app-plus"===this._clientInfo.platform&&(this._clientInfo.platform="app"),"h5"===this._clientInfo.platform&&(this._clientInfo.platform="web");const n=Object.assign({tokenExpiresIn:7200,tokenExpiresThreshold:1200,passwordErrorLimit:6,passwordErrorRetryTime:3600},e,e[this._clientInfo.platform]);return["tokenSecret","tokenExpiresIn"].forEach(e=>{if(!n||!n[e])throw new Error(`Config parameter missing, ${e} is required`)}),n}_getConfig(){return this._getPlatformConfig()}}for(const e in x)O.prototype[e]=x[e];function y(e){const n=new O(e);return new Proxy(n,{get(e,n){if(n in e&&0!==n.indexOf("_")){if("function"==typeof e[n])return(t=e[n],function(){let e;try{e=t.apply(this,arguments)}catch(e){if(c(e))return o.call(this,e),e;throw e}return i(e)?e.then(e=>(c(e)&&o.call(this,e),e),e=>{if(c(e))return o.call(this,e),e;throw e}):(c(e)&&o.call(this,e),e)}).bind(e);if("context"!==n&&"config"!==n)return e[n]}var t}})}O.prototype.createInstance=y;const N={createInstance:y};module.exports=N; diff --git a/uni_modules/uni-id-common/uniCloud/cloudfunctions/common/uni-id-common/package.json b/uni_modules/uni-id-common/uniCloud/cloudfunctions/common/uni-id-common/package.json index d73847b95785afffe937e9e5cee8bddc038c54f6..2e9d5bf0e2b1a7b317b7267bd0d609ed30f4f0fd 100644 --- a/uni_modules/uni-id-common/uniCloud/cloudfunctions/common/uni-id-common/package.json +++ b/uni_modules/uni-id-common/uniCloud/cloudfunctions/common/uni-id-common/package.json @@ -1,6 +1,6 @@ { "name": "uni-id-common", - "version": "1.0.0", + "version": "1.0.2", "description": "uni-id token生成、校验、刷新", "main": "index.js", "homepage": "https://uniapp.dcloud.io/uniCloud/uni-id-common.html",