UNPKG

t-comm

Version:

专业、稳定、纯粹的工具库

68 lines (65 loc) 2.23 kB
import { b as __awaiter, c as __generator } from '../../tslib.es6-096fffdd.js'; import axios from '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 __awaiter(this, void 0, void 0, function () { var uniqueKey, res, _d, apiTime, apiToken, result, e_1; return __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({ 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*/]; } }); }); } export { getCredential };