From 429513ad8d05857b6c6c8aaf575af0b2fa9c10f9 Mon Sep 17 00:00:00 2001 From: b2894lxlx <517289602@qq.com> Date: Mon, 19 Oct 2026 14:19:27 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=99=BB=E5=BD=95=E5=89=8D=E8=A1=A5?= =?UTF-8?q?=E9=BD=90=E7=A9=BA=E7=9A=84=20dcloud=5Fappid=EF=BC=8C=E9=81=BF?= =?UTF-8?q?=E5=85=8D=E8=A2=AB=E5=88=A4=E6=88=90=E7=94=A8=E6=88=B7=E4=B8=8D?= =?UTF-8?q?=E5=AD=98=E5=9C=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Cursor --- .../middleware/modules/fixEmptyDcloudAppid.js | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 uniCloud-tcb/cloudfunctions/router/middleware/modules/fixEmptyDcloudAppid.js diff --git a/uniCloud-tcb/cloudfunctions/router/middleware/modules/fixEmptyDcloudAppid.js b/uniCloud-tcb/cloudfunctions/router/middleware/modules/fixEmptyDcloudAppid.js new file mode 100644 index 0000000..cf75247 --- /dev/null +++ b/uniCloud-tcb/cloudfunctions/router/middleware/modules/fixEmptyDcloudAppid.js @@ -0,0 +1,49 @@ +/** + * 登录前修复 dcloud_appid 为空数组的用户 + * uni-id 只放行「字段不存在」或「数组包含当前 appid」的账号,空数组会被当成用户不存在 + */ + +const LOGIN_APP_IDS = ["__UNI__EB80D26", "__UNI__8BFD66E", "__UNI__0F0703D"]; + +function normalizeAccount(value) { + if (typeof value === "number") return String(value).trim(); + if (typeof value === "string") return value.trim(); + return ""; +} + +module.exports = [{ + id: "fixEmptyDcloudAppid", + regExp: [ + "^user/pub/login(.*)" + ], + description: "登录前把 dcloud_appid 为空数组的用户补上三个应用的 appid", + index: 201, + mode: "onActionExecuting", + enable: true, + main: async function(event) { + let { data = {}, util } = event; + let { db, _ } = util; + const username = normalizeAccount(data.username); + const mobile = normalizeAccount(data.mobile); + const email = normalizeAccount(data.email); + const whereList = []; + if (username) { + whereList.push({ username }, { mobile: username }, { email: username }); + } + if (mobile && mobile !== username) whereList.push({ mobile }); + if (email && email !== username) whereList.push({ email }); + if (whereList.length === 0) return { code: 0 }; + + const whereJson = whereList.length === 1 ? whereList[0] : _.or(whereList); + const userRes = await db.collection("uni-id-users").where(whereJson).field({ + dcloud_appid: true + }).limit(10).get(); + const users = (userRes.data || []).filter(user => Array.isArray(user.dcloud_appid) && user.dcloud_appid.length === 0); + for (let i = 0; i < users.length; i++) { + await db.collection("uni-id-users").doc(users[i]._id).update({ + dcloud_appid: LOGIN_APP_IDS + }); + } + return { code: 0 }; + } +}]