重构
This commit is contained in:
@@ -93,6 +93,28 @@ class UpdateProfileDto {
|
||||
username?: string;
|
||||
}
|
||||
|
||||
function safeTimeZone(input?: string): string {
|
||||
const value = input?.trim();
|
||||
if (!value) return 'UTC';
|
||||
try {
|
||||
new Intl.DateTimeFormat('en-US', { timeZone: value }).format(new Date());
|
||||
return value;
|
||||
} catch {
|
||||
return 'UTC';
|
||||
}
|
||||
}
|
||||
|
||||
function dayKeyInTimeZone(date: Date, timeZone: string): string {
|
||||
const parts = new Intl.DateTimeFormat('en-US', {
|
||||
timeZone,
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
day: '2-digit',
|
||||
}).formatToParts(date);
|
||||
const map = new Map(parts.map((part) => [part.type, part.value]));
|
||||
return `${map.get('year')}-${map.get('month')}-${map.get('day')}`;
|
||||
}
|
||||
|
||||
@ApiTags('Player')
|
||||
@Controller('player')
|
||||
@UseGuards(JwtAuthGuard, PlayerGuard)
|
||||
@@ -160,6 +182,7 @@ export class PlayerController {
|
||||
async home(
|
||||
@CurrentUser('locale') userLocale: string | undefined,
|
||||
@Headers('x-locale') headerLocale?: string,
|
||||
@Headers('x-time-zone') headerTimeZone?: string,
|
||||
) {
|
||||
const locale = userLocale || headerLocale || 'zh-CN';
|
||||
const [banners, announcements, allMatches] = await Promise.all([
|
||||
@@ -167,19 +190,13 @@ export class PlayerController {
|
||||
this.content.listActiveAnnouncements(locale),
|
||||
this.matches.listPublished(locale, undefined, { includeMarkets: false }),
|
||||
]);
|
||||
const now = new Date();
|
||||
const isKickoffToday = (startTime: string) => {
|
||||
const d = new Date(startTime);
|
||||
return (
|
||||
d.getFullYear() === now.getFullYear() &&
|
||||
d.getMonth() === now.getMonth() &&
|
||||
d.getDate() === now.getDate()
|
||||
);
|
||||
};
|
||||
const timeZone = safeTimeZone(headerTimeZone);
|
||||
const todayKey = dayKeyInTimeZone(new Date(), timeZone);
|
||||
const hotMatches = (allMatches as Array<{ isHot?: boolean }>).filter((m) => m.isHot);
|
||||
const todayMatches = (allMatches as Array<{ startTime: string }>).filter((m) =>
|
||||
isKickoffToday(m.startTime),
|
||||
);
|
||||
const todayMatches = (allMatches as Array<{ startTime: string }>).filter((m) => {
|
||||
const kickoff = new Date(m.startTime);
|
||||
return !Number.isNaN(kickoff.getTime()) && dayKeyInTimeZone(kickoff, timeZone) === todayKey;
|
||||
});
|
||||
return jsonResponse({
|
||||
banners,
|
||||
announcements,
|
||||
|
||||
Reference in New Issue
Block a user