feat: increase support for multiple time zones

* 优化实现方法
This commit is contained in:
zhongming4762
2025-10-29 19:47:10 +08:00
parent e01803ce9d
commit 4d713db546
31 changed files with 273 additions and 423 deletions

View File

@@ -93,6 +93,15 @@ type PageTransitionType = 'fade' | 'fade-down' | 'fade-slide' | 'fade-up';
*/
type AuthPageLayoutType = 'panel-center' | 'panel-left' | 'panel-right';
/**
* 时区选项
*/
interface TimezoneOption {
label: string;
offset: number;
timezone: string;
}
export type {
AccessModeType,
AuthPageLayoutType,
@@ -108,4 +117,5 @@ export type {
PreferencesButtonPositionType,
TabsStyleType,
ThemeModeType,
TimezoneOption,
};

View File

@@ -3,5 +3,4 @@ export type * from './basic';
export type * from './helper';
export type * from './menu-record';
export type * from './tabs';
export type * from './user-profile';
export type * from './vue-router';

View File

@@ -1,9 +0,0 @@
/**
* 时区选项
*/
interface TimezoneOption {
offset: number;
timezone: string;
}
export type { TimezoneOption };

View File

@@ -1,4 +1,4 @@
import type { BuiltinThemeType } from '@vben-core/typings';
import type { BuiltinThemeType, TimezoneOption } from '@vben-core/typings';
interface BuiltinThemePreset {
color: string;
@@ -81,8 +81,39 @@ const BUILT_IN_THEME_PRESETS: BuiltinThemePreset[] = [
},
];
/**
* 时区选项
*/
const DEFAULT_TIME_ZONE_OPTIONS: TimezoneOption[] = [
{
offset: -5,
timezone: 'America/New_York',
label: 'America/New_York(GMT-5)',
},
{
offset: 0,
timezone: 'Europe/London',
label: 'Europe/London(GMT0)',
},
{
offset: 8,
timezone: 'Asia/Shanghai',
label: 'Asia/Shanghai(GMT+8)',
},
{
offset: 9,
timezone: 'Asia/Tokyo',
label: 'Asia/Tokyo(GMT+9)',
},
{
offset: 9,
timezone: 'Asia/Seoul',
label: 'Asia/Seoul(GMT+9)',
},
];
export const COLOR_PRESETS = [...BUILT_IN_THEME_PRESETS].slice(0, 7);
export { BUILT_IN_THEME_PRESETS };
export { BUILT_IN_THEME_PRESETS, DEFAULT_TIME_ZONE_OPTIONS };
export type { BuiltinThemePreset };

View File

@@ -13,6 +13,7 @@ import {
LanguageToggle,
PreferencesButton,
ThemeToggle,
TimezoneButton,
} from '../../widgets';
interface Props {
@@ -172,6 +173,9 @@ function clearPreferencesAndLogout() {
<template v-else-if="slot.name === 'fullscreen'">
<VbenFullScreen class="mr-1" />
</template>
<template v-else-if="slot.name === 'timezone'">
<TimezoneButton class="mr-1 mt-[2px]" />
</template>
</slot>
</template>
</div>

View File

@@ -1,10 +1,9 @@
<script setup lang="ts">
import type { ExtendedModalApi } from '@vben-core/popup-ui';
import { ref, unref, watch } from 'vue';
import { ref, unref } from 'vue';
import { createIconifyIcon } from '@vben/icons';
import { $t } from '@vben/locales';
import { useTimezoneStore } from '@vben/stores';
import { useVbenModal } from '@vben-core/popup-ui';
import {
@@ -13,42 +12,43 @@ import {
VbenIconButton,
} from '@vben-core/shadcn-ui';
interface Props {
timezoneOptions: string[];
okHandler?: (
modalApi: ExtendedModalApi,
timezone?: string,
) => Promise<void> | void;
timezone?: string;
}
const props = defineProps<Props>();
const emit = defineEmits<{ change: [string] }>();
const TimezoneIcon = createIconifyIcon('fluent-mdl2:world-clock');
const timezoneStore = useTimezoneStore();
const timezoneRef = ref<null | string>(null);
const timezoneOptionsRef = ref<
{
label: string;
value: string;
}[]
>([]);
const [Modal, modalApi] = useVbenModal({
fullscreenButton: false,
onConfirm: () => {
props.okHandler?.(modalApi, unref(timezoneValue));
onConfirm: async () => {
try {
modalApi.setState({ confirmLoading: true });
if (timezoneRef.value) {
await timezoneStore.setTimezone(unref(timezoneRef));
}
modalApi.close();
} finally {
modalApi.setState({ confirmLoading: false });
}
},
async onOpenChange(isOpen) {
if (isOpen) {
timezoneRef.value = unref(timezoneStore.timezone);
timezoneOptionsRef.value = await timezoneStore.getTimezoneOptions();
}
},
});
const handleClick = () => {
modalApi.open();
};
const timezoneValue = ref<string | undefined>(unref(props.timezone));
watch(
() => props.timezone,
(newTimezone) => {
timezoneValue.value = unref(newTimezone);
},
);
const handleClickItem = (timezone: string) => {
timezoneValue.value = timezone;
emit('change', timezone);
};
</script>
<template>
@@ -62,15 +62,16 @@ const handleClickItem = (timezone: string) => {
</VbenIconButton>
<Modal :title="$t('ui.widgets.timezone.setTimezone')">
<div class="timezone-container">
<RadioGroup v-model="timezoneValue" class="flex flex-col gap-2">
<RadioGroup v-model="timezoneRef" class="flex flex-col gap-2">
<div
class="flex cursor-pointer items-center gap-2"
v-for="item in props.timezoneOptions"
:key="`container${item}`"
@click="handleClickItem(item)"
v-for="item in timezoneOptionsRef"
:key="`container${item.value}`"
>
<RadioGroupItem :id="item" :value="item" />
<label :for="item" class="cursor-pointer">{{ item }}</label>
<RadioGroupItem :id="item.value" :value="item.value" />
<label :for="item.value" class="cursor-pointer">{{
item.label
}}</label>
</div>
</RadioGroup>
</div>

View File

@@ -1,3 +1,4 @@
export * from './access';
export * from './tabbar';
export * from './timezone';
export * from './user';

View File

@@ -0,0 +1,125 @@
import { ref, unref } from 'vue';
import { DEFAULT_TIME_ZONE_OPTIONS } from '@vben-core/preferences';
import { getTimezone, setDefaultTimezone } from '@vben-core/shared/utils';
import { acceptHMRUpdate, defineStore } from 'pinia';
interface TimezoneHandler {
getTimezone?: () => Promise<null | string | undefined>;
getTimezoneOptions?: () => Promise<
{
label: string;
value: string;
}[]
>;
onTimezoneChange?: (timezone: string) => Promise<void>;
}
/**
* 默认时区处理模块
* 时区存储基于pinia存储插件
*/
const getDefaultTimezoneHandler = (): TimezoneHandler => {
return {
getTimezoneOptions: () => {
return Promise.resolve(
DEFAULT_TIME_ZONE_OPTIONS.map((item) => {
return {
label: item.label,
value: item.timezone,
};
}),
);
},
};
};
/**
* 自定义时区处理模块
*/
let customTimezoneHandler: null | Partial<TimezoneHandler> = null;
const setTimezoneHandler = (handler: Partial<TimezoneHandler>) => {
customTimezoneHandler = handler;
};
/**
* 获取时区处理模块
*/
const getTimezoneHandler = () => {
return {
...getDefaultTimezoneHandler(),
...customTimezoneHandler,
};
};
/**
* timezone支持模块
*/
const useTimezoneStore = defineStore(
'core-timezone',
() => {
const timezoneRef = ref(
getTimezone() || new Intl.DateTimeFormat().resolvedOptions().timeZone,
);
const timezoneHandler = getTimezoneHandler();
/**
* 初始化时区
* Initialize the timezone
*/
async function initTimezone() {
const timezone = await timezoneHandler.getTimezone?.();
if (timezone) {
timezoneRef.value = timezone;
}
// 设置dayjs默认时区
setDefaultTimezone(unref(timezoneRef));
}
/**
* 设置时区
* Set the timezone
* @param timezone 时区字符串
*/
async function setTimezone(timezone: string) {
await timezoneHandler.onTimezoneChange?.(timezone);
timezoneRef.value = timezone;
// 设置dayjs默认时区
setDefaultTimezone(timezone);
}
/**
* 获取时区选项
* Get the timezone options
*/
async function getTimezoneOptions() {
return await timezoneHandler.getTimezoneOptions();
}
initTimezone().catch((error) => {
console.error('Failed to initialize timezone during store setup:', error);
});
return {
timezone: timezoneRef,
setTimezone,
getTimezoneOptions,
};
},
{
persist: {
// 持久化
pick: ['timezone'],
},
},
);
export { setTimezoneHandler, useTimezoneStore };
// 解决热更新问题
const hot = import.meta.hot;
if (hot) {
hot.accept(acceptHMRUpdate(useTimezoneStore, hot));
}