feat(theme-4): sync inbox/announcements/presence/deposit from main
This commit is contained in:
@@ -4,6 +4,7 @@ import api from '../api';
|
||||
import type { BannerItem } from '../components/BannerCarousel.vue';
|
||||
import { resolveBanners } from '../constants/defaultBanner';
|
||||
import { resolveAnnouncements } from '../constants/defaultAnnouncement';
|
||||
import { stripHtml } from '../utils/html';
|
||||
|
||||
export interface PlayerHomeMatch {
|
||||
id: string;
|
||||
@@ -20,12 +21,52 @@ export interface PlayerHomeMatch {
|
||||
displayOrder?: number;
|
||||
}
|
||||
|
||||
export interface PlayerContentItem {
|
||||
id: string;
|
||||
contentType?: string;
|
||||
sortOrder?: number;
|
||||
createdAt?: string;
|
||||
linkType?: string | null;
|
||||
linkTarget?: string | null;
|
||||
translation?: {
|
||||
title?: string | null;
|
||||
body?: string | null;
|
||||
imageUrl?: string | null;
|
||||
};
|
||||
}
|
||||
|
||||
export type PlayerAnnouncementItem = PlayerContentItem;
|
||||
|
||||
interface HomePayload {
|
||||
banners?: BannerItem[];
|
||||
announcements?: Array<{ translation?: { title?: string; body?: string } }>;
|
||||
ticker?: Array<{ translation?: { title?: string; body?: string } }>;
|
||||
notices?: Array<{ translation?: { title?: string; body?: string } }>;
|
||||
banners?: PlayerContentItem[];
|
||||
announcements?: PlayerAnnouncementItem[];
|
||||
ticker?: PlayerAnnouncementItem[];
|
||||
notices?: PlayerAnnouncementItem[];
|
||||
hotMatches?: PlayerHomeMatch[];
|
||||
upcomingMatches?: PlayerHomeMatch[];
|
||||
inboxEnabled?: boolean;
|
||||
}
|
||||
|
||||
function mergeMatchList(
|
||||
existing: PlayerHomeMatch[] | undefined,
|
||||
fresh: PlayerHomeMatch[] | undefined,
|
||||
): PlayerHomeMatch[] | undefined {
|
||||
if (!fresh) return existing;
|
||||
if (!existing) return fresh;
|
||||
|
||||
const freshMap = new Map(fresh.map((m) => [m.id, m]));
|
||||
for (const m of existing) {
|
||||
const f = freshMap.get(m.id);
|
||||
if (f) Object.assign(m, f);
|
||||
}
|
||||
const existingIds = new Set(existing.map((m) => m.id));
|
||||
for (const fm of fresh) {
|
||||
if (!existingIds.has(fm.id)) existing.push(fm);
|
||||
}
|
||||
for (let i = existing.length - 1; i >= 0; i--) {
|
||||
if (!freshMap.has(existing[i].id)) existing.splice(i, 1);
|
||||
}
|
||||
return existing;
|
||||
}
|
||||
|
||||
const homeRaw = ref<HomePayload | null>(null);
|
||||
@@ -40,12 +81,22 @@ function collectAnnouncementLines(data: HomePayload | null): string[] {
|
||||
|
||||
const lines: string[] = [];
|
||||
for (const item of source) {
|
||||
const text = item.translation?.title || item.translation?.body;
|
||||
const title = item.translation?.title?.trim();
|
||||
const text = title || stripHtml(item.translation?.body ?? '');
|
||||
if (text) lines.push(text);
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
|
||||
function collectAnnouncementItems(data: HomePayload | null): PlayerAnnouncementItem[] {
|
||||
if (!data) return [];
|
||||
const source =
|
||||
data.announcements && data.announcements.length > 0
|
||||
? data.announcements
|
||||
: [...(data.ticker ?? []), ...(data.notices ?? [])];
|
||||
return source.filter((item) => item.translation?.title || item.translation?.body);
|
||||
}
|
||||
|
||||
/** 管理端公共内容 → 玩家端首页/跑马灯(单例,避免重复请求) */
|
||||
export function usePlayerHome() {
|
||||
const { t } = useI18n();
|
||||
@@ -64,26 +115,10 @@ export function usePlayerHome() {
|
||||
existing.announcements = fresh.announcements;
|
||||
existing.ticker = fresh.ticker;
|
||||
existing.notices = fresh.notices;
|
||||
existing.inboxEnabled = fresh.inboxEnabled;
|
||||
|
||||
if (fresh.hotMatches && existing.hotMatches) {
|
||||
const freshMap = new Map(fresh.hotMatches.map((m) => [m.id, m]));
|
||||
for (const m of existing.hotMatches) {
|
||||
const f = freshMap.get(m.id);
|
||||
if (f) Object.assign(m, f);
|
||||
}
|
||||
// 处理新增或删除的比赛
|
||||
const existingIds = new Set(existing.hotMatches.map((m) => m.id));
|
||||
for (const fm of fresh.hotMatches) {
|
||||
if (!existingIds.has(fm.id)) existing.hotMatches.push(fm);
|
||||
}
|
||||
for (let i = existing.hotMatches.length - 1; i >= 0; i--) {
|
||||
if (!freshMap.has(existing.hotMatches[i].id)) {
|
||||
existing.hotMatches.splice(i, 1);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
existing.hotMatches = fresh.hotMatches;
|
||||
}
|
||||
existing.hotMatches = mergeMatchList(existing.hotMatches, fresh.hotMatches);
|
||||
existing.upcomingMatches = mergeMatchList(existing.upcomingMatches, fresh.upcomingMatches);
|
||||
} else {
|
||||
homeRaw.value = fresh;
|
||||
}
|
||||
@@ -94,18 +129,24 @@ export function usePlayerHome() {
|
||||
}
|
||||
}
|
||||
|
||||
const banners = computed(() => resolveBanners(homeRaw.value?.banners));
|
||||
const banners = computed(() => resolveBanners(homeRaw.value?.banners as BannerItem[] | undefined));
|
||||
const bannerItems = computed(() => homeRaw.value?.banners ?? []);
|
||||
const announcements = computed(() =>
|
||||
resolveAnnouncements(collectAnnouncementLines(homeRaw.value), t('home.announcement_default')),
|
||||
);
|
||||
const announcementItems = computed(() => collectAnnouncementItems(homeRaw.value));
|
||||
const hotMatches = computed(() => homeRaw.value?.hotMatches ?? []);
|
||||
const upcomingMatches = computed(() => homeRaw.value?.upcomingMatches ?? []);
|
||||
|
||||
return {
|
||||
homeRaw,
|
||||
loading,
|
||||
load,
|
||||
banners,
|
||||
bannerItems,
|
||||
announcements,
|
||||
announcementItems,
|
||||
hotMatches,
|
||||
upcomingMatches,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user