@aomex-contrib/wechat-mini-program-login 0.0.0-next-20261122045441
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 +17 -0
- package/LICENSE +21 -0
- package/README.md +158 -0
- package/dist/index.d.ts +116 -0
- package/dist/index.js +210 -0
- package/dist/index.js.map +1 -0
- package/package.json +42 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# @aomex-contrib/wechat-mini-program-login
|
|
2
|
+
|
|
3
|
+
## 0.0.0-next-20261122045441
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [`3a0c0b2`](https://github.com/aomex/aomex-contrib/commit/3a0c0b29390d1c881a68d7d492d61f341354e965) Thanks [@geekact](https://github.com/geekact)! - 新增 `getAccessToken` / `deleteAccessToken` / `withAccessToken` / `assertOk` 与新版 `getPhoneNumber`;构造必填 `store`(`singleFlight` 防并发换票);上游失败抛 `HttpError`;peer 升至 `@aomex/*` `^4.4.0`
|
|
8
|
+
|
|
9
|
+
## 0.1.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- [`013319f`](https://github.com/aomex/aomex-contrib/commit/013319f96e95278a693f55708798733412f0a947) Thanks [@geekact](https://github.com/geekact)! - 新增微信相关包:
|
|
14
|
+
|
|
15
|
+
- `@aomex-contrib/oauth2-wechat`:开放平台扫码、公众号网页授权、App 换票;默认 profile `{ base, userinfo }`,`scopes` 必填并可收窄类型
|
|
16
|
+
- `@aomex-contrib/oauth2-wecom`:企微自建应用网页授权;`scopes` 必填;默认返回 `WecomProfile`(无伪装 access_token)
|
|
17
|
+
- `@aomex-contrib/wechat-mini-program-login`:小程序 / 小游戏 `jscode2session` 登录(非 OAuth)
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023-2024 geekact
|
|
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
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
# @aomex-contrib/wechat-mini-program-login
|
|
2
|
+
|
|
3
|
+
微信**小程序 / 小游戏**服务端身份能力(**不是 OAuth2**):
|
|
4
|
+
|
|
5
|
+
- `code2Session`:`wx.login` 的 code → `openid` / `session_key`
|
|
6
|
+
- `getAccessToken`:全局接口调用凭证(经 `@aomex/cache` 缓存)
|
|
7
|
+
- `getPhoneNumber`:新版手机号 code → 手机号
|
|
8
|
+
|
|
9
|
+
网页扫码 / 公众号 OAuth 请用 [`@aomex-contrib/oauth2-wechat`](../oauth2-wechat/README.md)。
|
|
10
|
+
|
|
11
|
+
### 身份能力边界
|
|
12
|
+
|
|
13
|
+
微信小程序**没有**「服务端用 token / code 换真实头像、昵称」的接口;`getUserInfo` / `getUserProfile` 也已调整,不再返回真实资料。
|
|
14
|
+
|
|
15
|
+
| 能换到的 | 换不到的 |
|
|
16
|
+
| ------------------------------------------- | ------------------------------ |
|
|
17
|
+
| `openid` / `session_key` / 可选 `unionid` | 真实微信头像、昵称、性别、地区 |
|
|
18
|
+
| 手机号(新版 `getPhoneNumber`,需付费额度) | |
|
|
19
|
+
|
|
20
|
+
因此本包做到 **静默登录 + 手机号** 即为身份链路上的极限。头像昵称需前端用[头像昵称填写](https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/userProfile.html)(`chooseAvatar` + `input type="nickname"`)收集后交给业务后端保存,不要期待类似 OAuth `onProfile` 的拉取。
|
|
21
|
+
|
|
22
|
+
常见注册串行:`wx.login` → `code2Session` →(可选)手机号 → 发自家登录态 →(可选)用户自填头像昵称。
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## 安装
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
pnpm add @aomex-contrib/wechat-mini-program-login
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
需自行安装 peer:`@aomex/cache`(及适配器,如 `@aomex/cache/memory`)、`@aomex/web`(`HttpError`)。
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## 用法
|
|
37
|
+
|
|
38
|
+
### 1. 后台
|
|
39
|
+
|
|
40
|
+
小程序后台 → 开发管理 → 开发设置,拿到 **AppID / AppSecret**。
|
|
41
|
+
小游戏使用同一套接口。
|
|
42
|
+
|
|
43
|
+
### 2. 静默登录(openid)
|
|
44
|
+
|
|
45
|
+
前端:
|
|
46
|
+
|
|
47
|
+
```js
|
|
48
|
+
wx.login({
|
|
49
|
+
success(res) {
|
|
50
|
+
if (res.code) {
|
|
51
|
+
wx.request({
|
|
52
|
+
url: 'https://example.com/api/mini/login',
|
|
53
|
+
method: 'POST',
|
|
54
|
+
data: { code: res.code },
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
服务端:
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
import { CacheMemoryAdapter } from '@aomex/cache/memory';
|
|
65
|
+
import { WechatMiniProgramLoginClient } from '@aomex-contrib/wechat-mini-program-login';
|
|
66
|
+
|
|
67
|
+
const mini = new WechatMiniProgramLoginClient({
|
|
68
|
+
appId: process.env.WX_MINI_APP_ID!,
|
|
69
|
+
appSecret: process.env.WX_MINI_APP_SECRET!,
|
|
70
|
+
// 生产建议 Redis 等,多实例共享 access_token;
|
|
71
|
+
// 构造时会 registerLifecycle(connect),应用关停时 disconnect
|
|
72
|
+
store: new CacheMemoryAdapter(),
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
const session = await mini.code2Session(code);
|
|
76
|
+
// { openid, session_key, unionid? }
|
|
77
|
+
// session_key:开放数据解密/验签用,仅服务端;新版手机号不需要;勿下发前端
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### 3. 获取手机号(新版)
|
|
81
|
+
|
|
82
|
+
前端(基础库 ≥ 2.21.2):
|
|
83
|
+
|
|
84
|
+
```html
|
|
85
|
+
<button open-type="getPhoneNumber" bindgetphonenumber="onGetPhoneNumber"></button>
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
```js
|
|
89
|
+
onGetPhoneNumber(e) {
|
|
90
|
+
if (e.detail.code) {
|
|
91
|
+
wx.request({
|
|
92
|
+
url: 'https://example.com/api/mini/phone',
|
|
93
|
+
method: 'POST',
|
|
94
|
+
data: { code: e.detail.code },
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
服务端:
|
|
101
|
+
|
|
102
|
+
```ts
|
|
103
|
+
const phone = await mini.getPhoneNumber(phoneCode);
|
|
104
|
+
// { phoneNumber, purePhoneNumber, countryCode, watermark }
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
手机号的 `code` 与 `wx.login` 的 `code` **不能混用**;新版**不需要** `session_key` AES 解密。
|
|
108
|
+
`getPhoneNumber` 经 `withAccessToken`:内部取缓存 `access_token`;若微信返回 40001 / 42001,会删除缓存并重新获取后再调一次。
|
|
109
|
+
|
|
110
|
+
### 4. 自己调小程序业务 API
|
|
111
|
+
|
|
112
|
+
优先用 `withAccessToken`(票失效会自动清缓存重试一次),再用 `assertOk` 校验响应:
|
|
113
|
+
|
|
114
|
+
```ts
|
|
115
|
+
const { openlink } = await mini.withAccessToken(async (accessToken) => {
|
|
116
|
+
const res = await fetch(
|
|
117
|
+
`https://api.weixin.qq.com/wxa/generatescheme?access_token=${accessToken}`,
|
|
118
|
+
{
|
|
119
|
+
method: 'POST',
|
|
120
|
+
headers: { 'content-type': 'application/json' },
|
|
121
|
+
body: JSON.stringify({
|
|
122
|
+
jump_wxa: { path: '/pages/index/index' },
|
|
123
|
+
}),
|
|
124
|
+
},
|
|
125
|
+
);
|
|
126
|
+
// 校验 errcode;成功则去掉 errcode/errmsg(40001/42001 会触发清缓存重试)
|
|
127
|
+
return mini.assertOk<{ openlink: string }>(await res.json());
|
|
128
|
+
});
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
也可以直接 `getAccessToken()`;确认票废时可再 `deleteAccessToken()` 清本地缓存(不会调用微信作废接口)。
|
|
132
|
+
**不要**把全局 `access_token` 下发给前端。
|
|
133
|
+
|
|
134
|
+
---
|
|
135
|
+
|
|
136
|
+
## API
|
|
137
|
+
|
|
138
|
+
| 方法 | 说明 |
|
|
139
|
+
| ---------------------------------------- | ------------------------------------------------------------------------- |
|
|
140
|
+
| `code2Session(jsCode)` | 换取 openid / session_key |
|
|
141
|
+
| `getAccessToken()` | 小程序全局 `access_token`(`cgi-bin/token`;TTL ≈ `expires_in - 300` 秒) |
|
|
142
|
+
| `withAccessToken(fn)` | 带 token 执行业务调用;遇 40001 / 42001 自动清缓存重试一次 |
|
|
143
|
+
| `assertOk<T>(json)` | 校验微信 JSON;成功则剥掉 `errcode`/`errmsg` 并返回业务字段 |
|
|
144
|
+
| `deleteAccessToken()` | 删除本应用本地全局 access_token 缓存 |
|
|
145
|
+
| `getPhoneNumber(phoneCode, { openid? })` | 新版换手机号(内部走 `withAccessToken`) |
|
|
146
|
+
|
|
147
|
+
---
|
|
148
|
+
|
|
149
|
+
## FAQ
|
|
150
|
+
|
|
151
|
+
**Q: 能像 OAuth 那样 `getProfile` 拉头像昵称吗?**
|
|
152
|
+
A: 不能。见上文「身份能力边界」;前端自行收集即可。
|
|
153
|
+
|
|
154
|
+
**Q: 旧版 encryptedData + AES 还支持吗?**
|
|
155
|
+
A: 本包不封装旧解密。官方建议使用新版 `code` 换号。
|
|
156
|
+
|
|
157
|
+
**Q: 想要跨公众号 / 小程序统一用户?**
|
|
158
|
+
A: 把公众号 / 小程序绑定到同一开放平台账号,再使用 `code2Session` 返回的 `unionid`。
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { CacheAdapter, Caching } from "@aomex/cache";
|
|
2
|
+
type WechatMiniProgramLoginOptions = {
|
|
3
|
+
/** 小程序 AppID */
|
|
4
|
+
appId: string;
|
|
5
|
+
/** 小程序 AppSecret */
|
|
6
|
+
appSecret: string;
|
|
7
|
+
/**
|
|
8
|
+
* 缓存适配器,用于缓存小程序全局 `access_token`
|
|
9
|
+
*(`getAccessToken` / `getPhoneNumber` 依赖)。
|
|
10
|
+
* 构造时会经 `registerLifecycle` 调用 `connect`,应用关停时 `disconnect`
|
|
11
|
+
*/
|
|
12
|
+
store: CacheAdapter;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* `jscode2session` 成功结果(**非 OAuth**)
|
|
16
|
+
* @see https://developers.weixin.qq.com/miniprogram/dev/server/API/user-login/api_code2session.html
|
|
17
|
+
*/
|
|
18
|
+
type WechatMiniProgramSession = {
|
|
19
|
+
/** 用户在该小程序下的唯一标识,可作业务用户主键 */
|
|
20
|
+
openid: string;
|
|
21
|
+
/**
|
|
22
|
+
* 用户会话密钥,基本用不上了,可忽略(仅服务端保存,**勿下发前端**)。
|
|
23
|
+
*
|
|
24
|
+
* 用途:校验 / 解密微信下发的开放数据(如旧版 `encryptedData` AES 解密、
|
|
25
|
+
* `sha1(rawData + session_key)` 验签)。与应用级 `access_token` 不同,它绑定单次
|
|
26
|
+
* `wx.login` 会话,过期需用户重新登录再换。
|
|
27
|
+
*
|
|
28
|
+
* 新版 `getPhoneNumber` **不需要** `session_key`;仅做静默登录 + 新版手机号时
|
|
29
|
+
*
|
|
30
|
+
*/
|
|
31
|
+
session_key: string;
|
|
32
|
+
/**
|
|
33
|
+
* 同一开放平台下多应用(公众号 / 小程序)的统一用户标识。
|
|
34
|
+
* 小程序已绑定开放平台且满足下发条件时才有
|
|
35
|
+
*/
|
|
36
|
+
unionid?: string;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* 新版手机号接口返回的 `phone_info`
|
|
40
|
+
* @see https://developers.weixin.qq.com/miniprogram/dev/server/API/user-info/phone-number/api_getphonenumber.html
|
|
41
|
+
*/
|
|
42
|
+
type WechatMiniProgramPhoneInfo = {
|
|
43
|
+
/** 用户绑定的手机号(含区号) */
|
|
44
|
+
phoneNumber: string;
|
|
45
|
+
/** 没有区号的手机号 */
|
|
46
|
+
purePhoneNumber: string;
|
|
47
|
+
/** 区号 */
|
|
48
|
+
countryCode: string;
|
|
49
|
+
/** 数据水印,用于校验来源与时效 */
|
|
50
|
+
watermark: {
|
|
51
|
+
/** 用户获取手机号操作的时间戳(秒) */
|
|
52
|
+
timestamp: number;
|
|
53
|
+
/** 小程序 appid,应与当前应用一致 */
|
|
54
|
+
appid: string;
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
type WechatErrorBody = {
|
|
58
|
+
errcode?: number;
|
|
59
|
+
errmsg?: string;
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* 微信小程序 / 小游戏登录与身份相关能力(**不是 OAuth2**)。
|
|
63
|
+
* - `code2Session`:`wx.login` 的 code → openid / session_key
|
|
64
|
+
* - `getAccessToken`:全局接口调用凭证(经 `store` 缓存)
|
|
65
|
+
* - `getPhoneNumber`:新版手机号 code → 手机号(内部用 access_token)
|
|
66
|
+
* @see https://developers.weixin.qq.com/miniprogram/dev/server/API/user-login/api_code2session.html
|
|
67
|
+
*/
|
|
68
|
+
export declare class WechatMiniProgramLoginClient {
|
|
69
|
+
protected readonly appId: string;
|
|
70
|
+
protected readonly appSecret: string;
|
|
71
|
+
protected readonly store: CacheAdapter;
|
|
72
|
+
protected readonly caching: Caching;
|
|
73
|
+
protected readonly tokenCacheKey: string;
|
|
74
|
+
constructor(opts: WechatMiniProgramLoginOptions);
|
|
75
|
+
/**
|
|
76
|
+
* 用 `wx.login` 得到的 `js_code` 换取 openid / session_key(及可选 unionid)。
|
|
77
|
+
* `session_key` 仅用于开放数据解密 / 验签,新版手机号不依赖它;勿下发前端。
|
|
78
|
+
*/
|
|
79
|
+
code2Session(jsCode: string): Promise<WechatMiniProgramSession>;
|
|
80
|
+
/**
|
|
81
|
+
* 获取小程序全局 `access_token`(经构造时的 `store` 缓存)。
|
|
82
|
+
* TTL 为 `max(60, expires_in - 300)` 秒(微信默认 expires_in=7200 → 约 6900 秒)。
|
|
83
|
+
* 未命中时用 `singleFlight`,避免并发多次向微信换票(新票会使旧票失效)。
|
|
84
|
+
*/
|
|
85
|
+
getAccessToken(): Promise<string>;
|
|
86
|
+
/** 删除本应用本地缓存的 `access_token`(不会调用微信作废接口) */
|
|
87
|
+
deleteAccessToken(): Promise<void>;
|
|
88
|
+
/**
|
|
89
|
+
* 用 `getPhoneNumber` 按钮回调的 code 换取手机号(新版接口,无需 session_key)。
|
|
90
|
+
* 经 {@link withAccessToken}:缓存 token 失效时会自动删除并重取后再调一次。
|
|
91
|
+
* @param phoneCode `e.detail.code`,与 `wx.login` 的 code 不同,不可混用
|
|
92
|
+
* @param opts.openid 可选;填入后微信会校验与 code 的绑定关系
|
|
93
|
+
*/
|
|
94
|
+
getPhoneNumber(phoneCode: string, opts?: {
|
|
95
|
+
openid?: string;
|
|
96
|
+
}): Promise<WechatMiniProgramPhoneInfo>;
|
|
97
|
+
/**
|
|
98
|
+
* 带自动刷新的 `access_token` 调用包装:先取缓存 token 执行 `fn`;
|
|
99
|
+
* 若抛出 `HttpError` 且 `code` 为 `40001` / `42001`,则清缓存并重取后再执行一次。
|
|
100
|
+
* 业务响应优先用 {@link assertOk}。
|
|
101
|
+
*/
|
|
102
|
+
withAccessToken<T>(fn: (accessToken: string) => Promise<T>): Promise<T>;
|
|
103
|
+
/**
|
|
104
|
+
* 校验微信业务接口 JSON:`errcode` 非 0 时抛 `HttpError`(`code` = errcode,可被 {@link withAccessToken} 识别);
|
|
105
|
+
* 成功则剥掉 `errcode` / `errmsg`,返回剩余业务字段(可用泛型标注)。
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* const data = mini.assertOk<{ openlink: string }>(await res.json());
|
|
109
|
+
*/
|
|
110
|
+
assertOk<T extends Record<string, unknown> = Record<string, unknown>>(json: WechatErrorBody & T): T;
|
|
111
|
+
/** 40001 invalid credential / 42001 access_token expired */
|
|
112
|
+
protected isStaleAccessTokenError(err: unknown): boolean;
|
|
113
|
+
protected fetchWechatJson(url: URL, errorKey: 'code2session_failed' | 'access_token_failed' | 'phone_number_failed'): Promise<Record<string, unknown> & WechatErrorBody>;
|
|
114
|
+
protected assertWechatOk(json: WechatErrorBody, errorKey: 'code2session_failed' | 'access_token_failed' | 'phone_number_failed'): void;
|
|
115
|
+
}
|
|
116
|
+
export type { WechatMiniProgramLoginOptions, WechatMiniProgramPhoneInfo, WechatMiniProgramSession };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { Caching } from "@aomex/cache";
|
|
2
|
+
import { I18n, createI18n, registerLifecycle, rule, validate } from "@aomex/common";
|
|
3
|
+
import { HttpError } from "@aomex/web";
|
|
4
|
+
// src/i18n/locales/zh-cn.ts
|
|
5
|
+
const zh = I18n.define({
|
|
6
|
+
app_id_empty: "appId 不能为空",
|
|
7
|
+
app_secret_empty: "appSecret 不能为空",
|
|
8
|
+
store_empty: "store 不能为空",
|
|
9
|
+
js_code_empty: "jsCode 不能为空",
|
|
10
|
+
phone_code_empty: "phoneCode 不能为空",
|
|
11
|
+
code2session_failed: "微信 code2session 失败:{{errcode}} {{errmsg}}",
|
|
12
|
+
access_token_failed: "微信获取 access_token 失败:{{errcode}} {{errmsg}}",
|
|
13
|
+
phone_number_failed: "微信获取手机号失败:{{errcode}} {{errmsg}}",
|
|
14
|
+
invalid_json: "响应不是有效 JSON"
|
|
15
|
+
});
|
|
16
|
+
// src/i18n/locales/en-us.ts
|
|
17
|
+
const en = I18n.satisfies(zh).define({
|
|
18
|
+
app_id_empty: "appId must not be empty",
|
|
19
|
+
app_secret_empty: "appSecret must not be empty",
|
|
20
|
+
store_empty: "store must not be empty",
|
|
21
|
+
js_code_empty: "jsCode must not be empty",
|
|
22
|
+
phone_code_empty: "phoneCode must not be empty",
|
|
23
|
+
code2session_failed: "WeChat code2session failed: {{errcode}} {{errmsg}}",
|
|
24
|
+
access_token_failed: "WeChat get access_token failed: {{errcode}} {{errmsg}}",
|
|
25
|
+
phone_number_failed: "WeChat get phone number failed: {{errcode}} {{errmsg}}",
|
|
26
|
+
invalid_json: "response is not valid JSON"
|
|
27
|
+
});
|
|
28
|
+
// src/i18n/index.ts
|
|
29
|
+
const i18n = createI18n({
|
|
30
|
+
zh,
|
|
31
|
+
en,
|
|
32
|
+
load: (file) => import(new URL(`./locales/${file}.js`, import.meta.url).href)
|
|
33
|
+
});
|
|
34
|
+
// src/client.ts
|
|
35
|
+
const CODE2SESSION_URL = "https://api.weixin.qq.com/sns/jscode2session";
|
|
36
|
+
const ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token";
|
|
37
|
+
const GET_PHONE_NUMBER_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber";
|
|
38
|
+
/**
|
|
39
|
+
* 相对微信返回的 `expires_in` 提前失效的秒数,避免边界过期。
|
|
40
|
+
* 实际缓存 TTL = max(60, expires_in - TOKEN_SKEW_SEC);
|
|
41
|
+
* 微信默认 expires_in=7200,故常见 TTL 为 6900 秒(约 1h55m)。
|
|
42
|
+
*/
|
|
43
|
+
const TOKEN_SKEW_SEC = 300;
|
|
44
|
+
/**
|
|
45
|
+
* 微信小程序 / 小游戏登录与身份相关能力(**不是 OAuth2**)。
|
|
46
|
+
* - `code2Session`:`wx.login` 的 code → openid / session_key
|
|
47
|
+
* - `getAccessToken`:全局接口调用凭证(经 `store` 缓存)
|
|
48
|
+
* - `getPhoneNumber`:新版手机号 code → 手机号(内部用 access_token)
|
|
49
|
+
* @see https://developers.weixin.qq.com/miniprogram/dev/server/API/user-login/api_code2session.html
|
|
50
|
+
*/
|
|
51
|
+
var WechatMiniProgramLoginClient = class {
|
|
52
|
+
appId;
|
|
53
|
+
appSecret;
|
|
54
|
+
store;
|
|
55
|
+
caching;
|
|
56
|
+
tokenCacheKey;
|
|
57
|
+
constructor(opts) {
|
|
58
|
+
if (!opts.appId) throw new Error(i18n.t("app_id_empty"));
|
|
59
|
+
if (!opts.appSecret) throw new Error(i18n.t("app_secret_empty"));
|
|
60
|
+
if (!opts.store) throw new Error(i18n.t("store_empty"));
|
|
61
|
+
this.appId = opts.appId;
|
|
62
|
+
this.appSecret = opts.appSecret;
|
|
63
|
+
this.store = opts.store;
|
|
64
|
+
this.caching = new Caching(opts.store);
|
|
65
|
+
this.tokenCacheKey = `wxmini:access_token:${opts.appId}`;
|
|
66
|
+
registerLifecycle({
|
|
67
|
+
onInit: () => this.store.connect(),
|
|
68
|
+
onDestroy: () => this.store.disconnect()
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* 用 `wx.login` 得到的 `js_code` 换取 openid / session_key(及可选 unionid)。
|
|
73
|
+
* `session_key` 仅用于开放数据解密 / 验签,新版手机号不依赖它;勿下发前端。
|
|
74
|
+
*/
|
|
75
|
+
async code2Session(jsCode) {
|
|
76
|
+
if (!jsCode) throw new Error(i18n.t("js_code_empty"));
|
|
77
|
+
const url = new URL(CODE2SESSION_URL);
|
|
78
|
+
url.searchParams.set("appid", this.appId);
|
|
79
|
+
url.searchParams.set("secret", this.appSecret);
|
|
80
|
+
url.searchParams.set("js_code", jsCode);
|
|
81
|
+
url.searchParams.set("grant_type", "authorization_code");
|
|
82
|
+
const json = await this.fetchWechatJson(url, "code2session_failed");
|
|
83
|
+
this.assertWechatOk(json, "code2session_failed");
|
|
84
|
+
return validate(json, {
|
|
85
|
+
openid: rule.string(),
|
|
86
|
+
session_key: rule.string(),
|
|
87
|
+
unionid: rule.string().optional()
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* 获取小程序全局 `access_token`(经构造时的 `store` 缓存)。
|
|
92
|
+
* TTL 为 `max(60, expires_in - 300)` 秒(微信默认 expires_in=7200 → 约 6900 秒)。
|
|
93
|
+
* 未命中时用 `singleFlight`,避免并发多次向微信换票(新票会使旧票失效)。
|
|
94
|
+
*/
|
|
95
|
+
async getAccessToken() {
|
|
96
|
+
const cached = await this.caching.get(this.tokenCacheKey);
|
|
97
|
+
if (cached?.access_token) return cached.access_token;
|
|
98
|
+
return this.caching.singleFlight(this.tokenCacheKey, async () => {
|
|
99
|
+
const again = await this.caching.get(this.tokenCacheKey);
|
|
100
|
+
if (again?.access_token) return again.access_token;
|
|
101
|
+
const url = new URL(ACCESS_TOKEN_URL);
|
|
102
|
+
url.searchParams.set("grant_type", "client_credential");
|
|
103
|
+
url.searchParams.set("appid", this.appId);
|
|
104
|
+
url.searchParams.set("secret", this.appSecret);
|
|
105
|
+
const json = await this.fetchWechatJson(url, "access_token_failed");
|
|
106
|
+
this.assertWechatOk(json, "access_token_failed");
|
|
107
|
+
const { access_token, expires_in } = await validate(json, {
|
|
108
|
+
access_token: rule.string(),
|
|
109
|
+
expires_in: rule.int().min(1).optional()
|
|
110
|
+
});
|
|
111
|
+
const ttlSec = Math.max(60, (expires_in ?? 7200) - TOKEN_SKEW_SEC);
|
|
112
|
+
await this.caching.set(this.tokenCacheKey, { access_token }, ttlSec * 1e3);
|
|
113
|
+
return access_token;
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
/** 删除本应用本地缓存的 `access_token`(不会调用微信作废接口) */
|
|
117
|
+
async deleteAccessToken() {
|
|
118
|
+
await this.caching.delete(this.tokenCacheKey);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* 用 `getPhoneNumber` 按钮回调的 code 换取手机号(新版接口,无需 session_key)。
|
|
122
|
+
* 经 {@link withAccessToken}:缓存 token 失效时会自动删除并重取后再调一次。
|
|
123
|
+
* @param phoneCode `e.detail.code`,与 `wx.login` 的 code 不同,不可混用
|
|
124
|
+
* @param opts.openid 可选;填入后微信会校验与 code 的绑定关系
|
|
125
|
+
*/
|
|
126
|
+
async getPhoneNumber(phoneCode, opts) {
|
|
127
|
+
if (!phoneCode) throw new Error(i18n.t("phone_code_empty"));
|
|
128
|
+
return this.withAccessToken(async (accessToken) => {
|
|
129
|
+
const url = new URL(GET_PHONE_NUMBER_URL);
|
|
130
|
+
url.searchParams.set("access_token", accessToken);
|
|
131
|
+
const body = { code: phoneCode };
|
|
132
|
+
if (opts?.openid) body["openid"] = opts.openid;
|
|
133
|
+
const res = await fetch(url, {
|
|
134
|
+
method: "POST",
|
|
135
|
+
headers: { "content-type": "application/json" },
|
|
136
|
+
body: JSON.stringify(body)
|
|
137
|
+
});
|
|
138
|
+
let json;
|
|
139
|
+
try {
|
|
140
|
+
json = await res.json();
|
|
141
|
+
} catch {
|
|
142
|
+
throw new HttpError(400, i18n.t("phone_number_failed", {
|
|
143
|
+
errcode: String(res.status),
|
|
144
|
+
errmsg: i18n.t("invalid_json")
|
|
145
|
+
}));
|
|
146
|
+
}
|
|
147
|
+
this.assertWechatOk(json, "phone_number_failed");
|
|
148
|
+
const { phone_info } = await validate(json, { phone_info: {
|
|
149
|
+
phoneNumber: rule.string(),
|
|
150
|
+
purePhoneNumber: rule.string(),
|
|
151
|
+
countryCode: rule.string(),
|
|
152
|
+
watermark: {
|
|
153
|
+
timestamp: rule.int(),
|
|
154
|
+
appid: rule.string()
|
|
155
|
+
}
|
|
156
|
+
} });
|
|
157
|
+
return phone_info;
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* 带自动刷新的 `access_token` 调用包装:先取缓存 token 执行 `fn`;
|
|
162
|
+
* 若抛出 `HttpError` 且 `code` 为 `40001` / `42001`,则清缓存并重取后再执行一次。
|
|
163
|
+
* 业务响应优先用 {@link assertOk}。
|
|
164
|
+
*/
|
|
165
|
+
async withAccessToken(fn) {
|
|
166
|
+
try {
|
|
167
|
+
return await fn(await this.getAccessToken());
|
|
168
|
+
} catch (err) {
|
|
169
|
+
if (!this.isStaleAccessTokenError(err)) throw err;
|
|
170
|
+
await this.deleteAccessToken();
|
|
171
|
+
return fn(await this.getAccessToken());
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* 校验微信业务接口 JSON:`errcode` 非 0 时抛 `HttpError`(`code` = errcode,可被 {@link withAccessToken} 识别);
|
|
176
|
+
* 成功则剥掉 `errcode` / `errmsg`,返回剩余业务字段(可用泛型标注)。
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* const data = mini.assertOk<{ openlink: string }>(await res.json());
|
|
180
|
+
*/
|
|
181
|
+
assertOk(json) {
|
|
182
|
+
if (typeof json.errcode === "number" && json.errcode !== 0) throw new HttpError(400, json.errmsg || `errcode ${json.errcode}`, { code: String(json.errcode) });
|
|
183
|
+
const { errcode: _errcode, errmsg: _errmsg, ...rest } = json;
|
|
184
|
+
return rest;
|
|
185
|
+
}
|
|
186
|
+
/** 40001 invalid credential / 42001 access_token expired */
|
|
187
|
+
isStaleAccessTokenError(err) {
|
|
188
|
+
return HttpError.isError(err) && (err.code === "40001" || err.code === "42001");
|
|
189
|
+
}
|
|
190
|
+
async fetchWechatJson(url, errorKey) {
|
|
191
|
+
const res = await fetch(url);
|
|
192
|
+
try {
|
|
193
|
+
return await res.json();
|
|
194
|
+
} catch {
|
|
195
|
+
throw new HttpError(400, i18n.t(errorKey, {
|
|
196
|
+
errcode: String(res.status),
|
|
197
|
+
errmsg: i18n.t("invalid_json")
|
|
198
|
+
}));
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
assertWechatOk(json, errorKey) {
|
|
202
|
+
if (typeof json.errcode === "number" && json.errcode !== 0) throw new HttpError(400, i18n.t(errorKey, {
|
|
203
|
+
errcode: String(json.errcode),
|
|
204
|
+
errmsg: json.errmsg ?? ""
|
|
205
|
+
}), { code: String(json.errcode) });
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
export { WechatMiniProgramLoginClient };
|
|
209
|
+
|
|
210
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/i18n/locales/zh-cn.ts","../src/i18n/locales/en-us.ts","../src/i18n/index.ts","../src/client.ts"],"sourcesContent":["import { I18n } from '@aomex/common';\n\nexport const zh = I18n.define({\n app_id_empty: 'appId 不能为空',\n app_secret_empty: 'appSecret 不能为空',\n store_empty: 'store 不能为空',\n js_code_empty: 'jsCode 不能为空',\n phone_code_empty: 'phoneCode 不能为空',\n code2session_failed: '微信 code2session 失败:{{errcode}} {{errmsg}}',\n access_token_failed: '微信获取 access_token 失败:{{errcode}} {{errmsg}}',\n phone_number_failed: '微信获取手机号失败:{{errcode}} {{errmsg}}',\n invalid_json: '响应不是有效 JSON',\n});\n","import { I18n } from '@aomex/common';\nimport { zh } from './zh-cn';\n\nexport const en = I18n.satisfies(zh).define({\n app_id_empty: 'appId must not be empty',\n app_secret_empty: 'appSecret must not be empty',\n store_empty: 'store must not be empty',\n js_code_empty: 'jsCode must not be empty',\n phone_code_empty: 'phoneCode must not be empty',\n code2session_failed: 'WeChat code2session failed: {{errcode}} {{errmsg}}',\n access_token_failed: 'WeChat get access_token failed: {{errcode}} {{errmsg}}',\n phone_number_failed: 'WeChat get phone number failed: {{errcode}} {{errmsg}}',\n invalid_json: 'response is not valid JSON',\n});\n","import { createI18n } from '@aomex/common';\nimport { en } from './locales/en-us';\nimport { zh } from './locales/zh-cn';\n\nexport const i18n = createI18n({\n zh,\n en,\n load: (file) => import(new URL(`./locales/${file}.js`, import.meta.url).href),\n});\n","import type { CacheAdapter } from '@aomex/cache';\nimport { Caching } from '@aomex/cache';\nimport { registerLifecycle, rule, validate } from '@aomex/common';\nimport { HttpError } from '@aomex/web';\nimport { i18n } from './i18n';\nimport type {\n WechatMiniProgramLoginOptions,\n WechatMiniProgramPhoneInfo,\n WechatMiniProgramSession,\n} from './types';\n\nconst CODE2SESSION_URL = 'https://api.weixin.qq.com/sns/jscode2session';\nconst ACCESS_TOKEN_URL = 'https://api.weixin.qq.com/cgi-bin/token';\nconst GET_PHONE_NUMBER_URL = 'https://api.weixin.qq.com/wxa/business/getuserphonenumber';\n\n/**\n * 相对微信返回的 `expires_in` 提前失效的秒数,避免边界过期。\n * 实际缓存 TTL = max(60, expires_in - TOKEN_SKEW_SEC);\n * 微信默认 expires_in=7200,故常见 TTL 为 6900 秒(约 1h55m)。\n */\nconst TOKEN_SKEW_SEC = 300;\n\ntype WechatErrorBody = {\n errcode?: number;\n errmsg?: string;\n};\n\ntype AccessTokenCache = {\n access_token: string;\n};\n\n/**\n * 微信小程序 / 小游戏登录与身份相关能力(**不是 OAuth2**)。\n * - `code2Session`:`wx.login` 的 code → openid / session_key\n * - `getAccessToken`:全局接口调用凭证(经 `store` 缓存)\n * - `getPhoneNumber`:新版手机号 code → 手机号(内部用 access_token)\n * @see https://developers.weixin.qq.com/miniprogram/dev/server/API/user-login/api_code2session.html\n */\nexport class WechatMiniProgramLoginClient {\n protected readonly appId: string;\n protected readonly appSecret: string;\n protected readonly store: CacheAdapter;\n protected readonly caching: Caching;\n protected readonly tokenCacheKey: string;\n\n constructor(opts: WechatMiniProgramLoginOptions) {\n if (!opts.appId) {\n throw new Error(i18n.t('app_id_empty'));\n }\n if (!opts.appSecret) {\n throw new Error(i18n.t('app_secret_empty'));\n }\n if (!opts.store) {\n throw new Error(i18n.t('store_empty'));\n }\n this.appId = opts.appId;\n this.appSecret = opts.appSecret;\n this.store = opts.store;\n this.caching = new Caching(opts.store);\n this.tokenCacheKey = `wxmini:access_token:${opts.appId}`;\n\n void registerLifecycle({\n onInit: () => this.store.connect(),\n onDestroy: () => this.store.disconnect(),\n });\n }\n\n /**\n * 用 `wx.login` 得到的 `js_code` 换取 openid / session_key(及可选 unionid)。\n * `session_key` 仅用于开放数据解密 / 验签,新版手机号不依赖它;勿下发前端。\n */\n async code2Session(jsCode: string): Promise<WechatMiniProgramSession> {\n if (!jsCode) {\n throw new Error(i18n.t('js_code_empty'));\n }\n\n const url = new URL(CODE2SESSION_URL);\n url.searchParams.set('appid', this.appId);\n url.searchParams.set('secret', this.appSecret);\n url.searchParams.set('js_code', jsCode);\n url.searchParams.set('grant_type', 'authorization_code');\n\n const json = await this.fetchWechatJson(url, 'code2session_failed');\n this.assertWechatOk(json, 'code2session_failed');\n\n return validate(json, {\n openid: rule.string(),\n session_key: rule.string(),\n unionid: rule.string().optional(),\n });\n }\n\n /**\n * 获取小程序全局 `access_token`(经构造时的 `store` 缓存)。\n * TTL 为 `max(60, expires_in - 300)` 秒(微信默认 expires_in=7200 → 约 6900 秒)。\n * 未命中时用 `singleFlight`,避免并发多次向微信换票(新票会使旧票失效)。\n */\n async getAccessToken(): Promise<string> {\n const cached = await this.caching.get<AccessTokenCache>(this.tokenCacheKey);\n if (cached?.access_token) {\n return cached.access_token;\n }\n\n return this.caching.singleFlight(this.tokenCacheKey, async () => {\n const again = await this.caching.get<AccessTokenCache>(this.tokenCacheKey);\n if (again?.access_token) {\n return again.access_token;\n }\n\n const url = new URL(ACCESS_TOKEN_URL);\n url.searchParams.set('grant_type', 'client_credential');\n url.searchParams.set('appid', this.appId);\n url.searchParams.set('secret', this.appSecret);\n\n const json = await this.fetchWechatJson(url, 'access_token_failed');\n this.assertWechatOk(json, 'access_token_failed');\n\n const { access_token, expires_in } = await validate(json, {\n access_token: rule.string(),\n expires_in: rule.int().min(1).optional(),\n });\n\n const expiresIn = expires_in ?? 7200;\n const ttlSec = Math.max(60, expiresIn - TOKEN_SKEW_SEC);\n await this.caching.set(\n this.tokenCacheKey,\n { access_token } satisfies AccessTokenCache,\n ttlSec * 1000,\n );\n\n return access_token;\n });\n }\n\n /** 删除本应用本地缓存的 `access_token`(不会调用微信作废接口) */\n async deleteAccessToken(): Promise<void> {\n await this.caching.delete(this.tokenCacheKey);\n }\n\n /**\n * 用 `getPhoneNumber` 按钮回调的 code 换取手机号(新版接口,无需 session_key)。\n * 经 {@link withAccessToken}:缓存 token 失效时会自动删除并重取后再调一次。\n * @param phoneCode `e.detail.code`,与 `wx.login` 的 code 不同,不可混用\n * @param opts.openid 可选;填入后微信会校验与 code 的绑定关系\n */\n async getPhoneNumber(\n phoneCode: string,\n opts?: { openid?: string },\n ): Promise<WechatMiniProgramPhoneInfo> {\n if (!phoneCode) {\n throw new Error(i18n.t('phone_code_empty'));\n }\n\n return this.withAccessToken(async (accessToken) => {\n const url = new URL(GET_PHONE_NUMBER_URL);\n url.searchParams.set('access_token', accessToken);\n\n const body: Record<string, string> = { code: phoneCode };\n if (opts?.openid) {\n body['openid'] = opts.openid;\n }\n\n const res = await fetch(url, {\n method: 'POST',\n headers: { 'content-type': 'application/json' },\n body: JSON.stringify(body),\n });\n\n let json: Record<string, unknown> & WechatErrorBody;\n try {\n json = (await res.json()) as typeof json;\n } catch {\n throw new HttpError(\n 400,\n i18n.t('phone_number_failed', {\n errcode: String(res.status),\n errmsg: i18n.t('invalid_json'),\n }),\n );\n }\n\n this.assertWechatOk(json, 'phone_number_failed');\n\n const { phone_info } = await validate(json, {\n phone_info: {\n phoneNumber: rule.string(),\n purePhoneNumber: rule.string(),\n countryCode: rule.string(),\n watermark: {\n timestamp: rule.int(),\n appid: rule.string(),\n },\n },\n });\n\n return phone_info;\n });\n }\n\n /**\n * 带自动刷新的 `access_token` 调用包装:先取缓存 token 执行 `fn`;\n * 若抛出 `HttpError` 且 `code` 为 `40001` / `42001`,则清缓存并重取后再执行一次。\n * 业务响应优先用 {@link assertOk}。\n */\n async withAccessToken<T>(fn: (accessToken: string) => Promise<T>): Promise<T> {\n try {\n return await fn(await this.getAccessToken());\n } catch (err) {\n if (!this.isStaleAccessTokenError(err)) {\n throw err;\n }\n await this.deleteAccessToken();\n return fn(await this.getAccessToken());\n }\n }\n\n /**\n * 校验微信业务接口 JSON:`errcode` 非 0 时抛 `HttpError`(`code` = errcode,可被 {@link withAccessToken} 识别);\n * 成功则剥掉 `errcode` / `errmsg`,返回剩余业务字段(可用泛型标注)。\n *\n * @example\n * const data = mini.assertOk<{ openlink: string }>(await res.json());\n */\n assertOk<T extends Record<string, unknown> = Record<string, unknown>>(\n json: WechatErrorBody & T,\n ): T {\n if (typeof json.errcode === 'number' && json.errcode !== 0) {\n throw new HttpError(400, json.errmsg || `errcode ${json.errcode}`, {\n code: String(json.errcode),\n });\n }\n const { errcode: _errcode, errmsg: _errmsg, ...rest } = json;\n return rest as T;\n }\n\n /** 40001 invalid credential / 42001 access_token expired */\n protected isStaleAccessTokenError(err: unknown): boolean {\n return HttpError.isError(err) && (err.code === '40001' || err.code === '42001');\n }\n\n protected async fetchWechatJson(\n url: URL,\n errorKey: 'code2session_failed' | 'access_token_failed' | 'phone_number_failed',\n ): Promise<Record<string, unknown> & WechatErrorBody> {\n const res = await fetch(url);\n try {\n return (await res.json()) as Record<string, unknown> & WechatErrorBody;\n } catch {\n throw new HttpError(\n 400,\n i18n.t(errorKey, {\n errcode: String(res.status),\n errmsg: i18n.t('invalid_json'),\n }),\n );\n }\n }\n\n protected assertWechatOk(\n json: WechatErrorBody,\n errorKey: 'code2session_failed' | 'access_token_failed' | 'phone_number_failed',\n ): void {\n if (typeof json.errcode === 'number' && json.errcode !== 0) {\n throw new HttpError(\n 400,\n i18n.t(errorKey, {\n errcode: String(json.errcode),\n errmsg: json.errmsg ?? '',\n }),\n { code: String(json.errcode) },\n );\n }\n }\n}\n"],"mappings":"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"}
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@aomex-contrib/wechat-mini-program-login",
|
|
3
|
+
"version": "0.0.0-next-20261122045441",
|
|
4
|
+
"description": "微信小程序 / 小游戏登录与手机号(jscode2session + access_token + getPhoneNumber,非 OAuth2)",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git@github.com:aomex/aomex-contrib.git"
|
|
8
|
+
},
|
|
9
|
+
"homepage": "https://aomex.js.org",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"type": "module",
|
|
12
|
+
"sideEffects": false,
|
|
13
|
+
"types": "dist/index.d.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"import": "./dist/index.js"
|
|
18
|
+
},
|
|
19
|
+
"./package.json": "./package.json"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist",
|
|
23
|
+
"LICENSE",
|
|
24
|
+
"package.json",
|
|
25
|
+
"README.md",
|
|
26
|
+
"CHANGELOG.md"
|
|
27
|
+
],
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"@aomex/cache": "^4.4.0",
|
|
33
|
+
"@aomex/common": "^4.4.0",
|
|
34
|
+
"@aomex/web": "^4.4.0"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@aomex/cache": "^4.4.0",
|
|
38
|
+
"@aomex/common": "^4.4.0",
|
|
39
|
+
"@aomex/web": "^4.4.0"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {}
|
|
42
|
+
}
|