mirror of
https://gitee.com/dapppp/ruoyi-plus-vben5.git
synced 2026-03-16 02:42:02 +08:00
feat: increase support for multiple time zones
* 优化实现方法
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
export * from './auth';
|
||||
export * from './menu';
|
||||
export * from './user';
|
||||
export * from './user-profile';
|
||||
|
||||
@@ -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,8 +1,7 @@
|
||||
<script lang="ts" setup>
|
||||
import type { ExtendedModalApi } from '@vben/common-ui';
|
||||
import type { NotificationItem } from '@vben/layouts';
|
||||
|
||||
import { computed, onMounted, ref, unref, watch } from 'vue';
|
||||
import { computed, ref, watch } from 'vue';
|
||||
|
||||
import { AuthenticationLoginExpiredModal } from '@vben/common-ui';
|
||||
import { VBEN_DOC_URL, VBEN_GITHUB_URL } from '@vben/constants';
|
||||
@@ -12,18 +11,14 @@ import {
|
||||
BasicLayout,
|
||||
LockScreen,
|
||||
Notification,
|
||||
TimezoneButton,
|
||||
UserDropdown,
|
||||
} from '@vben/layouts';
|
||||
import { preferences } from '@vben/preferences';
|
||||
import { useAccessStore, useUserStore } from '@vben/stores';
|
||||
import { openWindow } from '@vben/utils';
|
||||
|
||||
import { ElMessage } from 'element-plus';
|
||||
|
||||
import { getTimezoneOptionsApi } from '#/api';
|
||||
import { $t } from '#/locales';
|
||||
import { useAuthStore, useUserProfileStore } from '#/store';
|
||||
import { useAuthStore } from '#/store';
|
||||
import LoginForm from '#/views/_core/authentication/login.vue';
|
||||
|
||||
const notifications = ref<NotificationItem[]>([
|
||||
@@ -65,32 +60,6 @@ const showDot = computed(() =>
|
||||
notifications.value.some((item) => !item.isRead),
|
||||
);
|
||||
|
||||
const userProfileStore = useUserProfileStore();
|
||||
const computedTimezone = computed(() => unref(userProfileStore.timezone));
|
||||
|
||||
const timezoneOptions = ref<string[]>([]);
|
||||
onMounted(async () => {
|
||||
timezoneOptions.value = ((await getTimezoneOptionsApi()) || []).map(
|
||||
(item) => item.timezone,
|
||||
);
|
||||
});
|
||||
const handleSetTimezone = async (
|
||||
modalApi: ExtendedModalApi,
|
||||
timezone?: string,
|
||||
) => {
|
||||
if (!timezone) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
modalApi.setState({ confirmLoading: true });
|
||||
await userProfileStore.setTimezone(timezone);
|
||||
ElMessage.success($t('ui.widgets.timezone.setSuccess'));
|
||||
modalApi.close();
|
||||
} finally {
|
||||
modalApi.setState({ confirmLoading: false });
|
||||
}
|
||||
};
|
||||
|
||||
const menus = computed(() => [
|
||||
{
|
||||
handler: () => {
|
||||
@@ -178,14 +147,6 @@ watch(
|
||||
@make-all="handleMakeAll"
|
||||
/>
|
||||
</template>
|
||||
<template #timezone>
|
||||
<TimezoneButton
|
||||
:ok-handler="handleSetTimezone"
|
||||
:timezone="computedTimezone"
|
||||
:timezone-options="timezoneOptions"
|
||||
name="out"
|
||||
/>
|
||||
</template>
|
||||
<template #extra>
|
||||
<AuthenticationLoginExpiredModal
|
||||
v-model:open="accessStore.loginExpired"
|
||||
|
||||
@@ -1,2 +1 @@
|
||||
export * from './auth';
|
||||
export * from './user-profile';
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
Reference in New Issue
Block a user