@chenminai/cloud-shared 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.
Files changed (2) hide show
  1. package/auth.js +13 -8
  2. package/package.json +2 -2
package/auth.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * 云函数用户身份解析工具
3
3
  *
4
- * 统一处理小程序(openid → cbAuthUid)和 Web 端(_webUserId / _cbAuthUid)两种调用方式。
4
+ * 统一处理小程序(openid → cbAuthUid)和 Web (_cbAuthUid)两种调用方式。
5
5
  * 在每个需要用户身份的云函数中 require 此模块,避免重复代码。
6
6
  */
7
7
 
@@ -14,26 +14,31 @@ cloud.init({
14
14
  const db = cloud.database()
15
15
 
16
16
  /**
17
- * 解析调用方的 cbAuthUid
18
- * - 小程序调用:通过 wxContext.OPENID 查 users 表取 cbAuthUid
19
- * - Web 端调用:直接使用 params._cbAuthUid(或兼容旧字段 _webUserId)
17
+ * 解析调用方的 cbAuthUid,优先级从高到低:
18
+ *
19
+ * 1. params._cbAuthUid —— callFunctionWithAuth() 自动注入,新版小程序和 Web BFF 均走此路径,无需查库。
20
+ * 2. wxContext.OPENID —— 降级路径:未注入 _cbAuthUid 时,
21
+ * 通过微信服务端可信的 OPENID 查库取得 cbAuthUid。
20
22
  *
21
23
  * @param {object} wxContext cloud.getWXContext() 的返回值
22
24
  * @param {object} params event 解析后的参数对象
23
25
  * @returns {Promise<string|null>} cbAuthUid,或 null(无法识别身份)
24
26
  */
25
27
  async function resolveUserId(wxContext, params) {
28
+ // 优先使用注入字段,避免数据库查询
29
+ if (params._cbAuthUid) return params._cbAuthUid
30
+
31
+ // 降级:通过 OPENID 查库
26
32
  if (wxContext.OPENID) {
27
33
  const result = await db.collection('users')
28
34
  .where({ openid: wxContext.OPENID })
29
35
  .field({ cbAuthUid: true })
30
36
  .limit(1)
31
37
  .get()
32
- const cbAuthUid = result.data[0]?.cbAuthUid
33
- return cbAuthUid ?? null
38
+ return result.data[0]?.cbAuthUid ?? null
34
39
  }
35
- // Web 端:优先 _cbAuthUid,兼容旧字段 _webUserId
36
- return params._cbAuthUid || params._webUserId || null
40
+
41
+ return null
37
42
  }
38
43
 
39
44
  module.exports = { resolveUserId }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chenminai/cloud-shared",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
4
4
  "description": "云函数共享工具:用户身份解析等",
5
5
  "main": "index.js",
6
6
  "publishConfig": {
@@ -10,4 +10,4 @@
10
10
  "wx-server-sdk": "~2.6.3",
11
11
  "@cloudbase/node-sdk": "^3.18.1"
12
12
  }
13
- }
13
+ }