From 262f4f423214141a25d3ffa45b5425e0a8b9cf04 Mon Sep 17 00:00:00 2001 From: Jafeng <2998840497@qq.com> Date: Fri, 24 Apr 2026 10:46:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=20replace-team-qrcod?= =?UTF-8?q?e-url=20=E8=84=9A=E6=9C=AC=E4=BB=A5=E6=94=AF=E6=8C=81=E6=8C=87?= =?UTF-8?q?=E5=AE=9A=E7=9A=84=20corpId=20=E5=A4=84=E7=90=86=E5=92=8C?= =?UTF-8?q?=E8=BF=87=E6=BB=A4=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/replace-team-qrcode-url.js | 52 ++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/scripts/replace-team-qrcode-url.js b/scripts/replace-team-qrcode-url.js index 0a86886..0328b37 100644 --- a/scripts/replace-team-qrcode-url.js +++ b/scripts/replace-team-qrcode-url.js @@ -94,6 +94,7 @@ function createDefaultOptions() { mongoPass: "", mongoAuthDb: "", corpId: "", + corpIdSpecified: false, oldCorpId: "", newCorpId: "", oldPrefix: "https://www.youcan365.com/h5/#/", @@ -124,7 +125,10 @@ function parseArgs(argv) { else if (key === "mongouser") opts.mongoUser = value; else if (key === "mongopass") opts.mongoPass = value; else if (key === "mongoauthdb") opts.mongoAuthDb = value; - else if (key === "corpid") opts.corpId = value; + else if (key === "corpid") { + opts.corpId = value; + opts.corpIdSpecified = true; + } else if (key === "oldcorpid" || key === "sourcecorpid" || key === "targetcorpid") opts.oldCorpId = value; else if (key === "newcorpid" || key === "tokencorpid") opts.newCorpId = value; else if (key === "oldprefix") opts.oldPrefix = value; @@ -182,6 +186,7 @@ function parseArgs(argv) { i++; } else if (a === "--corpId") { opts.corpId = takeValue(i); + opts.corpIdSpecified = true; i++; } else if (a === "--oldCorpId" || a === "--sourceCorpId" || a === "--targetCorpId") { opts.oldCorpId = takeValue(i); @@ -224,6 +229,41 @@ function applyEnvFallbacks(opts) { if (!opts.newPrefix && process.env.NEW_QRCODE_PREFIX) opts.newPrefix = process.env.NEW_QRCODE_PREFIX; } +function uniqueNonEmptyStrings(values) { + const seen = new Set(); + const result = []; + for (const value of values) { + if (!isNonEmptyString(value)) continue; + const key = value.trim(); + if (seen.has(key)) continue; + seen.add(key); + result.push(key); + } + return result; +} + +function getFilterCorpIds(opts) { + if (opts.corpIdSpecified) { + return uniqueNonEmptyStrings([opts.corpId]); + } + return uniqueNonEmptyStrings([opts.corpId, opts.oldCorpId, opts.newCorpId]); +} + +function buildCorpFilter(corpIds) { + if (!Array.isArray(corpIds) || corpIds.length === 0) { + return {}; + } + if (corpIds.length === 1) { + const [corpId] = corpIds; + return { + $or: [{ corpId }, { corpid: corpId }, { corp_id: corpId }], + }; + } + return { + $or: [{ corpId: { $in: corpIds } }, { corpid: { $in: corpIds } }, { corp_id: { $in: corpIds } }], + }; +} + function die(msg) { console.error(msg); process.exit(1); @@ -502,7 +542,7 @@ async function main() { 可选参数: --dbName 数据库名,默认 corp --collection collection 名,默认 team - --corpId 仅处理指定机构 + --corpId 仅处理指定机构;未传时默认同时兼容 old/new corpId --oldCorpId URL 参数中的旧 corpId,默认取 OLD_CORP_ID / SOURCE_CORP_ID / TARGET_CORP_ID --newCorpId URL 参数中的新 corpId,默认取 NEW_CORP_ID / TOKEN_CORP_ID --oldPrefix 旧前缀,默认 https://www.youcan365.com/h5/#/ @@ -538,11 +578,11 @@ async function main() { const db = client.db(opts.dbName); const teamCol = db.collection(opts.collection); const staffCol = db.collection(opts.staffCollection); - const filter = {}; - if (isNonEmptyString(opts.corpId)) filter.corpId = opts.corpId; + const filterCorpIds = getFilterCorpIds(opts); + const filter = buildCorpFilter(filterCorpIds); console.log( - `[step] collection=${opts.dbName}.${opts.collection} corpId=${opts.corpId || "全部"} mode=${opts.dryRun ? "dry-run" : "apply"}` + `[step] collection=${opts.dbName}.${opts.collection} corpIds=${filterCorpIds.length ? filterCorpIds.join(",") : "全部"} mode=${opts.dryRun ? "dry-run" : "apply"}` ); const teamStats = await processCollectionDocs({ collection: teamCol, @@ -565,7 +605,7 @@ async function main() { }); console.log( - `[step] collection=${opts.dbName}.${opts.staffCollection} corpId=${opts.corpId || "全部"} mode=${opts.dryRun ? "dry-run" : "apply"}` + `[step] collection=${opts.dbName}.${opts.staffCollection} corpIds=${filterCorpIds.length ? filterCorpIds.join(",") : "全部"} mode=${opts.dryRun ? "dry-run" : "apply"}` ); const staffStats = await processCollectionDocs({ collection: staffCol,