🌱 尝试迁移部分请求

This commit is contained in:
BTMuli
2026-04-07 21:55:18 +08:00
parent 4b500f6169
commit 211a992022
5 changed files with 85 additions and 48 deletions

View File

@@ -27,12 +27,13 @@
<script lang="ts" setup>
import showDialog from "@comp/func/dialog.js";
import showSnackbar from "@comp/func/snackbar.js";
import ApiHubReq from "@req/apiHubReq.js";
import apiHubReq from "@req/apiHubReq.js";
import OtherApi from "@req/otherReq.js";
import useAppStore from "@store/app.js";
import { emit } from "@tauri-apps/api/event";
import { openUrl } from "@tauri-apps/plugin-opener";
import TGClient from "@utils/TGClient.js";
import TGHttps from "@utils/TGHttps.js";
import TGLogger from "@utils/TGLogger.js";
import { createPost } from "@utils/TGWindow.js";
import { storeToRefs } from "pinia";
@@ -67,16 +68,18 @@ watch(
async () => await loadNav(),
);
/**
* 加载组件数据
* @returns {Promise<void>}
*/
async function loadNav(): Promise<void> {
try {
nav.value = await ApiHubReq.home(props.gid);
console.warn(`[TGameNav][loadNav] 组件数据:`, nav.value);
const resp = await apiHubReq.home(props.gid);
nav.value = resp.data.data.navigator;
if (loadCode.value) loadCode.value = false;
} catch (e) {
if (TGHttps.isHttpErr(e)) {
const errMsg = e.status ? `[${e.status}] ${e.message}` : e.message;
showSnackbar.error(`加载导航失败: ${errMsg}`);
} else {
showSnackbar.error(`加载导航失败: ${String(e)}`);
}
await TGLogger.Error(`[TGameNav][loadNav] 加载组件数据失败:${e}`);
}
}
@@ -96,18 +99,29 @@ async function tryGetCode(): Promise<void> {
return;
}
actId.value = actIdFind;
const res = await OtherApi.code(actIdFind);
if (!Array.isArray(res)) {
try {
const resp = await OtherApi.code(actIdFind);
if (resp.data.retcode !== 0) {
loadCode.value = false;
showSnackbar.warn(`[${resp.data.retcode}] ${resp.data.message}`);
await TGLogger.Warn(`[TGameNav][tryGetCode] 获取兑换码失败:${JSON.stringify(resp.data)}`);
return;
}
codeData.value = resp.data.data.code_list;
console.debug(`[TGameNave][tryGetCode] 兑换码数据:`, codeData.value);
showSnackbar.success("获取兑换码成功");
showOverlay.value = true;
loadCode.value = false;
showSnackbar.warn(`[${res.retcode}] ${res.message}`);
await TGLogger.Warn(`[TGameNav][tryGetCode] 获取兑换码失败:${JSON.stringify(res)}`);
return;
} catch (e) {
loadCode.value = false;
if (TGHttps.isHttpErr(e)) {
const errMsg = e.status ? `[${e.status}] ${e.message}` : e.message;
showSnackbar.error(`获取兑换码失败: ${errMsg}`);
} else {
showSnackbar.error(`获取兑换码失败: ${String(e)}`);
}
await TGLogger.Error(`[TGameNav][tryGetCode] 获取兑换码失败:${e}`);
}
codeData.value = res;
console.debug(`[TGameNave][tryGetCode] 兑换码数据:`, codeData.value);
showSnackbar.success("获取兑换码成功");
showOverlay.value = true;
loadCode.value = false;
}
/**

View File

@@ -1,10 +1,11 @@
/**
* apiHub下的请求
* @since Beta v0.8.2
* @since Beta v0.9.9
*/
import { getRequestHeader } from "@utils/getRequestHeader.js";
import TGHttp from "@utils/TGHttp.js";
import TGHttps from "@utils/TGHttps.js";
// MysApiHubBaseUrl => Mahbu
const Mahbu: Readonly<string> = "https://bbs-api.miyoushe.com/apihub/";
@@ -152,18 +153,18 @@ async function getVoteResult(id: string, uid: string): Promise<TGApp.BBS.Vote.Re
/**
* 获取首页导航列表
* @since Beta v0.6.2
* @since Beta v0.9.9
* @param gid - GID
* @TODO 测试带ck请求
* @returns 首页导航列表
*/
async function homeNew(gid: number = 2): Promise<Array<TGApp.BBS.Navigator.Navigator>> {
return (
await TGHttp<TGApp.BBS.Navigator.HomeResp>(`${Mahbu}api/home/new`, {
method: "GET",
headers: { "x-rpc-client_type": "2" },
query: { gids: gid },
})
).data.navigator;
async function homeNew(
gid: number = 2,
): Promise<TGApp.App.Response.Resp<TGApp.BBS.Navigator.HomeResp>> {
return await TGHttps.get<TGApp.BBS.Navigator.HomeResp>(`${Mahbu}api/home/new`, {
headers: { "x-rpc-client_type": "2" },
query: { gids: gid },
});
}
/**

View File

@@ -1,16 +1,29 @@
/**
* Other API
* @since Beta v0.7.2
* @since Beta v0.9.9
*/
import TGBbs from "@utils/TGBbs.js";
import TGHttp from "@utils/TGHttp.js";
import TGHttps from "@utils/TGHttps.js";
import TGLogger from "@utils/TGLogger.js";
import { getInitDeviceInfo } from "@utils/toolFunc.js";
/**
* 设备指纹返回类型
* @since Beta v0.9.9
*/
type DeviceFpResp = {
/** 设备指纹 */
device_fp: string;
/** 状态码 */
code: number;
/** 信息 */
msg: string;
};
/**
* 获取设备指纹
* @since Beta v0.7.2
* @since Beta v0.9.9
* @param Info - 设备信息
* @returns 设备指纹
*/
@@ -90,14 +103,13 @@ async function getDeviceFp(
"x-requested-with": "com.mihoyo.hyperion",
Referer: "https://webstatic.mihoyo.com/",
};
type ResType = { device_fp: string; code: number; msg: string };
try {
const resp = await TGHttp<TGApp.BBS.Response.BaseWithData<ResType>>(
const resp = await TGHttps.post<TGApp.BBS.Response.BaseWithData<DeviceFpResp>>(
"https://public-data-api.mihoyo.com/device-fp/api/getFp",
{ method: "POST", body: JSON.stringify(data), headers: header },
{ headers: header, body: JSON.stringify(data) },
);
if (resp.retcode !== 0) info.device_fp = "0000000000000";
else info.device_fp = resp.data.device_fp;
if (resp.data.retcode !== 0) info.device_fp = "0000000000000";
else info.device_fp = resp.data.data.device_fp;
} catch (error) {
info.device_fp = "0000000000000";
await TGLogger.Error(`获取设备指纹失败: ${error}`);
@@ -107,19 +119,17 @@ async function getDeviceFp(
/**
* 获取兑换码请求
* @since Beta v0.5.3
* @since Beta v0.9.9
* @param actId - 活动 id
* @returns 兑换码
*/
async function refreshCode(
actId: string,
): Promise<Array<TGApp.BBS.Navigator.CodeData> | TGApp.BBS.Response.Base> {
const res = await TGHttp<TGApp.BBS.Navigator.CodeResp | TGApp.BBS.Response.Base>(
): Promise<TGApp.App.Response.Resp<TGApp.BBS.Navigator.CodeResp>> {
return await TGHttps.get<TGApp.BBS.Navigator.CodeResp>(
"https://api-takumi-static.mihoyo.com/event/miyolive/refreshCode",
{ method: "GET", headers: { "x-rpc-act_id": actId } },
{ headers: { "x-rpc-act_id": actId } },
);
if (res.retcode !== 0) return <TGApp.BBS.Response.Base>res;
return res.data.code_list;
}
const OtherApi = { code: refreshCode, fp: getDeviceFp };

View File

@@ -11,6 +11,7 @@ import TGLogger from "./TGLogger.js";
/**
* 请求参数
* @since Beta v0.9.1
* @remarks 请使用 TGHttpsConfig 类型
*/
type TGHttpParams = {
/** 请求方法 */
@@ -30,7 +31,7 @@ type TGHttpParams = {
/**
* 发送请求
* @since Beta v0.9.1
* @typeParam T - 返回数据类型
* @remarks 自 Beta v0.10.0 起弃用,请使用 TGHttps.get() 或 TGHttps.post() 替代
* @param url - 请求地址
* @param options - 请求参数
* @returns 请求结果

View File

@@ -6,8 +6,6 @@
import { type ClientOptions, fetch } from "@tauri-apps/plugin-http";
import JSONBig from "json-bigint";
import TGLogger from "./TGLogger.js";
/**
* 构建 URL 查询字符串
* @since Beta v0.10.0
@@ -165,10 +163,8 @@ async function request<T>(
} else {
httpError = createHttpError(String(error), { cause: error });
}
// 记录错误日志
await TGLogger.Error(`[TGHttps] Request failed: ${httpError.message}`);
// 记录错误日志 TODO根据实际情况调整日志
// await TGLogger.Error(`[TGHttps] Request failed: ${httpError.message}`);
throw httpError;
}
}
@@ -211,6 +207,21 @@ const TGHttps = {
url: string,
config?: TGApp.App.Response.ReqConfParams,
): Promise<TGApp.App.Response.Resp<T>> => request<T>(method, url, config),
/**
* 判断是否为 HTTP 错误
* @since Beta v0.10.0
* @param error - 错误对象
* @returns 是否为 HTTP 错误
*/
isHttpErr: (error: unknown): error is TGApp.App.Response.HttpErr => {
return (
typeof error === "object" &&
error !== null &&
"message" in error &&
typeof error.message === "string"
);
},
};
export default TGHttps;