bjchy-udg-sdk 1.6.0 → 1.7.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.
Files changed (3) hide show
  1. package/README.md +18 -1
  2. package/package.json +1 -1
  3. package/src/auth.js +30 -4
package/README.md CHANGED
@@ -63,6 +63,8 @@ const { token, openId } = await client.login() // openId 同时写入 JWT;
63
63
 
64
64
  // ①b web-view 内的 H5:拿不到 wx.login,由宿主小程序传 code 进来(详见下方「web-view 内 H5 的登录」)
65
65
  const { token, openId } = await client.login({ code })
66
+ // 目标租户关联了多个小程序时,还要带上宿主小程序的 appId(见下方「一个租户关联多个小程序」)
67
+ const r = await client.login({ code, miniAppId: 'wx8c6e005797744382' })
66
68
 
67
69
  // ①c 公众号内打开的 H5:微信网页授权回调把 code 带回本页(详见下方「公众号 H5 的登录与 JSSDK」)
68
70
  const { token, openId } = await client.login({ mpCode })
@@ -119,7 +121,7 @@ await client.login({ code: new URLSearchParams(location.search).get('c') })
119
121
  注意事项:
120
122
 
121
123
  - **不要改传 token**:URL 里的长效 JWT 在有效期内可被完全冒充,且 `init({ token })` 是 external 模式,SDK 不为它续期、过期即失效。
122
- - **`code` 由哪个小程序签发,该租户就要配同一个小程序**:网关拿 `appid` 查到租户配置的 `miniAppId`(管理后台配置,未配置则沿用 `appid` 本身)去调微信 `jscode2session`,`miniAppId` 与 `code` 的签发方不一致会拿到 `40013 invalid appid`。这一步是服务端配置,SDK/客户端参数不变,也不能传 `miniAppId` 覆盖(服务端强制解析,防止客户端冒领身份)。
124
+ - **`code` 由哪个小程序签发,该租户就要关联同一个小程序**:网关拿 `appid` 查到该租户关联的小程序(管理后台配置,未配置则沿用 `appid` 本身)去调微信 `jscode2session`,与 `code` 的签发方不一致会拿到 `40013 invalid appid`。租户只关联一个小程序时,SDK/客户端参数不变;关联多个时才需要 `login({ code, miniAppId })` 指明是哪一个(见下节)。
123
125
  - **页面刷新**:`login()` 幂等,缓存 token 未过期时直接复用,不会再用已废的 code;前提是 H5 每次 `init` 传的 `appid` 一致(token 存储键依赖它)。
124
126
  - **refreshToken 也过期时**:H5 拿不到新 code,SDK 的自动重登会返回失败并抛鉴权错误(这是预期行为)。业务兜底:`wx.miniProgram.redirectTo` 回中转页重取 code。注意 `wx.miniProgram.postMessage` **不是实时的**(小程序只在分享/复制链接/后退/组件销毁时才收到),不能用它通知小程序。
125
127
  - **CORS**:H5 域名需在 Gateway 的跨域白名单里。
@@ -261,6 +263,21 @@ wx.error((res) => {
261
263
 
262
264
  ⚠️ **小程序 openid 是按微信小程序(`miniAppId`)划分的,不是按 UDG 租户划分**:多个 UDG 租户若配了**同一个** `miniAppId`,同一个微信用户在这些租户里换回的 openid 是**同一个值**(可用于跨租户按 openid 关联同一自然人);配**不同** `miniAppId` 则 openid 不同、不能跨租户直接对人。
263
265
 
266
+ ### 一个租户关联多个小程序
267
+
268
+ **一套小程序代码部署到 N 个 appId、但数据只有一份**时(多街道 / 多门店同款小程序),不必为每个小程序建一个租户:在管理后台的租户配置里把这 N 个小程序都勾进「其它小程序」,各自用自己的 `wx.login()` 登录到同一个租户。
269
+
270
+ ```js
271
+ const client = udg.init({ endpoint, appid: 'wxxcx-oracle' }) // N 个小程序共用同一个租户 appid
272
+ await client.login() // SDK 自动带上当前小程序的真实 appId,网关据此选对 AppSecret
273
+ ```
274
+
275
+ - **小程序内**:`login()` 自动从 `wx.getAccountInfoSync()` 取当前 appId 上报,业务代码不用改,也不能自己传。
276
+ - **web-view 内的 H5**:拿不到 `wx.getAccountInfoSync`,要显式传 `login({ code, miniAppId })`(宿主小程序的 appId)。不传则网关只能用租户的默认小程序,与 code 签发方不一致时报 `40013`。
277
+ - 上报的 `miniAppId` **只能在该租户已关联的集合内选择**;传了没关联的会直接报 `appid=… 未关联小程序 …`,**不会**悄悄回落到默认小程序(这样"后台漏勾了一个"能一眼看出来,而不是表现成微信的 40013)。
278
+ - ⚠️ **这 N 个小程序之间 openid 不互通**(openid 按 `miniAppId` 划分)。同一自然人在各小程序里 `_openid` 不同,**不要**按 `_openid` 跨小程序关联到同一个人。需要跨端对人请用手机号等业务标识。
279
+ - 租户只关联一个小程序时,以上全部无感知,行为与之前完全一致。
280
+
264
281
  envId / mongoDatabase / AppSecret 由服务端从 `wxdb.config` 读取,客户端不传、不可传。
265
282
 
266
283
  **同一 appid 多数据源**(一个 client、一份 token,用 `dbKey` 切换;管理后台里租户的 `bindings[]` 把 `dbKey` 路由到数据源注册中心里的某个数据源,详见 `docs/datasource-registry-and-schema-dictionary.md`;`dbKey` 路由思路的历史版本见 `docs/archive/multi-database-dbkey.md`):
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bjchy-udg-sdk",
3
- "version": "1.6.0",
3
+ "version": "1.7.0",
4
4
  "description": "Universal Data Gateway SDK - 跨端统一数据访问(微信小程序 / Web / Node.js),API 兼容微信云开发",
5
5
  "main": "src/index.js",
6
6
  "type": "commonjs",
package/src/auth.js CHANGED
@@ -61,6 +61,23 @@ function wxLoginCode() {
61
61
  })
62
62
  }
63
63
 
64
+ /**
65
+ * 当前小程序的真实 appId(取自运行环境,业务不可传)。
66
+ * 供网关在「一个租户关联多个小程序」时选中对应的 AppSecret;
67
+ * 老网关会忽略这个字段,因此可以安全地始终携带。
68
+ */
69
+ function currentMiniAppId() {
70
+ try {
71
+ // eslint-disable-next-line no-undef
72
+ if (typeof wx === 'undefined' || typeof wx.getAccountInfoSync !== 'function') return ''
73
+ // eslint-disable-next-line no-undef
74
+ const info = wx.getAccountInfoSync()
75
+ return (info && info.miniProgram && info.miniProgram.appId) || ''
76
+ } catch (e) {
77
+ return ''
78
+ }
79
+ }
80
+
64
81
  class AuthManager {
65
82
  /**
66
83
  * @param {object} ctx SDK 上下文 { endpoint, appid, ... }
@@ -136,8 +153,9 @@ class AuthManager {
136
153
 
137
154
  /**
138
155
  * 登录(幂等:缓存 token 未过期时直接复用,force 可强制重登)。
139
- * - login():小程序环境自动 wx.login
140
- * - login({ code }):用外部拿到的微信登录 code 换 token(web-view 内 H5:由宿主小程序 wx.login 后经 URL 传入)
156
+ * - login():小程序环境自动 wx.login(并自动上报当前小程序 appId 供网关选凭据)
157
+ * - login({ code, miniAppId? }):用外部拿到的微信登录 code 换 token(web-view 内 H5:由宿主小程序 wx.login 后经 URL 传入);
158
+ * 若目标租户关联了多个小程序,需一并传 miniAppId(宿主小程序的 appId),否则网关只能用默认项
141
159
  * - login({ mpCode }):公众号网页授权 code 换 token(公众号内打开的 H5,见 client.login 文档)
142
160
  * - login({ apiKey }):密钥登录
143
161
  */
@@ -162,7 +180,11 @@ class AuthManager {
162
180
  // 外部传入的微信 code(web-view 内 H5 等拿不到 wx.login 的环境);与小程序内登录同一端点,
163
181
  // 换回的是完整 token + refreshToken,因此后续可自动续期,不同于 external token 直入。
164
182
  if (o.code) {
165
- const data = await this.rawPost('/auth/wxlogin', { appid: this.ctx.appid, code: o.code })
183
+ const body = { appid: this.ctx.appid, code: o.code }
184
+ // web-view 内的 H5 拿不到 wx.getAccountInfoSync,租户关联多个小程序时
185
+ // 必须由业务把宿主小程序的 appId 显式传进来,否则网关只能用默认项(与 code 签发方不符会 40013)
186
+ if (o.miniAppId) body.miniAppId = o.miniAppId
187
+ const data = await this.rawPost('/auth/wxlogin', body)
166
188
  this.openId = data.openId
167
189
  this.setTokens(data.token, data.refreshToken, 'wechat')
168
190
  return { token: this.token, openId: data.openId, cached: false }
@@ -174,7 +196,11 @@ class AuthManager {
174
196
  }
175
197
  if (isMiniProgram()) {
176
198
  const code = await wxLoginCode()
177
- const data = await this.rawPost('/auth/wxlogin', { appid: this.ctx.appid, code })
199
+ const body = { appid: this.ctx.appid, code }
200
+ // 当前小程序 appId 取自运行环境(不开放为参数),网关据此在租户授权集合内选凭据
201
+ const mini = currentMiniAppId()
202
+ if (mini) body.miniAppId = mini
203
+ const data = await this.rawPost('/auth/wxlogin', body)
178
204
  this.openId = data.openId
179
205
  this.setTokens(data.token, data.refreshToken, 'wechat')
180
206
  return { token: this.token, openId: data.openId, cached: false }