node-easywechat 3.0.0-alpha.1 → 3.0.0-alpha.2
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 +6 -0
- package/dist/Core/Config.d.ts +1 -1
- package/dist/Core/Config.js +2 -2
- package/dist/Core/Encryptor.d.ts +3 -3
- package/dist/Core/Encryptor.js +6 -7
- package/dist/Core/HttpClient/HttpClient.js +0 -3
- package/dist/Core/HttpClient/HttpClientResponse.d.ts +6 -0
- package/dist/Core/HttpClient/HttpClientResponse.js +36 -4
- package/dist/Core/Message.d.ts +11 -0
- package/dist/Core/Message.js +11 -0
- package/dist/MiniApp/AccessToken.d.ts +4 -0
- package/dist/MiniApp/AccessToken.js +8 -0
- package/dist/MiniApp/Account.d.ts +4 -0
- package/dist/MiniApp/Account.js +8 -0
- package/dist/MiniApp/Application.d.ts +59 -0
- package/dist/MiniApp/Application.js +123 -0
- package/dist/MiniApp/Contracts/AccountInterface.d.ts +23 -0
- package/dist/MiniApp/Contracts/AccountInterface.js +25 -0
- package/dist/MiniApp/Contracts/ApplicationInterface.d.ts +68 -0
- package/dist/MiniApp/Contracts/ApplicationInterface.js +60 -0
- package/dist/MiniApp/Decryptor.d.ts +4 -0
- package/dist/MiniApp/Decryptor.js +20 -0
- package/dist/MiniApp/Server.d.ts +4 -0
- package/dist/MiniApp/Server.js +9 -0
- package/dist/MiniApp/Utils.d.ts +22 -0
- package/dist/MiniApp/Utils.js +56 -0
- package/dist/OfficialAccount/AccessToken.js +10 -9
- package/dist/OfficialAccount/Account.js +0 -4
- package/dist/OfficialAccount/Server.js +1 -3
- package/dist/OfficialAccount/Utils.js +0 -1
- package/official_account.access_token.mock-access_token-appid.cache +1 -1
- package/official_account.jsapi_ticket.mock-ticket-appid.cache +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/dist/Core/Config.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import ConfigInterface from "./Contracts/ConfigInterface";
|
|
|
3
3
|
declare class Config implements ConfigInterface {
|
|
4
4
|
protected items: OfficialAccountConfig | MiniAppConfig | PayConfig | OpenPlatformConfig | WorkConfig | OpenWorkConfig;
|
|
5
5
|
protected requiredKeys: string[];
|
|
6
|
-
constructor(items
|
|
6
|
+
constructor(items?: OfficialAccountConfig | MiniAppConfig | PayConfig | OpenPlatformConfig | WorkConfig | OpenWorkConfig);
|
|
7
7
|
/**
|
|
8
8
|
* 检查是否有配置项缺失
|
|
9
9
|
* @returns
|
package/dist/Core/Config.js
CHANGED
|
@@ -5,8 +5,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
const merge_1 = __importDefault(require("merge"));
|
|
6
6
|
const Obj_1 = __importDefault(require("./Support/Obj"));
|
|
7
7
|
class Config {
|
|
8
|
-
constructor(items) {
|
|
9
|
-
this.items =
|
|
8
|
+
constructor(items = {}) {
|
|
9
|
+
this.items = items;
|
|
10
10
|
this.requiredKeys = [];
|
|
11
11
|
this.items = merge_1.default.recursive(true, items);
|
|
12
12
|
this.checkMissingKeys();
|
package/dist/Core/Encryptor.d.ts
CHANGED
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
declare class Encryptor {
|
|
3
3
|
protected appId: string;
|
|
4
4
|
protected token: string;
|
|
5
|
-
protected aesKey: Buffer;
|
|
6
|
-
protected blockSize: number;
|
|
5
|
+
protected aesKey: string | Buffer;
|
|
7
6
|
protected receiveId: string;
|
|
8
|
-
|
|
7
|
+
protected blockSize: number;
|
|
8
|
+
constructor(appId?: string, token?: string, aesKey?: string | Buffer, receiveId?: string);
|
|
9
9
|
/**
|
|
10
10
|
* 获取配置的token
|
|
11
11
|
* @returns
|
package/dist/Core/Encryptor.js
CHANGED
|
@@ -7,16 +7,15 @@ const Utils_1 = require("./Support/Utils");
|
|
|
7
7
|
const AES_1 = require("./Support/AES");
|
|
8
8
|
const PKCS_1 = __importDefault(require("./Support/PKCS"));
|
|
9
9
|
class Encryptor {
|
|
10
|
-
constructor(appId, token, aesKey, receiveId = null) {
|
|
11
|
-
this.appId = null;
|
|
12
|
-
this.token = null;
|
|
13
|
-
this.aesKey = null;
|
|
14
|
-
this.blockSize = 32;
|
|
15
|
-
this.receiveId = null;
|
|
10
|
+
constructor(appId = null, token = null, aesKey = null, receiveId = null) {
|
|
16
11
|
this.appId = appId;
|
|
17
12
|
this.token = token;
|
|
13
|
+
this.aesKey = aesKey;
|
|
18
14
|
this.receiveId = receiveId;
|
|
19
|
-
this.
|
|
15
|
+
this.blockSize = 32;
|
|
16
|
+
if (typeof this.aesKey === 'string') {
|
|
17
|
+
this.aesKey = Buffer.from(this.aesKey + '=', 'base64');
|
|
18
|
+
}
|
|
20
19
|
}
|
|
21
20
|
/**
|
|
22
21
|
* 获取配置的token
|
|
@@ -17,9 +17,6 @@ const HttpClientResponse_1 = __importDefault(require("./HttpClientResponse"));
|
|
|
17
17
|
const Utils_1 = require("../Support/Utils");
|
|
18
18
|
class HttpClient {
|
|
19
19
|
constructor(axios, failureJudge = null, throwError = false) {
|
|
20
|
-
this.axios = null;
|
|
21
|
-
this.failureJudge = null;
|
|
22
|
-
this.throwError = true;
|
|
23
20
|
this.axios = axios;
|
|
24
21
|
this.failureJudge = failureJudge;
|
|
25
22
|
this.throwError = throwError;
|
|
@@ -53,6 +53,12 @@ declare class HttpClientResponse implements HttpClientResponseInterface {
|
|
|
53
53
|
* @returns
|
|
54
54
|
*/
|
|
55
55
|
toDataUrl(): string;
|
|
56
|
+
/**
|
|
57
|
+
* 判断 content-type 是否指定类型
|
|
58
|
+
* @param type
|
|
59
|
+
* @returns
|
|
60
|
+
*/
|
|
61
|
+
is(type: 'json' | 'xml' | 'html' | 'image' | 'audio' | 'video' | 'text'): boolean;
|
|
56
62
|
/**
|
|
57
63
|
* 判断header是否存在
|
|
58
64
|
* @param key
|
|
@@ -16,9 +16,6 @@ const merge_1 = __importDefault(require("merge"));
|
|
|
16
16
|
const Utils_1 = require("../Support/Utils");
|
|
17
17
|
class HttpClientResponse {
|
|
18
18
|
constructor(response, failureJudge = null, throwError = true) {
|
|
19
|
-
this.response = null;
|
|
20
|
-
this.failureJudge = null;
|
|
21
|
-
this.throwError = false;
|
|
22
19
|
this.response = response;
|
|
23
20
|
this.failureJudge = failureJudge;
|
|
24
21
|
this.throwError = throwError;
|
|
@@ -47,7 +44,7 @@ class HttpClientResponse {
|
|
|
47
44
|
* @returns
|
|
48
45
|
*/
|
|
49
46
|
isFailed() {
|
|
50
|
-
if (this.failureJudge) {
|
|
47
|
+
if (this.is('text') && this.failureJudge) {
|
|
51
48
|
return this.failureJudge(this);
|
|
52
49
|
}
|
|
53
50
|
try {
|
|
@@ -121,6 +118,41 @@ class HttpClientResponse {
|
|
|
121
118
|
toDataUrl() {
|
|
122
119
|
return 'data:' + this.getHeader('content-type') + ';base64,' + this.response.data;
|
|
123
120
|
}
|
|
121
|
+
/**
|
|
122
|
+
* 判断 content-type 是否指定类型
|
|
123
|
+
* @param type
|
|
124
|
+
* @returns
|
|
125
|
+
*/
|
|
126
|
+
is(type) {
|
|
127
|
+
let contentType = this.getHeader('content-type');
|
|
128
|
+
let res = false;
|
|
129
|
+
switch (type.toLowerCase()) {
|
|
130
|
+
case 'json':
|
|
131
|
+
res = contentType.indexOf('/json') > -1;
|
|
132
|
+
break;
|
|
133
|
+
case 'xml':
|
|
134
|
+
res = contentType.indexOf('/xml') > -1;
|
|
135
|
+
break;
|
|
136
|
+
case 'html':
|
|
137
|
+
res = contentType.indexOf('/html') > -1;
|
|
138
|
+
break;
|
|
139
|
+
case 'image':
|
|
140
|
+
res = contentType.indexOf('image/') > -1;
|
|
141
|
+
break;
|
|
142
|
+
case 'audio':
|
|
143
|
+
res = contentType.indexOf('audio/') > -1;
|
|
144
|
+
break;
|
|
145
|
+
case 'video':
|
|
146
|
+
res = contentType.indexOf('video/') > -1;
|
|
147
|
+
break;
|
|
148
|
+
case 'text':
|
|
149
|
+
res = contentType.indexOf('text/') > -1
|
|
150
|
+
|| contentType.indexOf('/json') > -1
|
|
151
|
+
|| contentType.indexOf('/xml') > -1;
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
return res;
|
|
155
|
+
}
|
|
124
156
|
/**
|
|
125
157
|
* 判断header是否存在
|
|
126
158
|
* @param key
|
package/dist/Core/Message.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import ServerRequestInterface from "./Http/Contracts/ServerRequestInterface";
|
|
2
2
|
import HasAttributesMixin from "./Mixins/HasAttributesMixin";
|
|
3
3
|
declare class Message {
|
|
4
|
+
/**
|
|
5
|
+
* 原始消息内容
|
|
6
|
+
*/
|
|
4
7
|
protected originContent: string;
|
|
5
8
|
constructor(attributes: Record<string, any>, originContent?: string);
|
|
6
9
|
/**
|
|
@@ -9,7 +12,15 @@ declare class Message {
|
|
|
9
12
|
* @returns
|
|
10
13
|
*/
|
|
11
14
|
static createFromRequest(request: ServerRequestInterface): Promise<Message>;
|
|
15
|
+
/**
|
|
16
|
+
* 获取原始消息内容
|
|
17
|
+
* @returns
|
|
18
|
+
*/
|
|
12
19
|
getOriginalContents(): string;
|
|
20
|
+
/**
|
|
21
|
+
* 转为字符串
|
|
22
|
+
* @returns
|
|
23
|
+
*/
|
|
13
24
|
toString(): string;
|
|
14
25
|
}
|
|
15
26
|
interface Message extends HasAttributesMixin {
|
package/dist/Core/Message.js
CHANGED
|
@@ -15,6 +15,9 @@ const HasAttributesMixin_1 = __importDefault(require("./Mixins/HasAttributesMixi
|
|
|
15
15
|
const Utils_1 = require("./Support/Utils");
|
|
16
16
|
class Message {
|
|
17
17
|
constructor(attributes, originContent = '') {
|
|
18
|
+
/**
|
|
19
|
+
* 原始消息内容
|
|
20
|
+
*/
|
|
18
21
|
this.originContent = '';
|
|
19
22
|
this.attributes = attributes;
|
|
20
23
|
this.originContent = originContent;
|
|
@@ -61,9 +64,17 @@ class Message {
|
|
|
61
64
|
return new Message(attributes, originContent);
|
|
62
65
|
});
|
|
63
66
|
}
|
|
67
|
+
/**
|
|
68
|
+
* 获取原始消息内容
|
|
69
|
+
* @returns
|
|
70
|
+
*/
|
|
64
71
|
getOriginalContents() {
|
|
65
72
|
return this.originContent;
|
|
66
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* 转为字符串
|
|
76
|
+
* @returns
|
|
77
|
+
*/
|
|
67
78
|
toString() {
|
|
68
79
|
return this.toJson();
|
|
69
80
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
const AccessToken_1 = __importDefault(require("../OfficialAccount/AccessToken"));
|
|
6
|
+
class AccessToken extends AccessToken_1.default {
|
|
7
|
+
}
|
|
8
|
+
module.exports = AccessToken;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
const Account_1 = __importDefault(require("../OfficialAccount/Account"));
|
|
6
|
+
class Account extends Account_1.default {
|
|
7
|
+
}
|
|
8
|
+
module.exports = Account;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import AccessTokenInterface from '../Core/Contracts/AccessTokenInterface';
|
|
2
|
+
import ConfigInterface from '../Core/Contracts/ConfigInterface';
|
|
3
|
+
import ServerInterface from '../Core/Contracts/ServerInterface';
|
|
4
|
+
import Encryptor from '../Core/Encryptor';
|
|
5
|
+
import AccessTokenAwareClient from '../Core/HttpClient/AccessTokenAwareClient';
|
|
6
|
+
import CacheMixin from '../Core/Mixins/CacheMixin';
|
|
7
|
+
import ClientMixin from '../Core/Mixins/ClientMixin';
|
|
8
|
+
import ConfigMixin from '../Core/Mixins/ConfigMixin';
|
|
9
|
+
import HttpClientMixin from '../Core/Mixins/HttpClientMixin';
|
|
10
|
+
import ServerRequestMixin from '../Core/Mixins/ServerRequestMixin';
|
|
11
|
+
import { MiniAppConfig } from '../Types/global';
|
|
12
|
+
import AccountInterface from './Contracts/AccountInterface';
|
|
13
|
+
import ApplicationInterface from './Contracts/ApplicationInterface';
|
|
14
|
+
import Utils from './Utils';
|
|
15
|
+
declare class Application implements ApplicationInterface {
|
|
16
|
+
constructor(config: ConfigInterface | MiniAppConfig);
|
|
17
|
+
protected account: AccountInterface;
|
|
18
|
+
protected encryptor: Encryptor;
|
|
19
|
+
protected server: ServerInterface;
|
|
20
|
+
protected accessToken: AccessTokenInterface;
|
|
21
|
+
getAccount(): AccountInterface;
|
|
22
|
+
/**
|
|
23
|
+
* 设置当前账户实例
|
|
24
|
+
* @param account
|
|
25
|
+
* @returns
|
|
26
|
+
*/
|
|
27
|
+
setAccount(account: AccountInterface): this;
|
|
28
|
+
getEncryptor(): Encryptor;
|
|
29
|
+
/**
|
|
30
|
+
* 设置加密机实例
|
|
31
|
+
* @param encryptor
|
|
32
|
+
* @returns
|
|
33
|
+
*/
|
|
34
|
+
setEncryptor(encryptor: Encryptor): this;
|
|
35
|
+
getServer(): ServerInterface;
|
|
36
|
+
/**
|
|
37
|
+
* 设置服务端实例
|
|
38
|
+
* @param server
|
|
39
|
+
* @returns
|
|
40
|
+
*/
|
|
41
|
+
setServer(server: ServerInterface): this;
|
|
42
|
+
getAccessToken(): AccessTokenInterface;
|
|
43
|
+
/**
|
|
44
|
+
* 设置AccessToken实例
|
|
45
|
+
* @param accessToken
|
|
46
|
+
* @returns
|
|
47
|
+
*/
|
|
48
|
+
setAccessToken(accessToken: AccessTokenInterface): this;
|
|
49
|
+
getUtils(): Utils;
|
|
50
|
+
createClient(): AccessTokenAwareClient;
|
|
51
|
+
/**
|
|
52
|
+
* 获取请求默认配置
|
|
53
|
+
* @returns
|
|
54
|
+
*/
|
|
55
|
+
protected getHttpClientDefaultOptions(): Record<string, any>;
|
|
56
|
+
}
|
|
57
|
+
interface Application extends ConfigMixin, CacheMixin, ClientMixin, ServerRequestMixin, HttpClientMixin {
|
|
58
|
+
}
|
|
59
|
+
export = Application;
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
const merge_1 = __importDefault(require("merge"));
|
|
6
|
+
const ConfigInterface_1 = __importDefault(require("../Core/Contracts/ConfigInterface"));
|
|
7
|
+
const Encryptor_1 = __importDefault(require("../Core/Encryptor"));
|
|
8
|
+
const AccessTokenAwareClient_1 = __importDefault(require("../Core/HttpClient/AccessTokenAwareClient"));
|
|
9
|
+
const CacheMixin_1 = __importDefault(require("../Core/Mixins/CacheMixin"));
|
|
10
|
+
const ClientMixin_1 = __importDefault(require("../Core/Mixins/ClientMixin"));
|
|
11
|
+
const ConfigMixin_1 = __importDefault(require("../Core/Mixins/ConfigMixin"));
|
|
12
|
+
const HttpClientMixin_1 = __importDefault(require("../Core/Mixins/HttpClientMixin"));
|
|
13
|
+
const ServerRequestMixin_1 = __importDefault(require("../Core/Mixins/ServerRequestMixin"));
|
|
14
|
+
const Utils_1 = require("../Core/Support/Utils");
|
|
15
|
+
const AccessToken_1 = __importDefault(require("./AccessToken"));
|
|
16
|
+
const Account_1 = __importDefault(require("./Account"));
|
|
17
|
+
const Server_1 = __importDefault(require("./Server"));
|
|
18
|
+
const Utils_2 = __importDefault(require("./Utils"));
|
|
19
|
+
const Config_1 = __importDefault(require("../OfficialAccount/Config"));
|
|
20
|
+
class Application {
|
|
21
|
+
constructor(config) {
|
|
22
|
+
this.account = null;
|
|
23
|
+
this.encryptor = null;
|
|
24
|
+
this.server = null;
|
|
25
|
+
this.accessToken = null;
|
|
26
|
+
if (config instanceof ConfigInterface_1.default) {
|
|
27
|
+
this.setConfig(config);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
this.setConfig(new Config_1.default(config));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
getAccount() {
|
|
34
|
+
if (!this.account) {
|
|
35
|
+
this.account = new Account_1.default(this.config.get('app_id'), this.config.get('secret'), this.config.get('token'), this.config.get('aes_key'));
|
|
36
|
+
}
|
|
37
|
+
return this.account;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* 设置当前账户实例
|
|
41
|
+
* @param account
|
|
42
|
+
* @returns
|
|
43
|
+
*/
|
|
44
|
+
setAccount(account) {
|
|
45
|
+
this.account = account;
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
48
|
+
getEncryptor() {
|
|
49
|
+
if (!this.encryptor) {
|
|
50
|
+
let token = this.getAccount().getToken();
|
|
51
|
+
let aesKey = this.getAccount().getAesKey();
|
|
52
|
+
if (!token || !aesKey) {
|
|
53
|
+
throw new Error('token or aes_key cannot be empty.');
|
|
54
|
+
}
|
|
55
|
+
this.encryptor = new Encryptor_1.default(this.getAccount().getAppId(), token, aesKey, this.getAccount().getAppId());
|
|
56
|
+
}
|
|
57
|
+
return this.encryptor;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* 设置加密机实例
|
|
61
|
+
* @param encryptor
|
|
62
|
+
* @returns
|
|
63
|
+
*/
|
|
64
|
+
setEncryptor(encryptor) {
|
|
65
|
+
this.encryptor = encryptor;
|
|
66
|
+
return this;
|
|
67
|
+
}
|
|
68
|
+
getServer() {
|
|
69
|
+
if (!this.server) {
|
|
70
|
+
let token = this.getAccount().getToken();
|
|
71
|
+
let aesKey = this.getAccount().getAesKey();
|
|
72
|
+
if (!token || !aesKey) {
|
|
73
|
+
throw new Error('token or aes_key cannot be empty.');
|
|
74
|
+
}
|
|
75
|
+
this.server = new Server_1.default(this.getRequest(), this.getAccount().getAesKey() ? this.getEncryptor() : null);
|
|
76
|
+
}
|
|
77
|
+
return this.server;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* 设置服务端实例
|
|
81
|
+
* @param server
|
|
82
|
+
* @returns
|
|
83
|
+
*/
|
|
84
|
+
setServer(server) {
|
|
85
|
+
this.server = server;
|
|
86
|
+
return this;
|
|
87
|
+
}
|
|
88
|
+
getAccessToken() {
|
|
89
|
+
if (!this.accessToken) {
|
|
90
|
+
this.accessToken = new AccessToken_1.default(this.getAccount().getAppId(), this.getAccount().getSecret(), null, this.getCache(), this.getHttpClient());
|
|
91
|
+
}
|
|
92
|
+
return this.accessToken;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* 设置AccessToken实例
|
|
96
|
+
* @param accessToken
|
|
97
|
+
* @returns
|
|
98
|
+
*/
|
|
99
|
+
setAccessToken(accessToken) {
|
|
100
|
+
this.accessToken = accessToken;
|
|
101
|
+
return this;
|
|
102
|
+
}
|
|
103
|
+
getUtils() {
|
|
104
|
+
return new Utils_2.default(this);
|
|
105
|
+
}
|
|
106
|
+
createClient() {
|
|
107
|
+
let httpClient = this.getHttpClient();
|
|
108
|
+
return new AccessTokenAwareClient_1.default(httpClient, this.getAccessToken());
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* 获取请求默认配置
|
|
112
|
+
* @returns
|
|
113
|
+
*/
|
|
114
|
+
getHttpClientDefaultOptions() {
|
|
115
|
+
return (0, merge_1.default)(true, {
|
|
116
|
+
baseURL: 'https://api.weixin.qq.com/',
|
|
117
|
+
}, this.getConfig().get('http'));
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
;
|
|
121
|
+
;
|
|
122
|
+
(0, Utils_1.applyMixins)(Application, [ConfigMixin_1.default, CacheMixin_1.default, ClientMixin_1.default, ServerRequestMixin_1.default, HttpClientMixin_1.default]);
|
|
123
|
+
module.exports = Application;
|
|
@@ -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;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
class AccountInterface {
|
|
3
|
+
/**
|
|
4
|
+
* 获取appid
|
|
5
|
+
* @returns
|
|
6
|
+
*/
|
|
7
|
+
getAppId() { return null; }
|
|
8
|
+
/**
|
|
9
|
+
* 获取secret
|
|
10
|
+
* @returns
|
|
11
|
+
*/
|
|
12
|
+
getSecret() { return null; }
|
|
13
|
+
/**
|
|
14
|
+
* 获取token
|
|
15
|
+
* @returns
|
|
16
|
+
*/
|
|
17
|
+
getToken() { return null; }
|
|
18
|
+
/**
|
|
19
|
+
* 获取aesKey
|
|
20
|
+
* @returns
|
|
21
|
+
*/
|
|
22
|
+
getAesKey() { return null; }
|
|
23
|
+
}
|
|
24
|
+
;
|
|
25
|
+
module.exports = AccountInterface;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import AccessTokenInterface from "../../Core/Contracts/AccessTokenInterface";
|
|
2
|
+
import CacheInterface from "../../Core/Contracts/CacheInterface";
|
|
3
|
+
import ConfigInterface from "../../Core/Contracts/ConfigInterface";
|
|
4
|
+
import AccessTokenAwareClient from "../../Core/HttpClient/AccessTokenAwareClient";
|
|
5
|
+
import HttpClientInterface from "../../Core/HttpClient/Contracts/HttpClientInterface";
|
|
6
|
+
import ServerInterface from "../../Core/Contracts/ServerInterface";
|
|
7
|
+
import ServerRequestInterface from "../../Core/Http/Contracts/ServerRequestInterface";
|
|
8
|
+
import Encryptor from "../../Core/Encryptor";
|
|
9
|
+
import AccountInterface from "./AccountInterface";
|
|
10
|
+
import Utils from "../Utils";
|
|
11
|
+
declare abstract class ApplicationInterface {
|
|
12
|
+
/**
|
|
13
|
+
* 获取当前账户实例
|
|
14
|
+
* @returns
|
|
15
|
+
*/
|
|
16
|
+
getAccount(): AccountInterface;
|
|
17
|
+
/**
|
|
18
|
+
* 获取加密机实例
|
|
19
|
+
* @returns
|
|
20
|
+
*/
|
|
21
|
+
getEncryptor(): Encryptor;
|
|
22
|
+
/**
|
|
23
|
+
* 获取服务端实例
|
|
24
|
+
* @returns
|
|
25
|
+
*/
|
|
26
|
+
getServer(): ServerInterface;
|
|
27
|
+
/**
|
|
28
|
+
* 获取当前请求实例
|
|
29
|
+
* @returns
|
|
30
|
+
*/
|
|
31
|
+
getRequest(): ServerRequestInterface;
|
|
32
|
+
/**
|
|
33
|
+
* 获取客户端实例
|
|
34
|
+
* @returns
|
|
35
|
+
*/
|
|
36
|
+
getClient(): AccessTokenAwareClient;
|
|
37
|
+
/**
|
|
38
|
+
* 创建客户端实例
|
|
39
|
+
* @returns
|
|
40
|
+
*/
|
|
41
|
+
createClient(): AccessTokenAwareClient;
|
|
42
|
+
/**
|
|
43
|
+
* 获取网络请求客户端实例
|
|
44
|
+
* @returns
|
|
45
|
+
*/
|
|
46
|
+
getHttpClient(): HttpClientInterface;
|
|
47
|
+
/**
|
|
48
|
+
* 获取配置信息实例
|
|
49
|
+
* @returns
|
|
50
|
+
*/
|
|
51
|
+
getConfig(): ConfigInterface;
|
|
52
|
+
/**
|
|
53
|
+
* 获取AccessToken实例
|
|
54
|
+
* @returns
|
|
55
|
+
*/
|
|
56
|
+
getAccessToken(): AccessTokenInterface;
|
|
57
|
+
/**
|
|
58
|
+
* 获取缓存实例
|
|
59
|
+
* @returns
|
|
60
|
+
*/
|
|
61
|
+
getCache(): CacheInterface;
|
|
62
|
+
/**
|
|
63
|
+
* 获取工具实例
|
|
64
|
+
* @returns
|
|
65
|
+
*/
|
|
66
|
+
getUtils(): Utils;
|
|
67
|
+
}
|
|
68
|
+
export = ApplicationInterface;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
class ApplicationInterface {
|
|
3
|
+
/**
|
|
4
|
+
* 获取当前账户实例
|
|
5
|
+
* @returns
|
|
6
|
+
*/
|
|
7
|
+
getAccount() { return null; }
|
|
8
|
+
/**
|
|
9
|
+
* 获取加密机实例
|
|
10
|
+
* @returns
|
|
11
|
+
*/
|
|
12
|
+
getEncryptor() { return null; }
|
|
13
|
+
/**
|
|
14
|
+
* 获取服务端实例
|
|
15
|
+
* @returns
|
|
16
|
+
*/
|
|
17
|
+
getServer() { return null; }
|
|
18
|
+
/**
|
|
19
|
+
* 获取当前请求实例
|
|
20
|
+
* @returns
|
|
21
|
+
*/
|
|
22
|
+
getRequest() { return null; }
|
|
23
|
+
/**
|
|
24
|
+
* 获取客户端实例
|
|
25
|
+
* @returns
|
|
26
|
+
*/
|
|
27
|
+
getClient() { return null; }
|
|
28
|
+
/**
|
|
29
|
+
* 创建客户端实例
|
|
30
|
+
* @returns
|
|
31
|
+
*/
|
|
32
|
+
createClient() { return null; }
|
|
33
|
+
/**
|
|
34
|
+
* 获取网络请求客户端实例
|
|
35
|
+
* @returns
|
|
36
|
+
*/
|
|
37
|
+
getHttpClient() { return null; }
|
|
38
|
+
/**
|
|
39
|
+
* 获取配置信息实例
|
|
40
|
+
* @returns
|
|
41
|
+
*/
|
|
42
|
+
getConfig() { return null; }
|
|
43
|
+
/**
|
|
44
|
+
* 获取AccessToken实例
|
|
45
|
+
* @returns
|
|
46
|
+
*/
|
|
47
|
+
getAccessToken() { return null; }
|
|
48
|
+
/**
|
|
49
|
+
* 获取缓存实例
|
|
50
|
+
* @returns
|
|
51
|
+
*/
|
|
52
|
+
getCache() { return null; }
|
|
53
|
+
/**
|
|
54
|
+
* 获取工具实例
|
|
55
|
+
* @returns
|
|
56
|
+
*/
|
|
57
|
+
getUtils() { return null; }
|
|
58
|
+
}
|
|
59
|
+
;
|
|
60
|
+
module.exports = ApplicationInterface;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const AES_1 = require("../Core/Support/AES");
|
|
3
|
+
class Decryptor {
|
|
4
|
+
static decrypt(sessionKey, iv, ciphertext) {
|
|
5
|
+
let decrypted = null;
|
|
6
|
+
try {
|
|
7
|
+
// 解密
|
|
8
|
+
decrypted = AES_1.AES.decrypt(Buffer.from(ciphertext, 'base64'), Buffer.from(sessionKey, 'base64'), Buffer.from(iv, 'base64'), true, 'aes-128-cbc').toString('utf8');
|
|
9
|
+
decrypted = JSON.parse(decrypted);
|
|
10
|
+
if (!decrypted || typeof decrypted !== 'object') {
|
|
11
|
+
throw new Error('The given payload is invalid.');
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
catch (e) {
|
|
15
|
+
throw new Error(`Fail to decrypt data: ${e.message}`);
|
|
16
|
+
}
|
|
17
|
+
return decrypted;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
module.exports = Decryptor;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
const Server_1 = __importDefault(require("../OfficialAccount/Server"));
|
|
6
|
+
class Server extends Server_1.default {
|
|
7
|
+
}
|
|
8
|
+
;
|
|
9
|
+
module.exports = Server;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import Application from './Application';
|
|
2
|
+
declare class Utils {
|
|
3
|
+
protected app: Application;
|
|
4
|
+
constructor(app: Application);
|
|
5
|
+
/**
|
|
6
|
+
* 登录凭证校验
|
|
7
|
+
* @see https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html
|
|
8
|
+
* @param code
|
|
9
|
+
* @returns
|
|
10
|
+
*/
|
|
11
|
+
codeToSession(code: string): Promise<Record<string, any>>;
|
|
12
|
+
/**
|
|
13
|
+
* 数据解密
|
|
14
|
+
* @see https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/signature.html#加密数据解密算法
|
|
15
|
+
* @param sessionKey
|
|
16
|
+
* @param iv
|
|
17
|
+
* @param ciphertext
|
|
18
|
+
* @returns
|
|
19
|
+
*/
|
|
20
|
+
decryptSession(sessionKey: string, iv: string, ciphertext: string): object;
|
|
21
|
+
}
|
|
22
|
+
export = Utils;
|
|
@@ -0,0 +1,56 @@
|
|
|
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 Decryptor_1 = __importDefault(require("./Decryptor"));
|
|
15
|
+
class Utils {
|
|
16
|
+
constructor(app) {
|
|
17
|
+
this.app = null;
|
|
18
|
+
this.app = app;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* 登录凭证校验
|
|
22
|
+
* @see https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html
|
|
23
|
+
* @param code
|
|
24
|
+
* @returns
|
|
25
|
+
*/
|
|
26
|
+
codeToSession(code) {
|
|
27
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
28
|
+
let client = this.app.getHttpClient();
|
|
29
|
+
let response = yield (yield client.request('GET', '/sns/jscode2session', {
|
|
30
|
+
params: {
|
|
31
|
+
appid: this.app.getAccount().getAppId(),
|
|
32
|
+
secret: this.app.getAccount().getSecret(),
|
|
33
|
+
js_code: code,
|
|
34
|
+
grant_type: 'authorization_code',
|
|
35
|
+
}
|
|
36
|
+
})).toObject();
|
|
37
|
+
if (!response['openid']) {
|
|
38
|
+
throw new Error(`code2Session error: ${JSON.stringify(response)}`);
|
|
39
|
+
}
|
|
40
|
+
return response;
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* 数据解密
|
|
45
|
+
* @see https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/signature.html#加密数据解密算法
|
|
46
|
+
* @param sessionKey
|
|
47
|
+
* @param iv
|
|
48
|
+
* @param ciphertext
|
|
49
|
+
* @returns
|
|
50
|
+
*/
|
|
51
|
+
decryptSession(sessionKey, iv, ciphertext) {
|
|
52
|
+
return Decryptor_1.default.decrypt(sessionKey, iv, ciphertext);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
;
|
|
56
|
+
module.exports = Utils;
|
|
@@ -15,18 +15,19 @@ const HttpClient_1 = __importDefault(require("../Core/HttpClient/HttpClient"));
|
|
|
15
15
|
const FileCache_1 = __importDefault(require("../Core/Cache/FileCache"));
|
|
16
16
|
class AccessToken {
|
|
17
17
|
constructor(appId, secret, key = null, cache = null, httpClient = null) {
|
|
18
|
-
this.appId = null;
|
|
19
|
-
this.secret = null;
|
|
20
|
-
this.key = null;
|
|
21
|
-
this.cache = null;
|
|
22
|
-
this.httpClient = null;
|
|
23
18
|
this.appId = appId;
|
|
24
19
|
this.secret = secret;
|
|
25
20
|
this.key = key;
|
|
26
|
-
this.cache = cache
|
|
27
|
-
this.httpClient = httpClient
|
|
28
|
-
|
|
29
|
-
|
|
21
|
+
this.cache = cache;
|
|
22
|
+
this.httpClient = httpClient;
|
|
23
|
+
if (!this.cache) {
|
|
24
|
+
this.cache = new FileCache_1.default();
|
|
25
|
+
}
|
|
26
|
+
if (!this.httpClient) {
|
|
27
|
+
this.httpClient = HttpClient_1.default.create({
|
|
28
|
+
baseURL: 'https://api.weixin.qq.com/',
|
|
29
|
+
});
|
|
30
|
+
}
|
|
30
31
|
}
|
|
31
32
|
/**
|
|
32
33
|
* 获取access_token的缓存名称
|
|
@@ -19,11 +19,9 @@ const DecryptXmlMessageMixin_1 = __importDefault(require("../Core/Mixins/Decrypt
|
|
|
19
19
|
const ResponseXmlMessageMixin_1 = __importDefault(require("../Core/Mixins/ResponseXmlMessageMixin"));
|
|
20
20
|
class Server {
|
|
21
21
|
constructor(request = null, encryptor = null) {
|
|
22
|
-
this.request = null;
|
|
23
|
-
this.encryptor = null;
|
|
24
|
-
this.handlers = [];
|
|
25
22
|
this.request = request;
|
|
26
23
|
this.encryptor = encryptor;
|
|
24
|
+
this.handlers = [];
|
|
27
25
|
}
|
|
28
26
|
/**
|
|
29
27
|
* 服务端消息处理
|
|
@@ -14,7 +14,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
14
14
|
const merge_1 = __importDefault(require("merge"));
|
|
15
15
|
class Utils {
|
|
16
16
|
constructor(app) {
|
|
17
|
-
this.app = null;
|
|
18
17
|
this.app = app;
|
|
19
18
|
}
|
|
20
19
|
buildJsSdkConfig(url, jsApiList = [], openTagList = [], debug = false) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"data":"mock-access_token","lifeTime":
|
|
1
|
+
{"data":"mock-access_token","lifeTime":1654698919}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"data":"mock-ticket","lifeTime":
|
|
1
|
+
{"data":"mock-ticket","lifeTime":1654704619}
|