/** * 登录前修复 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 }; } }]