refactor(Mys): request 跟 utils 拿出来了

This commit is contained in:
BTMuli
2023-03-29 17:11:14 +08:00
parent 988f595435
commit 54cccb37a5
11 changed files with 388 additions and 264 deletions

View File

@@ -5,10 +5,37 @@
* @since Alpha
*/
// Post
import { getPostData } from "./request/post";
import { PostParser } from "./utils/parser";
// Gacha
import { getGachaData } from "./request/gacha";
import { getGachaCard } from "./utils/gacha";
// News
import { getNoticeList, getActivityList, getNewsList } from "./request/news";
import { getNoticeCard, getActivityCard, getNewsCard } from "./utils/news";
const Mys_Oper = {
PostParser,
const MysOper = {
Post: {
get: getPostData,
parser: PostParser,
},
Gacha: {
get: getGachaData,
card: getGachaCard,
},
News: {
get: {
notice: getNoticeList,
activity: getActivityList,
news: getNewsList,
},
card: {
notice: getNoticeCard,
activity: getActivityCard,
news: getNewsCard,
},
},
};
export default Mys_Oper;
export default MysOper;

View File

@@ -7,19 +7,6 @@
import { MysResponse } from "./base";
// 卡池 API
/**
* @description 获取卡池信息的返回类型
* @since Alpha
* @see GachaResponse
* @return {string}
*/
export const GACHA_POOL_API =
"https://api-takumi.mihoyo.com/common/blackboard/ys_obc/v1/gacha_pool?app_sn=ys_obc";
// 卡池接口
/**
* @description 获取卡池信息的返回类型
* @since Alpha
@@ -80,23 +67,23 @@ export interface GachaPool {
/**
* @description 用于渲染的卡池数据
* @since Alpha
* @interface GachaPoolRender
* @interface GachaCard
* @property {string} title 卡池标题
* @property {string} subtitle 卡池副标题
* @property {string} cover 卡池封面
* @property {string} post_id 卡池对应帖子ID
* @property {number} post_id 卡池对应帖子ID
* @property {GachaPool[]} characters 卡池包含的角色
* @property {GachaPool} voice 卡池角色语音
* @property time 卡池时间
* @property {string} time.start 卡池开始时间
* @property {string} time.end 卡池结束时间
* @return {GachaPoolRender}
* @return {GachaCard}
*/
export interface GachaPoolRender {
export interface GachaCard {
title: string;
subtitle: string;
cover: string;
post_id: string;
post_id: number;
characters: GachaPool[];
voice: GachaPool;
time: {

View File

@@ -10,52 +10,6 @@ import { Post, Forum, Topic, PostStat } from "./post";
import { User, SelfOperation } from "./user";
import { ImageData, HelpSys } from "./utils";
// 咨讯 API
/**
* @description 咨讯列表 API
* @since Alpha
* @param {number} ews_type 咨讯类型
* @see NewsType
* @return {string}
*/
export const NEWS_LIST_API =
"https://bbs-api.mihoyo.com/post/wapi/getNewsList?gids=2&type={news_type}";
// 咨讯相关枚举数据
/**
* @description 咨讯类型
* @enum NewsType
* @since Alpha
* @property {number} NOTICE 公告
* @property {number} ACTIVITY 活动
* @property {number} NEWS 咨讯
* @return {NewsType}
*/
export enum NewsType {
NOTICE = 1,
ACTIVITY = 2,
NEWS = 3,
}
/**
* @description 活动状态
* @enum ActivityStatus
* @since Alpha
* @property {number} STARTED 进行中
* @property {number} FINISHED 已结束
* @property {number} SELECTION 评选中
* @return {ActivityStatus}
*/
export enum ActivityStatus {
STARTED = 1,
FINISHED = 2,
SELECTION = 3,
}
// 咨讯接口
/**
* @description 咨讯返回数据
* @since Alpha
@@ -155,15 +109,17 @@ export interface NewsMeta {
* @interface NewsCard
* @property {string} title 标题
* @property {string} cover 封面图片 URL
* @property {string} post_id 帖子 ID
* @property {number} post_id 帖子 ID
* @property {string} subtitle 副标题
* @property {number} status 活动状态,仅活动咨讯有
* @property {string} status 活动状态,仅活动咨讯有
* @property {string} status_color 活动状态按钮背景色,仅活动咨讯有
* @return {NewsCard}
*/
export interface NewsCard {
title: string;
cover: string;
post_id: string;
post_id: number;
subtitle: string;
status?: number;
status?: string;
status_color?: string;
}

View File

@@ -10,28 +10,6 @@ import { NewsMeta } from "./news";
import { User, SelfOperation } from "./user";
import { ImageData, HelpSys } from "./utils";
// Post API
/**
* @description 帖子完整信息 API
* @since Alpha
* @see PostResponse
* @param {number} post_id 帖子 ID
* @return {string}
*/
export const POST_FULL_API =
"https://bbs-api.mihoyo.com/post/wapi/getPostFull?gids=2&post_id={post_id}";
/**
* @description 帖子完整信息 Referer
* @since Alpha
* @param {number} post_id 帖子 ID
* @return {string}
*/
export const POST_FULL_REFERER = "https://bbs.mihoyo.com/ys/article/{post_id}"; // 与 POST_FULL_API 中的 post_id 对应, 用于伪造 Referer
// Post Interface
/**
* @description 帖子返回数据
* @since Alpha

View File

@@ -0,0 +1,31 @@
/**
* @file plugins Mys request gacha.ts
* @description Mys抽卡请求
* @author BTMuli<bt-muli@outlook.com>
* @since Alpha
*/
import { http } from "@tauri-apps/api";
import { GachaResponse, GachaData } from "../interface/gacha";
// 卡池 API
const GACHA_POOL_API =
"https://api-takumi.mihoyo.com/common/blackboard/ys_obc/v1/gacha_pool?app_sn=ys_obc";
/**
* @description 获取卡池信息
* @since Alpha
* @return {Promise<GachaData[]>}
*/
export async function getGachaData(): Promise<GachaData[]> {
return await http
.fetch<GachaResponse>(GACHA_POOL_API, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
})
.then(res => {
return res.data.data.list;
});
}

View File

@@ -0,0 +1,70 @@
/**
* @file plugins Mys request news.ts
* @description Mys 插件咨讯请求
* @author BTMuli<bt-muli@outlook.com>
* @since Alpha
*/
import { http } from "@tauri-apps/api";
import { NewsData, NewsResponse } from "../interface/news";
// 咨讯 API
const NEWS_LIST_API =
"https://bbs-api.mihoyo.com/post/wapi/getNewsList?gids=2&page_size={page_size}&type={news_type}";
/**
* @description 咨讯类型
* @enum NewsType
* @since Alpha
* @property {string} NOTICE 公告
* @property {string} ACTIVITY 活动
* @property {string} NEWS 咨讯
* @return {NewsType}
*/
enum NewsType {
NOTICE = "1",
ACTIVITY = "2",
NEWS = "3",
}
/**
* @description 获取 Notice 列表
* @since Alpha
* @param {number} page_size 返回数量
* @return {Promise<NewsData>}
*/
export async function getNoticeList(page_size: number = 20): Promise<NewsData> {
const url = NEWS_LIST_API.replace("{page_size}", page_size.toString()).replace(
"{news_type}",
NewsType.NOTICE
);
return await http.fetch<NewsResponse>(url).then(res => res.data.data);
}
/**
* @description 获取 Activity 列表
* @since Alpha
* @param {number} page_size 返回数量
* @return {Promise<NewsData>}
*/
export async function getActivityList(page_size: number = 20): Promise<NewsData> {
const url = NEWS_LIST_API.replace("{page_size}", page_size.toString()).replace(
"{news_type}",
NewsType.ACTIVITY
);
return await http.fetch<NewsResponse>(url).then(res => res.data.data);
}
/**
* @description 获取 News 列表
* @since Alpha
* @param {number} page_size 返回数量
* @return {Promise<NewsData>}
*/
export async function getNewsList(page_size: number = 20): Promise<NewsData> {
const url = NEWS_LIST_API.replace("{page_size}", page_size.toString()).replace(
"{news_type}",
NewsType.NEWS
);
return await http.fetch<NewsResponse>(url).then(res => res.data.data);
}

View File

@@ -0,0 +1,33 @@
/**
* @file plugins Mys request post.ts
* @description Mys帖子请求
* @author BTMuli<bt-muli@outlook.com>
* @since Alpha
*/
import { http } from "@tauri-apps/api";
import { PostResponse, PostData } from "../interface/post";
// 帖子 API
const POST_API = "https://bbs-api.mihoyo.com/post/wapi/getPostFull?gids=2&post_id={post_id}";
const POST_REFERER = "https://bbs.mihoyo.com/ys/article/{post_id}";
/**
* @description 获取帖子信息
* @since Alpha
* @param {number} post_id 帖子 ID
* @return {Promise<PostData>}
*/
export async function getPostData(post_id: number): Promise<PostData> {
return await http
.fetch<PostResponse>(POST_API.replace("{post_id}", post_id.toString()), {
method: "GET",
headers: {
"Content-Type": "application/json",
Referer: POST_REFERER.replace("{post_id}", post_id.toString()),
},
})
.then(res => {
return res.data.data.post;
});
}

View File

@@ -0,0 +1,46 @@
/**
* @file plugins Mys utils gacha.ts
* @description Mys 插件抽卡工具
* @author BTMuli<bt-muli@outlook.com>
* @since Alpha
*/
import { getPostData } from "../request/post";
import { GachaCard, GachaData } from "../interface/gacha";
import { PostData } from "../interface/post";
/**
* @description 根据卡池信息转为渲染用的卡池信息
* @since Alpha
* @param {GachaData[]} gachaData 卡池信息
* @return {GachaCard[]}
*/
export async function getGachaCard(gachaData: GachaData[]): Promise<GachaCard[]> {
const gachaCard: GachaCard[] = [];
gachaData.map(async (data: GachaData) => {
const post_id: number | undefined = Number(data.activity_url.split("/").pop()) || undefined;
if (post_id === undefined) {
throw new Error("无法获取帖子 ID");
}
const post: PostData = await getPostData(post_id);
gachaCard.push({
title: data.title,
subtitle: data.content_before_act,
cover: post.cover?.url || post.post.images[0],
post_id: post_id,
characters: data.pool.map(character => ({
icon: character.icon,
url: character.url,
})),
voice: {
icon: data.voice_icon,
url: data.voice_url,
},
time: {
start: data.start_time,
end: data.end_time,
},
});
});
return gachaCard;
}

View File

@@ -0,0 +1,116 @@
/**
* @file plugins Mys utils news.ts
* @description Mys 插件咨讯工具
* @author BTMuli<bt-muli@outlook.com>
* @since Alpha
*/
import { NewsData, NewsItem, NewsCard } from "../interface/news";
/**
* @description 活动状态
* @enum ActivityStatus
* @since Alpha
* @property {string} STARTED 进行中
* @property {string} FINISHED 已结束
* @property {string} SELECTION 评选中
* @return {ActivityStatus}
*/
enum ActivityStatus {
STARTED = "进行中",
FINISHED = "已结束",
SELECTION = "评选中",
}
/**
* @description 获取活动状态
* @since Alpha
* @param {number} status 活动状态码
* @return {string}
*/
export function getActivityStatus(status: number): { status: ActivityStatus; color: string } {
switch (status) {
case 1:
return {
status: ActivityStatus.STARTED,
color: "#3c99aa",
};
case 2:
return {
status: ActivityStatus.FINISHED,
color: "#c7674b",
};
case 3:
return {
status: ActivityStatus.SELECTION,
color: "#849cc7",
};
default:
return {
status: ActivityStatus.FINISHED,
color: "#c7674b",
};
}
}
/**
* @description 获取渲染用公告数据
* @since Alpha
* @param {NewsData} noticeData 公告数据
* @return {NewsCard[]}
*/
export function getNoticeCard(noticeData: NewsData): NewsCard[] {
const noticeCard: NewsCard[] = [];
noticeData.list.map((item: NewsItem) => {
noticeCard.push({
title: item.post.subject,
cover: item.cover.url || item.post.cover || item.post.images[0],
post_id: Number(item.post.post_id),
subtitle: item.post.post_id,
});
});
return noticeCard;
}
/**
* @description 获取渲染用活动数据
* @since Alpha
* @param {NewsData} activityData 活动数据
* @return {NewsCard[]}
*/
export function getActivityCard(activityData: NewsData): NewsCard[] {
const activityCard: NewsCard[] = [];
activityData.list.map((item: NewsItem) => {
const start_time = new Date(Number(item.news_meta.start_at_sec) * 1000).toLocaleDateString();
const end_time = new Date(Number(item.news_meta.end_at_sec) * 1000).toLocaleDateString();
const status_info = getActivityStatus(item.news_meta.activity_status);
activityCard.push({
title: item.post.subject,
cover: item.cover.url || item.post.cover || item.post.images[0],
post_id: Number(item.post.post_id),
subtitle: `${start_time} - ${end_time}`,
status: status_info.status,
status_color: status_info.color,
});
});
return activityCard;
}
/**
* @description 获取渲染用新闻数据
* @since Alpha
* @param {NewsData} newsData 新闻数据
* @return {NewsCard[]}
*/
export function getNewsCard(newsData: NewsData): NewsCard[] {
const newsCard: NewsCard[] = [];
newsData.list.map((item: NewsItem) => {
newsCard.push({
title: item.post.subject,
cover: item.cover.url || item.post.cover || item.post.images[0],
post_id: Number(item.post.post_id),
subtitle: item.post.post_id,
});
});
return newsCard;
}