From 5ae65e1cf8a72e35490ee15cfdfc2ddde8060191 Mon Sep 17 00:00:00 2001 From: linyuchen Date: Fri, 1 Dec 2023 01:48:54 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E5=A4=9A=E4=B8=AA?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6=E4=B8=8A=E6=8A=A5=E5=9C=B0=E5=9D=80(http)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- manifest.json | 2 +- src/common/types.ts | 2 +- src/main/config.ts | 8 +++-- src/main/main.ts | 32 ++++++++++---------- src/renderer.ts | 72 +++++++++++++++++++++++++++++++++------------ 5 files changed, 79 insertions(+), 37 deletions(-) diff --git a/manifest.json b/manifest.json index 4b9bf59..22e26c1 100644 --- a/manifest.json +++ b/manifest.json @@ -4,7 +4,7 @@ "name": "LLOneBot", "slug": "LLOneBot", "description": "LiteLoaderQQNT的OneBotApi", - "version": "1.0.0", + "version": "1.1.0", "thumbnail": "./icon.png", "author": { "name": "linyuchen", diff --git a/src/common/types.ts b/src/common/types.ts index 7a7b129..53a42d7 100644 --- a/src/common/types.ts +++ b/src/common/types.ts @@ -156,5 +156,5 @@ export type PostDataSendMsg = { export type Config = { port: number, - host: string, + hosts: string[], } diff --git a/src/main/config.ts b/src/main/config.ts index 1fa2473..4f75085 100644 --- a/src/main/config.ts +++ b/src/main/config.ts @@ -11,10 +11,14 @@ export class ConfigUtil{ getConfig(): Config{ if (!fs.existsSync(this.configPath)) { - return {"port":3000, "host": "http://localhost:5000/"} + return {port:3000, hosts: ["http://192.168.1.2:5000/"]} } else { const data = fs.readFileSync(this.configPath, "utf-8"); - return JSON.parse(data); + let jsonData =JSON.parse(data); + if (!jsonData.hosts){ + jsonData.hosts = [] + } + return jsonData; } } } diff --git a/src/main/main.ts b/src/main/main.ts index 5aa8c3a..1f6c6cf 100644 --- a/src/main/main.ts +++ b/src/main/main.ts @@ -89,21 +89,23 @@ function onLoad(plugin: any) { }) ipcMain.on(CHANNEL_POST_ONEBOT_DATA, (event: any, arg: any) => { - try { - fetch(configUtil.getConfig().host, { - method: "POST", - headers: { - "Content-Type": "application/json", - "x-self-id": selfInfo.user_id - }, - body: JSON.stringify(arg) - }).then((res: any) => { - log("新消息事件上传"); - }, (err: any) => { - log("新消息事件上传失败:" + err + JSON.stringify(arg)); - }); - } catch (e: any) { - log(e.toString()) + for(const host of configUtil.getConfig().hosts) { + try { + fetch(host, { + method: "POST", + headers: { + "Content-Type": "application/json", + "x-self-id": selfInfo.user_id + }, + body: JSON.stringify(arg) + }).then((res: any) => { + log("新消息事件上传"); + }, (err: any) => { + log("新消息事件上传失败:" + err + JSON.stringify(arg)); + }); + } catch (e: any) { + log(e.toString()) + } } }) diff --git a/src/renderer.ts b/src/renderer.ts index 509f033..930fd5e 100644 --- a/src/renderer.ts +++ b/src/renderer.ts @@ -3,6 +3,7 @@ // import express from "express"; // const { ipcRenderer } = require('electron'); import {AtType, Group, MessageElement, OnebotGroupMemberRole, Peer, PostDataSendMsg, User} from "./common/types"; +import * as stream from "stream"; let self_qq: string = "" let groups: Group[] = [] @@ -219,8 +220,7 @@ async function listenSendMessage(postData: PostDataSendMsg) { let localFilePath = `${Date.now()}${ext}` if (uri.protocol == "file:") { localFilePath = url.split("file://")[1] - } - else{ + } else { await window.llonebot.downloadFile({uri: url, localFilePath: localFilePath}) } message.file = localFilePath @@ -388,41 +388,77 @@ function onLoad() { // 打开设置界面时触发 async function onConfigView(view: any) { - // 插件本体的路径 - // const plugin_path = (window as any).LiteLoader.plugins.LLOneBot.path; - const {port, host} = await window.llonebot.getConfig() + const {port, hosts} = await window.llonebot.getConfig() + + function creatHostEleStr(host: string) { + let eleStr = ` +
+

事件上报地址(http)

+ +
+ ` + return eleStr + } + + let hostsEleStr = "" + for (const host of hosts) { + hostsEleStr += creatHostEleStr(host); + } const html = ` -
-
- +
+
+

监听端口

- - +
- +
+ ${hostsEleStr} +
+
` + const parser = new DOMParser(); const doc = parser.parseFromString(html, "text/html"); + + + function addHostEle(initValue: string = "") { + let addressDoc = parser.parseFromString(creatHostEleStr(initValue), "text/html"); + let addressEle = addressDoc.querySelector("div") + let hostItemsEle = document.getElementById("hostItems"); + hostItemsEle.appendChild(addressEle); + } + + + doc.getElementById("addHost").addEventListener("click", () => addHostEle()) + doc.getElementById("save")?.addEventListener("click", () => { const portEle: HTMLInputElement = document.getElementById("port") as HTMLInputElement - const hostEle: HTMLInputElement = document.getElementById("host") as HTMLInputElement + const hostEles: HTMLCollectionOf = document.getElementsByClassName("host") as HTMLCollectionOf; // const port = doc.querySelector("input[type=number]")?.value // const host = doc.querySelector("input[type=text]")?.value // 获取端口和host const port = portEle.value - const host = hostEle.value - if (port && host) { - window.llonebot.setConfig({ - port: parseInt(port), - host: host - }) + let hosts: string[] = []; + for (const hostEle of hostEles) { + if (hostEle.value) { + hosts.push(hostEle.value); + } } + window.llonebot.setConfig({ + port: parseInt(port), + hosts: hosts + }) + alert("保存成功"); }) doc.querySelectorAll("section").forEach((node) => view.appendChild(node)); + + } export {