egg-wechat-sdk-x 1.0.7 → 1.0.8
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/LICENSE +21 -21
- package/README.md +65 -65
- package/app.js +15 -15
- package/config/config.default.js +9 -9
- package/index.d.ts +39 -39
- package/lib/WechatApi.js +101 -100
- package/lib/WechatOauth.js +71 -71
- package/package.json +58 -58
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
The MIT License (MIT)
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2019-present Alibaba Group Holding Limited and other contributors.
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019-present Alibaba Group Holding Limited and other contributors.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,65 +1,65 @@
|
|
|
1
|
-
# egg-wechat-api
|
|
2
|
-
|
|
3
|
-
[![NPM version][npm-image]][npm-url]
|
|
4
|
-
[![build status][travis-image]][travis-url]
|
|
5
|
-
[![Test coverage][codecov-image]][codecov-url]
|
|
6
|
-
[![David deps][david-image]][david-url]
|
|
7
|
-
[![Known Vulnerabilities][snyk-image]][snyk-url]
|
|
8
|
-
[![npm download][download-image]][download-url]
|
|
9
|
-
|
|
10
|
-
[npm-image]: https://img.shields.io/npm/v/egg-wechat-api.svg?style=flat-square
|
|
11
|
-
[npm-url]: https://npmjs.org/package/egg-wechat-api
|
|
12
|
-
[travis-image]: https://img.shields.io/travis/eggjs/egg-wechat-api.svg?style=flat-square
|
|
13
|
-
[travis-url]: https://travis-ci.org/eggjs/egg-wechat-api
|
|
14
|
-
[codecov-image]: https://img.shields.io/codecov/c/github/eggjs/egg-wechat-api.svg?style=flat-square
|
|
15
|
-
[codecov-url]: https://codecov.io/github/eggjs/egg-wechat-api?branch=master
|
|
16
|
-
[david-image]: https://img.shields.io/david/eggjs/egg-wechat-api.svg?style=flat-square
|
|
17
|
-
[david-url]: https://david-dm.org/eggjs/egg-wechat-api
|
|
18
|
-
[snyk-image]: https://snyk.io/test/npm/egg-wechat-api/badge.svg?style=flat-square
|
|
19
|
-
[snyk-url]: https://snyk.io/test/npm/egg-wechat-api
|
|
20
|
-
[download-image]: https://img.shields.io/npm/dm/egg-wechat-api.svg?style=flat-square
|
|
21
|
-
[download-url]: https://npmjs.org/package/egg-wechat-api
|
|
22
|
-
|
|
23
|
-
<!--
|
|
24
|
-
Description here.
|
|
25
|
-
-->
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
## Install
|
|
30
|
-
|
|
31
|
-
```bash
|
|
32
|
-
$ npm i egg-wechat-sdk-x --save
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
## Usage
|
|
36
|
-
|
|
37
|
-
```js
|
|
38
|
-
// {app_root}/config/plugin.js
|
|
39
|
-
exports.wechatSdk = {
|
|
40
|
-
enable: true,
|
|
41
|
-
package: 'egg-wechat-sdk-x',
|
|
42
|
-
};
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
## Configuration
|
|
46
|
-
|
|
47
|
-
```js
|
|
48
|
-
// {app_root}/config/config.default.js
|
|
49
|
-
exports.wechatSdk = {
|
|
50
|
-
};
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
see [config/config.default.js](config/config.default.js) for more detail.
|
|
54
|
-
|
|
55
|
-
## Example
|
|
56
|
-
|
|
57
|
-
<!-- example here -->
|
|
58
|
-
|
|
59
|
-
## Questions & Suggestions
|
|
60
|
-
|
|
61
|
-
Please open an issue [here](https://github.com/eggjs/egg/issues).
|
|
62
|
-
|
|
63
|
-
## License
|
|
64
|
-
|
|
65
|
-
[MIT](LICENSE)
|
|
1
|
+
# egg-wechat-api
|
|
2
|
+
|
|
3
|
+
[![NPM version][npm-image]][npm-url]
|
|
4
|
+
[![build status][travis-image]][travis-url]
|
|
5
|
+
[![Test coverage][codecov-image]][codecov-url]
|
|
6
|
+
[![David deps][david-image]][david-url]
|
|
7
|
+
[![Known Vulnerabilities][snyk-image]][snyk-url]
|
|
8
|
+
[![npm download][download-image]][download-url]
|
|
9
|
+
|
|
10
|
+
[npm-image]: https://img.shields.io/npm/v/egg-wechat-api.svg?style=flat-square
|
|
11
|
+
[npm-url]: https://npmjs.org/package/egg-wechat-api
|
|
12
|
+
[travis-image]: https://img.shields.io/travis/eggjs/egg-wechat-api.svg?style=flat-square
|
|
13
|
+
[travis-url]: https://travis-ci.org/eggjs/egg-wechat-api
|
|
14
|
+
[codecov-image]: https://img.shields.io/codecov/c/github/eggjs/egg-wechat-api.svg?style=flat-square
|
|
15
|
+
[codecov-url]: https://codecov.io/github/eggjs/egg-wechat-api?branch=master
|
|
16
|
+
[david-image]: https://img.shields.io/david/eggjs/egg-wechat-api.svg?style=flat-square
|
|
17
|
+
[david-url]: https://david-dm.org/eggjs/egg-wechat-api
|
|
18
|
+
[snyk-image]: https://snyk.io/test/npm/egg-wechat-api/badge.svg?style=flat-square
|
|
19
|
+
[snyk-url]: https://snyk.io/test/npm/egg-wechat-api
|
|
20
|
+
[download-image]: https://img.shields.io/npm/dm/egg-wechat-api.svg?style=flat-square
|
|
21
|
+
[download-url]: https://npmjs.org/package/egg-wechat-api
|
|
22
|
+
|
|
23
|
+
<!--
|
|
24
|
+
Description here.
|
|
25
|
+
-->
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
## Install
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
$ npm i egg-wechat-sdk-x --save
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Usage
|
|
36
|
+
|
|
37
|
+
```js
|
|
38
|
+
// {app_root}/config/plugin.js
|
|
39
|
+
exports.wechatSdk = {
|
|
40
|
+
enable: true,
|
|
41
|
+
package: 'egg-wechat-sdk-x',
|
|
42
|
+
};
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Configuration
|
|
46
|
+
|
|
47
|
+
```js
|
|
48
|
+
// {app_root}/config/config.default.js
|
|
49
|
+
exports.wechatSdk = {
|
|
50
|
+
};
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
see [config/config.default.js](config/config.default.js) for more detail.
|
|
54
|
+
|
|
55
|
+
## Example
|
|
56
|
+
|
|
57
|
+
<!-- example here -->
|
|
58
|
+
|
|
59
|
+
## Questions & Suggestions
|
|
60
|
+
|
|
61
|
+
Please open an issue [here](https://github.com/eggjs/egg/issues).
|
|
62
|
+
|
|
63
|
+
## License
|
|
64
|
+
|
|
65
|
+
[MIT](LICENSE)
|
package/app.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
/**
|
|
2
|
-
*
|
|
3
|
-
* @param {Egg.Application} app
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
const EggWechatApi = require("./lib/WechatApi");
|
|
7
|
-
const EggWechatOAuth = require("./lib/WechatOauth");
|
|
8
|
-
|
|
9
|
-
module.exports = app => {
|
|
10
|
-
if (app.config.wechatSdk) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
app.WechatApi = new EggWechatApi(app);
|
|
14
|
-
app.WechatOauth = new EggWechatOAuth(app);
|
|
15
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* @param {Egg.Application} app
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const EggWechatApi = require("./lib/WechatApi");
|
|
7
|
+
const EggWechatOAuth = require("./lib/WechatOauth");
|
|
8
|
+
|
|
9
|
+
module.exports = app => {
|
|
10
|
+
if (app.config.wechatSdk) {
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
app.WechatApi = new EggWechatApi(app);
|
|
14
|
+
app.WechatOauth = new EggWechatOAuth(app);
|
|
15
|
+
}
|
|
16
16
|
};
|
package/config/config.default.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* egg-wechat-api default config
|
|
5
|
-
* @member Config#wechatApi
|
|
6
|
-
* @property {String} SOME_KEY - some description
|
|
7
|
-
*/
|
|
8
|
-
exports.wechatApi = {
|
|
9
|
-
};
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* egg-wechat-api default config
|
|
5
|
+
* @member Config#wechatApi
|
|
6
|
+
* @property {String} SOME_KEY - some description
|
|
7
|
+
*/
|
|
8
|
+
exports.wechatApi = {
|
|
9
|
+
};
|
package/index.d.ts
CHANGED
|
@@ -1,39 +1,39 @@
|
|
|
1
|
-
|
|
2
|
-
// interface DataSources {
|
|
3
|
-
// datasources: EggSequelizeMultitableOptions[];
|
|
4
|
-
// }
|
|
5
|
-
// "name": "测试",
|
|
6
|
-
// "appid": "wxd0944fd71221c19c",
|
|
7
|
-
// "appsecret": "fe2574fdfce33dcaa5534d149b77c4a3",
|
|
8
|
-
// "token": "31427543139566790626",
|
|
9
|
-
import OAuth from "wechat-oauth";
|
|
10
|
-
import WechatApiSdk from "wechat-api";
|
|
11
|
-
import EggWechatApi from "./lib/WechatApi";
|
|
12
|
-
import EggWechatOAuth from "./lib/WechatOauth";
|
|
13
|
-
|
|
14
|
-
// "encodingAESKey": ""
|
|
15
|
-
interface WechatSdkOptions {
|
|
16
|
-
name?: string,
|
|
17
|
-
appId: string,
|
|
18
|
-
appSecret: string,
|
|
19
|
-
token?: string,
|
|
20
|
-
encodingAESKey?: string
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
interface EggWechatSdkOptions {
|
|
24
|
-
defaultAppId: string,
|
|
25
|
-
[propname: string]: WechatSdkOptions,
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
declare module 'egg' {
|
|
30
|
-
|
|
31
|
-
interface Application {
|
|
32
|
-
WechatApi: EggWechatApi;
|
|
33
|
-
WechatOauth: EggWechatOAuth;
|
|
34
|
-
}
|
|
35
|
-
// extend your config
|
|
36
|
-
interface EggAppConfig {
|
|
37
|
-
wechatSdk: EggWechatSdkOptions;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
1
|
+
|
|
2
|
+
// interface DataSources {
|
|
3
|
+
// datasources: EggSequelizeMultitableOptions[];
|
|
4
|
+
// }
|
|
5
|
+
// "name": "测试",
|
|
6
|
+
// "appid": "wxd0944fd71221c19c",
|
|
7
|
+
// "appsecret": "fe2574fdfce33dcaa5534d149b77c4a3",
|
|
8
|
+
// "token": "31427543139566790626",
|
|
9
|
+
import OAuth from "wechat-oauth";
|
|
10
|
+
import WechatApiSdk from "wechat-api";
|
|
11
|
+
import EggWechatApi from "./lib/WechatApi";
|
|
12
|
+
import EggWechatOAuth from "./lib/WechatOauth";
|
|
13
|
+
|
|
14
|
+
// "encodingAESKey": ""
|
|
15
|
+
interface WechatSdkOptions {
|
|
16
|
+
name?: string,
|
|
17
|
+
appId: string,
|
|
18
|
+
appSecret: string,
|
|
19
|
+
token?: string,
|
|
20
|
+
encodingAESKey?: string
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface EggWechatSdkOptions {
|
|
24
|
+
defaultAppId: string,
|
|
25
|
+
[propname: string]: WechatSdkOptions,
|
|
26
|
+
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
declare module 'egg' {
|
|
30
|
+
|
|
31
|
+
interface Application {
|
|
32
|
+
WechatApi: EggWechatApi;
|
|
33
|
+
WechatOauth: EggWechatOAuth;
|
|
34
|
+
}
|
|
35
|
+
// extend your config
|
|
36
|
+
interface EggAppConfig {
|
|
37
|
+
wechatSdk: EggWechatSdkOptions;
|
|
38
|
+
}
|
|
39
|
+
}
|
package/lib/WechatApi.js
CHANGED
|
@@ -1,101 +1,102 @@
|
|
|
1
|
-
const WechatApiSdk = require('wechat-api');
|
|
2
|
-
WechatApiSdk.patch("getCurrentAutoreplyInfo", "https://api.weixin.qq.com/cgi-bin/get_current_autoreply_info", true);
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
*
|
|
7
|
-
* @param {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
this.
|
|
12
|
-
this.
|
|
13
|
-
this.
|
|
14
|
-
this.
|
|
15
|
-
this.
|
|
16
|
-
this.
|
|
17
|
-
this.api.
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
await this.app.redis.
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
await this.app.redis.
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
*
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
*
|
|
86
|
-
* @
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}
|
|
1
|
+
const WechatApiSdk = require('wechat-api');
|
|
2
|
+
WechatApiSdk.patch("getCurrentAutoreplyInfo", "https://api.weixin.qq.com/cgi-bin/get_current_autoreply_info", true);
|
|
3
|
+
WechatApiSdk.patch("getPhonenumber", "https://api.weixin.qq.com/wxa/business/getuserphonenumber", true);
|
|
4
|
+
class WechatApi {
|
|
5
|
+
/**
|
|
6
|
+
*
|
|
7
|
+
* @param {Egg.Application} app
|
|
8
|
+
* @param {import("..").WechatOptions} _options
|
|
9
|
+
*/
|
|
10
|
+
constructor(app, _options) {
|
|
11
|
+
this.app = app;
|
|
12
|
+
this.options = _options;
|
|
13
|
+
this.access_token = null;
|
|
14
|
+
this.weixin_ticketToken = {};
|
|
15
|
+
this.ACCESS_TOKEN = "access_token_" + _options.appId;
|
|
16
|
+
this.WEIXIN_TICKETTOKEN = "weixin_tickettoken_" + _options.appId;
|
|
17
|
+
this.api = new WechatApiSdk(_options.appId, _options.appSecret, this.getToken.bind(this), this.setToken.bind(this));
|
|
18
|
+
this.api.registerTicketHandle(this.getTicketToken.bind(this), this.saveTicketToken.bind(this));
|
|
19
|
+
}
|
|
20
|
+
async getToken(callback) {
|
|
21
|
+
if (this.access_token) {
|
|
22
|
+
callback(null, this.access_token);
|
|
23
|
+
} else {
|
|
24
|
+
try {
|
|
25
|
+
const reply = await this.app.redis.get(this.ACCESS_TOKEN);
|
|
26
|
+
this.access_token = JSON.parse(reply);
|
|
27
|
+
callback(null, this.access_token);
|
|
28
|
+
} catch (error) {
|
|
29
|
+
callback(error);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
async setToken(token, callback) {
|
|
36
|
+
this.access_token = token;
|
|
37
|
+
try {
|
|
38
|
+
await this.app.redis.set(this.ACCESS_TOKEN, JSON.stringify(this.access_token));
|
|
39
|
+
await this.app.redis.expire(this.ACCESS_TOKEN, 7190);
|
|
40
|
+
} catch (error) {
|
|
41
|
+
callback(error);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
async getTicketToken(type, callback) {
|
|
47
|
+
|
|
48
|
+
if (this.weixin_ticketToken[type]) {
|
|
49
|
+
callback(null, this.weixin_ticketToken[type]);
|
|
50
|
+
} else {
|
|
51
|
+
|
|
52
|
+
try {
|
|
53
|
+
let reply = await this.app.redis.get(`${this.WEIXIN_TICKETTOKEN}type`);
|
|
54
|
+
this.weixin_ticketToken[type] = JSON.parse(reply);
|
|
55
|
+
callback(null, this.weixin_ticketToken[type]);
|
|
56
|
+
} catch (error) {
|
|
57
|
+
callback(error);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
}
|
|
63
|
+
// saveTicketToken
|
|
64
|
+
async saveTicketToken(type, _ticketToken, callback) {
|
|
65
|
+
this.weixin_ticketToken[type] = _ticketToken;
|
|
66
|
+
let key = `${this.WEIXIN_TICKETTOKEN}type`;
|
|
67
|
+
try {
|
|
68
|
+
await this.app.redis.set(key, JSON.stringify(this.weixin_ticketToken[type]));
|
|
69
|
+
await this.app.redis.expire(key, 7190);
|
|
70
|
+
} catch (error) {
|
|
71
|
+
callback(error);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
const WechatMap = {};
|
|
76
|
+
class EggWechatApi {
|
|
77
|
+
/**
|
|
78
|
+
*
|
|
79
|
+
* @param {Egg.Application} app
|
|
80
|
+
*/
|
|
81
|
+
constructor(app) {
|
|
82
|
+
this.app = app;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
*
|
|
86
|
+
* @param {string | null} appId
|
|
87
|
+
* @returns {WechatApiSdk}
|
|
88
|
+
*/
|
|
89
|
+
getIns(appId) {
|
|
90
|
+
appId = appId || this.app.config.wechatSdk.defaultAppId;
|
|
91
|
+
if (!WechatMap[appId]) {
|
|
92
|
+
const options = this.app.config.wechatSdk[appId];
|
|
93
|
+
if (options) {
|
|
94
|
+
WechatMap[appId] = new WechatApi(this.app, options);
|
|
95
|
+
} else {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return WechatMap[appId].api;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
101
102
|
module.exports = EggWechatApi;
|
package/lib/WechatOauth.js
CHANGED
|
@@ -1,72 +1,72 @@
|
|
|
1
|
-
const OAuth = require("wechat-oauth");
|
|
2
|
-
class WechatOauth {
|
|
3
|
-
/**
|
|
4
|
-
*
|
|
5
|
-
* @param {Egg.Application} app
|
|
6
|
-
* @param {import("..").WechatOptions} _options
|
|
7
|
-
*/
|
|
8
|
-
constructor(app, _options) {
|
|
9
|
-
this.app = app;
|
|
10
|
-
this.options = _options;
|
|
11
|
-
this.OAUTH_TOKEN = "auth_token_";
|
|
12
|
-
this.api = new OAuth(_options.appId, _options.appSecret, this.getToken.bind(this), this.setToken.bind(this));
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
*
|
|
16
|
-
* @param {String} openid
|
|
17
|
-
* @param {Function} callback
|
|
18
|
-
*/
|
|
19
|
-
async getToken(openid, callback) {
|
|
20
|
-
try {
|
|
21
|
-
let reply = await this.app.redis.get(`${this.OAUTH_TOKEN}${openid}`);
|
|
22
|
-
let token = JSON.parse(reply);
|
|
23
|
-
callback(null, token);
|
|
24
|
-
} catch (error) {
|
|
25
|
-
callback(error);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
*
|
|
30
|
-
* @param {String} openid
|
|
31
|
-
* @param {Object} tokenData
|
|
32
|
-
* @param {Function} callback
|
|
33
|
-
*/
|
|
34
|
-
async setToken(openid, token, callback) {
|
|
35
|
-
try {
|
|
36
|
-
let key = `${this.OAUTH_TOKEN}${openid}`;
|
|
37
|
-
await this.app.redis.set(key, JSON.stringify(token));
|
|
38
|
-
// await redisClient.expire(this.ACCESS_TOKEN, 7190);
|
|
39
|
-
callback(null);
|
|
40
|
-
} catch (error) {
|
|
41
|
-
callback(error);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
const WechatMap = {};
|
|
46
|
-
class EggWechatOAuth {
|
|
47
|
-
/**
|
|
48
|
-
*
|
|
49
|
-
* @param {Egg.Application} app
|
|
50
|
-
*/
|
|
51
|
-
constructor(app) {
|
|
52
|
-
this.app = app;
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
*
|
|
56
|
-
* @param {string | null} appId
|
|
57
|
-
* @returns {OAuth}
|
|
58
|
-
*/
|
|
59
|
-
getIns(appId) {
|
|
60
|
-
appId = appId || this.app.config.wechatSdk.defaultAppId;
|
|
61
|
-
if (!WechatMap[appId]) {
|
|
62
|
-
const options = this.app.config.wechatSdk[appId];
|
|
63
|
-
if (options) {
|
|
64
|
-
WechatMap[appId] = new WechatOauth(this.app, options);
|
|
65
|
-
} else {
|
|
66
|
-
return null;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
return WechatMap[appId].api;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
1
|
+
const OAuth = require("wechat-oauth");
|
|
2
|
+
class WechatOauth {
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
* @param {Egg.Application} app
|
|
6
|
+
* @param {import("..").WechatOptions} _options
|
|
7
|
+
*/
|
|
8
|
+
constructor(app, _options) {
|
|
9
|
+
this.app = app;
|
|
10
|
+
this.options = _options;
|
|
11
|
+
this.OAUTH_TOKEN = "auth_token_";
|
|
12
|
+
this.api = new OAuth(_options.appId, _options.appSecret, this.getToken.bind(this), this.setToken.bind(this));
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
*
|
|
16
|
+
* @param {String} openid
|
|
17
|
+
* @param {Function} callback
|
|
18
|
+
*/
|
|
19
|
+
async getToken(openid, callback) {
|
|
20
|
+
try {
|
|
21
|
+
let reply = await this.app.redis.get(`${this.OAUTH_TOKEN}${openid}`);
|
|
22
|
+
let token = JSON.parse(reply);
|
|
23
|
+
callback(null, token);
|
|
24
|
+
} catch (error) {
|
|
25
|
+
callback(error);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
*
|
|
30
|
+
* @param {String} openid
|
|
31
|
+
* @param {Object} tokenData
|
|
32
|
+
* @param {Function} callback
|
|
33
|
+
*/
|
|
34
|
+
async setToken(openid, token, callback) {
|
|
35
|
+
try {
|
|
36
|
+
let key = `${this.OAUTH_TOKEN}${openid}`;
|
|
37
|
+
await this.app.redis.set(key, JSON.stringify(token));
|
|
38
|
+
// await redisClient.expire(this.ACCESS_TOKEN, 7190);
|
|
39
|
+
callback(null);
|
|
40
|
+
} catch (error) {
|
|
41
|
+
callback(error);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
const WechatMap = {};
|
|
46
|
+
class EggWechatOAuth {
|
|
47
|
+
/**
|
|
48
|
+
*
|
|
49
|
+
* @param {Egg.Application} app
|
|
50
|
+
*/
|
|
51
|
+
constructor(app) {
|
|
52
|
+
this.app = app;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
*
|
|
56
|
+
* @param {string | null} appId
|
|
57
|
+
* @returns {OAuth}
|
|
58
|
+
*/
|
|
59
|
+
getIns(appId) {
|
|
60
|
+
appId = appId || this.app.config.wechatSdk.defaultAppId;
|
|
61
|
+
if (!WechatMap[appId]) {
|
|
62
|
+
const options = this.app.config.wechatSdk[appId];
|
|
63
|
+
if (options) {
|
|
64
|
+
WechatMap[appId] = new WechatOauth(this.app, options);
|
|
65
|
+
} else {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return WechatMap[appId].api;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
72
|
module.exports = EggWechatOAuth;
|
package/package.json
CHANGED
|
@@ -1,58 +1,58 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "egg-wechat-sdk-x",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "",
|
|
5
|
-
"eggPlugin": {
|
|
6
|
-
"name": "wechatSdk",
|
|
7
|
-
"dependencies": [
|
|
8
|
-
"redis"
|
|
9
|
-
]
|
|
10
|
-
},
|
|
11
|
-
"keywords": [
|
|
12
|
-
"egg",
|
|
13
|
-
"eggPlugin",
|
|
14
|
-
"egg-plugin"
|
|
15
|
-
],
|
|
16
|
-
"dependencies": {
|
|
17
|
-
"egg-redis": "^2.4.0",
|
|
18
|
-
"wechat-api": "^1.35.1",
|
|
19
|
-
"wechat-oauth": "^1.5.0"
|
|
20
|
-
},
|
|
21
|
-
"devDependencies": {
|
|
22
|
-
"egg": "^3",
|
|
23
|
-
"egg-bin": "^5",
|
|
24
|
-
"egg-ci": "^2",
|
|
25
|
-
"egg-mock": "^5",
|
|
26
|
-
"eslint": "^8",
|
|
27
|
-
"eslint-config-egg": "^12"
|
|
28
|
-
},
|
|
29
|
-
"engines": {
|
|
30
|
-
"node": ">=16.0.0"
|
|
31
|
-
},
|
|
32
|
-
"scripts": {
|
|
33
|
-
"test": "npm run lint -- --fix && npm run test-local",
|
|
34
|
-
"test-local": "egg-bin test",
|
|
35
|
-
"cov": "egg-bin cov",
|
|
36
|
-
"lint": "eslint .",
|
|
37
|
-
"ci": "npm run lint && npm run cov"
|
|
38
|
-
},
|
|
39
|
-
"files": [
|
|
40
|
-
"app.js",
|
|
41
|
-
"lib",
|
|
42
|
-
"config",
|
|
43
|
-
"index.d.ts"
|
|
44
|
-
],
|
|
45
|
-
"ci": {
|
|
46
|
-
"version": "16, 18"
|
|
47
|
-
},
|
|
48
|
-
"repository": {
|
|
49
|
-
"type": "git",
|
|
50
|
-
"url": "git+https://github.com/qoyf/egg-wechat-sdk.git"
|
|
51
|
-
},
|
|
52
|
-
"bugs": {
|
|
53
|
-
"url": "https://github.com/qoyf/egg-wechat-sdk/issues"
|
|
54
|
-
},
|
|
55
|
-
"homepage": "https://github.com/qoyf/egg-wechat-sdk#readme",
|
|
56
|
-
"author": "",
|
|
57
|
-
"license": "MIT"
|
|
58
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "egg-wechat-sdk-x",
|
|
3
|
+
"version": "1.0.8",
|
|
4
|
+
"description": "",
|
|
5
|
+
"eggPlugin": {
|
|
6
|
+
"name": "wechatSdk",
|
|
7
|
+
"dependencies": [
|
|
8
|
+
"redis"
|
|
9
|
+
]
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"egg",
|
|
13
|
+
"eggPlugin",
|
|
14
|
+
"egg-plugin"
|
|
15
|
+
],
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"egg-redis": "^2.4.0",
|
|
18
|
+
"wechat-api": "^1.35.1",
|
|
19
|
+
"wechat-oauth": "^1.5.0"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"egg": "^3",
|
|
23
|
+
"egg-bin": "^5",
|
|
24
|
+
"egg-ci": "^2",
|
|
25
|
+
"egg-mock": "^5",
|
|
26
|
+
"eslint": "^8",
|
|
27
|
+
"eslint-config-egg": "^12"
|
|
28
|
+
},
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=16.0.0"
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"test": "npm run lint -- --fix && npm run test-local",
|
|
34
|
+
"test-local": "egg-bin test",
|
|
35
|
+
"cov": "egg-bin cov",
|
|
36
|
+
"lint": "eslint .",
|
|
37
|
+
"ci": "npm run lint && npm run cov"
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"app.js",
|
|
41
|
+
"lib",
|
|
42
|
+
"config",
|
|
43
|
+
"index.d.ts"
|
|
44
|
+
],
|
|
45
|
+
"ci": {
|
|
46
|
+
"version": "16, 18"
|
|
47
|
+
},
|
|
48
|
+
"repository": {
|
|
49
|
+
"type": "git",
|
|
50
|
+
"url": "git+https://github.com/qoyf/egg-wechat-sdk.git"
|
|
51
|
+
},
|
|
52
|
+
"bugs": {
|
|
53
|
+
"url": "https://github.com/qoyf/egg-wechat-sdk/issues"
|
|
54
|
+
},
|
|
55
|
+
"homepage": "https://github.com/qoyf/egg-wechat-sdk#readme",
|
|
56
|
+
"author": "",
|
|
57
|
+
"license": "MIT"
|
|
58
|
+
}
|