-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #567 from bietiaop/webapi-bietiaop
refactor:优化WebUI后端代码格式(无新功能添加)
- Loading branch information
Showing
25 changed files
with
482 additions
and
376 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,64 @@ | ||
import { RequestHandler } from 'express'; | ||
import { AuthHelper } from '../helper/SignToken'; | ||
import { WebUiDataRuntime } from '../helper/Data'; | ||
|
||
import { WebUiConfig } from '@/webui'; | ||
|
||
const isEmpty = (data: any) => data === undefined || data === null || data === ''; | ||
import { AuthHelper } from '@webapi/helper/SignToken'; | ||
import { WebUiDataRuntime } from '@webapi/helper/Data'; | ||
import { sendSuccess, sendError } from '@webapi/utils/response'; | ||
import { isEmpty } from '@webapi/utils/check'; | ||
|
||
// 登录 | ||
export const LoginHandler: RequestHandler = async (req, res) => { | ||
// 获取WebUI配置 | ||
const WebUiConfigData = await WebUiConfig.GetWebUIConfig(); | ||
// 获取请求体中的token | ||
const { token } = req.body; | ||
// 如果token为空,返回错误信息 | ||
if (isEmpty(token)) { | ||
res.json({ | ||
code: -1, | ||
message: 'token is empty', | ||
}); | ||
return; | ||
return sendError(res, 'token is empty'); | ||
} | ||
if (!await WebUiDataRuntime.checkLoginRate(WebUiConfigData.loginRate)) { | ||
res.json({ | ||
code: -1, | ||
message: 'login rate limit', | ||
}); | ||
return; | ||
// 检查登录频率 | ||
if (!(await WebUiDataRuntime.checkLoginRate(WebUiConfigData.loginRate))) { | ||
return sendError(res, 'login rate limit'); | ||
} | ||
//验证config.token是否等于token | ||
if (WebUiConfigData.token !== token) { | ||
res.json({ | ||
code: -1, | ||
message: 'token is invalid', | ||
}); | ||
return; | ||
return sendError(res, 'token is invalid'); | ||
} | ||
const signCredential = Buffer.from(JSON.stringify(await AuthHelper.signCredential(WebUiConfigData.token))).toString('base64'); | ||
res.json({ | ||
code: 0, | ||
message: 'success', | ||
data: { | ||
'Credential': signCredential, | ||
}, | ||
// 签发凭证 | ||
const signCredential = Buffer.from(JSON.stringify(await AuthHelper.signCredential(WebUiConfigData.token))).toString( | ||
'base64' | ||
); | ||
// 返回成功信息 | ||
return sendSuccess(res, { | ||
Credential: signCredential, | ||
}); | ||
return; | ||
}; | ||
export const LogoutHandler: RequestHandler = (req, res) => { | ||
// 这玩意无状态销毁个灯 得想想办法 | ||
res.json({ | ||
code: 0, | ||
message: 'success', | ||
}); | ||
return; | ||
|
||
// 退出登录 | ||
export const LogoutHandler: RequestHandler = (_, res) => { | ||
// TODO: 这玩意无状态销毁个灯 得想想办法 | ||
return sendSuccess(res, null); | ||
}; | ||
|
||
// 检查登录状态 | ||
export const checkHandler: RequestHandler = async (req, res) => { | ||
// 获取WebUI配置 | ||
const WebUiConfigData = await WebUiConfig.GetWebUIConfig(); | ||
// 获取请求头中的Authorization | ||
const authorization = req.headers.authorization; | ||
// 检查凭证 | ||
try { | ||
// 从Authorization中获取凭证 | ||
const CredentialBase64: string = authorization?.split(' ')[1] as string; | ||
// 解析凭证 | ||
const Credential = JSON.parse(Buffer.from(CredentialBase64, 'base64').toString()); | ||
// 验证凭证是否在一小时内有效 | ||
await AuthHelper.validateCredentialWithinOneHour(WebUiConfigData.token, Credential); | ||
res.json({ | ||
code: 0, | ||
message: 'success', | ||
}); | ||
return; | ||
// 返回成功信息 | ||
return sendSuccess(res, null); | ||
} catch (e) { | ||
res.json({ | ||
code: -1, | ||
message: 'failed', | ||
}); | ||
// 返回错误信息 | ||
return sendError(res, 'Authorization Faild'); | ||
} | ||
return; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
import { RequestHandler } from 'express'; | ||
|
||
export const LogFileListHandler: RequestHandler = async (req, res) => { | ||
res.send({ | ||
code: 0, | ||
data: { | ||
uin: 0, | ||
nick: 'NapCat', | ||
avatar: 'https://q1.qlogo.cn/g?b=qq&nk=0&s=640', | ||
status: 'online', | ||
boottime: Date.now() | ||
} | ||
}); | ||
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,58 @@ | ||
import { RequestHandler } from 'express'; | ||
import { WebUiDataRuntime } from '../helper/Data'; | ||
import { existsSync, readFileSync } from 'node:fs'; | ||
import { OneBotConfig } from '@/onebot/config/config'; | ||
import { resolve } from 'node:path'; | ||
|
||
import { OneBotConfig } from '@/onebot/config/config'; | ||
|
||
import { webUiPathWrapper } from '@/webui'; | ||
import { WebUiDataRuntime } from '@webapi/helper/Data'; | ||
import { sendError, sendSuccess } from '@webapi/utils/response'; | ||
import { isEmpty } from '@webapi/utils/check'; | ||
|
||
const isEmpty = (data: any) => data === undefined || data === null || data === ''; | ||
export const OB11GetConfigHandler: RequestHandler = async (req, res) => { | ||
// 获取OneBot11配置 | ||
export const OB11GetConfigHandler: RequestHandler = async (_, res) => { | ||
// 获取QQ登录状态 | ||
const isLogin = await WebUiDataRuntime.getQQLoginStatus(); | ||
// 如果未登录,返回错误 | ||
if (!isLogin) { | ||
res.send({ | ||
code: -1, | ||
message: 'Not Login', | ||
}); | ||
return; | ||
return sendError(res, 'Not Login'); | ||
} | ||
// 获取登录的QQ号 | ||
const uin = await WebUiDataRuntime.getQQLoginUin(); | ||
// 读取配置文件 | ||
const configFilePath = resolve(webUiPathWrapper.configPath, `./onebot11_${uin}.json`); | ||
//console.log(configFilePath); | ||
let data: OneBotConfig; | ||
// 尝试解析配置文件 | ||
try { | ||
data = JSON.parse( | ||
// 读取配置文件 | ||
const data = JSON.parse( | ||
existsSync(configFilePath) | ||
? readFileSync(configFilePath).toString() | ||
: readFileSync(resolve(webUiPathWrapper.configPath, './onebot11.json')).toString() | ||
); | ||
) as OneBotConfig; | ||
// 返回配置文件 | ||
return sendSuccess(res, data); | ||
} catch (e) { | ||
data = {} as OneBotConfig; | ||
res.send({ | ||
code: -1, | ||
message: 'Config Get Error', | ||
}); | ||
return; | ||
return sendError(res, 'Config Get Error'); | ||
} | ||
res.send({ | ||
code: 0, | ||
message: 'success', | ||
data: data, | ||
}); | ||
return; | ||
}; | ||
|
||
// 写入OneBot11配置 | ||
export const OB11SetConfigHandler: RequestHandler = async (req, res) => { | ||
// 获取QQ登录状态 | ||
const isLogin = await WebUiDataRuntime.getQQLoginStatus(); | ||
// 如果未登录,返回错误 | ||
if (!isLogin) { | ||
res.send({ | ||
code: -1, | ||
message: 'Not Login', | ||
}); | ||
return; | ||
return sendError(res, 'Not Login'); | ||
} | ||
// 如果配置为空,返回错误 | ||
if (isEmpty(req.body.config)) { | ||
res.send({ | ||
code: -1, | ||
message: 'config is empty', | ||
}); | ||
return; | ||
return sendError(res, 'config is empty'); | ||
} | ||
let SetResult; | ||
// 写入配置 | ||
try { | ||
await WebUiDataRuntime.setOB11Config(JSON.parse(req.body.config)); | ||
SetResult = true; | ||
return sendSuccess(res, null); | ||
} catch (e) { | ||
SetResult = false; | ||
} | ||
if (SetResult) { | ||
res.send({ | ||
code: 0, | ||
message: 'success', | ||
}); | ||
} else { | ||
res.send({ | ||
code: -1, | ||
message: 'Config Set Error', | ||
}); | ||
return sendError(res, 'Config Set Error'); | ||
} | ||
|
||
return; | ||
}; |
Oops, something went wrong.