coa-wx-isv
Version:
一个轻量的微信SDK服务商版 for Node.js
41 lines (40 loc) • 1.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.WxIsvTokenService = void 0;
const coa_helper_1 = require("coa-helper");
const WxIsvServiceBase_1 = require("../libs/WxIsvServiceBase");
const ISV_ACCESS_TOKEN = 'isvAccessToken';
const ISV_VERIFY_TICKET = 'isvVerifyTicket';
class WxIsvTokenService extends WxIsvServiceBase_1.WxIsvServiceBase {
// 获取accessToken
async getToken() {
var _a;
const result = (_a = (await this.bin.storage.get(ISV_ACCESS_TOKEN))) !== null && _a !== void 0 ? _a : { token: '', expire: 0 };
if (!result.token) {
const param = {
component_appid: this.config.appId,
component_appsecret: this.config.appSecret,
component_verify_ticket: await this.getTicket(),
};
const data = await this.request('POST', '/cgi-bin/component/api_component_token', param, {});
const ms = coa_helper_1._.toInteger(data.expiresIn) * 1e3 - 3 * 60 * 1e3;
result.expire = coa_helper_1._.now() + ms;
result.token = data.componentAccessToken || '';
await this.bin.storage.set(ISV_ACCESS_TOKEN, result, ms);
}
return result.token;
}
// 获取ticket
async getTicket() {
var _a;
const result = (_a = (await this.bin.storage.get(ISV_VERIFY_TICKET))) !== null && _a !== void 0 ? _a : { ticket: '', expire: 1 };
return result.ticket;
}
// 设置ticket
async setTicket(ticket) {
const ms = 24 * 3600 * 1e3;
const expire = coa_helper_1._.now() + ms;
return await this.bin.storage.set(ISV_VERIFY_TICKET, { ticket, expire }, ms);
}
}
exports.WxIsvTokenService = WxIsvTokenService;