UNPKG

zwdd

Version:
77 lines 2.96 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.sign = exports.request = exports.DEFAULT_ENDPOINT = void 0; const request_1 = require("./lib/request"); exports.request = request_1.default; const crypto_1 = require("./lib/crypto"); Object.defineProperty(exports, "sign", { enumerable: true, get: function () { return crypto_1.sign; } }); __exportStar(require("./lib/request"), exports); __exportStar(require("./types"), exports); exports.DEFAULT_ENDPOINT = 'https://openplatform.dg-work.cn'; class ZWDD { constructor(options) { this.accessTokenExpiresAt = 0; this.appKey = options.appKey; this.appSecret = options.appSecret; this.endpoint = (options.endpoint || exports.DEFAULT_ENDPOINT).replace(/\/+$/, ''); this.hostIp = options.hostIp || '127.0.0.1'; this.hostMac = options.hostMac || '00:00:00:00:00:00'; } async request(api, init = {}) { api = !api.startsWith('/') ? `/${api}` : api; return (0, request_1.default)(api, { ...init, hostIp: this.hostIp, hostMac: this.hostMac, appKey: this.appKey, appSecret: this.appSecret, endpoint: this.endpoint, }); } async getAccessToken() { if (this.accessToken && this.accessTokenExpiresAt > Date.now()) { return this.accessToken; } const res = await this.request('/gettoken.json', { query: { appKey: this.appKey, appSecret: this.appSecret, }, }); this.accessToken = res.accessToken; this.accessTokenExpiresAt = Date.now() + res.expiresIn * 1000; return this.accessToken; } async getAppUserInfo(authCode) { const accessToken = await this.getAccessToken(); return await this.request('/rpc/oauth2/dingtalk_app_user.json', { method: 'POST', body: { access_token: accessToken, auth_code: authCode, }, }); } async getJSApiTicket() { const accessToken = await this.getAccessToken(); const { accessToken: ticket } = await this.request('/get_jsapi_token.json', { method: 'POST', body: { access_token: accessToken, }, }); return ticket; } } exports.default = ZWDD; //# sourceMappingURL=index.js.map