31 changed files with 272 additions and 422 deletions
@ -1,23 +0,0 @@ |
|||
import type { TimezoneOption } from '@vben/types'; |
|||
|
|||
import { requestClient } from '#/api/request'; |
|||
|
|||
/** |
|||
* 获取系统支持的时区列表 |
|||
*/ |
|||
export async function getTimezoneOptionsApi() { |
|||
return requestClient.get<TimezoneOption[]>('/profile/timezone'); |
|||
} |
|||
/** |
|||
* 获取用户时区 |
|||
*/ |
|||
export async function getUserTimezoneApi(): Promise<null | string | undefined> { |
|||
return requestClient.get<null | string | undefined>('/user/timezone'); |
|||
} |
|||
/** |
|||
* 设置用户时区 |
|||
* @param timezone 时区 |
|||
*/ |
|||
export async function setUserTimezoneApi(timezone: string) { |
|||
return requestClient.post('/user/setTimezone', { timezone }); |
|||
} |
|||
@ -1,58 +0,0 @@ |
|||
import { ref } from 'vue'; |
|||
|
|||
import { getTimezone, setDefaultTimezone } from '@vben/utils'; |
|||
|
|||
import { acceptHMRUpdate, defineStore } from 'pinia'; |
|||
|
|||
import { getUserTimezoneApi, setUserTimezoneApi } from '#/api'; |
|||
|
|||
const useUserProfileStore = defineStore('user-profile', () => { |
|||
const timezoneRef = ref( |
|||
getTimezone() || new Intl.DateTimeFormat().resolvedOptions().timeZone, |
|||
); |
|||
|
|||
/** |
|||
* 设置用户时区 |
|||
* Set the user's timezone |
|||
* @param timezone 时区字符串 |
|||
*/ |
|||
async function setTimezone(timezone: string) { |
|||
// 保存用户的时区设置
|
|||
await setUserTimezoneApi(timezone); |
|||
timezoneRef.value = timezone; |
|||
// 设置dayjs默认时区
|
|||
setDefaultTimezone(timezone); |
|||
} |
|||
|
|||
/** |
|||
* 初始化用户时区 |
|||
* Initialize the user's timezone |
|||
*/ |
|||
async function initTimezone() { |
|||
// 从服务器获取用户时区
|
|||
const timezone = await getUserTimezoneApi(); |
|||
if (timezone) { |
|||
timezoneRef.value = timezone; |
|||
// 设置dayjs默认时区
|
|||
setDefaultTimezone(timezone); |
|||
} |
|||
} |
|||
|
|||
initTimezone().catch((error) => { |
|||
console.error('Failed to initialize timezone during store setup:', error); |
|||
}); |
|||
|
|||
return { |
|||
timezone: timezoneRef, |
|||
setTimezone, |
|||
initTimezone, |
|||
}; |
|||
}); |
|||
|
|||
export { useUserProfileStore }; |
|||
|
|||
// 解决热更新问题
|
|||
const hot = import.meta.hot; |
|||
if (hot) { |
|||
hot.accept(acceptHMRUpdate(useUserProfileStore, hot)); |
|||
} |
|||
@ -1,23 +0,0 @@ |
|||
import type { TimezoneOption } from '@vben/types'; |
|||
|
|||
import { requestClient } from '#/api/request'; |
|||
|
|||
/** |
|||
* 获取系统支持的时区列表 |
|||
*/ |
|||
export async function getTimezoneOptionsApi() { |
|||
return requestClient.get<TimezoneOption[]>('/profile/timezone'); |
|||
} |
|||
/** |
|||
* 获取用户时区 |
|||
*/ |
|||
export async function getUserTimezoneApi(): Promise<null | string | undefined> { |
|||
return requestClient.get<null | string | undefined>('/user/timezone'); |
|||
} |
|||
/** |
|||
* 设置用户时区 |
|||
* @param timezone 时区 |
|||
*/ |
|||
export async function setUserTimezoneApi(timezone: string) { |
|||
return requestClient.post('/user/setTimezone', { timezone }); |
|||
} |
|||
@ -1,58 +0,0 @@ |
|||
import { ref } from 'vue'; |
|||
|
|||
import { getTimezone, setDefaultTimezone } from '@vben/utils'; |
|||
|
|||
import { acceptHMRUpdate, defineStore } from 'pinia'; |
|||
|
|||
import { getUserTimezoneApi, setUserTimezoneApi } from '#/api'; |
|||
|
|||
const useUserProfileStore = defineStore('user-profile', () => { |
|||
const timezoneRef = ref( |
|||
getTimezone() || new Intl.DateTimeFormat().resolvedOptions().timeZone, |
|||
); |
|||
|
|||
/** |
|||
* 设置用户时区 |
|||
* Set the user's timezone |
|||
* @param timezone 时区字符串 |
|||
*/ |
|||
async function setTimezone(timezone: string) { |
|||
// 保存用户的时区设置
|
|||
await setUserTimezoneApi(timezone); |
|||
timezoneRef.value = timezone; |
|||
// 设置dayjs默认时区
|
|||
setDefaultTimezone(timezone); |
|||
} |
|||
|
|||
/** |
|||
* 初始化用户时区 |
|||
* Initialize the user's timezone |
|||
*/ |
|||
async function initTimezone() { |
|||
// 从服务器获取用户时区
|
|||
const timezone = await getUserTimezoneApi(); |
|||
if (timezone) { |
|||
timezoneRef.value = timezone; |
|||
// 设置dayjs默认时区
|
|||
setDefaultTimezone(timezone); |
|||
} |
|||
} |
|||
|
|||
initTimezone().catch((error) => { |
|||
console.error('Failed to initialize timezone during store setup:', error); |
|||
}); |
|||
|
|||
return { |
|||
timezone: timezoneRef, |
|||
setTimezone, |
|||
initTimezone, |
|||
}; |
|||
}); |
|||
|
|||
export { useUserProfileStore }; |
|||
|
|||
// 解决热更新问题
|
|||
const hot = import.meta.hot; |
|||
if (hot) { |
|||
hot.accept(acceptHMRUpdate(useUserProfileStore, hot)); |
|||
} |
|||
@ -1,23 +0,0 @@ |
|||
import type { TimezoneOption } from '@vben/types'; |
|||
|
|||
import { requestClient } from '#/api/request'; |
|||
|
|||
/** |
|||
* 获取系统支持的时区列表 |
|||
*/ |
|||
export async function getTimezoneOptionsApi() { |
|||
return requestClient.get<TimezoneOption[]>('/profile/timezone'); |
|||
} |
|||
/** |
|||
* 获取用户时区 |
|||
*/ |
|||
export async function getUserTimezoneApi(): Promise<null | string | undefined> { |
|||
return requestClient.get<null | string | undefined>('/user/timezone'); |
|||
} |
|||
/** |
|||
* 设置用户时区 |
|||
* @param timezone 时区 |
|||
*/ |
|||
export async function setUserTimezoneApi(timezone: string) { |
|||
return requestClient.post('/user/setTimezone', { timezone }); |
|||
} |
|||
@ -1,58 +0,0 @@ |
|||
import { ref } from 'vue'; |
|||
|
|||
import { getTimezone, setDefaultTimezone } from '@vben/utils'; |
|||
|
|||
import { acceptHMRUpdate, defineStore } from 'pinia'; |
|||
|
|||
import { getUserTimezoneApi, setUserTimezoneApi } from '#/api'; |
|||
|
|||
const useUserProfileStore = defineStore('user-profile', () => { |
|||
const timezoneRef = ref( |
|||
getTimezone() || new Intl.DateTimeFormat().resolvedOptions().timeZone, |
|||
); |
|||
|
|||
/** |
|||
* 设置用户时区 |
|||
* Set the user's timezone |
|||
* @param timezone 时区字符串 |
|||
*/ |
|||
async function setTimezone(timezone: string) { |
|||
// 保存用户的时区设置
|
|||
await setUserTimezoneApi(timezone); |
|||
timezoneRef.value = timezone; |
|||
// 设置dayjs默认时区
|
|||
setDefaultTimezone(timezone); |
|||
} |
|||
|
|||
/** |
|||
* 初始化用户时区 |
|||
* Initialize the user's timezone |
|||
*/ |
|||
async function initTimezone() { |
|||
// 从服务器获取用户时区
|
|||
const timezone = await getUserTimezoneApi(); |
|||
if (timezone) { |
|||
timezoneRef.value = timezone; |
|||
// 设置dayjs默认时区
|
|||
setDefaultTimezone(timezone); |
|||
} |
|||
} |
|||
|
|||
initTimezone().catch((error) => { |
|||
console.error('Failed to initialize timezone during store setup:', error); |
|||
}); |
|||
|
|||
return { |
|||
timezone: timezoneRef, |
|||
setTimezone, |
|||
initTimezone, |
|||
}; |
|||
}); |
|||
|
|||
export { useUserProfileStore }; |
|||
|
|||
// 解决热更新问题
|
|||
const hot = import.meta.hot; |
|||
if (hot) { |
|||
hot.accept(acceptHMRUpdate(useUserProfileStore, hot)); |
|||
} |
|||
@ -1,9 +0,0 @@ |
|||
/** |
|||
* 时区选项 |
|||
*/ |
|||
interface TimezoneOption { |
|||
offset: number; |
|||
timezone: string; |
|||
} |
|||
|
|||
export type { TimezoneOption }; |
|||
@ -1,3 +1,4 @@ |
|||
export * from './access'; |
|||
export * from './tabbar'; |
|||
export * from './timezone'; |
|||
export * from './user'; |
|||
|
|||
@ -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)); |
|||
} |
|||
@ -1,3 +1,4 @@ |
|||
export * from './auth'; |
|||
export * from './menu'; |
|||
export * from './timezone'; |
|||
export * from './user'; |
|||
|
|||
@ -0,0 +1,30 @@ |
|||
import type { TimezoneOption } from '@vben/types'; |
|||
|
|||
import { requestClient } from '#/api/request'; |
|||
|
|||
/** |
|||
* 获取系统支持的时区列表 |
|||
*/ |
|||
export async function getTimezoneOptionsApi() { |
|||
const dataList = |
|||
(await requestClient.get<TimezoneOption[]>( |
|||
'/timezone/getTimezoneOptions', |
|||
)) || []; |
|||
return dataList.map((item) => ({ |
|||
label: item.timezone, |
|||
value: item.timezone, |
|||
})); |
|||
} |
|||
/** |
|||
* 获取用户时区 |
|||
*/ |
|||
export async function getTimezoneApi(): Promise<null | string | undefined> { |
|||
return requestClient.get<null | string | undefined>('/timezone/getTimezone'); |
|||
} |
|||
/** |
|||
* 设置用户时区 |
|||
* @param timezone 时区 |
|||
*/ |
|||
export async function setTimezoneApi(timezone: string) { |
|||
return requestClient.post('/timezone/setTimezone', { timezone }); |
|||
} |
|||
@ -0,0 +1,20 @@ |
|||
import { setTimezoneHandler } from '@vben/stores'; |
|||
|
|||
import { getTimezoneApi, getTimezoneOptionsApi, setTimezoneApi } from '#/api'; |
|||
|
|||
/** |
|||
* 初始化时区处理,通过API保存时区设置 |
|||
*/ |
|||
export function initTimezone() { |
|||
setTimezoneHandler({ |
|||
getTimezone() { |
|||
return getTimezoneApi(); |
|||
}, |
|||
setTimezone(timezone: string) { |
|||
return setTimezoneApi(timezone); |
|||
}, |
|||
getTimezoneOptions() { |
|||
return getTimezoneOptionsApi(); |
|||
}, |
|||
}); |
|||
} |
|||
Loading…
Reference in new issue