Add WebUI token update and callback handling

Introduces callback mechanisms for WebUI token changes and QQ login status updates. The WebUI token is now updated and communicated via a callback after login, and related runtime and type definitions are extended to support these features. Also sets a static default token value in the config schema.
This commit is contained in:
手瓜一十雪 committed 2025-09-06 18:15:35 +08:00
1 parent 88ee8f89fe
commit 5c120a8231
5 files changed
+60 -4

No files matched your search

+16 -2
View File
@@ -62,13 +62,13 @@ export async function InitWebUi(logger: LogWrapper, pathWrapper: NapCatPathWrapp
webUiPathWrapper = pathWrapper;
WebUiConfig = new WebUiConfigWrapper();
const config = await WebUiConfig.GetWebUIConfig();
// 检查是否禁用WebUI
if (config.disableWebUI) {
logger.log('[NapCat] [WebUi] WebUI is disabled by configuration.');
return;
}
const [host, port, token] = await InitPort(config);
webUiRuntimePort = port;
if (port == 0) {
@@ -90,6 +90,19 @@ export async function InitWebUi(logger: LogWrapper, pathWrapper: NapCatPathWrapp
}
}
});
WebUiDataRuntime.setQQLoginCallback(async (_status: boolean) => {
try {
if ((await WebUiConfig.GetWebUIConfig()).defaultToken) {
let randomToken = Math.random().toString(36).slice(-8);
await WebUiConfig.UpdateWebUIConfig({ token: randomToken });
console.log(`[NapCat] [WebUi] Update WebUi Token: ${randomToken}`);
await WebUiDataRuntime.getWebUiTokenChangeCallback()(randomToken);
}
} catch (error) {
console.log(`[NapCat] [WebUi] Update WebUi Token failed.` + error);
}
});
// ------------注册中间件------------
// 使用express的json中间件
app.use(express.json());
@@ -177,6 +190,7 @@ export async function InitWebUi(logger: LogWrapper, pathWrapper: NapCatPathWrapp
);
}
});
// ------------Over!------------
}