使用外部数据库
This commit is contained in:
71
utils.js
Normal file
71
utils.js
Normal file
@@ -0,0 +1,71 @@
|
||||
const NOCODB_CONFIG = {
|
||||
// Token and URL are loaded from env.js (ENV_SECRETS global variable)
|
||||
TOKEN: (typeof ENV_SECRETS !== 'undefined' && ENV_SECRETS.NOCODB_TOKEN) ? ENV_SECRETS.NOCODB_TOKEN : "",
|
||||
LIST_URL: (typeof ENV_SECRETS !== 'undefined' && ENV_SECRETS.NOCODB_LIST_URL) ? ENV_SECRETS.NOCODB_LIST_URL : ""
|
||||
};
|
||||
|
||||
/**
|
||||
* 从 NocoDB 获取所有别名记录并整理为键值对
|
||||
* @returns {Promise<Object>} { "账号ID": "别名", ... }
|
||||
*/
|
||||
async function fetchAliasesFromDB() {
|
||||
const listUrl = NOCODB_CONFIG.LIST_URL;
|
||||
const headers = {
|
||||
"xc-token": NOCODB_CONFIG.TOKEN,
|
||||
"Content-Type": "application/json"
|
||||
};
|
||||
|
||||
if (!listUrl || !NOCODB_CONFIG.TOKEN) {
|
||||
console.error("Teams Alias: 缺少配置 (URL 或 Token),请检查 .env 文件。");
|
||||
throw new Error("Missing configuration");
|
||||
}
|
||||
|
||||
let allAliases = {};
|
||||
let offset = 0;
|
||||
const limit = 1000;
|
||||
|
||||
try {
|
||||
while (true) {
|
||||
// 构建带参数的 URL
|
||||
const url = new URL(listUrl);
|
||||
url.searchParams.append("limit", limit);
|
||||
url.searchParams.append("fields", "账号ID,_最终输出");
|
||||
url.searchParams.append("offset", offset);
|
||||
|
||||
const response = await fetch(url.toString(), { headers });
|
||||
|
||||
if (!response.ok) {
|
||||
console.error(`获取数据失败: ${response.status} ${response.statusText}`);
|
||||
throw new Error("网络请求失败");
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
const rows = data.list || [];
|
||||
|
||||
if (rows.length === 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
// 整理数据
|
||||
rows.forEach(row => {
|
||||
if (row["账号ID"] && row["_最终输出"]) {
|
||||
// key = 账号ID, value = _最终输出
|
||||
allAliases[row["账号ID"]] = row["_最终输出"];
|
||||
}
|
||||
});
|
||||
|
||||
if (rows.length < limit) {
|
||||
break; // 已获取所有数据
|
||||
}
|
||||
|
||||
offset += limit;
|
||||
}
|
||||
|
||||
console.log(`同步完成,共获取 ${Object.keys(allAliases).length} 条别名记录。`);
|
||||
return allAliases;
|
||||
|
||||
} catch (error) {
|
||||
console.error("同步别名数据时出错:", error);
|
||||
throw error; // 向抛出以便调用者处理
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user