node-easywechat 3.2.0 → 3.3.1
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/.github/workflows/publish-npm-2.yml +1 -1
- package/.github/workflows/publish-npm-3.yml +1 -1
- package/CHANGELOG.md +13 -0
- package/README.md +3 -3
- package/dist/MiniApp/Application.d.ts +4 -4
- package/dist/OfficialAccount/Application.d.ts +4 -4
- package/dist/OpenPlatform/Application.d.ts +5 -5
- package/dist/OpenPlatform/Application.js +1 -1
- package/dist/OpenWork/Account.d.ts +17 -0
- package/dist/OpenWork/Account.js +30 -0
- package/dist/OpenWork/Application.d.ts +120 -0
- package/dist/OpenWork/Application.js +294 -0
- package/dist/OpenWork/Authorization.d.ts +12 -0
- package/dist/OpenWork/Authorization.js +49 -0
- package/dist/OpenWork/AuthorizerAccessToken.d.ts +38 -0
- package/dist/OpenWork/AuthorizerAccessToken.js +105 -0
- package/dist/OpenWork/Config.d.ts +5 -0
- package/dist/OpenWork/Config.js +19 -0
- package/dist/OpenWork/Contracts/AccountInterface.d.ts +33 -0
- package/dist/OpenWork/Contracts/AccountInterface.js +35 -0
- package/dist/OpenWork/Contracts/ApplicationInterface.d.ts +108 -0
- package/dist/OpenWork/Contracts/ApplicationInterface.js +96 -0
- package/dist/OpenWork/Contracts/SuiteTicketInterface.d.ts +23 -0
- package/dist/OpenWork/Contracts/SuiteTicketInterface.js +25 -0
- package/dist/OpenWork/Encryptor.d.ts +9 -0
- package/dist/OpenWork/Encryptor.js +17 -0
- package/dist/OpenWork/JsApiTicket.d.ts +50 -0
- package/dist/OpenWork/JsApiTicket.js +152 -0
- package/dist/OpenWork/Message.d.ts +78 -0
- package/dist/OpenWork/Message.js +9 -0
- package/dist/OpenWork/ProviderAccessToken.d.ts +26 -0
- package/dist/OpenWork/ProviderAccessToken.js +84 -0
- package/dist/OpenWork/Server.d.ts +103 -0
- package/dist/OpenWork/Server.js +242 -0
- package/dist/OpenWork/SuiteAccessToken.d.ts +28 -0
- package/dist/OpenWork/SuiteAccessToken.js +86 -0
- package/dist/OpenWork/SuiteEncryptor.d.ts +9 -0
- package/dist/OpenWork/SuiteEncryptor.js +17 -0
- package/dist/OpenWork/SuiteTicket.d.ts +13 -0
- package/dist/OpenWork/SuiteTicket.js +49 -0
- package/dist/Pay/Application.d.ts +4 -4
- package/dist/Types/global.d.ts +11 -1
- package/dist/Work/Application.d.ts +5 -5
- package/dist/Work/Application.js +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.js +3 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,19 @@
|
|
|
1
1
|
# CHANGELOG
|
|
2
2
|
|
|
3
3
|
|
|
4
|
+
## v3.3.1 (2023-03-27)
|
|
5
|
+
|
|
6
|
+
- Fix: 调整3.x分支为npm的latest标签
|
|
7
|
+
|
|
8
|
+
## v3.3.0 (2023-03-22)
|
|
9
|
+
|
|
10
|
+
- Feat: 新增企业微信开放平台模块
|
|
11
|
+
|
|
12
|
+
- Fix: 完善企业微信开放平台的配置项
|
|
13
|
+
- Fix: 将企业微信开放平台添加到全局入口
|
|
14
|
+
- Fix: 调整配置项提示
|
|
15
|
+
- Fix: 修复各模块服务端添加的处理方法无法正确提示的问题
|
|
16
|
+
|
|
4
17
|
## v3.2.0 (2023-03-10)
|
|
5
18
|
|
|
6
19
|
- Feat: 新增企业微信模块
|
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
|
|
15
15
|
### 安装
|
|
16
16
|
|
|
17
|
-
`npm install -S node-easywechat
|
|
17
|
+
`npm install -S node-easywechat`
|
|
18
18
|
|
|
19
19
|
**注:3.x 版本需要 Node.js 15.6.0 及以上版本**
|
|
20
20
|
|
|
@@ -301,5 +301,5 @@ app.setRequest(request);
|
|
|
301
301
|
- [x] 小程序
|
|
302
302
|
- [x] 开放平台
|
|
303
303
|
- [x] 企业微信
|
|
304
|
-
- [
|
|
305
|
-
- [
|
|
304
|
+
- [x] 企业微信开放平台
|
|
305
|
+
- [x] 自定义
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import ConfigInterface from '../Core/Contracts/ConfigInterface';
|
|
2
|
-
import ServerInterface from '../Core/Contracts/ServerInterface';
|
|
3
2
|
import Encryptor from '../Core/Encryptor';
|
|
4
3
|
import AccessTokenAwareClient from '../Core/HttpClient/AccessTokenAwareClient';
|
|
5
4
|
import CacheMixin from '../Core/Mixins/CacheMixin';
|
|
@@ -10,6 +9,7 @@ import ServerRequestMixin from '../Core/Mixins/ServerRequestMixin';
|
|
|
10
9
|
import { MiniAppConfig } from '../Types/global';
|
|
11
10
|
import AccountInterface from './Contracts/AccountInterface';
|
|
12
11
|
import ApplicationInterface from './Contracts/ApplicationInterface';
|
|
12
|
+
import Server from './Server';
|
|
13
13
|
import Utils from './Utils';
|
|
14
14
|
import AccessTokenInterface from '../Core/Contracts/AccessTokenInterface';
|
|
15
15
|
/**
|
|
@@ -19,7 +19,7 @@ declare class Application implements ApplicationInterface {
|
|
|
19
19
|
constructor(config: ConfigInterface | MiniAppConfig);
|
|
20
20
|
protected account: AccountInterface;
|
|
21
21
|
protected encryptor: Encryptor;
|
|
22
|
-
protected server:
|
|
22
|
+
protected server: Server;
|
|
23
23
|
protected accessToken: AccessTokenInterface;
|
|
24
24
|
getAccount(): AccountInterface;
|
|
25
25
|
/**
|
|
@@ -35,13 +35,13 @@ declare class Application implements ApplicationInterface {
|
|
|
35
35
|
* @returns
|
|
36
36
|
*/
|
|
37
37
|
setEncryptor(encryptor: Encryptor): this;
|
|
38
|
-
getServer():
|
|
38
|
+
getServer(): Server;
|
|
39
39
|
/**
|
|
40
40
|
* 设置服务端实例
|
|
41
41
|
* @param server
|
|
42
42
|
* @returns
|
|
43
43
|
*/
|
|
44
|
-
setServer(server:
|
|
44
|
+
setServer(server: Server): this;
|
|
45
45
|
getAccessToken(): AccessTokenInterface;
|
|
46
46
|
/**
|
|
47
47
|
* 设置AccessToken实例
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import ProviderInterface from 'node-socialite/dist/Core/ProviderInterface';
|
|
2
2
|
import ConfigInterface from '../Core/Contracts/ConfigInterface';
|
|
3
|
-
import ServerInterface from '../Core/Contracts/ServerInterface';
|
|
4
3
|
import Encryptor from '../Core/Encryptor';
|
|
5
4
|
import AccessTokenAwareClient from '../Core/HttpClient/AccessTokenAwareClient';
|
|
6
5
|
import CacheMixin from '../Core/Mixins/CacheMixin';
|
|
@@ -12,6 +11,7 @@ import { OfficialAccountConfig, OfficialAccountOAuthFactory } from '../Types/glo
|
|
|
12
11
|
import AccountInterface from './Contracts/AccountInterface';
|
|
13
12
|
import ApplicationInterface from './Contracts/ApplicationInterface';
|
|
14
13
|
import JsApiTicket from './JsApiTicket';
|
|
14
|
+
import Server from './Server';
|
|
15
15
|
import Utils from './Utils';
|
|
16
16
|
import AccessTokenInterface from '../Core/Contracts/AccessTokenInterface';
|
|
17
17
|
/**
|
|
@@ -21,7 +21,7 @@ declare class Application implements ApplicationInterface {
|
|
|
21
21
|
constructor(config: ConfigInterface | OfficialAccountConfig);
|
|
22
22
|
protected account: AccountInterface;
|
|
23
23
|
protected encryptor: Encryptor;
|
|
24
|
-
protected server:
|
|
24
|
+
protected server: Server;
|
|
25
25
|
protected accessToken: AccessTokenInterface;
|
|
26
26
|
protected oauthFactory: OfficialAccountOAuthFactory;
|
|
27
27
|
protected ticket: JsApiTicket;
|
|
@@ -39,13 +39,13 @@ declare class Application implements ApplicationInterface {
|
|
|
39
39
|
* @returns
|
|
40
40
|
*/
|
|
41
41
|
setEncryptor(encryptor: Encryptor): this;
|
|
42
|
-
getServer():
|
|
42
|
+
getServer(): Server;
|
|
43
43
|
/**
|
|
44
44
|
* 设置服务端实例
|
|
45
45
|
* @param server
|
|
46
46
|
* @returns
|
|
47
47
|
*/
|
|
48
|
-
setServer(server:
|
|
48
|
+
setServer(server: Server): this;
|
|
49
49
|
getAccessToken(): AccessTokenInterface;
|
|
50
50
|
/**
|
|
51
51
|
* 设置AccessToken实例
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import ProviderInterface from 'node-socialite/dist/Core/ProviderInterface';
|
|
2
2
|
import ConfigInterface from '../Core/Contracts/ConfigInterface';
|
|
3
|
-
import ServerInterface from '../Core/Contracts/ServerInterface';
|
|
4
3
|
import Encryptor from '../Core/Encryptor';
|
|
5
4
|
import AccessTokenAwareClient from '../Core/HttpClient/AccessTokenAwareClient';
|
|
6
5
|
import CacheMixin from '../Core/Mixins/CacheMixin';
|
|
@@ -13,18 +12,19 @@ import MiniAppApplication from '../MiniApp/Application';
|
|
|
13
12
|
import { OfficialAccountConfig, OfficialAccountOAuthFactory, MiniAppConfig } from '../Types/global';
|
|
14
13
|
import AccountInterface from './Contracts/AccountInterface';
|
|
15
14
|
import ApplicationInterface from './Contracts/ApplicationInterface';
|
|
15
|
+
import Server from './Server';
|
|
16
16
|
import AccessTokenInterface from '../Core/Contracts/AccessTokenInterface';
|
|
17
17
|
import VerifyTicketInterface from './Contracts/VerifyTicketInterface';
|
|
18
18
|
import Authorization from './Authorization';
|
|
19
19
|
import AuthorizerAccessToken from './AuthorizerAccessToken';
|
|
20
20
|
/**
|
|
21
|
-
*
|
|
21
|
+
* 开放平台应用
|
|
22
22
|
*/
|
|
23
23
|
declare class Application implements ApplicationInterface {
|
|
24
24
|
constructor(config: ConfigInterface | OfficialAccountConfig);
|
|
25
25
|
protected account: AccountInterface;
|
|
26
26
|
protected encryptor: Encryptor;
|
|
27
|
-
protected server:
|
|
27
|
+
protected server: Server;
|
|
28
28
|
protected componentAccessToken: AccessTokenInterface;
|
|
29
29
|
protected verifyTicket: VerifyTicketInterface;
|
|
30
30
|
getAccount(): AccountInterface;
|
|
@@ -43,13 +43,13 @@ declare class Application implements ApplicationInterface {
|
|
|
43
43
|
* @returns
|
|
44
44
|
*/
|
|
45
45
|
setEncryptor(encryptor: Encryptor): this;
|
|
46
|
-
getServer():
|
|
46
|
+
getServer(): Server;
|
|
47
47
|
/**
|
|
48
48
|
* 设置服务端实例
|
|
49
49
|
* @param server
|
|
50
50
|
* @returns
|
|
51
51
|
*/
|
|
52
|
-
setServer(server:
|
|
52
|
+
setServer(server: Server): this;
|
|
53
53
|
getAccessToken(): AccessTokenInterface;
|
|
54
54
|
getComponentAccessToken(): AccessTokenInterface;
|
|
55
55
|
/**
|
|
@@ -33,7 +33,7 @@ const ComponentAccessToken_1 = __importDefault(require("./ComponentAccessToken")
|
|
|
33
33
|
const Authorization_1 = __importDefault(require("./Authorization"));
|
|
34
34
|
const AuthorizerAccessToken_1 = __importDefault(require("./AuthorizerAccessToken"));
|
|
35
35
|
/**
|
|
36
|
-
*
|
|
36
|
+
* 开放平台应用
|
|
37
37
|
*/
|
|
38
38
|
class Application {
|
|
39
39
|
constructor(config) {
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import AccountInterface from "./Contracts/AccountInterface";
|
|
2
|
+
declare class Account implements AccountInterface {
|
|
3
|
+
protected corpId: string;
|
|
4
|
+
protected providerSecret: string;
|
|
5
|
+
protected suiteId: string;
|
|
6
|
+
protected suiteSecret: string;
|
|
7
|
+
protected token: string;
|
|
8
|
+
protected aesKey: string;
|
|
9
|
+
constructor(corpId: string, providerSecret: string, suiteId: string, suiteSecret: string, token?: string, aesKey?: string);
|
|
10
|
+
getCorpId(): string;
|
|
11
|
+
getProviderSecret(): string;
|
|
12
|
+
getSuiteId(): string;
|
|
13
|
+
getSuiteSecret(): string;
|
|
14
|
+
getToken(): string;
|
|
15
|
+
getAesKey(): string;
|
|
16
|
+
}
|
|
17
|
+
export = Account;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
class Account {
|
|
3
|
+
constructor(corpId, providerSecret, suiteId, suiteSecret, token = null, aesKey = null) {
|
|
4
|
+
this.corpId = corpId;
|
|
5
|
+
this.providerSecret = providerSecret;
|
|
6
|
+
this.suiteId = suiteId;
|
|
7
|
+
this.suiteSecret = suiteSecret;
|
|
8
|
+
this.token = token;
|
|
9
|
+
this.aesKey = aesKey;
|
|
10
|
+
}
|
|
11
|
+
getCorpId() {
|
|
12
|
+
return this.corpId;
|
|
13
|
+
}
|
|
14
|
+
getProviderSecret() {
|
|
15
|
+
return this.providerSecret;
|
|
16
|
+
}
|
|
17
|
+
getSuiteId() {
|
|
18
|
+
return this.suiteId;
|
|
19
|
+
}
|
|
20
|
+
getSuiteSecret() {
|
|
21
|
+
return this.suiteSecret;
|
|
22
|
+
}
|
|
23
|
+
getToken() {
|
|
24
|
+
return this.token;
|
|
25
|
+
}
|
|
26
|
+
getAesKey() {
|
|
27
|
+
return this.aesKey;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
module.exports = Account;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import ProviderInterface from 'node-socialite/dist/Core/ProviderInterface';
|
|
2
|
+
import ConfigInterface from '../Core/Contracts/ConfigInterface';
|
|
3
|
+
import Encryptor from './Encryptor';
|
|
4
|
+
import AccessTokenAwareClient from '../Core/HttpClient/AccessTokenAwareClient';
|
|
5
|
+
import CacheMixin from '../Core/Mixins/CacheMixin';
|
|
6
|
+
import ClientMixin from '../Core/Mixins/ClientMixin';
|
|
7
|
+
import ConfigMixin from '../Core/Mixins/ConfigMixin';
|
|
8
|
+
import HttpClientMixin from '../Core/Mixins/HttpClientMixin';
|
|
9
|
+
import ServerRequestMixin from '../Core/Mixins/ServerRequestMixin';
|
|
10
|
+
import { OpenWorkConfig } from '../Types/global';
|
|
11
|
+
import AccountInterface from './Contracts/AccountInterface';
|
|
12
|
+
import ApplicationInterface from './Contracts/ApplicationInterface';
|
|
13
|
+
import Server from './Server';
|
|
14
|
+
import SuiteTicketInterface from './Contracts/SuiteTicketInterface';
|
|
15
|
+
import SuiteEncryptor from './SuiteEncryptor';
|
|
16
|
+
import SuiteAccessToken from './SuiteAccessToken';
|
|
17
|
+
import Authorization from './Authorization';
|
|
18
|
+
import AuthorizerAccessToken from './AuthorizerAccessToken';
|
|
19
|
+
import ProviderAccessToken from './ProviderAccessToken';
|
|
20
|
+
import JsApiTicket from './JsApiTicket';
|
|
21
|
+
/**
|
|
22
|
+
* 企业微信开放平台应用
|
|
23
|
+
*/
|
|
24
|
+
declare class Application implements ApplicationInterface {
|
|
25
|
+
constructor(config: ConfigInterface | OpenWorkConfig);
|
|
26
|
+
protected account: AccountInterface;
|
|
27
|
+
protected encryptor: Encryptor;
|
|
28
|
+
protected server: Server;
|
|
29
|
+
protected accessToken: ProviderAccessToken;
|
|
30
|
+
protected suiteEncryptor: SuiteEncryptor;
|
|
31
|
+
protected suiteAccessToken: SuiteAccessToken;
|
|
32
|
+
protected suiteTicket: SuiteTicketInterface;
|
|
33
|
+
protected authorizerAccessToken: AuthorizerAccessToken;
|
|
34
|
+
getAccount(): AccountInterface;
|
|
35
|
+
/**
|
|
36
|
+
* 设置当前账户实例
|
|
37
|
+
* @param account
|
|
38
|
+
* @returns
|
|
39
|
+
*/
|
|
40
|
+
setAccount(account: AccountInterface): this;
|
|
41
|
+
getEncryptor(): Encryptor;
|
|
42
|
+
/**
|
|
43
|
+
* 设置加密机实例
|
|
44
|
+
* @param encryptor
|
|
45
|
+
* @returns
|
|
46
|
+
*/
|
|
47
|
+
setEncryptor(encryptor: Encryptor): this;
|
|
48
|
+
getSuiteEncryptor(): SuiteEncryptor;
|
|
49
|
+
/**
|
|
50
|
+
* 设置授权应用的加密机实例
|
|
51
|
+
* @param suiteEncryptor
|
|
52
|
+
* @returns
|
|
53
|
+
*/
|
|
54
|
+
setSuiteEncryptor(suiteEncryptor: SuiteEncryptor): this;
|
|
55
|
+
getServer(): Server;
|
|
56
|
+
/**
|
|
57
|
+
* 设置服务端实例
|
|
58
|
+
* @param server
|
|
59
|
+
* @returns
|
|
60
|
+
*/
|
|
61
|
+
setServer(server: Server): this;
|
|
62
|
+
getProviderAccessToken(): ProviderAccessToken;
|
|
63
|
+
/**
|
|
64
|
+
* 设置开放平台应用的AccessToken实例
|
|
65
|
+
* @param accessToken
|
|
66
|
+
* @returns
|
|
67
|
+
*/
|
|
68
|
+
setProviderAccessToken(accessToken: ProviderAccessToken): this;
|
|
69
|
+
getSuiteAccessToken(): SuiteAccessToken;
|
|
70
|
+
/**
|
|
71
|
+
* 设置授权应用的AccessToken实例
|
|
72
|
+
* @param suiteAccessToken
|
|
73
|
+
* @returns
|
|
74
|
+
*/
|
|
75
|
+
setSuiteAccessToken(suiteAccessToken: SuiteAccessToken): this;
|
|
76
|
+
getSuiteTicket(): SuiteTicketInterface;
|
|
77
|
+
/**
|
|
78
|
+
* 设置授权应用的Ticket实例
|
|
79
|
+
* @param suiteTicket
|
|
80
|
+
* @returns
|
|
81
|
+
*/
|
|
82
|
+
setSuiteTicket(suiteTicket: SuiteTicketInterface): this;
|
|
83
|
+
/**
|
|
84
|
+
* 获取企业授权信息
|
|
85
|
+
* @see https://developer.work.weixin.qq.com/document/10975#获取企业授权信息
|
|
86
|
+
* @param corpId 授权方corpid
|
|
87
|
+
* @param permanentCode 永久授权码,通过get_permanent_code获取
|
|
88
|
+
* @param suiteAccessToken
|
|
89
|
+
* @returns
|
|
90
|
+
*/
|
|
91
|
+
getAuthorization(corpId: string, permanentCode: string, suiteAccessToken?: SuiteAccessToken): Promise<Authorization>;
|
|
92
|
+
/**
|
|
93
|
+
* 获取预授权码
|
|
94
|
+
* @see https://developer.work.weixin.qq.com/document/10975#获取预授权码
|
|
95
|
+
* @param suiteAccessToken
|
|
96
|
+
* @returns
|
|
97
|
+
*/
|
|
98
|
+
createPreAuthorizationCode(suiteAccessToken?: SuiteAccessToken): Promise<import("../Types/global").WeixinResponse>;
|
|
99
|
+
/**
|
|
100
|
+
* 获取企业授权令牌
|
|
101
|
+
* @param corpId
|
|
102
|
+
* @param permanentCode
|
|
103
|
+
* @param suiteAccessToken
|
|
104
|
+
* @returns
|
|
105
|
+
*/
|
|
106
|
+
getAuthorizerAccessToken(corpId: string, permanentCode: string, suiteAccessToken?: SuiteAccessToken): Promise<AuthorizerAccessToken>;
|
|
107
|
+
createClient(): AccessTokenAwareClient;
|
|
108
|
+
getAuthorizerClient(corpId: string, permanentCode: string, suiteAccessToken?: SuiteAccessToken): Promise<AccessTokenAwareClient>;
|
|
109
|
+
getJsApiTicket(corpId: string, permanentCode: string, suiteAccessToken?: SuiteAccessToken): Promise<JsApiTicket>;
|
|
110
|
+
getOAuth(suiteId: string, suiteAccessToken?: SuiteAccessToken): Promise<ProviderInterface>;
|
|
111
|
+
getCorpOAuth(corpId: string, suiteAccessToken?: SuiteAccessToken): Promise<ProviderInterface>;
|
|
112
|
+
/**
|
|
113
|
+
* 获取请求默认配置
|
|
114
|
+
* @returns
|
|
115
|
+
*/
|
|
116
|
+
protected getHttpClientDefaultOptions(): Record<string, any>;
|
|
117
|
+
}
|
|
118
|
+
interface Application extends ConfigMixin, CacheMixin, ClientMixin, ServerRequestMixin, HttpClientMixin {
|
|
119
|
+
}
|
|
120
|
+
export = Application;
|
|
@@ -0,0 +1,294 @@
|
|
|
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 OpenWeWork_1 = __importDefault(require("node-socialite/dist/Providers/OpenWeWork"));
|
|
16
|
+
const Config_1 = __importDefault(require("./Config"));
|
|
17
|
+
const ConfigInterface_1 = __importDefault(require("../Core/Contracts/ConfigInterface"));
|
|
18
|
+
const Encryptor_1 = __importDefault(require("./Encryptor"));
|
|
19
|
+
const AccessTokenAwareClient_1 = __importDefault(require("../Core/HttpClient/AccessTokenAwareClient"));
|
|
20
|
+
const CacheMixin_1 = __importDefault(require("../Core/Mixins/CacheMixin"));
|
|
21
|
+
const ClientMixin_1 = __importDefault(require("../Core/Mixins/ClientMixin"));
|
|
22
|
+
const ConfigMixin_1 = __importDefault(require("../Core/Mixins/ConfigMixin"));
|
|
23
|
+
const HttpClientMixin_1 = __importDefault(require("../Core/Mixins/HttpClientMixin"));
|
|
24
|
+
const ServerRequestMixin_1 = __importDefault(require("../Core/Mixins/ServerRequestMixin"));
|
|
25
|
+
const Utils_1 = require("../Core/Support/Utils");
|
|
26
|
+
const Account_1 = __importDefault(require("./Account"));
|
|
27
|
+
const Server_1 = __importDefault(require("./Server"));
|
|
28
|
+
const SuiteTicket_1 = __importDefault(require("./SuiteTicket"));
|
|
29
|
+
const SuiteEncryptor_1 = __importDefault(require("./SuiteEncryptor"));
|
|
30
|
+
const SuiteAccessToken_1 = __importDefault(require("./SuiteAccessToken"));
|
|
31
|
+
const Authorization_1 = __importDefault(require("./Authorization"));
|
|
32
|
+
const AuthorizerAccessToken_1 = __importDefault(require("./AuthorizerAccessToken"));
|
|
33
|
+
const ProviderAccessToken_1 = __importDefault(require("./ProviderAccessToken"));
|
|
34
|
+
const JsApiTicket_1 = __importDefault(require("./JsApiTicket"));
|
|
35
|
+
/**
|
|
36
|
+
* 企业微信开放平台应用
|
|
37
|
+
*/
|
|
38
|
+
class Application {
|
|
39
|
+
constructor(config) {
|
|
40
|
+
this.account = null;
|
|
41
|
+
this.encryptor = null;
|
|
42
|
+
this.server = null;
|
|
43
|
+
this.accessToken = null;
|
|
44
|
+
this.suiteEncryptor = null;
|
|
45
|
+
this.suiteAccessToken = null;
|
|
46
|
+
this.suiteTicket = null;
|
|
47
|
+
this.authorizerAccessToken = null;
|
|
48
|
+
if (config instanceof ConfigInterface_1.default) {
|
|
49
|
+
this.setConfig(config);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
this.setConfig(new Config_1.default(config));
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
getAccount() {
|
|
56
|
+
if (!this.account) {
|
|
57
|
+
this.account = new Account_1.default(this.config.get('corp_id'), this.config.get('provider_secret'), this.config.get('suite_id'), this.config.get('suite_secret'), this.config.get('token'), this.config.get('aes_key'));
|
|
58
|
+
}
|
|
59
|
+
return this.account;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* 设置当前账户实例
|
|
63
|
+
* @param account
|
|
64
|
+
* @returns
|
|
65
|
+
*/
|
|
66
|
+
setAccount(account) {
|
|
67
|
+
this.account = account;
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
getEncryptor() {
|
|
71
|
+
if (!this.encryptor) {
|
|
72
|
+
let token = this.getAccount().getToken();
|
|
73
|
+
let aesKey = this.getAccount().getAesKey();
|
|
74
|
+
if (!token || !aesKey) {
|
|
75
|
+
throw new Error('token or aes_key cannot be empty.');
|
|
76
|
+
}
|
|
77
|
+
this.encryptor = new Encryptor_1.default(this.getAccount().getCorpId(), token, aesKey);
|
|
78
|
+
}
|
|
79
|
+
return this.encryptor;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* 设置加密机实例
|
|
83
|
+
* @param encryptor
|
|
84
|
+
* @returns
|
|
85
|
+
*/
|
|
86
|
+
setEncryptor(encryptor) {
|
|
87
|
+
this.encryptor = encryptor;
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
getSuiteEncryptor() {
|
|
91
|
+
if (!this.suiteEncryptor) {
|
|
92
|
+
let token = this.getAccount().getToken();
|
|
93
|
+
let aesKey = this.getAccount().getAesKey();
|
|
94
|
+
if (!token || !aesKey) {
|
|
95
|
+
throw new Error('token or aes_key cannot be empty.');
|
|
96
|
+
}
|
|
97
|
+
this.suiteEncryptor = new SuiteEncryptor_1.default(this.getAccount().getSuiteId(), token, aesKey);
|
|
98
|
+
}
|
|
99
|
+
return this.suiteEncryptor;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* 设置授权应用的加密机实例
|
|
103
|
+
* @param suiteEncryptor
|
|
104
|
+
* @returns
|
|
105
|
+
*/
|
|
106
|
+
setSuiteEncryptor(suiteEncryptor) {
|
|
107
|
+
this.suiteEncryptor = suiteEncryptor;
|
|
108
|
+
return this;
|
|
109
|
+
}
|
|
110
|
+
getServer() {
|
|
111
|
+
if (!this.server) {
|
|
112
|
+
this.server = new Server_1.default(this.getSuiteEncryptor(), this.getEncryptor(), this.getRequest());
|
|
113
|
+
this.server.withDefaultSuiteTicketHandler((message, next) => __awaiter(this, void 0, void 0, function* () {
|
|
114
|
+
if (message.SuiteId === this.getAccount().getSuiteId()) {
|
|
115
|
+
yield this.getSuiteTicket().setTicket(message.SuiteTicket);
|
|
116
|
+
}
|
|
117
|
+
return next(message);
|
|
118
|
+
}));
|
|
119
|
+
}
|
|
120
|
+
return this.server;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* 设置服务端实例
|
|
124
|
+
* @param server
|
|
125
|
+
* @returns
|
|
126
|
+
*/
|
|
127
|
+
setServer(server) {
|
|
128
|
+
this.server = server;
|
|
129
|
+
return this;
|
|
130
|
+
}
|
|
131
|
+
getProviderAccessToken() {
|
|
132
|
+
if (!this.accessToken) {
|
|
133
|
+
this.accessToken = new ProviderAccessToken_1.default(this.getAccount().getCorpId(), this.getAccount().getProviderSecret(), null, this.getCache(), this.getHttpClient());
|
|
134
|
+
}
|
|
135
|
+
return this.accessToken;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* 设置开放平台应用的AccessToken实例
|
|
139
|
+
* @param accessToken
|
|
140
|
+
* @returns
|
|
141
|
+
*/
|
|
142
|
+
setProviderAccessToken(accessToken) {
|
|
143
|
+
this.accessToken = accessToken;
|
|
144
|
+
return this;
|
|
145
|
+
}
|
|
146
|
+
getSuiteAccessToken() {
|
|
147
|
+
if (!this.suiteAccessToken) {
|
|
148
|
+
this.suiteAccessToken = new SuiteAccessToken_1.default(this.getAccount().getSuiteId(), this.getAccount().getSuiteSecret(), this.getSuiteTicket(), null, this.getCache(), this.getHttpClient());
|
|
149
|
+
}
|
|
150
|
+
return this.suiteAccessToken;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* 设置授权应用的AccessToken实例
|
|
154
|
+
* @param suiteAccessToken
|
|
155
|
+
* @returns
|
|
156
|
+
*/
|
|
157
|
+
setSuiteAccessToken(suiteAccessToken) {
|
|
158
|
+
this.suiteAccessToken = suiteAccessToken;
|
|
159
|
+
return this;
|
|
160
|
+
}
|
|
161
|
+
getSuiteTicket() {
|
|
162
|
+
if (!this.suiteTicket) {
|
|
163
|
+
this.suiteTicket = new SuiteTicket_1.default(this.getAccount().getSuiteId(), this.getCache());
|
|
164
|
+
}
|
|
165
|
+
return this.suiteTicket;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* 设置授权应用的Ticket实例
|
|
169
|
+
* @param suiteTicket
|
|
170
|
+
* @returns
|
|
171
|
+
*/
|
|
172
|
+
setSuiteTicket(suiteTicket) {
|
|
173
|
+
this.suiteTicket = suiteTicket;
|
|
174
|
+
return this;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* 获取企业授权信息
|
|
178
|
+
* @see https://developer.work.weixin.qq.com/document/10975#获取企业授权信息
|
|
179
|
+
* @param corpId 授权方corpid
|
|
180
|
+
* @param permanentCode 永久授权码,通过get_permanent_code获取
|
|
181
|
+
* @param suiteAccessToken
|
|
182
|
+
* @returns
|
|
183
|
+
*/
|
|
184
|
+
getAuthorization(corpId, permanentCode, suiteAccessToken = null) {
|
|
185
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
186
|
+
if (!suiteAccessToken)
|
|
187
|
+
suiteAccessToken = this.getSuiteAccessToken();
|
|
188
|
+
let response = (yield this.getClient().request('post', 'cgi-bin/service/get_auth_info', {
|
|
189
|
+
params: {
|
|
190
|
+
suite_access_token: yield suiteAccessToken.getToken(),
|
|
191
|
+
},
|
|
192
|
+
json: {
|
|
193
|
+
auth_corpid: corpId,
|
|
194
|
+
permanent_code: permanentCode,
|
|
195
|
+
}
|
|
196
|
+
})).toObject();
|
|
197
|
+
if (!response['auth_corp_info']) {
|
|
198
|
+
throw new Error('Failed to get auth_corp_info: ' + JSON.stringify(response));
|
|
199
|
+
}
|
|
200
|
+
return new Authorization_1.default(response);
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* 获取预授权码
|
|
205
|
+
* @see https://developer.work.weixin.qq.com/document/10975#获取预授权码
|
|
206
|
+
* @param suiteAccessToken
|
|
207
|
+
* @returns
|
|
208
|
+
*/
|
|
209
|
+
createPreAuthorizationCode(suiteAccessToken = null) {
|
|
210
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
211
|
+
if (!suiteAccessToken)
|
|
212
|
+
suiteAccessToken = this.getSuiteAccessToken();
|
|
213
|
+
let response = (yield this.getClient().request('post', 'cgi-bin/service/get_pre_auth_code', {
|
|
214
|
+
params: {
|
|
215
|
+
suite_access_token: yield suiteAccessToken.getToken(),
|
|
216
|
+
}
|
|
217
|
+
})).toObject();
|
|
218
|
+
if (!response['pre_auth_code']) {
|
|
219
|
+
throw new Error('Failed to get pre_auth_code: ' + JSON.stringify(response));
|
|
220
|
+
}
|
|
221
|
+
return response;
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* 获取企业授权令牌
|
|
226
|
+
* @param corpId
|
|
227
|
+
* @param permanentCode
|
|
228
|
+
* @param suiteAccessToken
|
|
229
|
+
* @returns
|
|
230
|
+
*/
|
|
231
|
+
getAuthorizerAccessToken(corpId, permanentCode, suiteAccessToken = null) {
|
|
232
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
233
|
+
if (!suiteAccessToken)
|
|
234
|
+
suiteAccessToken = this.getSuiteAccessToken();
|
|
235
|
+
return new AuthorizerAccessToken_1.default(corpId, permanentCode, suiteAccessToken, null, this.getCache(), this.getHttpClient());
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
createClient() {
|
|
239
|
+
return (new AccessTokenAwareClient_1.default(this.getHttpClient(), this.getProviderAccessToken(), (response) => { var _a; return (_a = response.toObject()['errcode']) !== null && _a !== void 0 ? _a : 0; }, this.getConfig().get('http.throw', true)))
|
|
240
|
+
.setPresets(this.getConfig().all());
|
|
241
|
+
}
|
|
242
|
+
getAuthorizerClient(corpId, permanentCode, suiteAccessToken = null) {
|
|
243
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
244
|
+
return (new AccessTokenAwareClient_1.default(this.getHttpClient(), yield this.getAuthorizerAccessToken(corpId, permanentCode, suiteAccessToken), (response) => { var _a; return (_a = response.toObject()['errcode']) !== null && _a !== void 0 ? _a : 0; }, this.getConfig().get('http.throw', true)))
|
|
245
|
+
.setPresets(this.getConfig().all());
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
getJsApiTicket(corpId, permanentCode, suiteAccessToken = null) {
|
|
249
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
250
|
+
return new JsApiTicket_1.default(corpId, null, this.getCache(), yield this.getAuthorizerClient(corpId, permanentCode, suiteAccessToken));
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
getOAuth(suiteId, suiteAccessToken = null) {
|
|
254
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
255
|
+
if (!suiteAccessToken)
|
|
256
|
+
suiteAccessToken = this.getSuiteAccessToken();
|
|
257
|
+
return (new OpenWeWork_1.default({
|
|
258
|
+
client_id: suiteId,
|
|
259
|
+
client_secret: '',
|
|
260
|
+
redirect: this.getConfig().get('oauth.redirect_url'),
|
|
261
|
+
}))
|
|
262
|
+
.withSuiteTicket(yield this.getSuiteTicket().getTicket())
|
|
263
|
+
.withSuiteAccessToken(yield suiteAccessToken.getToken())
|
|
264
|
+
.scopes(this.getConfig().get('oauth.scopes', 'snsapi_base'));
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
getCorpOAuth(corpId, suiteAccessToken = null) {
|
|
268
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
269
|
+
if (!suiteAccessToken)
|
|
270
|
+
suiteAccessToken = this.getSuiteAccessToken();
|
|
271
|
+
return (new OpenWeWork_1.default({
|
|
272
|
+
client_id: corpId,
|
|
273
|
+
client_secret: '',
|
|
274
|
+
redirect: this.getConfig().get('oauth.redirect_url'),
|
|
275
|
+
}))
|
|
276
|
+
.withSuiteTicket(yield this.getSuiteTicket().getTicket())
|
|
277
|
+
.withSuiteAccessToken(yield suiteAccessToken.getToken())
|
|
278
|
+
.scopes(this.getConfig().get('oauth.scopes', 'snsapi_base'));
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* 获取请求默认配置
|
|
283
|
+
* @returns
|
|
284
|
+
*/
|
|
285
|
+
getHttpClientDefaultOptions() {
|
|
286
|
+
return (0, merge_1.default)(true, {
|
|
287
|
+
baseURL: 'https://api.weixin.qq.com/',
|
|
288
|
+
}, this.getConfig().get('http', {}));
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
;
|
|
292
|
+
;
|
|
293
|
+
(0, Utils_1.applyMixins)(Application, [ConfigMixin_1.default, CacheMixin_1.default, ClientMixin_1.default, ServerRequestMixin_1.default, HttpClientMixin_1.default]);
|
|
294
|
+
module.exports = Application;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import HasAttributesMixin from "../Core/Mixins/HasAttributesMixin";
|
|
2
|
+
declare class Authorization {
|
|
3
|
+
constructor(attributes?: Record<string, any>);
|
|
4
|
+
/**
|
|
5
|
+
* 获取corpid
|
|
6
|
+
* @returns
|
|
7
|
+
*/
|
|
8
|
+
getCorpId(): string;
|
|
9
|
+
}
|
|
10
|
+
interface Authorization extends HasAttributesMixin {
|
|
11
|
+
}
|
|
12
|
+
export = Authorization;
|