feat: 更新 replace-team-qrcode-url 脚本以支持指定的 corpId 处理和过滤逻辑

This commit is contained in:
Jafeng 2026-04-24 10:46:01 +08:00
parent 562655bdea
commit 262f4f4232

View File

@ -94,6 +94,7 @@ function createDefaultOptions() {
mongoPass: "", mongoPass: "",
mongoAuthDb: "", mongoAuthDb: "",
corpId: "", corpId: "",
corpIdSpecified: false,
oldCorpId: "", oldCorpId: "",
newCorpId: "", newCorpId: "",
oldPrefix: "https://www.youcan365.com/h5/#/", oldPrefix: "https://www.youcan365.com/h5/#/",
@ -124,7 +125,10 @@ function parseArgs(argv) {
else if (key === "mongouser") opts.mongoUser = value; else if (key === "mongouser") opts.mongoUser = value;
else if (key === "mongopass") opts.mongoPass = value; else if (key === "mongopass") opts.mongoPass = value;
else if (key === "mongoauthdb") opts.mongoAuthDb = 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 === "oldcorpid" || key === "sourcecorpid" || key === "targetcorpid") opts.oldCorpId = value;
else if (key === "newcorpid" || key === "tokencorpid") opts.newCorpId = value; else if (key === "newcorpid" || key === "tokencorpid") opts.newCorpId = value;
else if (key === "oldprefix") opts.oldPrefix = value; else if (key === "oldprefix") opts.oldPrefix = value;
@ -182,6 +186,7 @@ function parseArgs(argv) {
i++; i++;
} else if (a === "--corpId") { } else if (a === "--corpId") {
opts.corpId = takeValue(i); opts.corpId = takeValue(i);
opts.corpIdSpecified = true;
i++; i++;
} else if (a === "--oldCorpId" || a === "--sourceCorpId" || a === "--targetCorpId") { } else if (a === "--oldCorpId" || a === "--sourceCorpId" || a === "--targetCorpId") {
opts.oldCorpId = takeValue(i); 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; 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) { function die(msg) {
console.error(msg); console.error(msg);
process.exit(1); process.exit(1);
@ -502,7 +542,7 @@ async function main() {
可选参数 可选参数
--dbName <db> 数据库名默认 corp --dbName <db> 数据库名默认 corp
--collection <name> collection 默认 team --collection <name> collection 默认 team
--corpId <corpId> 仅处理指定机构 --corpId <corpId> 仅处理指定机构未传时默认同时兼容 old/new corpId
--oldCorpId <corpId> URL 参数中的旧 corpId默认取 OLD_CORP_ID / SOURCE_CORP_ID / TARGET_CORP_ID --oldCorpId <corpId> URL 参数中的旧 corpId默认取 OLD_CORP_ID / SOURCE_CORP_ID / TARGET_CORP_ID
--newCorpId <corpId> URL 参数中的新 corpId默认取 NEW_CORP_ID / TOKEN_CORP_ID --newCorpId <corpId> URL 参数中的新 corpId默认取 NEW_CORP_ID / TOKEN_CORP_ID
--oldPrefix <url> 旧前缀默认 https://www.youcan365.com/h5/#/ --oldPrefix <url> 旧前缀默认 https://www.youcan365.com/h5/#/
@ -538,11 +578,11 @@ async function main() {
const db = client.db(opts.dbName); const db = client.db(opts.dbName);
const teamCol = db.collection(opts.collection); const teamCol = db.collection(opts.collection);
const staffCol = db.collection(opts.staffCollection); const staffCol = db.collection(opts.staffCollection);
const filter = {}; const filterCorpIds = getFilterCorpIds(opts);
if (isNonEmptyString(opts.corpId)) filter.corpId = opts.corpId; const filter = buildCorpFilter(filterCorpIds);
console.log( 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({ const teamStats = await processCollectionDocs({
collection: teamCol, collection: teamCol,
@ -565,7 +605,7 @@ async function main() {
}); });
console.log( 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({ const staffStats = await processCollectionDocs({
collection: staffCol, collection: staffCol,