From 84f0e43369cb4ff2b65874bae01a5854c2a5dfad Mon Sep 17 00:00:00 2001 From: bietiaop <1527109126@qq.com> Date: Sun, 26 Jan 2025 10:44:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20debug=20=E9=85=8D=E7=BD=AE=E8=AE=B0?= =?UTF-8?q?=E5=BF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- napcat.webui/src/components/onebot/api/debug.tsx | 16 +++++++++------- napcat.webui/src/const/key.ts | 4 +++- .../pages/dashboard/debug/websocket/index.tsx | 11 ++++++++--- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/napcat.webui/src/components/onebot/api/debug.tsx b/napcat.webui/src/components/onebot/api/debug.tsx index ae18606e..91bf6c72 100644 --- a/napcat.webui/src/components/onebot/api/debug.tsx +++ b/napcat.webui/src/components/onebot/api/debug.tsx @@ -2,12 +2,14 @@ import { Button } from '@heroui/button' import { Card, CardBody, CardHeader } from '@heroui/card' import { Input } from '@heroui/input' import { Snippet } from '@heroui/snippet' +import { useLocalStorage } from '@uidotdev/usehooks' import { motion } from 'motion/react' import { useEffect, useState } from 'react' import toast from 'react-hot-toast' import { IoLink, IoSend } from 'react-icons/io5' import { PiCatDuotone } from 'react-icons/pi' +import key from '@/const/key' import { OneBotHttpApiContent, OneBotHttpApiPath } from '@/const/ob_api' import ChatInputModal from '@/components/chat_input/modal' @@ -27,12 +29,10 @@ export interface OneBotApiDebugProps { const OneBotApiDebug: React.FC = (props) => { const { path, data } = props - const url = new URL(window.location.origin).href - let defaultHttpUrl = url.replace(':6099', ':3000') - if (defaultHttpUrl.endsWith('/')) { - defaultHttpUrl = defaultHttpUrl.slice(0, -1) - } - const [httpConfig, setHttpConfig] = useState({ + const currentURL = new URL(window.location.origin) + currentURL.port = '3000' + const defaultHttpUrl = currentURL.href + const [httpConfig, setHttpConfig] = useLocalStorage(key.httpDebugConfig, { url: defaultHttpUrl, token: '' }) @@ -50,8 +50,10 @@ const OneBotApiDebug: React.FC = (props) => { const r = toast.loading('正在发送请求...') try { const parsedRequestBody = JSON.parse(requestBody) + const requestURL = new URL(httpConfig.url) + requestURL.pathname = path request - .post(httpConfig.url + path, parsedRequestBody, { + .post(requestURL.href, parsedRequestBody, { headers: { Authorization: `Bearer ${httpConfig.token}` }, diff --git a/napcat.webui/src/const/key.ts b/napcat.webui/src/const/key.ts index 8453166f..f6ada054 100644 --- a/napcat.webui/src/const/key.ts +++ b/napcat.webui/src/const/key.ts @@ -7,7 +7,9 @@ enum key { autoPlay = 'auto-play', customIcons = 'custom-icons', isCollapsedMusicPlayer = 'is-collapsed-music-player', - sideBarOpen = 'side-bar-open' + sideBarOpen = 'side-bar-open', + httpDebugConfig = 'http-debug-config', + wsDebugConfig = 'ws-debug-config' } export default key diff --git a/napcat.webui/src/pages/dashboard/debug/websocket/index.tsx b/napcat.webui/src/pages/dashboard/debug/websocket/index.tsx index 8bc7e1d8..6bd4da5e 100644 --- a/napcat.webui/src/pages/dashboard/debug/websocket/index.tsx +++ b/napcat.webui/src/pages/dashboard/debug/websocket/index.tsx @@ -1,9 +1,12 @@ import { Button } from '@heroui/button' import { Card, CardBody } from '@heroui/card' import { Input } from '@heroui/input' +import { useLocalStorage } from '@uidotdev/usehooks' import { useCallback, useState } from 'react' import toast from 'react-hot-toast' +import key from '@/const/key' + import ChatInputModal from '@/components/chat_input/modal' import OneBotMessageList from '@/components/onebot/message_list' import OneBotSendModal from '@/components/onebot/send_modal' @@ -12,9 +15,11 @@ import WSStatus from '@/components/onebot/ws_status' import { useWebSocketDebug } from '@/hooks/use-websocket-debug' export default function WSDebug() { - const url = new URL(window.location.origin).href - const defaultWsUrl = url.replace('http', 'ws').replace(':6099', ':3000') - const [socketConfig, setSocketConfig] = useState({ + const url = new URL(window.location.origin) + url.port = '3000' + url.protocol = 'ws:' + const defaultWsUrl = url.href + const [socketConfig, setSocketConfig] = useLocalStorage(key.wsDebugConfig, { url: defaultWsUrl, token: '' })