mirror of
https://github.com/BTMuli/TeyvatGuide.git
synced 2026-04-22 21:59:49 +08:00
🔥 移除 json-bigint,buffer请求解耦
This commit is contained in:
@@ -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",
|
||||
|
||||
15
pnpm-lock.yaml
generated
15
pnpm-lock.yaml
generated
@@ -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: {}
|
||||
|
||||
@@ -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<TGApp.Plugins.Hutao.Gacha.EntryRes | TGApp.Plugins.Hutao.Base.Resp> {
|
||||
const url = `${GachaUrl}Entries`;
|
||||
const url = `${HUTAO_GACHA_API}Entries`;
|
||||
const header = await getReqHeader(tk);
|
||||
const resp = await TGHttp<TGApp.Plugins.Hutao.Gacha.EntryResp>(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<TGApp.Plugins.Hutao.Gacha.EndIdRes | TGApp.Plugins.Hutao.Base.Resp> {
|
||||
const url = `${GachaUrl}EndIds`;
|
||||
const url = `${HUTAO_GACHA_API}EndIds`;
|
||||
const header = await getReqHeader(tk);
|
||||
const resp = await TGHttp<TGApp.Plugins.Hutao.Gacha.EndIdResp>(url, {
|
||||
method: "GET",
|
||||
headers: header,
|
||||
query: { Uid: uid },
|
||||
hasBigInt: true,
|
||||
});
|
||||
if (resp.retcode !== 0) return <TGApp.Plugins.Hutao.Base.Resp>resp;
|
||||
return <TGApp.Plugins.Hutao.Gacha.EndIdRes>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<TGApp.Plugins.Hutao.Gacha.GachaLogResp> {
|
||||
const url = `${GachaUrl}LimitedRetrieve`;
|
||||
const url = `${HUTAO_GACHA_API}LimitedRetrieve`;
|
||||
const header = await getReqHeader(tk);
|
||||
const params: Record<string, string | number> = {
|
||||
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<TGApp.Plugins.Hutao.Gacha.UploadResp> {
|
||||
const url = `${GachaUrl}Upload`;
|
||||
const url = `${HUTAO_GACHA_API}Upload`;
|
||||
const header = await getReqHeader(tk);
|
||||
return await TGHttp<TGApp.Plugins.Hutao.Gacha.UploadResp>(url, {
|
||||
method: "POST",
|
||||
@@ -110,7 +110,7 @@ export async function deleteGachaLogs(
|
||||
tk: string,
|
||||
uid: string,
|
||||
): Promise<TGApp.Plugins.Hutao.Gacha.DeleteResp> {
|
||||
const url = `${GachaUrl}Delete`;
|
||||
const url = `${HUTAO_GACHA_API}Delete`;
|
||||
const header = await getReqHeader(tk);
|
||||
return await TGHttp<TGApp.Plugins.Hutao.Gacha.DeleteResp>(url, {
|
||||
method: "GET",
|
||||
|
||||
4
src/types/App/Response.d.ts
vendored
4
src/types/App/Response.d.ts
vendored
@@ -36,10 +36,6 @@ declare namespace TGApp.App.Response {
|
||||
query?: Record<string, string | number | boolean>;
|
||||
/** 请求体 */
|
||||
body?: string | Record<string, unknown>;
|
||||
/** 是否返回 Blob 数据 */
|
||||
isBlob?: boolean;
|
||||
/** 是否包含 BigInt 数据 */
|
||||
hasBigInt?: boolean;
|
||||
/** 请求超时时间(毫秒) */
|
||||
timeout?: number;
|
||||
/** AbortSignal 用于取消请求 */
|
||||
|
||||
@@ -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<T>(
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -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<T>(
|
||||
response: Response,
|
||||
config: TGApp.App.Response.ReqConf,
|
||||
): Promise<T> {
|
||||
if (config.isBlob) {
|
||||
return <T>await response.arrayBuffer();
|
||||
}
|
||||
if (config.hasBigInt) {
|
||||
return <T>JSONBig.parse(await response.text());
|
||||
}
|
||||
return <T>await response.json();
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行 HTTP 请求
|
||||
* @since Beta v0.10.0
|
||||
@@ -104,11 +83,7 @@ async function request<T>(
|
||||
}
|
||||
|
||||
// 调试日志
|
||||
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<T>(
|
||||
data: errorText,
|
||||
});
|
||||
}
|
||||
|
||||
// 解析响应
|
||||
const data = await parseResponse<T>(rawResponse, config);
|
||||
|
||||
const data = <T>await rawResponse.json();
|
||||
return {
|
||||
data: data,
|
||||
status: rawResponse.status,
|
||||
@@ -194,6 +166,39 @@ const TGHttps = {
|
||||
config?: TGApp.App.Response.ReqConfParams,
|
||||
): Promise<TGApp.App.Response.Resp<T>> => request<T>("POST", url, config),
|
||||
|
||||
/**
|
||||
* 用于获取图像 ArrayBuffer
|
||||
* @since Beta v0.10.0
|
||||
* @remarks 目前需求较为简单,故不做额外处理
|
||||
* @param url - 图像地址
|
||||
* @returns ArrayBuffer
|
||||
*/
|
||||
async buffer(url: string): Promise<ArrayBuffer> {
|
||||
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
|
||||
|
||||
@@ -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<string> {
|
||||
const res = await TGHttp<Uint8Array>(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<string> {
|
||||
|
||||
/**
|
||||
* 返回图片 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<ArrayBuffer> {
|
||||
return await TGHttp<ArrayBuffer>(url, { method: "GET", isBlob: true });
|
||||
return await TGHttp.buffer(url);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user