@tnwx/accesstoken
Version:
TNWX 微信系开发脚手架之微信 accesstoken
88 lines • 3.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const cache_1 = require("@tnwx/cache");
/**
* @author Javen
* @copyright javendev@126.com
* @description 企业微信 QyApiConfigKit
*/
class QyApiConfigKit {
set devMode(devMode) {
this.devMode = devMode;
}
static isDevMode() {
return this.devMode;
}
static putApiConfig(apiConfig) {
return this.CFG_MAP.set(apiConfig.getAppId.concat(this.SEPARATOR).concat(apiConfig.getCorpId), apiConfig);
}
static removeApiConfigByConfig(apiConfig) {
return this.removeApiConfig(apiConfig.getAppId, apiConfig.getCorpId);
}
static removeApiConfig(appId, corpId) {
return this.CFG_MAP.delete(appId.concat(this.SEPARATOR).concat(corpId));
}
static setCurrentAppId(appId, corpId) {
this.currentAppId = appId.concat(this.SEPARATOR).concat(corpId);
}
static removeCurrentAppId() {
this.currentAppId = '';
}
static get getCorpId() {
let corpId;
if (this.currentAppId) {
corpId = this.currentAppId.split(this.SEPARATOR)[1];
console.debug(`getCorpId: ${corpId}`);
}
if (!corpId) {
throw new Error('需事先调用 QyApiConfigKit.putApiConfig(apiConfig) 将 appId 对应的 ApiConfig 对象存入后, 才可以使用 QyApiConfigKit.getCorpId 系列方法');
}
return corpId;
}
static get getAppId() {
let appId;
if (this.currentAppId) {
appId = this.currentAppId.split(this.SEPARATOR)[0];
}
if (!appId) {
throw new Error('需事先调用 QyApiConfigKit.putApiConfig(apiConfig) 将 appId 对应的 ApiConfig 对象存入后, 才可以使用 QyApiConfigKit.getAppId 系列方法');
}
return appId;
}
static get getToken() {
let token;
let apiConfig = this.CFG_MAP.get(this.currentAppId);
if (apiConfig) {
token = apiConfig.getToken;
}
if (!token) {
throw new Error('需事先调用 ApiConfigKit.putApiConfig(apiConfig) 将 appId 对应的 ApiConfig 对象存入后, 才可以使用 ApiConfigKit.getToken 系列方法');
}
return token;
}
static get getApiConfig() {
return this.getApiConfigByAppId(this.getAppId, this.getCorpId);
}
static getApiConfigByAppId(appId, corpId) {
if (this.isDevMode) {
console.debug(`getApiConfigByAppId appId : ${appId} corpId: ${corpId}`);
}
let cfg = this.CFG_MAP.get(appId.concat(this.SEPARATOR).concat(corpId));
if (!cfg) {
throw new Error('需事先调用 QyApiConfigKit.putApiConfig(apiConfig) 将 appId 对应的 ApiConfig 对象存入后,才可以使用 ApiConfigKit.getApiConfigByAppId 系列方法');
}
return cfg;
}
static get getCache() {
return this._cache;
}
static set setCache(cache) {
this._cache = cache;
}
}
exports.QyApiConfigKit = QyApiConfigKit;
QyApiConfigKit.CFG_MAP = new Map();
QyApiConfigKit.SEPARATOR = '_';
QyApiConfigKit.devMode = false;
QyApiConfigKit._cache = new cache_1.DefaultCache();
//# sourceMappingURL=QyApiConfigKit.js.map