Files
xk-admin/apps/web-antd/src/layouts/basic.vue

286 lines
7.6 KiB
Vue
Raw Normal View History

2024-05-19 21:20:42 +08:00
<script lang="ts" setup>
import type { NotificationItem } from '@vben/layouts';
import {computed, onUnmounted, ref, watch} from 'vue';
2024-06-01 23:15:29 +08:00
import { AuthenticationLoginExpiredModal } from '@vben/common-ui';
2025-03-19 17:06:36 +08:00
2025-03-07 08:18:45 +08:00
// import { VBEN_DOC_URL, VBEN_GITHUB_URL } from '@vben/constants';
import { useWatermark } from '@vben/hooks';
2025-03-07 08:18:45 +08:00
// import { BookOpenText, CircleHelp, MdiGithub } from '@vben/icons';
2025-03-19 17:06:36 +08:00
// import { CircleHelp } from '@vben/icons';
import {
BasicLayout,
LockScreen,
Notification,
UserDropdown,
} from '@vben/layouts';
import { preferences } from '@vben/preferences';
import { useAccessStore, useUserStore } from '@vben/stores';
2025-03-07 08:18:45 +08:00
// import { openWindow } from '@vben/utils';
2024-05-19 21:20:42 +08:00
import {
Bell,
CalendarClock,
Megaphone,
2026-05-30 16:58:05 +08:00
Users,
} from 'lucide-vue-next';
2025-03-07 08:18:45 +08:00
// import { $t } from '#/locales';
import DoctorTransferFloat from '#/components/doctor-transfer-float/DoctorTransferFloat.vue';
2026-08-04 14:53:11 +08:00
import HeaderVipBadge from '#/components/vip/HeaderVipBadge.vue';
2024-07-30 21:10:28 +08:00
import { useAuthStore } from '#/store';
import LoginForm from '#/views/_core/authentication/login.vue';
2026-05-30 16:58:05 +08:00
import SwitchAccountModal from '#/layouts/components/SwitchAccountModal.vue';
import { useRouter } from 'vue-router';
import {getNoticeListApi, readAllApi} from "#/views/notice/api";
import {formatTimeToRelative} from "#/util/tool";
import {notification} from "ant-design-vue";
const router = useRouter();
2026-05-30 16:58:05 +08:00
const switchAccountModalRef = ref<InstanceType<typeof SwitchAccountModal>>();
// 样式相关
const typeIcons = {
0: Megaphone,
1: Bell,
2: CalendarClock,
};
const typeColors = {
0: 'text-sky-500',
1: 'text-amber-500',
2: 'text-emerald-500',
};
const typeBgColors = {
0: 'bg-sky-50',
1: 'bg-amber-50',
2: 'bg-emerald-50',
};
const typeBorderColors = {
0: 'border-sky-200',
1: 'border-amber-200',
2: 'border-emerald-200',
};
const notifications = ref<NotificationItem[]>([]);
// 获取消息列表数据
const getNoticeList = async () => {
try {
const res = await getNoticeListApi({
status: 0,
});
let message = '未读消息';
if (res.items.length > 0) {
if (res.items.length > notifications.value.length) {
if (notifications.value.length === 0) {
message = '新消息';
}
notification.info({
message: `您有${message}`,
description: `您有 ${res.items.length}${message}请注意查收`,
duration: 5,
});
}
notifications.value = res.items.map(item => ({
id: item.id,
icon: typeIcons[item.type],
// 将API返回的数据映射到组件需要的格式
message: item.title,
title: getMessageTypeName(item.type),
date: formatTimeToRelative(item.created_at),
color: typeColors[item.type],
isRead: item.status
}));
2025-04-12 17:03:31 +08:00
} else {
notifications.value = [];
}
} catch (error) {
console.error('获取消息列表失败:', error);
}
};
getNoticeList();
// 每隔二十秒刷新一次
let intervalId = setInterval(getNoticeList, 20_000);
// 封装定时器重启逻辑
const restartInterval = () => {
clearInterval(intervalId);
getNoticeList(); // 立即获取最新数据
intervalId = setInterval(getNoticeList, 20_000);
};
// 处理跨标签页修改
const handleStorageChange = (event) => {
if (event.key === 'readStatus') {
restartInterval();
}
};
// 处理当前标签页修改(核心新增部分)
const handleLocalChange = () => {
restartInterval();
};
// 添加双重监听
window.addEventListener('storage', handleStorageChange); // 跨标签页监听
const originalSetItem = localStorage.setItem.bind(localStorage);
// 拦截当前页的 localStorage.setItem 操作
localStorage.setItem = (key, value) => {
originalSetItem(key, value);
if (key === 'readStatus') {
handleLocalChange(); // 手动触发当前页回调
}
};
// 组件卸载时清理
onUnmounted(() => {
clearInterval(intervalId);
window.removeEventListener('storage', handleStorageChange);
localStorage.setItem = originalSetItem; // 恢复原始方法
});
// 消息类型定义
const messageTypes = [
{ id: -1, name: '全部消息' },
{ id: 0, name: '系统公告', icon: Megaphone },
{ id: 1, name: '系统通知', icon: Bell },
{ id: 2, name: '周报提醒', icon: CalendarClock },
];
// 根据类型ID获取类型名称
const getMessageTypeName = (typeId) => {
const type = messageTypes.find(t => t.id === typeId);
return type ? type.name : '未知类型';
};
const goNoticeList = () => {
// TODO 跳转通知列表
router.push('/notice/list');
};
// 处理消息点击
const handleItemClick = (item) => {
notifications.value.forEach((value) => {
if (value.id === item.id) {
value.status = 1;
value.isRead = 1;
}
});
router.push(`/notice/detail/${item.id}`);
};
2024-05-19 21:20:42 +08:00
2024-07-30 21:10:28 +08:00
const userStore = useUserStore();
const authStore = useAuthStore();
const accessStore = useAccessStore();
const { destroyWatermark, updateWatermark } = useWatermark();
const showDot = computed(() =>
notifications.value.some((item) => !item.isRead),
);
/** 与原先悬浮窗一致:有 doctor_id 且绑定门店才显示传方入口 */
const showDoctorTransfer = computed(() => {
const info = userStore.userInfo as Record<string, any> | null | undefined;
if (!info) return false;
return !!(info.doctor_id && info.store_id);
});
2024-05-19 21:20:42 +08:00
const menus = computed(() => [
2026-05-30 16:58:05 +08:00
{
handler: () => {
switchAccountModalRef.value?.openModal();
},
icon: Users,
text: '切换账号',
},
2024-05-19 21:20:42 +08:00
]);
const avatar = computed(() => {
2024-07-30 21:10:28 +08:00
return userStore.userInfo?.avatar ?? preferences.app.defaultAvatar;
});
async function handleLogout() {
await authStore.logout(false);
2025-03-07 08:18:45 +08:00
destroyWatermark();
2024-05-19 21:20:42 +08:00
}
function handleNoticeClear() {
readAllApi().then(() => {
notifications.value = [];
// 成功后可以重新获取数据或者直接更新本地状态
notification.success({
message: '标记成功',
duration: 2,
});
});
2024-05-19 21:20:42 +08:00
}
function handleMakeAll() {
notifications.value.forEach((item) => (item.isRead = true));
}
watch(
() => preferences.app.watermark,
async (enable) => {
if (enable) {
await updateWatermark({
2025-03-07 08:18:45 +08:00
// 这里更改水印内容
// content: `${userStore.userInfo?.nick_name || import.meta.env.VITE_APP_TITLE}`,
content: `${userStore.userInfo?.nick_name}\r\n${userStore.userInfo?.phone || import.meta.env.VITE_APP_TITLE}`,
});
} else {
destroyWatermark();
}
},
{
immediate: true,
},
);
2024-05-19 21:20:42 +08:00
</script>
<template>
<BasicLayout @clear-preferences-and-logout="handleLogout">
<!-- index=55紧挨全局搜索(50)右侧 -->
<template #header-right-55>
<DoctorTransferFloat v-if="showDoctorTransfer" />
</template>
2024-05-19 21:20:42 +08:00
<template #user-dropdown>
2026-08-04 14:53:11 +08:00
<div class="flex items-center">
<HeaderVipBadge />
<UserDropdown
:avatar
:description="userStore.userInfo?.email"
:menus
:tag-text="userStore.userInfo?.roles?.name"
:text="userStore.userInfo?.nick_name"
@logout="handleLogout"
/>
</div>
2024-05-19 21:20:42 +08:00
</template>
<template #notification>
<Notification
:dot="showDot"
2024-05-19 21:20:42 +08:00
:notifications="notifications"
@clear="handleNoticeClear"
@make-all="handleMakeAll"
@read="handleItemClick"
@view-all="goNoticeList"
2024-05-19 21:20:42 +08:00
/>
</template>
<template #extra>
<AuthenticationLoginExpiredModal
2024-07-30 21:10:28 +08:00
v-model:open="accessStore.loginExpired"
2024-07-18 21:31:34 +08:00
:avatar
>
<LoginForm />
</AuthenticationLoginExpiredModal>
2026-05-30 16:58:05 +08:00
<SwitchAccountModal ref="switchAccountModalRef" />
</template>
<template #lock-screen>
<LockScreen :avatar @to-login="handleLogout" />
</template>
2024-05-19 21:20:42 +08:00
</BasicLayout>
</template>