From 33545be45be2b68cf02d89e6472aae9d7b9fb774 Mon Sep 17 00:00:00 2001 From: jingrow Date: Fri, 26 Dec 2025 23:45:41 +0800 Subject: [PATCH] feat: auto-detect user timezone in system settings Automatically detect and use user's system timezone when no timezone preference is saved. Falls back to 'Asia/Shanghai' if detection fails. --- .../frontend/src/shared/utils/timezone.ts | 26 ++++++++++++++++++- .../frontend/src/views/settings/Settings.vue | 3 ++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/apps/jingrow/frontend/src/shared/utils/timezone.ts b/apps/jingrow/frontend/src/shared/utils/timezone.ts index e470e48..8a22993 100644 --- a/apps/jingrow/frontend/src/shared/utils/timezone.ts +++ b/apps/jingrow/frontend/src/shared/utils/timezone.ts @@ -2,11 +2,35 @@ * 时区工具函数 */ +/** + * 获取用户系统时区 + * 使用浏览器 API 自动检测用户当前所在时区 + */ +export function getUserSystemTimezone(): string { + try { + // 使用 Intl API 获取用户系统时区 + const systemTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone + if (systemTimezone) { + return systemTimezone + } + } catch (error) { + console.warn('Failed to get user system timezone:', error) + } + // 如果获取失败,返回默认时区 + return 'Asia/Shanghai' +} + /** * 获取当前设置的时区 + * 优先使用用户保存的时区设置,如果没有则自动获取用户系统时区 */ export function getCurrentTimezone(): string { - return localStorage.getItem('timezone') || 'Asia/Shanghai' + const savedTimezone = localStorage.getItem('timezone') + if (savedTimezone) { + return savedTimezone + } + // 如果没有保存的时区,自动获取用户系统时区 + return getUserSystemTimezone() } /** diff --git a/apps/jingrow/frontend/src/views/settings/Settings.vue b/apps/jingrow/frontend/src/views/settings/Settings.vue index b6fc591..012d038 100644 --- a/apps/jingrow/frontend/src/views/settings/Settings.vue +++ b/apps/jingrow/frontend/src/views/settings/Settings.vue @@ -210,6 +210,7 @@ import { Icon } from '@iconify/vue' import { getCurrentLocale, setLocale, locales, initLocale, t } from '../../shared/i18n' import { useAuthStore } from '../../shared/stores/auth' import { getEnvironmentConfig, updateEnvironmentConfig, restartEnvironment, type EnvironmentConfig } from '../../shared/api/system' +import { getCurrentTimezone } from '../../shared/utils/timezone' const message = useMessage() const dialog = useDialog() @@ -236,7 +237,7 @@ const systemSettings = reactive({ appName: localStorage.getItem('appName') || 'Jingrow', language: getCurrentLocale(), itemsPerPage: parseInt(localStorage.getItem('itemsPerPage') || '10'), - timezone: localStorage.getItem('timezone') || 'Asia/Shanghai' + timezone: getCurrentTimezone() }) // 语言选项