t-comm
Version:
专业、稳定、纯粹的工具库
76 lines (69 loc) • 2.52 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var tslib_es6 = require('../../tslib.es6-01322ba9.js');
var axios = require('axios');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var axios__default = /*#__PURE__*/_interopDefaultLegacy(axios);
var tokenMap = {};
/**
* 获取凭据的通用代码
* @ignore
*
* @param {object} options 配置
* @param {string} options.apiKey apiKey
* @param {string} options.loginName loginName
* @param {Function} options.getPwdCode getPwdCode
* @param {Function} options.encrypt encrypt
*
* @returns {Promise<{auth: string, apiToken: string, apiTime: string}>} 凭据信息
*/
function getCredential(_a) {
var _b, _c;
var apiKey = _a.apiKey,
loginName = _a.loginName,
getPwdCode = _a.getPwdCode,
encrypt = _a.encrypt;
return tslib_es6.__awaiter(this, void 0, void 0, function () {
var uniqueKey, res, _d, apiTime, apiToken, result, e_1;
return tslib_es6.__generator(this, function (_e) {
switch (_e.label) {
case 0:
_e.trys.push([0, 2,, 3]);
uniqueKey = "".concat(loginName, "-").concat(apiKey);
if (tokenMap[uniqueKey]) {
return [2 /*return*/, tokenMap[uniqueKey]];
}
return [4 /*yield*/, axios__default["default"]({
method: 'POST',
url: '/api/interface/getApiToken',
data: {
isFresh: 0,
loginName: loginName,
apiKey: encrypt(apiKey, loginName)
}
})];
case 1:
res = _e.sent();
if ((_b = res === null || res === void 0 ? void 0 : res.data) === null || _b === void 0 ? void 0 : _b.retcode) {
console.warn(res.data);
throw new Error('获取凭据失败!');
}
_d = ((_c = res === null || res === void 0 ? void 0 : res.data) === null || _c === void 0 ? void 0 : _c.data) || {}, apiTime = _d.api_time, apiToken = _d.api_token;
result = {
auth: getPwdCode(apiKey, apiToken, +apiTime),
apiToken: apiToken,
apiTime: apiTime
};
tokenMap[uniqueKey] = result;
return [2 /*return*/, result];
case 2:
e_1 = _e.sent();
console.warn(e_1);
throw new Error('获取凭据失败!');
case 3:
return [2 /*return*/];
}
});
});
}
exports.getCredential = getCredential;