From b795e6c3d2753f0b672db631c13359d749aebbe5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Mon, 13 May 2024 18:08:46 +0800 Subject: [PATCH] refactor: umami --- src/common/utils/request.ts | 26 +++++++++++++++ src/common/utils/umami.ts | 65 +++++++++++++------------------------ src/index.ts | 2 +- 3 files changed, 50 insertions(+), 43 deletions(-) diff --git a/src/common/utils/request.ts b/src/common/utils/request.ts index c49ba0c3..619a3c5d 100644 --- a/src/common/utils/request.ts +++ b/src/common/utils/request.ts @@ -1,4 +1,5 @@ export class RequestUtil { + //适用于获取服务器下发cookies时获取 static async HttpGetCookies(url: string, method: string = 'GET'): Promise> { const response = await fetch(url, { method: method }); if (!response.ok) { @@ -18,6 +19,7 @@ export class RequestUtil { return result; } + // 请求和回复都是JSON data传原始内容 自动编码json static async HttpGetJson(url: string, method: string = 'GET', data?: any, headers: Record = {}): Promise { let body: BodyInit | undefined = undefined; @@ -45,4 +47,28 @@ export class RequestUtil { throw new Error(`Failed to fetch JSON: ${error.message}`); } } + // 请求返回都是原始内容 + static async HttpGetText(url: string, method: string = 'GET', data?: any, headers: Record = {}): Promise { + let requestInit: RequestInit = { method: method }; + if (method.toUpperCase() === 'POST' && data !== undefined) { + if (headers) { + headers['Content-Type'] = 'application/json'; + requestInit.headers = new Headers(headers); + } else { + requestInit.headers = new Headers({ 'Content-Type': 'application/json' }); + } + } else { + requestInit.headers = new Headers(headers); + } + try { + const response = await fetch(url, { ...requestInit, body: data }); + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + const jsonResult = await response.text(); + return jsonResult; + } catch (error: any) { + throw new Error(`Failed to fetch JSON: ${error.message}`); + } + } } \ No newline at end of file diff --git a/src/common/utils/umami.ts b/src/common/utils/umami.ts index 699ff485..fbdb1532 100644 --- a/src/common/utils/umami.ts +++ b/src/common/utils/umami.ts @@ -1,44 +1,25 @@ -import { request } from 'node:https'; -export function postLoginStatus() { - const req = request( - { - hostname: 'napcat.wumiao.wang', - path: '/api/send', - port: 443, - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0' - } - }, - (res) => { - //let data = ''; - res.on('data', (chunk) => { - //data += chunk; - }); - res.on('error', (err) => { - }); - res.on('end', () => { - //console.log('Response:', data); - }); - } - ); - req.on('error', (e) => { - // console.error('Request error:', e); - }); - const StatesData = { - type: 'event', - payload: { - 'website': '952bf82f-8f49-4456-aec5-e17db5f27f7e', - 'hostname': 'napcat.demo.cn', - 'screen': '1920x1080', - 'language': 'zh-CN', - 'title': 'OneBot.Login', - 'url': '/login/onebot11/1.3.2', - 'referrer': 'https://napcat.demo.cn/login?type=onebot11' - } - }; - req.write(JSON.stringify(StatesData)); +import { RequestUtil } from './request'; - req.end(); +export async function postLoginStatus() { + return new Promise(async (resolve, reject) => { + const StatesData = { + type: 'event', + payload: { + 'website': '952bf82f-8f49-4456-aec5-e17db5f27f7e', + 'hostname': 'napcat.demo.cn', + 'screen': '1920x1080', + 'language': 'zh-CN', + 'title': 'OneBot.Login', + 'url': '/login/onebot11/1.3.5', + 'referrer': 'https://napcat.demo.cn/login?type=onebot11' + } + }; + await RequestUtil.HttpGetText('https://napcat.wumiao.wang/api/send', + 'POST', + StatesData, { + 'Content-Type': 'application/json', + 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0' + }); + resolve(true); + }); } diff --git a/src/index.ts b/src/index.ts index 6f5b9be0..029065ed 100644 --- a/src/index.ts +++ b/src/index.ts @@ -41,7 +41,7 @@ napCatCore.onLoginSuccess((uin, uid) => { console.log('登录成功!'); WebUiDataRuntime.setQQLoginStatus(true); WebUiDataRuntime.setQQLoginUin(uin.toString()); - postLoginStatus(); + postLoginStatus().catch(logDebug); }); const showQRCode = async (url: string, base64: string, buffer: Buffer) => { await WebUiDataRuntime.setQQLoginQrcodeURL(url);