diff --git a/apps/webUI.js b/apps/webUI.js index 01e7822..c3467c1 100644 --- a/apps/webUI.js +++ b/apps/webUI.js @@ -2,6 +2,7 @@ import { REDIS_YUNZAI_WEBUI } from "../constants/constant.js"; import config from "../model/config.js"; import { constructPublicIPsMsg } from "../utils/network.js"; import { redisSetKey } from "../utils/redis-util.js"; +import { buildNextJs } from "../utils/start-nextjs.js"; import { getBotLoginInfo, getBotStatus, getBotVersionInfo, sendPrivateMsg } from "../utils/yunzai-util.js"; export class WebUI extends plugin { @@ -48,13 +49,15 @@ export class WebUI extends plugin { async rWebSwitch(e) { config.updateField("tools", "isOpenWebUI", !this.isOpenWebUI); const realIsOpenWebUI = config.getConfig("tools").isOpenWebUI; - // 初始化数据 - await this.initData(e, realIsOpenWebUI); - // 这里有点延迟,需要写反 - e.reply(`R插件可视化面板:${ realIsOpenWebUI ? "✅已开启" : "❌已关闭" },重启后生效`); if (realIsOpenWebUI) { + // 初始化数据 + await this.initData(e, realIsOpenWebUI); + e.reply(`R插件可视化面板:正在构建中,请稍等...`); + // 动态编译生产环境 + await buildNextJs(); await sendPrivateMsg(e, constructPublicIPsMsg()); } + e.reply(`R插件可视化面板:${ realIsOpenWebUI ? "✅已开启" : "❌已关闭" },重启后生效`); return true; } diff --git a/index.js b/index.js index 51d3779..cdc5be4 100644 --- a/index.js +++ b/index.js @@ -42,7 +42,7 @@ for (let i in files) { // 检查是否启动 webui if (isOpenWebUI) { - startNextJs('dev'); + startNextJs('start'); logger.info(constructPublicIPsMsg()); } diff --git a/package.json b/package.json index ed6ff48..0a5a716 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "dev": "cd server && next dev -p 4016", "dev6": "cd server && HOST=:: next dev -p 4016", "start": "cd server && next start -p 4016", + "start6": "cd server && HOST=:: next start -p 4016", "build": "cd server && next build" }, "dependencies": { diff --git a/server/app/r/api/network2/route.js b/server/app/r/api/network2/route.js index a3b5fd3..c8ef821 100644 --- a/server/app/r/api/network2/route.js +++ b/server/app/r/api/network2/route.js @@ -1,3 +1,4 @@ +import { unstable_noStore as noStore } from 'next/cache'; import { promises as fs } from 'fs'; import os from 'os'; import si from 'systeminformation'; @@ -85,6 +86,8 @@ async function getNetworkStats() { } export async function GET() { + // 这个不允许删除,否则无法做到实时获取 + noStore(); try { const stats = await getNetworkStats(); return new Response(JSON.stringify(stats), { diff --git a/server/app/r/api/system/route.js b/server/app/r/api/system/route.js index af08533..10b627d 100644 --- a/server/app/r/api/system/route.js +++ b/server/app/r/api/system/route.js @@ -1,7 +1,10 @@ +import { unstable_noStore as noStore } from 'next/cache'; import si from 'systeminformation'; import os from 'os'; export async function GET(request, { params }) { + // 这个不允许删除,否则无法做到实时获取 + noStore(); try { // 获取CPU信息 const cpuInfo = await si.cpu(); diff --git a/utils/start-nextjs.js b/utils/start-nextjs.js index 4703f76..9bd37fb 100644 --- a/utils/start-nextjs.js +++ b/utils/start-nextjs.js @@ -5,6 +5,28 @@ logger.mark(`[R插件][WebUI], 父进程 PID: ${process.pid}`); let nextjsProcess = null; +// 构建应用程序 +export const buildNextJs = () => { + logger.info(logger.yellow('[R插件][WebUI],正在构建 Next.js 应用...')); + return new Promise((resolve, reject) => { + const buildProcess = spawn('pnpm', ['run', 'build'], { + cwd: './plugins/rconsole-plugin/server', + stdio: 'ignore', + shell: true, + }); + + buildProcess.on('close', (code) => { + if (code === 0) { + logger.info(logger.yellow('[R插件][Next.js监测],构建完成。')); + resolve(); + } else { + logger.error(`[R插件][WebUI监测],构建失败,退出码:${code}`); + reject(new Error('Build failed')); + } + }); + }); +}; + // 启动子进程运行 Next.js export const startNextJs = (mode = 'start') => { let script = mode === 'start' ? 'start' : 'dev'; @@ -13,7 +35,7 @@ export const startNextJs = (mode = 'start') => { // 判断是不是只有ipv6地址 if (hasIPv6Only()) { - script = 'dev6'; + script = 'start6'; } nextjsProcess = spawn('pnpm', ['run', script], {