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.
This commit is contained in:
jingrow 2025-12-26 23:45:41 +08:00
parent 52442082a2
commit 33545be45b
2 changed files with 27 additions and 2 deletions

View File

@ -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()
}
/**

View File

@ -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()
})
//