mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2026-02-06 13:05:09 +00:00
refactor: 重构 Onebot 配置格式,增强可读性
This commit is contained in:
@@ -3,84 +3,88 @@ import { WebUiDataRuntime } from "../helper/Data";
|
||||
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
||||
import { resolve } from "node:path";
|
||||
import { OB11Config } from "@/webui/ui/components/WebUiApiOB11Config";
|
||||
const isEmpty = (data: any) => data === undefined || data === null || data === '';
|
||||
const isEmpty = (data: any) =>
|
||||
data === undefined || data === null || data === "";
|
||||
export const OB11GetConfigHandler: RequestHandler = async (req, res) => {
|
||||
let isLogin = await WebUiDataRuntime.getQQLoginStatus();
|
||||
if (!isLogin) {
|
||||
res.send({
|
||||
code: -1,
|
||||
message: 'Not Login'
|
||||
});
|
||||
return;
|
||||
}
|
||||
const uin = await WebUiDataRuntime.getQQLoginUin();
|
||||
let configFilePath = resolve(__dirname, `./config/onebot11_${uin}.json`);
|
||||
//console.log(configFilePath);
|
||||
let data: OB11Config;
|
||||
try {
|
||||
data = JSON.parse(existsSync(configFilePath) ? readFileSync(configFilePath).toString() : readFileSync(resolve(__dirname, `./config/onebot11.json`)).toString());
|
||||
}
|
||||
catch (e) {
|
||||
data = {} as OB11Config;
|
||||
res.send({
|
||||
code: -1,
|
||||
message: 'Config Get Error'
|
||||
});
|
||||
return;
|
||||
}
|
||||
let isLogin = await WebUiDataRuntime.getQQLoginStatus();
|
||||
if (!isLogin) {
|
||||
res.send({
|
||||
code: 0,
|
||||
message: 'success',
|
||||
data: data
|
||||
code: -1,
|
||||
message: "Not Login",
|
||||
});
|
||||
return;
|
||||
}
|
||||
export const OB11SetConfigHandler: RequestHandler = async (req, res) => {
|
||||
let isLogin = await WebUiDataRuntime.getQQLoginStatus();
|
||||
if (!isLogin) {
|
||||
res.send({
|
||||
code: -1,
|
||||
message: 'Not Login'
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (isEmpty(req.body.config)) {
|
||||
res.send({
|
||||
code: -1,
|
||||
message: 'config is empty'
|
||||
});
|
||||
return;
|
||||
}
|
||||
let SetResult;
|
||||
try {
|
||||
await WebUiDataRuntime.setOB11Config(JSON.parse(req.body.config));
|
||||
SetResult = true;
|
||||
} catch (e) {
|
||||
SetResult = false;
|
||||
}
|
||||
|
||||
// let configFilePath = resolve(__dirname, `./config/onebot11_${await WebUiDataRuntime.getQQLoginUin()}.json`);
|
||||
// try {
|
||||
// JSON.parse(req.body.config)
|
||||
// readFileSync(configFilePath);
|
||||
// }
|
||||
// catch (e) {
|
||||
// //console.log(e);
|
||||
// configFilePath = resolve(__dirname, `./config/onebot11.json`);
|
||||
// }
|
||||
// //console.log(configFilePath,JSON.parse(req.body.config));
|
||||
// writeFileSync(configFilePath, JSON.stringify(JSON.parse(req.body.config), null, 4));
|
||||
if (SetResult) {
|
||||
res.send({
|
||||
code: 0,
|
||||
message: 'success'
|
||||
});
|
||||
} else {
|
||||
res.send({
|
||||
code: -1,
|
||||
message: 'Config Set Error'
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
const uin = await WebUiDataRuntime.getQQLoginUin();
|
||||
let configFilePath = resolve(__dirname, `./config/onebot11_${uin}.json`);
|
||||
//console.log(configFilePath);
|
||||
let data: OB11Config;
|
||||
try {
|
||||
data = JSON.parse(
|
||||
existsSync(configFilePath)
|
||||
? readFileSync(configFilePath).toString()
|
||||
: readFileSync(resolve(__dirname, `./config/onebot11.json`)).toString()
|
||||
);
|
||||
} catch (e) {
|
||||
data = {} as OB11Config;
|
||||
res.send({
|
||||
code: -1,
|
||||
message: "Config Get Error",
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
res.send({
|
||||
code: 0,
|
||||
message: "success",
|
||||
data: data,
|
||||
});
|
||||
return;
|
||||
};
|
||||
export const OB11SetConfigHandler: RequestHandler = async (req, res) => {
|
||||
let isLogin = await WebUiDataRuntime.getQQLoginStatus();
|
||||
if (!isLogin) {
|
||||
res.send({
|
||||
code: -1,
|
||||
message: "Not Login",
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (isEmpty(req.body.config)) {
|
||||
res.send({
|
||||
code: -1,
|
||||
message: "config is empty",
|
||||
});
|
||||
return;
|
||||
}
|
||||
let SetResult;
|
||||
try {
|
||||
await WebUiDataRuntime.setOB11Config(JSON.parse(req.body.config));
|
||||
SetResult = true;
|
||||
} catch (e) {
|
||||
SetResult = false;
|
||||
}
|
||||
|
||||
// let configFilePath = resolve(__dirname, `./config/onebot11_${await WebUiDataRuntime.getQQLoginUin()}.json`);
|
||||
// try {
|
||||
// JSON.parse(req.body.config)
|
||||
// readFileSync(configFilePath);
|
||||
// }
|
||||
// catch (e) {
|
||||
// //console.log(e);
|
||||
// configFilePath = resolve(__dirname, `./config/onebot11.json`);
|
||||
// }
|
||||
// //console.log(configFilePath,JSON.parse(req.body.config));
|
||||
// writeFileSync(configFilePath, JSON.stringify(JSON.parse(req.body.config), null, 4));
|
||||
if (SetResult) {
|
||||
res.send({
|
||||
code: 0,
|
||||
message: "success",
|
||||
});
|
||||
} else {
|
||||
res.send({
|
||||
code: -1,
|
||||
message: "Config Set Error",
|
||||
});
|
||||
}
|
||||
|
||||
return;
|
||||
};
|
||||
|
||||
@@ -34,36 +34,36 @@ async function onSettingWindowCreated(view: Element) {
|
||||
SettingItem(
|
||||
'启用 HTTP 服务',
|
||||
undefined,
|
||||
SettingSwitch('ob11.enableHttp', ob11Config.enableHttp, { 'control-display-id': 'config-ob11-httpPort' }),
|
||||
SettingSwitch('ob11.http.enable', ob11Config.http.enable, { 'control-display-id': 'config-ob11-httpPort' }),
|
||||
),
|
||||
SettingItem(
|
||||
'HTTP 服务监听端口',
|
||||
undefined,
|
||||
`<div class="q-input"><input class="q-input__inner" data-config-key="ob11.httpPort" type="number" min="1" max="65534" value="${ob11Config.httpPort}" placeholder="${ob11Config.httpPort}" /></div>`,
|
||||
'config-ob11-httpPort',
|
||||
ob11Config.enableHttp,
|
||||
ob11Config.http.enable,
|
||||
),
|
||||
SettingItem(
|
||||
'启用 HTTP 心跳',
|
||||
undefined,
|
||||
SettingSwitch('ob11.enableHttpHeart', ob11Config.enableHttpHeart, {
|
||||
SettingSwitch('ob11.http.enableHeart', ob11Config.http.enableHeart, {
|
||||
'control-display-id': 'config-ob11-enableHttpHeart',
|
||||
}),
|
||||
),
|
||||
SettingItem(
|
||||
'启用 HTTP 事件上报',
|
||||
undefined,
|
||||
SettingSwitch('ob11.enableHttpPost', ob11Config.enableHttpPost, {
|
||||
SettingSwitch('ob11.http.enablePost', ob11Config.http.enablePost, {
|
||||
'control-display-id': 'config-ob11-httpPostUrls',
|
||||
}),
|
||||
),
|
||||
`<div class="config-host-list" id="config-ob11-httpPostUrls" ${ob11Config.enableHttpPost ? '' : 'is-hidden'}>
|
||||
`<div class="config-host-list" id="config-ob11-httpPostUrls" ${ob11Config.http.enablePost ? '' : 'is-hidden'}>
|
||||
<setting-item data-direction="row">
|
||||
<div>
|
||||
<setting-text>HTTP 事件上报密钥</setting-text>
|
||||
</div>
|
||||
<div class="q-input">
|
||||
<input id="config-ob11-httpSecret" class="q-input__inner" data-config-key="ob11.httpSecret" type="text" value="${ob11Config.httpSecret
|
||||
<input id="config-ob11-httpSecret" class="q-input__inner" data-config-key="ob11.http.secret" type="text" value="${ob11Config.http.secret
|
||||
}" placeholder="未设置" />
|
||||
</div>
|
||||
</setting-item>
|
||||
|
||||
@@ -1,64 +1,70 @@
|
||||
export interface OB11Config {
|
||||
[key: string]: any,
|
||||
httpHost: "",
|
||||
httpPort: number;
|
||||
httpPostUrls: string[];
|
||||
httpSecret: "",
|
||||
wsHost: "",
|
||||
wsPort: number;
|
||||
wsReverseUrls: string[];
|
||||
enableHttp: boolean;
|
||||
enableHttpHeart: boolean;
|
||||
enableHttpPost: boolean;
|
||||
enableWs: boolean;
|
||||
enableWsReverse: boolean;
|
||||
messagePostFormat: 'array' | 'string';
|
||||
reportSelfMessage: boolean;
|
||||
enableLocalFile2Url: boolean;
|
||||
debug: boolean;
|
||||
heartInterval: number;
|
||||
token: "",
|
||||
musicSignUrl: "",
|
||||
[key: string]: any;
|
||||
http: {
|
||||
enable: boolean;
|
||||
host: "";
|
||||
port: number;
|
||||
secret: "";
|
||||
enableHeart: boolean;
|
||||
enablePost: boolean;
|
||||
postUrls: string[];
|
||||
};
|
||||
ws: {
|
||||
enable: boolean;
|
||||
host: "";
|
||||
port: number;
|
||||
};
|
||||
reverseWs: {
|
||||
enable: boolean;
|
||||
urls: string[];
|
||||
};
|
||||
|
||||
debug: boolean;
|
||||
heartInterval: number;
|
||||
messagePostFormat: "array" | "string";
|
||||
enableLocalFile2Url: boolean;
|
||||
musicSignUrl: "";
|
||||
reportSelfMessage: boolean;
|
||||
token: "";
|
||||
}
|
||||
|
||||
class WebUiApiOB11ConfigWrapper {
|
||||
private retCredential: string = "";
|
||||
async Init(Credential: string) {
|
||||
this.retCredential = Credential;
|
||||
private retCredential: string = "";
|
||||
async Init(Credential: string) {
|
||||
this.retCredential = Credential;
|
||||
}
|
||||
async GetOB11Config(): Promise<OB11Config> {
|
||||
let ConfigResponse = await fetch("/api/OB11Config/GetConfig", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: "Bearer " + this.retCredential,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
if (ConfigResponse.status == 200) {
|
||||
let ConfigResponseJson = await ConfigResponse.json();
|
||||
if (ConfigResponseJson.code == 0) {
|
||||
return ConfigResponseJson?.data;
|
||||
}
|
||||
}
|
||||
async GetOB11Config(): Promise<OB11Config> {
|
||||
let ConfigResponse = await fetch('/api/OB11Config/GetConfig', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': "Bearer " + this.retCredential,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
if (ConfigResponse.status == 200) {
|
||||
let ConfigResponseJson = await ConfigResponse.json();
|
||||
if (ConfigResponseJson.code == 0) {
|
||||
return ConfigResponseJson?.data;
|
||||
}
|
||||
}
|
||||
return {} as OB11Config;
|
||||
}
|
||||
async SetOB11Config(config: OB11Config): Promise<Boolean> {
|
||||
let ConfigResponse = await fetch('/api/OB11Config/SetConfig',
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': "Bearer " + this.retCredential,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ config: JSON.stringify(config) })
|
||||
}
|
||||
);
|
||||
if (ConfigResponse.status == 200) {
|
||||
let ConfigResponseJson = await ConfigResponse.json();
|
||||
if (ConfigResponseJson.code == 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return {} as OB11Config;
|
||||
}
|
||||
async SetOB11Config(config: OB11Config): Promise<Boolean> {
|
||||
let ConfigResponse = await fetch("/api/OB11Config/SetConfig", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: "Bearer " + this.retCredential,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({ config: JSON.stringify(config) }),
|
||||
});
|
||||
if (ConfigResponse.status == 200) {
|
||||
let ConfigResponseJson = await ConfigResponse.json();
|
||||
if (ConfigResponseJson.code == 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
export const OB11ConfigWrapper = new WebUiApiOB11ConfigWrapper();
|
||||
export const OB11ConfigWrapper = new WebUiApiOB11ConfigWrapper();
|
||||
|
||||
Reference in New Issue
Block a user