node-easywechat 3.1.3 → 3.2.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/.github/workflows/build-2.yml +21 -0
- package/.github/workflows/build-3.yml +21 -0
- package/.github/workflows/publish-npm-2.yml +24 -0
- package/.github/workflows/publish-npm-3.yml +24 -0
- package/CHANGELOG.md +6 -0
- package/README.md +23 -2
- package/dist/Types/global.d.ts +8 -0
- package/dist/Work/AccessToken.d.ts +26 -0
- package/dist/Work/AccessToken.js +84 -0
- package/dist/Work/Account.d.ts +13 -0
- package/dist/Work/Account.js +25 -0
- package/dist/Work/Application.d.ts +75 -0
- package/dist/Work/Application.js +177 -0
- package/dist/Work/Config.d.ts +5 -0
- package/dist/Work/Config.js +17 -0
- package/dist/Work/Contracts/AccountInterface.d.ts +23 -0
- package/dist/Work/Contracts/AccountInterface.js +25 -0
- package/dist/Work/Contracts/ApplicationInterface.d.ts +87 -0
- package/dist/Work/Contracts/ApplicationInterface.js +76 -0
- package/dist/Work/Encryptor.d.ts +9 -0
- package/dist/Work/Encryptor.js +17 -0
- package/dist/Work/JsApiTicket.d.ts +45 -0
- package/dist/Work/JsApiTicket.js +141 -0
- package/dist/Work/Message.d.ts +142 -0
- package/dist/Work/Message.js +9 -0
- package/dist/Work/Server.d.ts +90 -0
- package/dist/Work/Server.js +212 -0
- package/dist/Work/Utils.d.ts +25 -0
- package/dist/Work/Utils.js +56 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +3 -1
- package/package.json +1 -1
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# 2.x 所有提交自动构建并测试
|
|
2
|
+
|
|
3
|
+
name: Build 2.x
|
|
4
|
+
|
|
5
|
+
on:
|
|
6
|
+
push:
|
|
7
|
+
branches:
|
|
8
|
+
- '2.x'
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
build:
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
steps:
|
|
14
|
+
- uses: actions/checkout@v3
|
|
15
|
+
- uses: actions/setup-node@v3
|
|
16
|
+
with:
|
|
17
|
+
node-version: 16
|
|
18
|
+
- run: npm install
|
|
19
|
+
- run: npm run build
|
|
20
|
+
- run: npm test
|
|
21
|
+
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# 3.x 所有提交自动构建并测试
|
|
2
|
+
|
|
3
|
+
name: Build 3.x
|
|
4
|
+
|
|
5
|
+
on:
|
|
6
|
+
push:
|
|
7
|
+
branches:
|
|
8
|
+
- '3.x'
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
build:
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
steps:
|
|
14
|
+
- uses: actions/checkout@v3
|
|
15
|
+
- uses: actions/setup-node@v3
|
|
16
|
+
with:
|
|
17
|
+
node-version: 16
|
|
18
|
+
- run: npm install
|
|
19
|
+
- run: npm run build
|
|
20
|
+
- run: npm test
|
|
21
|
+
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# 构建、测试并发布2.x分支
|
|
2
|
+
|
|
3
|
+
name: Publish 2.x to npm
|
|
4
|
+
|
|
5
|
+
on:
|
|
6
|
+
push:
|
|
7
|
+
tags:
|
|
8
|
+
- v2.*
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
publish-npm:
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
steps:
|
|
14
|
+
- uses: actions/checkout@v3
|
|
15
|
+
- uses: actions/setup-node@v3
|
|
16
|
+
with:
|
|
17
|
+
node-version: 16
|
|
18
|
+
registry-url: https://registry.npmjs.org/
|
|
19
|
+
- run: npm install
|
|
20
|
+
- run: npm run build
|
|
21
|
+
- run: npm test
|
|
22
|
+
- run: npm publish --tag latest
|
|
23
|
+
env:
|
|
24
|
+
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# 构建、测试并发布3.x分支
|
|
2
|
+
|
|
3
|
+
name: Publish 3.x to npm
|
|
4
|
+
|
|
5
|
+
on:
|
|
6
|
+
push:
|
|
7
|
+
tags:
|
|
8
|
+
- v3.*
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
publish-npm:
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
steps:
|
|
14
|
+
- uses: actions/checkout@v3
|
|
15
|
+
- uses: actions/setup-node@v3
|
|
16
|
+
with:
|
|
17
|
+
node-version: 16
|
|
18
|
+
registry-url: https://registry.npmjs.org/
|
|
19
|
+
- run: npm install
|
|
20
|
+
- run: npm run build
|
|
21
|
+
- run: npm test
|
|
22
|
+
- run: npm publish --tag next
|
|
23
|
+
env:
|
|
24
|
+
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
|
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
|
|
2
2
|
## EasyWechat for Node.js
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
 [](https://www.npmjs.com/package/node-easywechat) [](LICENSE)
|
|
5
5
|
|
|
6
6
|
**注:3.x分支针对 EasyWechat 的 6.x版本。**
|
|
7
7
|
|
|
@@ -53,6 +53,13 @@ let app = new OpenPlatform({
|
|
|
53
53
|
// 配置项
|
|
54
54
|
});
|
|
55
55
|
|
|
56
|
+
// 企业微信
|
|
57
|
+
const { Work } = require('node-easywechat');
|
|
58
|
+
// 实例化应用
|
|
59
|
+
let app = new Work({
|
|
60
|
+
// 配置项
|
|
61
|
+
});
|
|
62
|
+
|
|
56
63
|
// ----- 以下为通用的 api 调用方法 -----
|
|
57
64
|
|
|
58
65
|
// 获取 api 调用客户端
|
|
@@ -165,6 +172,20 @@ let data = response.toObject();
|
|
|
165
172
|
}
|
|
166
173
|
```
|
|
167
174
|
|
|
175
|
+
``` js
|
|
176
|
+
// 企业微信配置
|
|
177
|
+
{
|
|
178
|
+
// 企业微信的 corp id
|
|
179
|
+
corp_id: '',
|
|
180
|
+
// 企业微信的 secret
|
|
181
|
+
secret: '',
|
|
182
|
+
// 企业微信的 token
|
|
183
|
+
token: '',
|
|
184
|
+
// EncodingAESKey
|
|
185
|
+
aes_key: '',
|
|
186
|
+
}
|
|
187
|
+
```
|
|
188
|
+
|
|
168
189
|
### 自定义模块(模块替换)使用方法
|
|
169
190
|
|
|
170
191
|
#### 日志模块
|
|
@@ -279,6 +300,6 @@ app.setRequest(request);
|
|
|
279
300
|
- [x] 微信支付
|
|
280
301
|
- [x] 小程序
|
|
281
302
|
- [x] 开放平台
|
|
282
|
-
- [
|
|
303
|
+
- [x] 企业微信
|
|
283
304
|
- [ ] 企业微信开放平台
|
|
284
305
|
- [ ] 自定义
|
package/dist/Types/global.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
2
2
|
import ProviderInterface from 'node-socialite/dist/Core/ProviderInterface';
|
|
3
3
|
import OfficialAccountApplicationInterface from '../OfficialAccount/Contracts/ApplicationInterface';
|
|
4
|
+
import WorkApplicationInterface from '../Work/Contracts/ApplicationInterface';
|
|
4
5
|
import Message from '../Core/Message';
|
|
5
6
|
import HttpClientResponseInterface from '../Core/HttpClient/Contracts/HttpClientResponseInterface';
|
|
6
7
|
import { PublicKey } from "../Core/Support/PublicKey";
|
|
@@ -294,6 +295,13 @@ export declare interface OpenWorkConfig extends BaseConfig {
|
|
|
294
295
|
*/
|
|
295
296
|
export declare type OfficialAccountOAuthFactory = (app: OfficialAccountApplicationInterface) => ProviderInterface;
|
|
296
297
|
|
|
298
|
+
/**
|
|
299
|
+
* 企业微信OAuth工厂方法
|
|
300
|
+
* @param app 企业微信应用实例
|
|
301
|
+
* @returns OAuth服务供应商实例
|
|
302
|
+
*/
|
|
303
|
+
export declare type WorkOAuthFactory = (app: WorkApplicationInterface) => ProviderInterface;
|
|
304
|
+
|
|
297
305
|
/**
|
|
298
306
|
* 服务端通知处理项
|
|
299
307
|
*/
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import CacheInterface from "../Core/Contracts/CacheInterface";
|
|
2
|
+
import HttpClientInterface from "../Core/HttpClient/Contracts/HttpClientInterface";
|
|
3
|
+
import RefreshableAccessTokenInterface from "../Core/Contracts/RefreshableAccessTokenInterface";
|
|
4
|
+
declare class AccessToken implements RefreshableAccessTokenInterface {
|
|
5
|
+
protected corpId: string;
|
|
6
|
+
protected secret: string;
|
|
7
|
+
protected key: string;
|
|
8
|
+
protected cache: CacheInterface;
|
|
9
|
+
protected httpClient: HttpClientInterface;
|
|
10
|
+
constructor(corpId: string, secret: string, key?: string, cache?: CacheInterface, httpClient?: HttpClientInterface);
|
|
11
|
+
/**
|
|
12
|
+
* 获取access_token的缓存名称
|
|
13
|
+
* @returns
|
|
14
|
+
*/
|
|
15
|
+
getKey(): string;
|
|
16
|
+
/**
|
|
17
|
+
* 设置access_token的缓存名称
|
|
18
|
+
* @param key
|
|
19
|
+
* @returns
|
|
20
|
+
*/
|
|
21
|
+
setKey(key: string): this;
|
|
22
|
+
getToken(): Promise<string>;
|
|
23
|
+
toQuery(): Promise<Record<string, any>>;
|
|
24
|
+
refresh(): Promise<string>;
|
|
25
|
+
}
|
|
26
|
+
export = AccessToken;
|
|
@@ -0,0 +1,84 @@
|
|
|
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
|
+
class AccessToken {
|
|
16
|
+
constructor(corpId, secret, key = null, cache = null, httpClient = null) {
|
|
17
|
+
this.corpId = corpId;
|
|
18
|
+
this.secret = secret;
|
|
19
|
+
this.key = key;
|
|
20
|
+
this.cache = cache;
|
|
21
|
+
this.httpClient = httpClient;
|
|
22
|
+
if (!this.httpClient) {
|
|
23
|
+
this.httpClient = HttpClient_1.default.create({
|
|
24
|
+
baseURL: 'https://qyapi.weixin.qq.com/',
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* 获取access_token的缓存名称
|
|
30
|
+
* @returns
|
|
31
|
+
*/
|
|
32
|
+
getKey() {
|
|
33
|
+
if (!this.key) {
|
|
34
|
+
this.key = `work.access_token.${this.corpId}`;
|
|
35
|
+
}
|
|
36
|
+
return this.key;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* 设置access_token的缓存名称
|
|
40
|
+
* @param key
|
|
41
|
+
* @returns
|
|
42
|
+
*/
|
|
43
|
+
setKey(key) {
|
|
44
|
+
this.key = key;
|
|
45
|
+
return this;
|
|
46
|
+
}
|
|
47
|
+
getToken() {
|
|
48
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
49
|
+
let token = '';
|
|
50
|
+
if (this.cache) {
|
|
51
|
+
token = yield this.cache.get(this.getKey());
|
|
52
|
+
}
|
|
53
|
+
if (!!token && typeof token === 'string') {
|
|
54
|
+
return token;
|
|
55
|
+
}
|
|
56
|
+
return this.refresh();
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
toQuery() {
|
|
60
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
61
|
+
return {
|
|
62
|
+
access_token: yield this.getToken(),
|
|
63
|
+
};
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
refresh() {
|
|
67
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
68
|
+
let response = (yield this.httpClient.request('get', 'cgi-bin/gettoken', {
|
|
69
|
+
params: {
|
|
70
|
+
corpid: this.corpId,
|
|
71
|
+
corpsecret: this.secret,
|
|
72
|
+
}
|
|
73
|
+
})).toObject();
|
|
74
|
+
if (!response['access_token']) {
|
|
75
|
+
throw new Error('Failed to get access_token: ' + JSON.stringify(response));
|
|
76
|
+
}
|
|
77
|
+
if (this.cache) {
|
|
78
|
+
yield this.cache.set(this.getKey(), response['access_token'], parseInt(response['expires_in']));
|
|
79
|
+
}
|
|
80
|
+
return response['access_token'];
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
module.exports = AccessToken;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import AccountInterface from "./Contracts/AccountInterface";
|
|
2
|
+
declare class Account implements AccountInterface {
|
|
3
|
+
protected corpId: string;
|
|
4
|
+
protected secret: string;
|
|
5
|
+
protected token: string;
|
|
6
|
+
protected aesKey: string;
|
|
7
|
+
constructor(corpId: string, secret: string, token?: string, aesKey?: string);
|
|
8
|
+
getCorpId(): string;
|
|
9
|
+
getSecret(): string;
|
|
10
|
+
getToken(): string;
|
|
11
|
+
getAesKey(): string;
|
|
12
|
+
}
|
|
13
|
+
export = Account;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
class Account {
|
|
3
|
+
constructor(corpId, secret, token = null, aesKey = null) {
|
|
4
|
+
this.corpId = corpId;
|
|
5
|
+
this.secret = secret;
|
|
6
|
+
this.token = token;
|
|
7
|
+
this.aesKey = aesKey;
|
|
8
|
+
}
|
|
9
|
+
getCorpId() {
|
|
10
|
+
return this.corpId;
|
|
11
|
+
}
|
|
12
|
+
getSecret() {
|
|
13
|
+
if (null === this.secret) {
|
|
14
|
+
throw new Error("No secret configured.");
|
|
15
|
+
}
|
|
16
|
+
return this.secret;
|
|
17
|
+
}
|
|
18
|
+
getToken() {
|
|
19
|
+
return this.token;
|
|
20
|
+
}
|
|
21
|
+
getAesKey() {
|
|
22
|
+
return this.aesKey;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
module.exports = Account;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import ProviderInterface from 'node-socialite/dist/Core/ProviderInterface';
|
|
2
|
+
import ConfigInterface from '../Core/Contracts/ConfigInterface';
|
|
3
|
+
import ServerInterface from '../Core/Contracts/ServerInterface';
|
|
4
|
+
import Encryptor from './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 { WorkConfig, WorkOAuthFactory } from '../Types/global';
|
|
12
|
+
import AccountInterface from './Contracts/AccountInterface';
|
|
13
|
+
import ApplicationInterface from './Contracts/ApplicationInterface';
|
|
14
|
+
import JsApiTicket from './JsApiTicket';
|
|
15
|
+
import Utils from './Utils';
|
|
16
|
+
import AccessTokenInterface from '../Core/Contracts/AccessTokenInterface';
|
|
17
|
+
/**
|
|
18
|
+
* 公众号应用
|
|
19
|
+
*/
|
|
20
|
+
declare class Application implements ApplicationInterface {
|
|
21
|
+
constructor(config: ConfigInterface | WorkConfig);
|
|
22
|
+
protected account: AccountInterface;
|
|
23
|
+
protected encryptor: Encryptor;
|
|
24
|
+
protected server: ServerInterface;
|
|
25
|
+
protected accessToken: AccessTokenInterface;
|
|
26
|
+
protected oauthFactory: WorkOAuthFactory;
|
|
27
|
+
protected ticket: JsApiTicket;
|
|
28
|
+
getAccount(): AccountInterface;
|
|
29
|
+
/**
|
|
30
|
+
* 设置当前账户实例
|
|
31
|
+
* @param account
|
|
32
|
+
* @returns
|
|
33
|
+
*/
|
|
34
|
+
setAccount(account: AccountInterface): this;
|
|
35
|
+
getEncryptor(): Encryptor;
|
|
36
|
+
/**
|
|
37
|
+
* 设置加密机实例
|
|
38
|
+
* @param encryptor
|
|
39
|
+
* @returns
|
|
40
|
+
*/
|
|
41
|
+
setEncryptor(encryptor: Encryptor): this;
|
|
42
|
+
getServer(): ServerInterface;
|
|
43
|
+
/**
|
|
44
|
+
* 设置服务端实例
|
|
45
|
+
* @param server
|
|
46
|
+
* @returns
|
|
47
|
+
*/
|
|
48
|
+
setServer(server: ServerInterface): this;
|
|
49
|
+
getAccessToken(): AccessTokenInterface;
|
|
50
|
+
/**
|
|
51
|
+
* 设置AccessToken实例
|
|
52
|
+
* @param accessToken
|
|
53
|
+
* @returns
|
|
54
|
+
*/
|
|
55
|
+
setAccessToken(accessToken: AccessTokenInterface): this;
|
|
56
|
+
setOAuthFactory(oauthFactory: WorkOAuthFactory): this;
|
|
57
|
+
getOAuth(): Promise<ProviderInterface>;
|
|
58
|
+
getTicket(): JsApiTicket;
|
|
59
|
+
/**
|
|
60
|
+
* 设置JsApiTicket实例
|
|
61
|
+
* @param ticket
|
|
62
|
+
* @returns
|
|
63
|
+
*/
|
|
64
|
+
setTicket(ticket: JsApiTicket): this;
|
|
65
|
+
getUtils(): Utils;
|
|
66
|
+
createClient(): AccessTokenAwareClient;
|
|
67
|
+
/**
|
|
68
|
+
* 获取请求默认配置
|
|
69
|
+
* @returns
|
|
70
|
+
*/
|
|
71
|
+
protected getHttpClientDefaultOptions(): Record<string, any>;
|
|
72
|
+
}
|
|
73
|
+
interface Application extends ConfigMixin, CacheMixin, ClientMixin, ServerRequestMixin, HttpClientMixin {
|
|
74
|
+
}
|
|
75
|
+
export = Application;
|
|
@@ -0,0 +1,177 @@
|
|
|
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 WeWork_1 = __importDefault(require("node-socialite/dist/Providers/WeWork"));
|
|
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 AccessToken_1 = __importDefault(require("./AccessToken"));
|
|
27
|
+
const Account_1 = __importDefault(require("./Account"));
|
|
28
|
+
const JsApiTicket_1 = __importDefault(require("./JsApiTicket"));
|
|
29
|
+
const Server_1 = __importDefault(require("./Server"));
|
|
30
|
+
const Utils_2 = __importDefault(require("./Utils"));
|
|
31
|
+
/**
|
|
32
|
+
* 公众号应用
|
|
33
|
+
*/
|
|
34
|
+
class Application {
|
|
35
|
+
constructor(config) {
|
|
36
|
+
this.account = null;
|
|
37
|
+
this.encryptor = null;
|
|
38
|
+
this.server = null;
|
|
39
|
+
this.accessToken = null;
|
|
40
|
+
this.oauthFactory = null;
|
|
41
|
+
this.ticket = null;
|
|
42
|
+
if (config instanceof ConfigInterface_1.default) {
|
|
43
|
+
this.setConfig(config);
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
this.setConfig(new Config_1.default(config));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
getAccount() {
|
|
50
|
+
if (!this.account) {
|
|
51
|
+
this.account = new Account_1.default(this.config.get('corp_id'), this.config.get('secret'), this.config.get('token'), this.config.get('aes_key'));
|
|
52
|
+
}
|
|
53
|
+
return this.account;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* 设置当前账户实例
|
|
57
|
+
* @param account
|
|
58
|
+
* @returns
|
|
59
|
+
*/
|
|
60
|
+
setAccount(account) {
|
|
61
|
+
this.account = account;
|
|
62
|
+
return this;
|
|
63
|
+
}
|
|
64
|
+
getEncryptor() {
|
|
65
|
+
if (!this.encryptor) {
|
|
66
|
+
let token = this.getAccount().getToken();
|
|
67
|
+
let aesKey = this.getAccount().getAesKey();
|
|
68
|
+
if (!token || !aesKey) {
|
|
69
|
+
throw new Error('token or aes_key cannot be empty.');
|
|
70
|
+
}
|
|
71
|
+
this.encryptor = new Encryptor_1.default(this.getAccount().getCorpId(), token, aesKey);
|
|
72
|
+
}
|
|
73
|
+
return this.encryptor;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* 设置加密机实例
|
|
77
|
+
* @param encryptor
|
|
78
|
+
* @returns
|
|
79
|
+
*/
|
|
80
|
+
setEncryptor(encryptor) {
|
|
81
|
+
this.encryptor = encryptor;
|
|
82
|
+
return this;
|
|
83
|
+
}
|
|
84
|
+
getServer() {
|
|
85
|
+
if (!this.server) {
|
|
86
|
+
this.server = new Server_1.default(this.getEncryptor(), this.getRequest());
|
|
87
|
+
}
|
|
88
|
+
return this.server;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* 设置服务端实例
|
|
92
|
+
* @param server
|
|
93
|
+
* @returns
|
|
94
|
+
*/
|
|
95
|
+
setServer(server) {
|
|
96
|
+
this.server = server;
|
|
97
|
+
return this;
|
|
98
|
+
}
|
|
99
|
+
getAccessToken() {
|
|
100
|
+
if (!this.accessToken) {
|
|
101
|
+
this.accessToken = new AccessToken_1.default(this.getAccount().getCorpId(), this.getAccount().getSecret(), null, this.getCache(), this.getHttpClient());
|
|
102
|
+
}
|
|
103
|
+
return this.accessToken;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* 设置AccessToken实例
|
|
107
|
+
* @param accessToken
|
|
108
|
+
* @returns
|
|
109
|
+
*/
|
|
110
|
+
setAccessToken(accessToken) {
|
|
111
|
+
this.accessToken = accessToken;
|
|
112
|
+
return this;
|
|
113
|
+
}
|
|
114
|
+
setOAuthFactory(oauthFactory) {
|
|
115
|
+
this.oauthFactory = oauthFactory;
|
|
116
|
+
return this;
|
|
117
|
+
}
|
|
118
|
+
getOAuth() {
|
|
119
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
120
|
+
if (!this.oauthFactory) {
|
|
121
|
+
this.oauthFactory = ((app) => {
|
|
122
|
+
return new WeWork_1.default({
|
|
123
|
+
client_id: app.getAccount().getCorpId(),
|
|
124
|
+
client_secret: app.getAccount().getSecret(),
|
|
125
|
+
redirect: app.getConfig().get('oauth.redirect_url'),
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
let provider = this.oauthFactory.call(null, this);
|
|
130
|
+
if (!(provider instanceof WeWork_1.default)) {
|
|
131
|
+
throw new Error(`The factory must return a \`ProviderInterface\` instance.`);
|
|
132
|
+
}
|
|
133
|
+
provider.withApiAccessToken(yield this.getAccessToken().getToken());
|
|
134
|
+
provider.scopes(this.getConfig().get('oauth.scopes', 'snsapi_userinfo'));
|
|
135
|
+
let agent_id = parseInt(this.config.get('agent_id')) || null;
|
|
136
|
+
if (agent_id) {
|
|
137
|
+
provider.setAgentId(agent_id);
|
|
138
|
+
}
|
|
139
|
+
return provider;
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
getTicket() {
|
|
143
|
+
if (!this.ticket) {
|
|
144
|
+
this.ticket = new JsApiTicket_1.default(this.getAccount().getCorpId(), this.getAccount().getSecret(), null, this.getCache(), this.getClient());
|
|
145
|
+
}
|
|
146
|
+
return this.ticket;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* 设置JsApiTicket实例
|
|
150
|
+
* @param ticket
|
|
151
|
+
* @returns
|
|
152
|
+
*/
|
|
153
|
+
setTicket(ticket) {
|
|
154
|
+
this.ticket = ticket;
|
|
155
|
+
return this;
|
|
156
|
+
}
|
|
157
|
+
getUtils() {
|
|
158
|
+
return new Utils_2.default(this);
|
|
159
|
+
}
|
|
160
|
+
createClient() {
|
|
161
|
+
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)))
|
|
162
|
+
.setPresets(this.getConfig().all());
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* 获取请求默认配置
|
|
166
|
+
* @returns
|
|
167
|
+
*/
|
|
168
|
+
getHttpClientDefaultOptions() {
|
|
169
|
+
return (0, merge_1.default)(true, {
|
|
170
|
+
baseURL: 'https://qyapi.weixin.qq.com/',
|
|
171
|
+
}, this.getConfig().get('http'));
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
;
|
|
175
|
+
;
|
|
176
|
+
(0, Utils_1.applyMixins)(Application, [ConfigMixin_1.default, CacheMixin_1.default, ClientMixin_1.default, ServerRequestMixin_1.default, HttpClientMixin_1.default]);
|
|
177
|
+
module.exports = Application;
|
|
@@ -0,0 +1,17 @@
|
|
|
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
|
+
'corp_id',
|
|
11
|
+
'secret',
|
|
12
|
+
'token',
|
|
13
|
+
'aes_key',
|
|
14
|
+
];
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
module.exports = Config;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
declare abstract class AccountInterface {
|
|
2
|
+
/**
|
|
3
|
+
* 获取corpid
|
|
4
|
+
* @returns
|
|
5
|
+
*/
|
|
6
|
+
getCorpId(): 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
|
+
* 获取corpid
|
|
5
|
+
* @returns
|
|
6
|
+
*/
|
|
7
|
+
getCorpId() { 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;
|