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:
@@ -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