/** * @file request/apiHubReq.ts * @description apiHub下的请求 * @since Beta v0.8.2 */ import { getRequestHeader } from "@utils/getRequestHeader.js"; import TGHttp from "@utils/TGHttp.js"; // MysApiHubBaseUrl => Mahbu const Mahbu: Readonly = "https://bbs-api.miyoushe.com/apihub/"; const Referer: Readonly = "https://bbs.mihoyo.com/"; /** * @description 获取所有版块 * @since Beta v0.6.8 * @return {Promise>} */ async function getAllGamesForums(): Promise> { return ( await TGHttp(`${Mahbu}wapi/getAllGamesForums`, { method: "GET", headers: { "Content-Type": "application/json", referer: Referer }, }) ).data.list; } /** * @description 获取应用配置 * @since Beta v0.8.2 * @param {string} gid 分区ID * @return {Promise} */ async function getAppConfig( gid?: string, ): Promise { let url = `${Mahbu}api/getAppConfig`; if (gid) url += `?gid=${gid}`; const resp = await TGHttp(url, { method: "GET", headers: { "Content-Type": "application/json", referer: Referer }, }); if (resp.retcode === 0) return resp.data.config; return resp; } /** * @description 获取所有分区 * @since Beta v0.6.8 * @return {Promise>} */ async function getGameList(): Promise> { return ( await TGHttp(`${Mahbu}wapi/getGameList`, { method: "GET", headers: { "Content-Type": "application/json", referer: Referer }, }) ).data.list; } /** * @description 获取用户米游币任务完成情况 * @since Beta v0.7.2 * @param {Record} cookie 用户 Cookie * @return {Promise} */ async function getMissions(cookie: Record): Promise { const param = { point_sn: "myb" }; const header = getRequestHeader(cookie, "GET", param); return await TGHttp(`${Mahbu}wapi/getMissions`, { method: "GET", headers: header, query: param, }); } /** * @description 获取分享配置 * @since Beta v0.7.0 * @description **需要验证码登录返回的 Cookie** * @param {string} postId 帖子 ID * @param {Record} cookie 用户 Cookie * @return {Promise} */ async function getShareConf( postId: string, cookie: Record, ): Promise { const params = { entity_id: postId, entity_type: 1 }; const header = { ...getRequestHeader(cookie, "GET", params, "K2", true), "x-rpc-client_type": "2", }; return await TGHttp(`${Mahbu}api/getShareConf`, { method: "GET", headers: header, query: params, }); } /** * @description 获取任务完成情况 * @since Beta v0.7.2 * @description **需要验证码登录的 Cookie** * @param {Record} cookie 用户 Cookie * @return {Promise} */ async function getUserMissionsState( cookie: Record, ): Promise { const param = { point_sn: "myb" }; const header = getRequestHeader(cookie, "GET", param); return await TGHttp(`${Mahbu}wapi/getUserMissionsState`, { method: "GET", headers: header, query: param, }); } /** * @description 获取投票信息 * @since Beta v0.6.2 * @param {string} id 投票 ID * @param {string} uid 用户 ID * @return {Promise} */ async function getVotes(id: string, uid: string): Promise { return ( await TGHttp(`${Mahbu}api/getVotes`, { method: "GET", headers: { "Content-Type": "application/json", referer: Referer }, query: { owner_uid: uid, vote_ids: id }, }) ).data.data[0]; } /** * @description 获取投票结果 * @since Beta v0.6.2 * @param {string} id 投票 ID * @param {string} uid 用户 ID * @return {Promise} */ async function getVoteResult(id: string, uid: string): Promise { return ( await TGHttp(`${Mahbu}api/getVotesResult`, { method: "GET", headers: { "Content-Type": "application/json", referer: Referer }, query: { owner_uid: uid, vote_ids: id }, }) ).data.data[0]; } /** * @description 获取首页导航列表 * @since Beta v0.6.2 * @param {number} gid GID * @return {Promise} */ async function homeNew(gid: number = 2): Promise { return ( await TGHttp(`${Mahbu}api/home/new`, { method: "GET", headers: { "x-rpc-client_type": "2" }, query: { gids: gid }, }) ).data.navigator; } /** * @description 签到 * @since Beta v0.7.1 * @description **需要验证码登录获取的 Cookie** * @param {Record} cookie 用户 Cookie * @param {string} gid * @param {string} challenge * @return {Promise} */ async function signIn( cookie: Record, gid: number = 2, challenge?: string, ): Promise { const data = { gids: gid }; let header: Record = { ...getRequestHeader(cookie, "POST", JSON.stringify(data), "X6"), "x-rpc-client_type": "2", }; if (challenge) header = { ...header, "x-rpc-challenge": challenge }; return await TGHttp(`${Mahbu}app/api/signIn`, { method: "POST", headers: header, body: JSON.stringify(data), }); } /** * @description 点赞 * @since Beta v0.7.0 * @param {string} id 帖子 ID * @param {Record} cookie 用户 Cookie * @param {boolean} cancel 是否取消点赞 * @return {Promise} */ async function upVotePost( id: string, cookie: Record, cancel: boolean = false, ): Promise { const data = { is_cancel: cancel, post_id: id }; const header = { ...getRequestHeader(cookie, "POST", data, "K2", true), "x-rpc-client_type": "2", }; return await TGHttp(`${Mahbu}api/upvotePost`, { method: "POST", headers: header, body: JSON.stringify(data), }); } const apiHubReq = { vote: { info: getVotes, result: getVoteResult }, appConfig: getAppConfig, home: homeNew, forum: getAllGamesForums, game: getGameList, mission: { list: getMissions, state: getUserMissionsState }, sign: signIn, post: { like: upVotePost, share: getShareConf }, }; export default apiHubReq;