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:
parent
52442082a2
commit
33545be45b
@ -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 {
|
export function getCurrentTimezone(): string {
|
||||||
return localStorage.getItem('timezone') || 'Asia/Shanghai'
|
const savedTimezone = localStorage.getItem('timezone')
|
||||||
|
if (savedTimezone) {
|
||||||
|
return savedTimezone
|
||||||
|
}
|
||||||
|
// 如果没有保存的时区,自动获取用户系统时区
|
||||||
|
return getUserSystemTimezone()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -210,6 +210,7 @@ import { Icon } from '@iconify/vue'
|
|||||||
import { getCurrentLocale, setLocale, locales, initLocale, t } from '../../shared/i18n'
|
import { getCurrentLocale, setLocale, locales, initLocale, t } from '../../shared/i18n'
|
||||||
import { useAuthStore } from '../../shared/stores/auth'
|
import { useAuthStore } from '../../shared/stores/auth'
|
||||||
import { getEnvironmentConfig, updateEnvironmentConfig, restartEnvironment, type EnvironmentConfig } from '../../shared/api/system'
|
import { getEnvironmentConfig, updateEnvironmentConfig, restartEnvironment, type EnvironmentConfig } from '../../shared/api/system'
|
||||||
|
import { getCurrentTimezone } from '../../shared/utils/timezone'
|
||||||
|
|
||||||
const message = useMessage()
|
const message = useMessage()
|
||||||
const dialog = useDialog()
|
const dialog = useDialog()
|
||||||
@ -236,7 +237,7 @@ const systemSettings = reactive({
|
|||||||
appName: localStorage.getItem('appName') || 'Jingrow',
|
appName: localStorage.getItem('appName') || 'Jingrow',
|
||||||
language: getCurrentLocale(),
|
language: getCurrentLocale(),
|
||||||
itemsPerPage: parseInt(localStorage.getItem('itemsPerPage') || '10'),
|
itemsPerPage: parseInt(localStorage.getItem('itemsPerPage') || '10'),
|
||||||
timezone: localStorage.getItem('timezone') || 'Asia/Shanghai'
|
timezone: getCurrentTimezone()
|
||||||
})
|
})
|
||||||
|
|
||||||
// 语言选项
|
// 语言选项
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user