feat: increase support for multiple time zones

* 优化实现方法
This commit is contained in:
zhongming4762 2025-10-29 20:03:21 +08:00
parent 4d713db546
commit 3eed51fd3e
6 changed files with 28 additions and 19 deletions

View File

@ -3,5 +3,9 @@ import { TIME_ZONE_OPTIONS } from '~/utils/mock-data';
import { useResponseSuccess } from '~/utils/response';
export default eventHandler(() => {
return useResponseSuccess(TIME_ZONE_OPTIONS);
const data = TIME_ZONE_OPTIONS.map((o) => ({
label: `${o.timezone} (GMT${o.offset >= 0 ? `+${o.offset}` : o.offset})`,
value: o.timezone,
}));
return useResponseSuccess(data);
});

View File

@ -1,5 +1,6 @@
import { eventHandler, readBody } from 'h3';
import { verifyAccessToken } from '~/utils/jwt-utils';
import { TIME_ZONE_OPTIONS } from '~/utils/mock-data';
import { unAuthorizedResponse, useResponseSuccess } from '~/utils/response';
import { setTimezone } from '~/utils/timezone-utils';
@ -8,7 +9,14 @@ export default eventHandler(async (event) => {
if (!userinfo) {
return unAuthorizedResponse(event);
}
const { timezone } = await readBody(event);
const body = await readBody<{ timezone?: unknown }>(event);
const timezone =
typeof body?.timezone === 'string' ? body.timezone : undefined;
const allowed = TIME_ZONE_OPTIONS.some((o) => o.timezone === timezone);
if (!timezone || !allowed) {
setResponseStatus(event, 400);
return useResponseError('Bad Request', 'Invalid timezone');
}
setTimezone(timezone);
return useResponseSuccess({});
});

View File

@ -9,7 +9,7 @@ export interface UserInfo {
export interface TimezoneOption {
offset: number;
timeZone: string;
timezone: string;
}
export const MOCK_USERS: UserInfo[] = [

View File

@ -30,8 +30,9 @@ const [Modal, modalApi] = useVbenModal({
onConfirm: async () => {
try {
modalApi.setState({ confirmLoading: true });
if (timezoneRef.value) {
await timezoneStore.setTimezone(unref(timezoneRef));
const timezone = unref(timezoneRef);
if (timezone) {
await timezoneStore.setTimezone(timezone);
}
modalApi.close();
} finally {

View File

@ -13,7 +13,7 @@ interface TimezoneHandler {
value: string;
}[]
>;
onTimezoneChange?: (timezone: string) => Promise<void>;
setTimezone?: (timezone: string) => Promise<void>;
}
/**
@ -63,13 +63,12 @@ const useTimezoneStore = defineStore(
getTimezone() || new Intl.DateTimeFormat().resolvedOptions().timeZone,
);
const timezoneHandler = getTimezoneHandler();
/**
*
* Initialize the timezone
*/
async function initTimezone() {
const timezoneHandler = getTimezoneHandler();
const timezone = await timezoneHandler.getTimezone?.();
if (timezone) {
timezoneRef.value = timezone;
@ -84,7 +83,8 @@ const useTimezoneStore = defineStore(
* @param timezone
*/
async function setTimezone(timezone: string) {
await timezoneHandler.onTimezoneChange?.(timezone);
const timezoneHandler = getTimezoneHandler();
await timezoneHandler.setTimezone?.(timezone);
timezoneRef.value = timezone;
// 设置dayjs默认时区
setDefaultTimezone(timezone);
@ -95,7 +95,8 @@ const useTimezoneStore = defineStore(
* Get the timezone options
*/
async function getTimezoneOptions() {
return await timezoneHandler.getTimezoneOptions();
const timezoneHandler = getTimezoneHandler();
return (await timezoneHandler.getTimezoneOptions?.()) || [];
}
initTimezone().catch((error) => {

View File

@ -6,14 +6,9 @@ import { requestClient } from '#/api/request';
*
*/
export async function getTimezoneOptionsApi() {
const dataList =
(await requestClient.get<TimezoneOption[]>(
return await requestClient.get<TimezoneOption[]>(
'/timezone/getTimezoneOptions',
)) || [];
return dataList.map((item) => ({
label: item.timezone,
value: item.timezone,
}));
);
}
/**
*
@ -25,6 +20,6 @@ export async function getTimezoneApi(): Promise<null | string | undefined> {
*
* @param timezone
*/
export async function setTimezoneApi(timezone: string) {
export async function setTimezoneApi(timezone: string): Promise<void> {
return requestClient.post('/timezone/setTimezone', { timezone });
}