From 2ed83a0e303573b62020d2077617c33bd0dcc2dc Mon Sep 17 00:00:00 2001 From: bietiaop <1527109126@qq.com> Date: Mon, 16 Dec 2024 12:46:27 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9F=A5=E7=9C=8B=E7=99=BB=E5=BD=95QQ?= =?UTF-8?q?=E4=BF=A1=E6=81=AF&=E8=8E=B7=E5=8F=96=E5=BF=AB=E9=80=9F?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E5=88=97=E8=A1=A8=E8=AF=A6=E7=BB=86=E4=BF=A1?= =?UTF-8?q?=E6=81=AF&=E8=8E=B7=E5=8F=96nc=E7=9A=84=E5=8C=85=E4=BF=A1?= =?UTF-8?q?=E6=81=AF&=E4=BC=98=E5=8C=96=E4=BA=86=E9=83=A8=E5=88=86?= =?UTF-8?q?=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/onebot/index.ts | 8 ++--- src/shell/base.ts | 6 ++-- src/webui/src/api/Auth.ts | 2 +- src/webui/src/api/BaseInfo.ts | 14 +++----- src/webui/src/api/OB11Config.ts | 8 ++--- src/webui/src/api/QQLogin.ts | 26 ++++++++++---- src/webui/src/helper/Data.ts | 62 +++++++++++++++++++++++---------- src/webui/src/router/Base.ts | 8 +++++ src/webui/src/router/QQLogin.ts | 6 ++++ src/webui/src/router/index.ts | 3 ++ src/webui/src/types/data.d.ts | 5 +++ 11 files changed, 101 insertions(+), 47 deletions(-) create mode 100644 src/webui/src/router/Base.ts diff --git a/src/onebot/index.ts b/src/onebot/index.ts index 0c1f69512..8a4437315 100644 --- a/src/onebot/index.ts +++ b/src/onebot/index.ts @@ -157,9 +157,9 @@ export class NapCatOneBot11Adapter { this.initBuddyListener(); this.initGroupListener(); - await WebUiDataRuntime.setQQLoginUin(selfInfo.uin.toString()); - await WebUiDataRuntime.setQQLoginStatus(true); - await WebUiDataRuntime.setOnOB11ConfigChanged(async (newConfig) => { + WebUiDataRuntime.setQQLoginInfo(selfInfo); + WebUiDataRuntime.setQQLoginStatus(true); + WebUiDataRuntime.setOnOB11ConfigChanged(async (newConfig) => { const prev = this.configLoader.configData; this.configLoader.save(newConfig); this.context.logger.log(`OneBot11 配置更改:${JSON.stringify(prev)} -> ${JSON.stringify(newConfig)}`); @@ -207,7 +207,7 @@ export class NapCatOneBot11Adapter { } } } - // 通知新配置重载 删除关闭的 加入新开的 + // 通知新配置重载 删除关闭的 加入新开的 for (const adapterConfig of nowConfig) { const existingAdapter = this.networkManager.findSomeAdapter(adapterConfig.name); if (existingAdapter) { diff --git a/src/shell/base.ts b/src/shell/base.ts index abe2dc602..7c1d03c30 100644 --- a/src/shell/base.ts +++ b/src/shell/base.ts @@ -175,7 +175,9 @@ async function handleLogin( loginService.getLoginList().then((res) => { // 遍历 res.LocalLoginInfoList[x].isQuickLogin是否可以 res.LocalLoginInfoList[x].uin 转为string 加入string[] 最后遍历完成调用WebUiDataRuntime.setQQQuickLoginList - WebUiDataRuntime.setQQQuickLoginList(res.LocalLoginInfoList.filter((item) => item.isQuickLogin).map((item) => item.uin.toString())); + const list = res.LocalLoginInfoList.filter((item) => item.isQuickLogin); + WebUiDataRuntime.setQQQuickLoginList(list.map((item) => item.uin.toString())); + WebUiDataRuntime.setQQNewLoginList(list); }); WebUiDataRuntime.setQuickLoginCall(async (uin: string) => { @@ -285,7 +287,7 @@ export async function NCoreInitShell() { await initializeEngine(engine, basicInfoWrapper, dataPathGlobal, systemPlatform, systemVersion); await initializeLoginService(loginService, basicInfoWrapper, dataPathGlobal, systemVersion, hostname); - + program.option('-q, --qq [number]', 'QQ号').parse(process.argv); const cmdOptions = program.opts(); const quickLoginUin = cmdOptions.qq; diff --git a/src/webui/src/api/Auth.ts b/src/webui/src/api/Auth.ts index 62e68e827..370261a7d 100644 --- a/src/webui/src/api/Auth.ts +++ b/src/webui/src/api/Auth.ts @@ -18,7 +18,7 @@ export const LoginHandler: RequestHandler = async (req, res) => { return sendError(res, 'token is empty'); } // 检查登录频率 - if (!(await WebUiDataRuntime.checkLoginRate(WebUiConfigData.loginRate))) { + if (!WebUiDataRuntime.checkLoginRate(WebUiConfigData.loginRate)) { return sendError(res, 'login rate limit'); } //验证config.token是否等于token diff --git a/src/webui/src/api/BaseInfo.ts b/src/webui/src/api/BaseInfo.ts index acdcb40fe..e2043232f 100644 --- a/src/webui/src/api/BaseInfo.ts +++ b/src/webui/src/api/BaseInfo.ts @@ -1,15 +1,9 @@ import { RequestHandler } from 'express'; +import { WebUiDataRuntime } from '@webapi/helper/Data'; import { sendSuccess } from '@webapi/utils/response'; -// TODO: Implement LogFileListHandler -export const LogFileListHandler: RequestHandler = async (_, res) => { - const fakeData = { - uin: 0, - nick: 'NapCat', - avatar: 'https://q1.qlogo.cn/g?b=qq&nk=0&s=640', - status: 'online', - boottime: Date.now(), - }; - sendSuccess(res, fakeData); +export const PackageInfoHandler: RequestHandler = (_, res) => { + const data = WebUiDataRuntime.getPackageJson(); + sendSuccess(res, data); }; diff --git a/src/webui/src/api/OB11Config.ts b/src/webui/src/api/OB11Config.ts index 15fdc5d45..1fcad5221 100644 --- a/src/webui/src/api/OB11Config.ts +++ b/src/webui/src/api/OB11Config.ts @@ -10,15 +10,15 @@ import { sendError, sendSuccess } from '@webapi/utils/response'; import { isEmpty } from '@webapi/utils/check'; // 获取OneBot11配置 -export const OB11GetConfigHandler: RequestHandler = async (_, res) => { +export const OB11GetConfigHandler: RequestHandler = (_, res) => { // 获取QQ登录状态 - const isLogin = await WebUiDataRuntime.getQQLoginStatus(); + const isLogin = WebUiDataRuntime.getQQLoginStatus(); // 如果未登录,返回错误 if (!isLogin) { return sendError(res, 'Not Login'); } // 获取登录的QQ号 - const uin = await WebUiDataRuntime.getQQLoginUin(); + const uin = WebUiDataRuntime.getQQLoginUin(); // 读取配置文件 const configFilePath = resolve(webUiPathWrapper.configPath, `./onebot11_${uin}.json`); // 尝试解析配置文件 @@ -39,7 +39,7 @@ export const OB11GetConfigHandler: RequestHandler = async (_, res) => { // 写入OneBot11配置 export const OB11SetConfigHandler: RequestHandler = async (req, res) => { // 获取QQ登录状态 - const isLogin = await WebUiDataRuntime.getQQLoginStatus(); + const isLogin = WebUiDataRuntime.getQQLoginStatus(); // 如果未登录,返回错误 if (!isLogin) { return sendError(res, 'Not Login'); diff --git a/src/webui/src/api/QQLogin.ts b/src/webui/src/api/QQLogin.ts index 9dda0aca0..4f324d226 100644 --- a/src/webui/src/api/QQLogin.ts +++ b/src/webui/src/api/QQLogin.ts @@ -7,12 +7,12 @@ import { sendError, sendSuccess } from '@webapi/utils/response'; // 获取QQ登录二维码 export const QQGetQRcodeHandler: RequestHandler = async (req, res) => { // 判断是否已经登录 - if (await WebUiDataRuntime.getQQLoginStatus()) { + if (WebUiDataRuntime.getQQLoginStatus()) { // 已经登录 return sendError(res, 'QQ Is Logined'); } // 获取二维码 - const qrcodeUrl = await WebUiDataRuntime.getQQLoginQrcodeURL(); + const qrcodeUrl = WebUiDataRuntime.getQQLoginQrcodeURL(); // 判断二维码是否为空 if (isEmpty(qrcodeUrl)) { return sendError(res, 'QRCode Get Error'); @@ -27,8 +27,8 @@ export const QQGetQRcodeHandler: RequestHandler = async (req, res) => { // 获取QQ登录状态 export const QQCheckLoginStatusHandler: RequestHandler = async (req, res) => { const data = { - isLogin: await WebUiDataRuntime.getQQLoginStatus(), - qrcodeurl: await WebUiDataRuntime.getQQLoginQrcodeURL(), + isLogin: WebUiDataRuntime.getQQLoginStatus(), + qrcodeurl: WebUiDataRuntime.getQQLoginQrcodeURL(), }; return sendSuccess(res, data); }; @@ -38,7 +38,7 @@ export const QQSetQuickLoginHandler: RequestHandler = async (req, res) => { // 获取QQ号 const { uin } = req.body; // 判断是否已经登录 - const isLogin = await WebUiDataRuntime.getQQLoginStatus(); + const isLogin = WebUiDataRuntime.getQQLoginStatus(); if (isLogin) { return sendError(res, 'QQ Is Logined'); } @@ -53,12 +53,24 @@ export const QQSetQuickLoginHandler: RequestHandler = async (req, res) => { return sendError(res, message); } //本来应该验证 但是http不宜这么搞 建议前端验证 - //isLogin = await WebUiDataRuntime.getQQLoginStatus(); + //isLogin = WebUiDataRuntime.getQQLoginStatus(); return sendSuccess(res, null); }; // 获取快速登录列表 export const QQGetQuickLoginListHandler: RequestHandler = async (_, res) => { - const quickLoginList = await WebUiDataRuntime.getQQQuickLoginList(); + const quickLoginList = WebUiDataRuntime.getQQQuickLoginList(); return sendSuccess(res, quickLoginList); }; + +// 获取快速登录列表(新) +export const QQGetLoginListNewHandler: RequestHandler = async (_, res) => { + const newLoginList = WebUiDataRuntime.getQQNewLoginList(); + return sendSuccess(res, newLoginList); +}; + +// 获取登录的QQ的信息 +export const getQQLoginInfoHandler: RequestHandler = async (_, res) => { + const data = WebUiDataRuntime.getQQLoginInfo(); + return sendSuccess(res, data); +}; diff --git a/src/webui/src/helper/Data.ts b/src/webui/src/helper/Data.ts index 8c23153d4..f07052cd8 100644 --- a/src/webui/src/helper/Data.ts +++ b/src/webui/src/helper/Data.ts @@ -1,11 +1,16 @@ -import { OneBotConfig } from '@/onebot/config/config'; - +import type { LoginRuntimeType } from '../types/data'; +import packageJson from '../../../../package.json'; const LoginRuntime: LoginRuntimeType = { LoginCurrentTime: Date.now(), LoginCurrentRate: 0, QQLoginStatus: false, //已实现 但太傻了 得去那边注册个回调刷新 QQQRCodeURL: '', QQLoginUin: '', + QQLoginInfo: { + uid: '', + uin: '', + nick: '', + }, NapCatHelper: { onOB11ConfigChanged: async () => { return; @@ -14,11 +19,13 @@ const LoginRuntime: LoginRuntimeType = { return { result: false, message: '' }; }, QQLoginList: [], + NewQQLoginList: [], }, + packageJson: packageJson, }; export const WebUiDataRuntime = { - checkLoginRate: async function (RateLimit: number): Promise { + checkLoginRate(RateLimit: number): boolean { LoginRuntime.LoginCurrentRate++; //console.log(RateLimit, LoginRuntime.LoginCurrentRate, Date.now() - LoginRuntime.LoginCurrentTime); if (Date.now() - LoginRuntime.LoginCurrentTime > 1000 * 60) { @@ -29,51 +36,68 @@ export const WebUiDataRuntime = { return LoginRuntime.LoginCurrentRate <= RateLimit; }, - getQQLoginStatus: async function (): Promise { + getQQLoginStatus(): LoginRuntimeType['QQLoginStatus'] { return LoginRuntime.QQLoginStatus; }, - setQQLoginStatus: async function (status: boolean): Promise { + setQQLoginStatus(status: LoginRuntimeType['QQLoginStatus']): void { LoginRuntime.QQLoginStatus = status; }, - setQQLoginQrcodeURL: async function (url: string): Promise { + setQQLoginQrcodeURL(url: LoginRuntimeType['QQQRCodeURL']): void { LoginRuntime.QQQRCodeURL = url; }, - getQQLoginQrcodeURL: async function (): Promise { + getQQLoginQrcodeURL(): LoginRuntimeType['QQQRCodeURL'] { return LoginRuntime.QQQRCodeURL; }, - setQQLoginUin: async function (uin: string): Promise { - LoginRuntime.QQLoginUin = uin; + setQQLoginInfo(info: LoginRuntimeType['QQLoginInfo']): void { + LoginRuntime.QQLoginInfo = info; + LoginRuntime.QQLoginUin = info.uin.toString(); }, - getQQLoginUin: async function (): Promise { + getQQLoginInfo(): LoginRuntimeType['QQLoginInfo'] { + return LoginRuntime.QQLoginInfo; + }, + + getQQLoginUin(): LoginRuntimeType['QQLoginUin'] { return LoginRuntime.QQLoginUin; }, - getQQQuickLoginList: async function (): Promise { + getQQQuickLoginList(): LoginRuntimeType['NapCatHelper']['QQLoginList'] { return LoginRuntime.NapCatHelper.QQLoginList; }, - setQQQuickLoginList: async function (list: string[]): Promise { + setQQQuickLoginList(list: LoginRuntimeType['NapCatHelper']['QQLoginList']): void { LoginRuntime.NapCatHelper.QQLoginList = list; }, - setQuickLoginCall(func: (uin: string) => Promise<{ result: boolean; message: string }>): void { - LoginRuntime.NapCatHelper.onQuickLoginRequested = func; + getQQNewLoginList(): LoginRuntimeType['NapCatHelper']['NewQQLoginList'] { + return LoginRuntime.NapCatHelper.NewQQLoginList; }, - requestQuickLogin: async function (uin: string): Promise<{ result: boolean; message: string }> { - return await LoginRuntime.NapCatHelper.onQuickLoginRequested(uin); + setQQNewLoginList(list: LoginRuntimeType['NapCatHelper']['NewQQLoginList']): void { + LoginRuntime.NapCatHelper.NewQQLoginList = list; }, - setOnOB11ConfigChanged: async function (func: (ob11: OneBotConfig) => Promise): Promise { + setQuickLoginCall(func: LoginRuntimeType['NapCatHelper']['onQuickLoginRequested']): void { + LoginRuntime.NapCatHelper.onQuickLoginRequested = func; + }, + + requestQuickLogin: function (uin) { + return LoginRuntime.NapCatHelper.onQuickLoginRequested(uin); + } as LoginRuntimeType['NapCatHelper']['onQuickLoginRequested'], + + setOnOB11ConfigChanged(func: LoginRuntimeType['NapCatHelper']['onOB11ConfigChanged']): void { LoginRuntime.NapCatHelper.onOB11ConfigChanged = func; }, - setOB11Config: async function (ob11: OneBotConfig): Promise { - await LoginRuntime.NapCatHelper.onOB11ConfigChanged(ob11); + setOB11Config: function (ob11) { + return LoginRuntime.NapCatHelper.onOB11ConfigChanged(ob11); + } as LoginRuntimeType['NapCatHelper']['onOB11ConfigChanged'], + + getPackageJson() { + return LoginRuntime.packageJson; }, }; diff --git a/src/webui/src/router/Base.ts b/src/webui/src/router/Base.ts new file mode 100644 index 000000000..e332770f1 --- /dev/null +++ b/src/webui/src/router/Base.ts @@ -0,0 +1,8 @@ +import { Router } from 'express'; +import { PackageInfoHandler } from '../api/BaseInfo'; + +const router = Router(); +// router: 获取nc的package.json信息 +router.get('/PackageInfo', PackageInfoHandler); + +export { router as BaseRouter }; diff --git a/src/webui/src/router/QQLogin.ts b/src/webui/src/router/QQLogin.ts index 7af23dbdd..5fd4ef180 100644 --- a/src/webui/src/router/QQLogin.ts +++ b/src/webui/src/router/QQLogin.ts @@ -5,16 +5,22 @@ import { QQGetQRcodeHandler, QQGetQuickLoginListHandler, QQSetQuickLoginHandler, + QQGetLoginListNewHandler, + getQQLoginInfoHandler, } from '@webapi/api/QQLogin'; const router = Router(); // router:获取快速登录列表 router.all('/GetQuickLoginList', QQGetQuickLoginListHandler); +// router:获取快速登录列表(新) +router.all('/GetQuickLoginListNew', QQGetLoginListNewHandler); // router:检查QQ登录状态 router.post('/CheckLoginStatus', QQCheckLoginStatusHandler); // router:获取QQ登录二维码 router.post('/GetQQLoginQrcode', QQGetQRcodeHandler); // router:设置QQ快速登录 router.post('/SetQuickLogin', QQSetQuickLoginHandler); +// router:获取QQ登录信息 +router.post('/GetQQLoginInfo', getQQLoginInfoHandler); export { router as QQLoginRouter }; diff --git a/src/webui/src/router/index.ts b/src/webui/src/router/index.ts index 679bec571..eaa960d35 100644 --- a/src/webui/src/router/index.ts +++ b/src/webui/src/router/index.ts @@ -11,6 +11,7 @@ import { sendSuccess } from '@webapi/utils/response'; import { QQLoginRouter } from '@webapi/router/QQLogin'; import { AuthRouter } from '@webapi/router/auth'; import { LogRouter } from '@webapi/router/Log'; +import { BaseRouter } from '@webapi/router/Base'; const router = Router(); @@ -21,6 +22,8 @@ router.use(auth); router.all('/test', (_, res) => { return sendSuccess(res); }); +// router:基础信息相关路由 +router.use('/base', BaseRouter); // router:WebUI登录相关路由 router.use('/auth', AuthRouter); // router:QQ登录相关路由 diff --git a/src/webui/src/types/data.d.ts b/src/webui/src/types/data.d.ts index b0bb1217d..a3ea9852a 100644 --- a/src/webui/src/types/data.d.ts +++ b/src/webui/src/types/data.d.ts @@ -1,12 +1,17 @@ +import type { LoginListItem, SelfInfo } from '@/core'; + interface LoginRuntimeType { LoginCurrentTime: number; LoginCurrentRate: number; QQLoginStatus: boolean; QQQRCodeURL: string; QQLoginUin: string; + QQLoginInfo: SelfInfo; NapCatHelper: { onQuickLoginRequested: (uin: string) => Promise<{ result: boolean; message: string }>; onOB11ConfigChanged: (ob11: OneBotConfig) => Promise; QQLoginList: string[]; + NewQQLoginList: LoginListItem[]; }; + packageJson: object; }