diff --git a/package.json b/package.json index 122189ed..8d1052f2 100644 --- a/package.json +++ b/package.json @@ -97,7 +97,6 @@ "ini": "^6.0.0", "js-md5": "^0.8.3", "jsencrypt": "^3.5.4", - "json-bigint": "^1.0.0", "pinia": "^3.0.4", "pinia-plugin-persistedstate": "^4.7.1", "qrcode.vue": "^3.8.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7c052414..e5745fed 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -89,9 +89,6 @@ importers: jsencrypt: specifier: ^3.5.4 version: 3.5.4 - json-bigint: - specifier: ^1.0.0 - version: 1.0.0 pinia: specifier: ^3.0.4 version: 3.0.4(typescript@6.0.2)(vue@3.5.32(typescript@6.0.2)) @@ -2091,9 +2088,6 @@ packages: engines: {node: '>=6.0.0'} hasBin: true - bignumber.js@9.3.1: - resolution: {integrity: sha512-Ko0uX15oIUS7wJ3Rb30Fs6SkVbLmPBAKdlm7q9+ak9bbIeFf0MwuBsQV6z7+X768/cHsfg+WlysDWJcmthjsjQ==} - birpc@2.9.0: resolution: {integrity: sha512-KrayHS5pBi69Xi9JmvoqrIgYGDkD6mcSe/i6YKi3w5kekCLzrX4+nawcXqrj2tIp50Kw/mT/s3p+GVK0A0sKxw==} @@ -3060,9 +3054,6 @@ packages: engines: {node: '>=6'} hasBin: true - json-bigint@1.0.0: - resolution: {integrity: sha512-SiPv/8VpZuWbvLSMtTDU8hEfrZWg/mH/nV/b4o0CYbSxu1UIQPLdwKOCIyLQX+VIPO5vrLX3i8qtqFyhdPSUSQ==} - json-buffer@3.0.1: resolution: {integrity: sha512-4bV5BfR2mqfQTJm+V5tPPdf+ZpuhiIvTuAB5g8kcrXOZpTT/QwwVRWBywX1ozr6lEuPdbHxwaJlm9G6mI2sfSQ==} @@ -6339,8 +6330,6 @@ snapshots: baseline-browser-mapping@2.10.16: {} - bignumber.js@9.3.1: {} - birpc@2.9.0: {} boolbase@1.0.0: {} @@ -7399,10 +7388,6 @@ snapshots: jsesc@3.1.0: {} - json-bigint@1.0.0: - dependencies: - bignumber.js: 9.3.1 - json-buffer@3.0.1: {} json-parse-even-better-errors@2.3.1: {} diff --git a/src/plugins/Hutao/request/gachaReq.ts b/src/plugins/Hutao/request/gachaReq.ts index ce3604f4..ab29b89b 100644 --- a/src/plugins/Hutao/request/gachaReq.ts +++ b/src/plugins/Hutao/request/gachaReq.ts @@ -1,11 +1,12 @@ /** * 祈愿相关请求 - * @since Beta v0.9.1 + * @since Beta v0.10.0 */ -import { getReqHeader } from "@Hutao/utils/authUtils.js"; import TGHttp from "@utils/TGHttp.js"; -const GachaUrl = "https://homa.gentle.house/GachaLog/"; +import { getReqHeader } from "../utils/authUtils.js"; + +const HUTAO_GACHA_API = "https://homa.gentle.house/GachaLog/"; /** * 获取抽卡入口 @@ -15,7 +16,7 @@ const GachaUrl = "https://homa.gentle.house/GachaLog/"; export async function getEntries( tk: string, ): Promise { - const url = `${GachaUrl}Entries`; + const url = `${HUTAO_GACHA_API}Entries`; const header = await getReqHeader(tk); const resp = await TGHttp(url, { method: "GET", @@ -27,6 +28,7 @@ export async function getEntries( /** * 获取抽卡EndId + * @since Beta v0.10.0 * @param tk - token * @param uid - 记录UID * @returns EndId @@ -35,13 +37,12 @@ export async function getEndIds( tk: string, uid: string, ): Promise { - const url = `${GachaUrl}EndIds`; + const url = `${HUTAO_GACHA_API}EndIds`; const header = await getReqHeader(tk); const resp = await TGHttp(url, { method: "GET", headers: header, query: { Uid: uid }, - hasBigInt: true, }); if (resp.retcode !== 0) return resp; return resp.data; @@ -49,7 +50,7 @@ export async function getEndIds( /** * 获取抽卡记录 - * @since Beta v0.9.1 + * @since Beta v0.10.0 * @param tk - token * @param uid - 记录UID * @param gType - 祈愿类型,按照EndId来 @@ -63,7 +64,7 @@ export async function getGachaLogs( count: number, endId: string | undefined = undefined, ): Promise { - const url = `${GachaUrl}LimitedRetrieve`; + const url = `${HUTAO_GACHA_API}LimitedRetrieve`; const header = await getReqHeader(tk); const params: Record = { uid: uid, @@ -75,7 +76,6 @@ export async function getGachaLogs( method: "GET", headers: header, query: params, - hasBigInt: true, }); } @@ -90,7 +90,7 @@ export async function uploadGachaLogs( tk: string, data: TGApp.Plugins.Hutao.Gacha.UploadData, ): Promise { - const url = `${GachaUrl}Upload`; + const url = `${HUTAO_GACHA_API}Upload`; const header = await getReqHeader(tk); return await TGHttp(url, { method: "POST", @@ -110,7 +110,7 @@ export async function deleteGachaLogs( tk: string, uid: string, ): Promise { - const url = `${GachaUrl}Delete`; + const url = `${HUTAO_GACHA_API}Delete`; const header = await getReqHeader(tk); return await TGHttp(url, { method: "GET", diff --git a/src/types/App/Response.d.ts b/src/types/App/Response.d.ts index 9c1889ea..8a98e46b 100644 --- a/src/types/App/Response.d.ts +++ b/src/types/App/Response.d.ts @@ -36,10 +36,6 @@ declare namespace TGApp.App.Response { query?: Record; /** 请求体 */ body?: string | Record; - /** 是否返回 Blob 数据 */ - isBlob?: boolean; - /** 是否包含 BigInt 数据 */ - hasBigInt?: boolean; /** 请求超时时间(毫秒) */ timeout?: number; /** AbortSignal 用于取消请求 */ diff --git a/src/utils/TGHttp.ts b/src/utils/TGHttp.ts index c933a6f3..37328884 100644 --- a/src/utils/TGHttp.ts +++ b/src/utils/TGHttp.ts @@ -5,7 +5,6 @@ */ import { type ClientOptions, fetch } from "@tauri-apps/plugin-http"; -import JSONBig from "json-bigint"; import TGLogger from "./TGLogger.js"; @@ -25,8 +24,6 @@ type TGHttpParams = { body?: string; /** 是否是Blob */ isBlob?: boolean; - /** 是否有BigInt */ - hasBigInt?: boolean; }; /** @@ -70,11 +67,7 @@ async function TGHttp( return await fetch(url, fetchOptions) .then(async (res) => { if (res.ok) { - const data = options.isBlob - ? await res.arrayBuffer() - : options.hasBigInt - ? JSONBig.parse(await res.text()) - : await res.json(); + const data = options.isBlob ? await res.arrayBuffer() : await res.json(); if (fullResponse) return { data, resp: res }; return data; } diff --git a/src/utils/TGHttps.ts b/src/utils/TGHttps.ts index 25396753..ab468f23 100644 --- a/src/utils/TGHttps.ts +++ b/src/utils/TGHttps.ts @@ -4,7 +4,6 @@ */ import { type ClientOptions, fetch } from "@tauri-apps/plugin-http"; -import JSONBig from "json-bigint"; /** * 构建 URL 查询字符串 @@ -40,26 +39,6 @@ function createHttpError( }; } -/** - * 解析响应数据 - * @since Beta v0.10.0 - * @param response - 原始响应 - * @param config - 请求配置 - * @returns 解析后的数据 - */ -async function parseResponse( - response: Response, - config: TGApp.App.Response.ReqConf, -): Promise { - if (config.isBlob) { - return await response.arrayBuffer(); - } - if (config.hasBigInt) { - return JSONBig.parse(await response.text()); - } - return await response.json(); -} - /** * 执行 HTTP 请求 * @since Beta v0.10.0 @@ -104,11 +83,7 @@ async function request( } // 调试日志 - if (config.isBlob) { - console.debug(`[TGHttps] Fetch Blob: ${finalUrl}`); - } else { - console.debug(`[TGHttps] ${method} ${finalUrl}`); - } + console.debug(`[TGHttps] ${method} ${finalUrl}`); // 创建超时控制器 const timeoutController = new AbortController(); @@ -138,10 +113,7 @@ async function request( data: errorText, }); } - - // 解析响应 - const data = await parseResponse(rawResponse, config); - + const data = await rawResponse.json(); return { data: data, status: rawResponse.status, @@ -194,6 +166,39 @@ const TGHttps = { config?: TGApp.App.Response.ReqConfParams, ): Promise> => request("POST", url, config), + /** + * 用于获取图像 ArrayBuffer + * @since Beta v0.10.0 + * @remarks 目前需求较为简单,故不做额外处理 + * @param url - 图像地址 + * @returns ArrayBuffer + */ + async buffer(url: string): Promise { + console.debug(`[TGHttps] Fetch Buffer: ${url}`); + try { + const rawResponse = await fetch(url); + if (!rawResponse.ok) { + const errorText = await rawResponse.text().catch(() => "Unknown error"); + throw createHttpError(`HTTP Error: ${rawResponse.status} ${rawResponse.statusText}`, { + status: rawResponse.status, + statusText: rawResponse.statusText, + data: errorText, + }); + } + return await rawResponse.arrayBuffer(); + } catch (error) { + let httpError: TGApp.App.Response.HttpErr; + if (this.isHttpErr(error)) { + httpError = error; + } else if (error instanceof Error) { + httpError = createHttpError(error.message, { cause: error }); + } else { + httpError = createHttpError(String(error), { cause: error }); + } + throw httpError; + } + }, + /** * 通用请求方法 * @since Beta v0.10.0 diff --git a/src/utils/TGShare.ts b/src/utils/TGShare.ts index c406fb50..cea62424 100644 --- a/src/utils/TGShare.ts +++ b/src/utils/TGShare.ts @@ -1,6 +1,6 @@ /** * 生成分享截图并保存到本地 - * @since Beta v0.9.0 + * @since Beta v0.10.0 */ import showSnackbar from "@comp/func/snackbar.js"; @@ -13,7 +13,7 @@ import { platform } from "@tauri-apps/plugin-os"; import html2canvas from "html2canvas"; import { storeToRefs } from "pinia"; -import TGHttp from "./TGHttp.js"; +import TGHttps from "./TGHttps.js"; import TGLogger from "./TGLogger.js"; import { bytesToSize } from "./toolFunc.js"; @@ -50,12 +50,13 @@ export async function saveCanvasImg( /** * 将图片保存到本地 - * @since Beta v0.5.0 + * @since Beta v0.10.0 + * @todo format param * @param url - 图片链接 * @returns 图片元素 */ export async function saveImgLocal(url: string): Promise { - const res = await TGHttp(url, { method: "GET", isBlob: true }); + const res = await TGHttps.buffer(url); const buffer = new Uint8Array(res); const blob = new Blob([buffer], { type: "image/png" }); return URL.createObjectURL(blob); @@ -63,12 +64,13 @@ export async function saveImgLocal(url: string): Promise { /** * 返回图片 buffer - * @since Beta v0.9.0 + * @since Beta v0.10.0 + * @deprecated 使用 TGHttps.buffer * @param url - 图片链接 * @returns 图片 buffer */ export async function getImageBuffer(url: string): Promise { - return await TGHttp(url, { method: "GET", isBlob: true }); + return await TGHttp.buffer(url); } /**