♻️ 调整结构

This commit is contained in:
目棃
2025-03-11 10:28:17 +08:00
parent c577a6f1ea
commit c0d9830670
3 changed files with 45 additions and 93 deletions

View File

@@ -64,11 +64,20 @@ import { useAppStore } from "@/store/modules/app.js";
import TGLogger from "@/utils/TGLogger.js";
import { decodeRegExp } from "@/utils/toolFunc.js";
import Hk4eApi, { type AnnoLang, AnnoServer } from "@/web/request/hk4eReq.js";
import { getAnnoCard } from "@/web/utils/getAnnoCard.js";
type AnnoSelect = { text: string; value: string };
type AnnoKey = keyof typeof AnnoType;
type AnnoCard = { [key in AnnoKey]: Array<TGApp.App.Announcement.ListCard> };
type AnnoCard = {
id: number;
title: string;
subtitle: string;
banner: string;
typeLabel: string;
tagIcon: string;
tagLabel: string;
timeStr: string;
};
type AnnoList = { [key in AnnoKey]: Array<AnnoCard> };
const annoServerList: Array<AnnoSelect> = [
{ text: "国服-官方服", value: AnnoServer.CN_ISLAND },
@@ -94,7 +103,7 @@ const { server, lang } = storeToRefs(useAppStore());
const router = useRouter();
const tabValues: Readonly<Array<AnnoKey>> = ["activity", "game"];
const tab = ref<AnnoKey>("activity");
const annoCards = shallowRef<AnnoCard>({ activity: [], game: [] });
const annoCards = shallowRef<AnnoList>({ activity: [], game: [] });
const isReq = ref<boolean>(false);
watch(
@@ -130,7 +139,7 @@ async function loadData(): Promise<void> {
`服务器:${getRegionName(server.value)},语言:${getLangName(lang.value)}`,
);
const annoData = await Hk4eApi.anno.list(server.value, lang.value);
const listCards = getAnnoCard(annoData);
const listCards = annoData.list.map((list) => list.list.map((anno) => getAnnoCard(anno))).flat();
await showLoading.update("", { title: "正在解析游戏内公告时间" });
for (const item of listCards) {
if (item.typeLabel === AnnoType.game) continue;
@@ -147,6 +156,38 @@ async function loadData(): Promise<void> {
isReq.value = false;
}
function getAnnoTag(tag: string): string {
switch (tag) {
case "1":
case "11":
case "重要":
return "公告";
case "2":
case "扭蛋":
return "祈愿";
case "3":
return "活动";
default:
return tag;
}
}
function getAnnoCard(anno: TGApp.BBS.Announcement.AnnoSingle): AnnoCard {
const timeStart = anno.start_time.split(" ")[0];
const timeEnd = anno.end_time.split(" ")[0];
const time = `${timeStart} ~ ${timeEnd}`;
return {
id: anno.ann_id,
title: anno.title,
subtitle: anno.subtitle.replace(/<br \/>/g, " "),
banner: anno.banner,
typeLabel: anno.type === 2 ? "游戏公告" : "活动公告",
tagIcon: anno.tag_icon,
tagLabel: getAnnoTag(anno.tag_label),
timeStr: time,
};
}
function getRegionName(value: AnnoServer): string {
return annoServerList.find((item) => item.value === value)?.text ?? annoServerList[0].text;
}

View File

@@ -1,32 +0,0 @@
/**
* @file types App Announcement.d.ts
* @description 应用公告相关类型定义文件
* @since Beta v0.3.3
*/
declare namespace TGApp.App.Announcement {
/**
* @description 渲染用的公告列表数据类型
* @since Beta v0.3.3
* @interface ListCard
* @property {number} id - 公告 ID
* @property {string} title - 公告标题
* @property {string} subtitle - 公告副标题
* @property {string} banner - 公告横幅
* @property {string} typeLabel - 公告类型标签
* @property {string} tagIcon - 公告标签图标
* @property {string} tagLabel - 公告标签文字
* @property {string} timeStr - 公告时间字符串
* @return ListCard
*/
interface ListCard {
id: number;
title: string;
subtitle: string;
banner: string;
typeLabel: string;
tagIcon: string;
tagLabel: string;
timeStr: string;
}
}

View File

@@ -1,57 +0,0 @@
/**
* @file web utils transAnno.ts
* @description 公告数据转换工具
* @since Beta v0.6.1
*/
/**
* @description 获取公告标签
* @since Beta v0.4.4
* @param {string} tag 标签
* @returns {string} 标签
*/
function getAnnoTag(tag: string): string {
switch (tag) {
case "1":
case "11":
case "重要":
return "公告";
case "2":
case "扭蛋":
return "祈愿";
case "3":
return "活动";
default:
return tag;
}
}
/**
* @description 将获取到的数据转为渲染用的卡片
* @since Beta v0.6.1
* @param {TGApp.BBS.Announcement.ListData[]} data 公告数据
* @returns {TGApp.App.Announcement.ListCard[]} 渲染用的卡片
*/
export function getAnnoCard(
data: TGApp.BBS.Announcement.ListData,
): Array<TGApp.App.Announcement.ListCard> {
const cards: Array<TGApp.App.Announcement.ListCard> = [];
for (const annoList of data.list) {
for (const anno of annoList.list) {
const timeStart = anno.start_time.split(" ")[0];
const timeEnd = anno.end_time.split(" ")[0];
const time = `${timeStart} ~ ${timeEnd}`;
cards.push({
id: anno.ann_id,
title: anno.title,
subtitle: anno.subtitle.replace(/<br \/>/g, " "),
banner: anno.banner,
typeLabel: anno.type === 2 ? "游戏公告" : "活动公告",
tagIcon: anno.tag_icon,
tagLabel: getAnnoTag(anno.tag_label),
timeStr: time,
});
}
}
return cards;
}