node-easywechat 3.0.0 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -0
- package/README.md +22 -1
- package/dist/Core/Config.d.ts +1 -1
- package/dist/Core/Config.js +3 -1
- package/dist/Core/HttpClient/Contracts/HttpClientInterface.d.ts +7 -1
- package/dist/Core/HttpClient/Contracts/HttpClientInterface.js +6 -0
- package/dist/Core/HttpClient/HttpClient.d.ts +1 -4
- package/dist/Core/HttpClient/HttpClient.js +4 -4
- package/dist/Core/HttpClient/Mixins/PresetMixin.d.ts +0 -12
- package/dist/Core/HttpClient/Mixins/PresetMixin.js +0 -18
- package/dist/Core/Support/Obj.js +4 -2
- package/dist/MiniApp/Application.d.ts +4 -4
- package/dist/MiniApp/Application.js +1 -1
- package/dist/MiniApp/Contracts/ApplicationInterface.d.ts +2 -2
- package/dist/OfficialAccount/Application.d.ts +4 -4
- package/dist/OfficialAccount/Application.js +1 -1
- package/dist/OfficialAccount/Contracts/ApplicationInterface.d.ts +2 -2
- package/dist/OpenPlatform/Account.d.ts +13 -0
- package/dist/OpenPlatform/Account.js +22 -0
- package/dist/OpenPlatform/Application.d.ts +152 -0
- package/dist/OpenPlatform/Application.js +355 -0
- package/dist/OpenPlatform/Authorization.d.ts +23 -0
- package/dist/OpenPlatform/Authorization.js +74 -0
- package/dist/OpenPlatform/AuthorizerAccessToken.d.ts +19 -0
- package/dist/OpenPlatform/AuthorizerAccessToken.js +43 -0
- package/dist/OpenPlatform/ComponentAccessToken.d.ts +28 -0
- package/dist/OpenPlatform/ComponentAccessToken.js +85 -0
- package/dist/OpenPlatform/Config.d.ts +5 -0
- package/dist/OpenPlatform/Config.js +16 -0
- package/dist/OpenPlatform/Contracts/AccountInterface.d.ts +23 -0
- package/dist/OpenPlatform/Contracts/AccountInterface.js +25 -0
- package/dist/OpenPlatform/Contracts/ApplicationInterface.d.ts +68 -0
- package/dist/OpenPlatform/Contracts/ApplicationInterface.js +60 -0
- package/dist/OpenPlatform/Contracts/VerifyTicketInterface.d.ts +23 -0
- package/dist/OpenPlatform/Contracts/VerifyTicketInterface.js +25 -0
- package/dist/OpenPlatform/Message.d.ts +43 -0
- package/dist/OpenPlatform/Message.js +9 -0
- package/dist/OpenPlatform/Server.d.ts +61 -0
- package/dist/OpenPlatform/Server.js +136 -0
- package/dist/OpenPlatform/VerifyTicket.d.ts +13 -0
- package/dist/OpenPlatform/VerifyTicket.js +49 -0
- package/dist/Pay/Application.d.ts +4 -4
- package/dist/Pay/Application.js +1 -1
- package/dist/Pay/Client.d.ts +12 -0
- package/dist/Pay/Client.js +27 -3
- package/dist/Pay/Signature.js +7 -6
- package/dist/index.d.ts +3 -2
- package/dist/index.js +3 -1
- package/package.json +1 -1
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
const merge_1 = __importDefault(require("merge"));
|
|
15
|
+
const ProviderInterface_1 = __importDefault(require("node-socialite/dist/Core/ProviderInterface"));
|
|
16
|
+
const WeChat_1 = __importDefault(require("node-socialite/dist/Providers/WeChat"));
|
|
17
|
+
const Config_1 = __importDefault(require("./Config"));
|
|
18
|
+
const ConfigInterface_1 = __importDefault(require("../Core/Contracts/ConfigInterface"));
|
|
19
|
+
const Encryptor_1 = __importDefault(require("../Core/Encryptor"));
|
|
20
|
+
const AccessTokenAwareClient_1 = __importDefault(require("../Core/HttpClient/AccessTokenAwareClient"));
|
|
21
|
+
const CacheMixin_1 = __importDefault(require("../Core/Mixins/CacheMixin"));
|
|
22
|
+
const ClientMixin_1 = __importDefault(require("../Core/Mixins/ClientMixin"));
|
|
23
|
+
const ConfigMixin_1 = __importDefault(require("../Core/Mixins/ConfigMixin"));
|
|
24
|
+
const HttpClientMixin_1 = __importDefault(require("../Core/Mixins/HttpClientMixin"));
|
|
25
|
+
const ServerRequestMixin_1 = __importDefault(require("../Core/Mixins/ServerRequestMixin"));
|
|
26
|
+
const Utils_1 = require("../Core/Support/Utils");
|
|
27
|
+
const Application_1 = __importDefault(require("../OfficialAccount/Application"));
|
|
28
|
+
const Application_2 = __importDefault(require("../MiniApp/Application"));
|
|
29
|
+
const Account_1 = __importDefault(require("./Account"));
|
|
30
|
+
const Server_1 = __importDefault(require("./Server"));
|
|
31
|
+
const VerifyTicket_1 = __importDefault(require("./VerifyTicket"));
|
|
32
|
+
const ComponentAccessToken_1 = __importDefault(require("./ComponentAccessToken"));
|
|
33
|
+
const Authorization_1 = __importDefault(require("./Authorization"));
|
|
34
|
+
const AuthorizerAccessToken_1 = __importDefault(require("./AuthorizerAccessToken"));
|
|
35
|
+
/**
|
|
36
|
+
* 公众号应用
|
|
37
|
+
*/
|
|
38
|
+
class Application {
|
|
39
|
+
constructor(config) {
|
|
40
|
+
this.account = null;
|
|
41
|
+
this.encryptor = null;
|
|
42
|
+
this.server = null;
|
|
43
|
+
this.componentAccessToken = null;
|
|
44
|
+
this.verifyTicket = null;
|
|
45
|
+
if (config instanceof ConfigInterface_1.default) {
|
|
46
|
+
this.setConfig(config);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
this.setConfig(new Config_1.default(config));
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
getAccount() {
|
|
53
|
+
if (!this.account) {
|
|
54
|
+
this.account = new Account_1.default(this.config.get('app_id'), this.config.get('secret'), this.config.get('token'), this.config.get('aes_key'));
|
|
55
|
+
}
|
|
56
|
+
return this.account;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* 设置当前账户实例
|
|
60
|
+
* @param account
|
|
61
|
+
* @returns
|
|
62
|
+
*/
|
|
63
|
+
setAccount(account) {
|
|
64
|
+
this.account = account;
|
|
65
|
+
return this;
|
|
66
|
+
}
|
|
67
|
+
getVerifyTicket() {
|
|
68
|
+
if (!this.verifyTicket) {
|
|
69
|
+
this.verifyTicket = new VerifyTicket_1.default(this.getAccount().getAppId(), null, this.getCache());
|
|
70
|
+
}
|
|
71
|
+
return this.verifyTicket;
|
|
72
|
+
}
|
|
73
|
+
setVerifyTicket(verifyTicket) {
|
|
74
|
+
this.verifyTicket = verifyTicket;
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
getEncryptor() {
|
|
78
|
+
if (!this.encryptor) {
|
|
79
|
+
let token = this.getAccount().getToken();
|
|
80
|
+
let aesKey = this.getAccount().getAesKey();
|
|
81
|
+
if (!token || !aesKey) {
|
|
82
|
+
throw new Error('token or aes_key cannot be empty.');
|
|
83
|
+
}
|
|
84
|
+
this.encryptor = new Encryptor_1.default(this.getAccount().getAppId(), token, aesKey, this.getAccount().getAppId());
|
|
85
|
+
}
|
|
86
|
+
return this.encryptor;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* 设置加密机实例
|
|
90
|
+
* @param encryptor
|
|
91
|
+
* @returns
|
|
92
|
+
*/
|
|
93
|
+
setEncryptor(encryptor) {
|
|
94
|
+
this.encryptor = encryptor;
|
|
95
|
+
return this;
|
|
96
|
+
}
|
|
97
|
+
getServer() {
|
|
98
|
+
if (!this.server) {
|
|
99
|
+
this.server = new Server_1.default(this.getAccount().getAesKey() ? this.getEncryptor() : null, this.getRequest());
|
|
100
|
+
}
|
|
101
|
+
return this.server;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* 设置服务端实例
|
|
105
|
+
* @param server
|
|
106
|
+
* @returns
|
|
107
|
+
*/
|
|
108
|
+
setServer(server) {
|
|
109
|
+
this.server = server;
|
|
110
|
+
return this;
|
|
111
|
+
}
|
|
112
|
+
getAccessToken() {
|
|
113
|
+
return this.getComponentAccessToken();
|
|
114
|
+
}
|
|
115
|
+
getComponentAccessToken() {
|
|
116
|
+
if (!this.componentAccessToken) {
|
|
117
|
+
this.componentAccessToken = new ComponentAccessToken_1.default(this.getAccount().getAppId(), this.getAccount().getSecret(), this.getVerifyTicket(), null, this.getCache(), this.getHttpClient());
|
|
118
|
+
}
|
|
119
|
+
return this.componentAccessToken;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* 设置AccessToken实例
|
|
123
|
+
* @param accessToken
|
|
124
|
+
* @returns
|
|
125
|
+
*/
|
|
126
|
+
setComponentAccessToken(componentAccessToken) {
|
|
127
|
+
this.componentAccessToken = componentAccessToken;
|
|
128
|
+
return this;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* 使用授权码获取授权信息
|
|
132
|
+
* @see https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/ThirdParty/token/authorization_info.html
|
|
133
|
+
* @param authorizationCode
|
|
134
|
+
* @returns
|
|
135
|
+
*/
|
|
136
|
+
getAuthorization(authorizationCode) {
|
|
137
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
138
|
+
let response = (yield this.getClient().request('post', 'cgi-bin/component/api_query_auth', {
|
|
139
|
+
params: {
|
|
140
|
+
component_appid: this.getAccount().getAppId(),
|
|
141
|
+
authorization_code: authorizationCode,
|
|
142
|
+
}
|
|
143
|
+
})).toObject();
|
|
144
|
+
if (!response['authorization_info']) {
|
|
145
|
+
throw new Error('Failed to get authorization_info: ' + JSON.stringify(response));
|
|
146
|
+
}
|
|
147
|
+
return new Authorization_1.default(response);
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* 获取/刷新接口调用令牌
|
|
152
|
+
* @see https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/ThirdParty/token/api_authorizer_token.html
|
|
153
|
+
* @param authorizerAppId
|
|
154
|
+
* @param authorizerRefreshToken
|
|
155
|
+
* @returns
|
|
156
|
+
*/
|
|
157
|
+
refreshAuthorizerToken(authorizerAppId, authorizerRefreshToken) {
|
|
158
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
159
|
+
let response = (yield this.getClient().request('post', 'cgi-bin/component/api_authorizer_token', {
|
|
160
|
+
params: {
|
|
161
|
+
component_appid: this.getAccount().getAppId(),
|
|
162
|
+
authorizer_appid: authorizerAppId,
|
|
163
|
+
authorizer_refresh_token: authorizerRefreshToken,
|
|
164
|
+
}
|
|
165
|
+
})).toObject();
|
|
166
|
+
if (!response['authorizer_access_token']) {
|
|
167
|
+
throw new Error('Failed to get authorizer_access_token: ' + JSON.stringify(response));
|
|
168
|
+
}
|
|
169
|
+
return response;
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* 获取预授权码
|
|
174
|
+
* @see https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/ThirdParty/token/pre_auth_code.html
|
|
175
|
+
* @returns
|
|
176
|
+
*/
|
|
177
|
+
createPreAuthorizationCode() {
|
|
178
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
179
|
+
let response = (yield this.getClient().request('post', 'cgi-bin/component/api_create_preauthcode', {
|
|
180
|
+
params: {
|
|
181
|
+
component_appid: this.getAccount().getAppId(),
|
|
182
|
+
}
|
|
183
|
+
})).toObject();
|
|
184
|
+
if (!response['pre_auth_code']) {
|
|
185
|
+
throw new Error('Failed to get pre_auth_code: ' + JSON.stringify(response));
|
|
186
|
+
}
|
|
187
|
+
return response;
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
getOAuth() {
|
|
191
|
+
let oauthFactory = ((app) => {
|
|
192
|
+
return (new WeChat_1.default({
|
|
193
|
+
client_id: app.getAccount().getAppId(),
|
|
194
|
+
client_secret: app.getAccount().getSecret(),
|
|
195
|
+
redirect: app.getConfig().get('oauth.redirect_url'),
|
|
196
|
+
})).scopes(this.getConfig().get('oauth.scopes', 'snsapi_userinfo'));
|
|
197
|
+
});
|
|
198
|
+
let provider = oauthFactory.call(null, this);
|
|
199
|
+
if (!(provider instanceof ProviderInterface_1.default)) {
|
|
200
|
+
throw new Error(`The factory must return a \`ProviderInterface\` instance.`);
|
|
201
|
+
}
|
|
202
|
+
return provider;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* 根据刷新令牌获取公众号实例
|
|
206
|
+
* @param appId
|
|
207
|
+
* @param refreshToken
|
|
208
|
+
* @param config
|
|
209
|
+
* @returns
|
|
210
|
+
*/
|
|
211
|
+
getOfficialAccountWithRefreshToken(appId, refreshToken, config = null) {
|
|
212
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
213
|
+
return this.getOfficialAccountWithAccessToken(appId, yield this.getAuthorizerAccessToken(appId, refreshToken), config);
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* 根据访问令牌获取公众号实例
|
|
218
|
+
* @param appId
|
|
219
|
+
* @param accessToken
|
|
220
|
+
* @param config
|
|
221
|
+
* @returns
|
|
222
|
+
*/
|
|
223
|
+
getOfficialAccountWithAccessToken(appId, accessToken, config = null) {
|
|
224
|
+
return this.getOfficialAccount(new AuthorizerAccessToken_1.default(appId, accessToken), config);
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* 获取公众号实例
|
|
228
|
+
* @param authorizerAccessToken
|
|
229
|
+
* @param config
|
|
230
|
+
* @returns
|
|
231
|
+
*/
|
|
232
|
+
getOfficialAccount(authorizerAccessToken, config = null) {
|
|
233
|
+
if (!(config instanceof ConfigInterface_1.default)) {
|
|
234
|
+
config.app_id = authorizerAccessToken.getAppId();
|
|
235
|
+
config.token = this.config.get('token');
|
|
236
|
+
config.aes_key = this.config.get('aes_key');
|
|
237
|
+
config.http = this.config.get('http', {});
|
|
238
|
+
config = new Config_1.default(config);
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
config.set('app_id', authorizerAccessToken.getAppId());
|
|
242
|
+
config.set('token', this.config.get('token'));
|
|
243
|
+
config.set('aes_key', this.config.get('aes_key'));
|
|
244
|
+
config.set('http', this.config.get('http', {}));
|
|
245
|
+
}
|
|
246
|
+
let app = new Application_1.default(config);
|
|
247
|
+
app.setAccessToken(authorizerAccessToken);
|
|
248
|
+
app.setEncryptor(this.getEncryptor());
|
|
249
|
+
app.setOAuthFactory(this.createAuthorizerOAuthFactory(authorizerAccessToken.getAppId(), config));
|
|
250
|
+
return app;
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* 创建第三方oauth工厂
|
|
254
|
+
* @param authorizerAppId
|
|
255
|
+
* @param config
|
|
256
|
+
* @returns
|
|
257
|
+
*/
|
|
258
|
+
createAuthorizerOAuthFactory(authorizerAppId, config) {
|
|
259
|
+
return ((app) => {
|
|
260
|
+
return (new WeChat_1.default({
|
|
261
|
+
client_id: authorizerAppId,
|
|
262
|
+
component: {
|
|
263
|
+
component_app_id: this.getAccount().getSecret(),
|
|
264
|
+
component_access_token: this.getComponentAccessToken().getToken(),
|
|
265
|
+
},
|
|
266
|
+
redirect: this.config.get('oauth.redirect_url'),
|
|
267
|
+
})).scopes(config.get('oauth.scopes', 'snsapi_userinfo'));
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* 根据刷新令牌获取小程序实例
|
|
272
|
+
* @param appId
|
|
273
|
+
* @param refreshToken
|
|
274
|
+
* @param config
|
|
275
|
+
* @returns
|
|
276
|
+
*/
|
|
277
|
+
getMiniAppWithRefreshToken(appId, refreshToken, config = null) {
|
|
278
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
279
|
+
return this.getMiniAppWithAccessToken(appId, yield this.getAuthorizerAccessToken(appId, refreshToken), config);
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* 根据访问令牌获取小程序实例
|
|
284
|
+
* @param appId
|
|
285
|
+
* @param accessToken
|
|
286
|
+
* @param config
|
|
287
|
+
* @returns
|
|
288
|
+
*/
|
|
289
|
+
getMiniAppWithAccessToken(appId, accessToken, config = null) {
|
|
290
|
+
return this.getMiniApp(new AuthorizerAccessToken_1.default(appId, accessToken), config);
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* 获取小程序实例
|
|
294
|
+
* @param authorizerAccessToken
|
|
295
|
+
* @param config
|
|
296
|
+
* @returns
|
|
297
|
+
*/
|
|
298
|
+
getMiniApp(authorizerAccessToken, config = null) {
|
|
299
|
+
if (!(config instanceof ConfigInterface_1.default)) {
|
|
300
|
+
config.app_id = authorizerAccessToken.getAppId();
|
|
301
|
+
config.token = this.config.get('token');
|
|
302
|
+
config.aes_key = this.config.get('aes_key');
|
|
303
|
+
config.http = this.config.get('http');
|
|
304
|
+
config = new Config_1.default(config);
|
|
305
|
+
}
|
|
306
|
+
else {
|
|
307
|
+
config.set('app_id', authorizerAccessToken.getAppId());
|
|
308
|
+
config.set('token', this.config.get('token'));
|
|
309
|
+
config.set('aes_key', this.config.get('aes_key'));
|
|
310
|
+
config.set('http', this.config.get('http'));
|
|
311
|
+
}
|
|
312
|
+
let app = new Application_2.default(config);
|
|
313
|
+
app.setAccessToken(authorizerAccessToken);
|
|
314
|
+
app.setEncryptor(this.getEncryptor());
|
|
315
|
+
return app;
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* 获取第三方授权令牌
|
|
319
|
+
* @param appId
|
|
320
|
+
* @param refreshToken
|
|
321
|
+
* @returns
|
|
322
|
+
*/
|
|
323
|
+
getAuthorizerAccessToken(appId, refreshToken) {
|
|
324
|
+
var _a;
|
|
325
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
326
|
+
let md5RefreshToken = (0, Utils_1.createHash)(refreshToken, 'md5');
|
|
327
|
+
let cacheKey = `open-platform.authorizer_access_token.${appId}.${md5RefreshToken}`;
|
|
328
|
+
let cache = this.getCache();
|
|
329
|
+
let authorizerAccessToken = yield cache.get(cacheKey);
|
|
330
|
+
if (!authorizerAccessToken) {
|
|
331
|
+
let response = yield this.refreshAuthorizerToken(appId, refreshToken);
|
|
332
|
+
let authorizerAccessToken = response['authorizer_access_token'];
|
|
333
|
+
yield cache.set(cacheKey, authorizerAccessToken, ((_a = parseInt(response['expires_in'])) !== null && _a !== void 0 ? _a : 7200) - 500);
|
|
334
|
+
}
|
|
335
|
+
return authorizerAccessToken;
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
createClient() {
|
|
339
|
+
return (new AccessTokenAwareClient_1.default(this.getHttpClient(), this.getAccessToken(), (response) => { var _a; return (_a = response.toObject()['errcode']) !== null && _a !== void 0 ? _a : 0; }, this.getConfig().get('http.throw', true)))
|
|
340
|
+
.setPresets(this.getConfig().all());
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* 获取请求默认配置
|
|
344
|
+
* @returns
|
|
345
|
+
*/
|
|
346
|
+
getHttpClientDefaultOptions() {
|
|
347
|
+
return (0, merge_1.default)(true, {
|
|
348
|
+
baseURL: 'https://api.weixin.qq.com/',
|
|
349
|
+
}, this.getConfig().get('http', {}));
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
;
|
|
353
|
+
;
|
|
354
|
+
(0, Utils_1.applyMixins)(Application, [ConfigMixin_1.default, CacheMixin_1.default, ClientMixin_1.default, ServerRequestMixin_1.default, HttpClientMixin_1.default]);
|
|
355
|
+
module.exports = Application;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import HasAttributesMixin from "../Core/Mixins/HasAttributesMixin";
|
|
2
|
+
import AuthorizerAccessToken from "./AuthorizerAccessToken";
|
|
3
|
+
declare class Authorization {
|
|
4
|
+
constructor(attributes?: Record<string, any>);
|
|
5
|
+
/**
|
|
6
|
+
* 获取appid
|
|
7
|
+
* @returns
|
|
8
|
+
*/
|
|
9
|
+
getAppId(): string;
|
|
10
|
+
/**
|
|
11
|
+
* 获取access_token
|
|
12
|
+
* @returns
|
|
13
|
+
*/
|
|
14
|
+
getAccessToken(): AuthorizerAccessToken;
|
|
15
|
+
/**
|
|
16
|
+
* 获取refresh_token
|
|
17
|
+
* @returns
|
|
18
|
+
*/
|
|
19
|
+
getRefreshToken(): string;
|
|
20
|
+
}
|
|
21
|
+
interface Authorization extends HasAttributesMixin {
|
|
22
|
+
}
|
|
23
|
+
export = Authorization;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
const merge = __importStar(require("merge"));
|
|
29
|
+
const HasAttributesMixin_1 = __importDefault(require("../Core/Mixins/HasAttributesMixin"));
|
|
30
|
+
const Utils_1 = require("../Core/Support/Utils");
|
|
31
|
+
const AuthorizerAccessToken_1 = __importDefault(require("./AuthorizerAccessToken"));
|
|
32
|
+
class Authorization {
|
|
33
|
+
constructor(attributes = null) {
|
|
34
|
+
if (attributes) {
|
|
35
|
+
this.attributes = merge.recursive({}, attributes);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* 获取appid
|
|
40
|
+
* @returns
|
|
41
|
+
*/
|
|
42
|
+
getAppId() {
|
|
43
|
+
if (!this.attributes['authorization_info'])
|
|
44
|
+
return '';
|
|
45
|
+
return this.attributes['authorization_info']['authorizer_appid'] || '';
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* 获取access_token
|
|
49
|
+
* @returns
|
|
50
|
+
*/
|
|
51
|
+
getAccessToken() {
|
|
52
|
+
let appid = '';
|
|
53
|
+
let token = '';
|
|
54
|
+
if (this.attributes['authorization_info'] && this.attributes['authorization_info']['authorizer_appid']) {
|
|
55
|
+
appid = this.attributes['authorization_info']['authorizer_appid'];
|
|
56
|
+
}
|
|
57
|
+
if (this.attributes['authorization_info'] && this.attributes['authorization_info']['authorizer_access_token']) {
|
|
58
|
+
token = this.attributes['authorization_info']['authorizer_access_token'];
|
|
59
|
+
}
|
|
60
|
+
return new AuthorizerAccessToken_1.default(appid, token);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* 获取refresh_token
|
|
64
|
+
* @returns
|
|
65
|
+
*/
|
|
66
|
+
getRefreshToken() {
|
|
67
|
+
if (!this.attributes['authorization_info'])
|
|
68
|
+
return '';
|
|
69
|
+
return this.attributes['authorization_info']['authorizer_refresh_token'] || '';
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
;
|
|
73
|
+
(0, Utils_1.applyMixins)(Authorization, [HasAttributesMixin_1.default]);
|
|
74
|
+
module.exports = Authorization;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import AccessTokenInterface from "../Core/Contracts/AccessTokenInterface";
|
|
2
|
+
declare class AuthorizerAccessToken implements AccessTokenInterface {
|
|
3
|
+
protected appId: string;
|
|
4
|
+
protected accessToken: string;
|
|
5
|
+
constructor(appId: string, accessToken: string);
|
|
6
|
+
/**
|
|
7
|
+
* 获取appId
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
10
|
+
getAppId(): string;
|
|
11
|
+
getToken(): Promise<string>;
|
|
12
|
+
toQuery(): Promise<Record<string, any>>;
|
|
13
|
+
/**
|
|
14
|
+
* 转为字符串
|
|
15
|
+
* @returns
|
|
16
|
+
*/
|
|
17
|
+
toString(): string;
|
|
18
|
+
}
|
|
19
|
+
export = AuthorizerAccessToken;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
class AuthorizerAccessToken {
|
|
12
|
+
constructor(appId, accessToken) {
|
|
13
|
+
this.appId = appId;
|
|
14
|
+
this.accessToken = accessToken;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* 获取appId
|
|
18
|
+
* @returns
|
|
19
|
+
*/
|
|
20
|
+
getAppId() {
|
|
21
|
+
return this.appId;
|
|
22
|
+
}
|
|
23
|
+
getToken() {
|
|
24
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
+
return this.accessToken;
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
toQuery() {
|
|
29
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
30
|
+
return {
|
|
31
|
+
'access_token': yield this.getToken(),
|
|
32
|
+
};
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* 转为字符串
|
|
37
|
+
* @returns
|
|
38
|
+
*/
|
|
39
|
+
toString() {
|
|
40
|
+
return this.accessToken;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
module.exports = AuthorizerAccessToken;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import CacheInterface from "../Core/Contracts/CacheInterface";
|
|
2
|
+
import HttpClientInterface from "../Core/HttpClient/Contracts/HttpClientInterface";
|
|
3
|
+
import RefreshableAccessTokenInterface from "../Core/Contracts/RefreshableAccessTokenInterface";
|
|
4
|
+
import VerifyTicketInterface from "./Contracts/VerifyTicketInterface";
|
|
5
|
+
declare class ComponentAccessToken implements RefreshableAccessTokenInterface {
|
|
6
|
+
protected appId: string;
|
|
7
|
+
protected secret: string;
|
|
8
|
+
protected verifyTicket: VerifyTicketInterface;
|
|
9
|
+
protected key: string;
|
|
10
|
+
protected cache: CacheInterface;
|
|
11
|
+
protected httpClient: HttpClientInterface;
|
|
12
|
+
constructor(appId: string, secret: string, verifyTicket: VerifyTicketInterface, key?: string, cache?: CacheInterface, httpClient?: HttpClientInterface);
|
|
13
|
+
/**
|
|
14
|
+
* 获取access_token的缓存名称
|
|
15
|
+
* @returns
|
|
16
|
+
*/
|
|
17
|
+
getKey(): string;
|
|
18
|
+
/**
|
|
19
|
+
* 设置access_token的缓存名称
|
|
20
|
+
* @param key
|
|
21
|
+
* @returns
|
|
22
|
+
*/
|
|
23
|
+
setKey(key: string): this;
|
|
24
|
+
getToken(): Promise<string>;
|
|
25
|
+
toQuery(): Promise<Record<string, any>>;
|
|
26
|
+
refresh(): Promise<string>;
|
|
27
|
+
}
|
|
28
|
+
export = ComponentAccessToken;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
const HttpClient_1 = __importDefault(require("../Core/HttpClient/HttpClient"));
|
|
15
|
+
const FileCache_1 = __importDefault(require("../Core/Cache/FileCache"));
|
|
16
|
+
class ComponentAccessToken {
|
|
17
|
+
constructor(appId, secret, verifyTicket, key = null, cache = null, httpClient = null) {
|
|
18
|
+
this.appId = appId;
|
|
19
|
+
this.secret = secret;
|
|
20
|
+
this.verifyTicket = verifyTicket;
|
|
21
|
+
this.key = key;
|
|
22
|
+
this.cache = cache;
|
|
23
|
+
this.httpClient = httpClient;
|
|
24
|
+
if (!this.cache) {
|
|
25
|
+
this.cache = new FileCache_1.default();
|
|
26
|
+
}
|
|
27
|
+
if (!this.httpClient) {
|
|
28
|
+
this.httpClient = HttpClient_1.default.create({
|
|
29
|
+
baseURL: 'https://api.weixin.qq.com/',
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* 获取access_token的缓存名称
|
|
35
|
+
* @returns
|
|
36
|
+
*/
|
|
37
|
+
getKey() {
|
|
38
|
+
if (!this.key) {
|
|
39
|
+
this.key = `open_platform.component_access_token.${this.appId}`;
|
|
40
|
+
}
|
|
41
|
+
return this.key;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* 设置access_token的缓存名称
|
|
45
|
+
* @param key
|
|
46
|
+
* @returns
|
|
47
|
+
*/
|
|
48
|
+
setKey(key) {
|
|
49
|
+
this.key = key;
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
getToken() {
|
|
53
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
54
|
+
let token = yield this.cache.get(this.getKey());
|
|
55
|
+
if (!!token && typeof token === 'string') {
|
|
56
|
+
return token;
|
|
57
|
+
}
|
|
58
|
+
return this.refresh();
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
toQuery() {
|
|
62
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
63
|
+
return {
|
|
64
|
+
component_access_token: yield this.getToken(),
|
|
65
|
+
};
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
refresh() {
|
|
69
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
70
|
+
let response = (yield this.httpClient.request('post', 'cgi-bin/component/api_component_token', {
|
|
71
|
+
params: {
|
|
72
|
+
component_appid: this.appId,
|
|
73
|
+
component_appsecret: this.secret,
|
|
74
|
+
component_verify_ticket: yield this.verifyTicket.getTicket(),
|
|
75
|
+
}
|
|
76
|
+
})).toObject();
|
|
77
|
+
if (!response['component_access_token']) {
|
|
78
|
+
throw new Error('Failed to get component_access_token: ' + JSON.stringify(response));
|
|
79
|
+
}
|
|
80
|
+
yield this.cache.set(this.getKey(), response['component_access_token'], parseInt(response['expires_in']) - 100);
|
|
81
|
+
return response['component_access_token'];
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
module.exports = ComponentAccessToken;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
const Config_1 = __importDefault(require("../Core/Config"));
|
|
6
|
+
class Config extends Config_1.default {
|
|
7
|
+
constructor() {
|
|
8
|
+
super(...arguments);
|
|
9
|
+
this.requiredKeys = [
|
|
10
|
+
'app_id',
|
|
11
|
+
'secret',
|
|
12
|
+
'aes_key',
|
|
13
|
+
];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
module.exports = Config;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
declare abstract class AccountInterface {
|
|
2
|
+
/**
|
|
3
|
+
* 获取appid
|
|
4
|
+
* @returns
|
|
5
|
+
*/
|
|
6
|
+
getAppId(): string;
|
|
7
|
+
/**
|
|
8
|
+
* 获取secret
|
|
9
|
+
* @returns
|
|
10
|
+
*/
|
|
11
|
+
getSecret(): string;
|
|
12
|
+
/**
|
|
13
|
+
* 获取token
|
|
14
|
+
* @returns
|
|
15
|
+
*/
|
|
16
|
+
getToken(): string;
|
|
17
|
+
/**
|
|
18
|
+
* 获取aesKey
|
|
19
|
+
* @returns
|
|
20
|
+
*/
|
|
21
|
+
getAesKey(): string;
|
|
22
|
+
}
|
|
23
|
+
export = AccountInterface;
|