admin-vben5/apps/web-antd/src/store/notify.ts

144 lines
3.4 KiB
TypeScript
Raw Normal View History

2024-08-07 08:57:56 +08:00
import type { NotificationItem } from '@vben/layouts';
import { computed, ref, watch } from 'vue';
import { useAppConfig } from '@vben/hooks';
2024-09-27 14:14:29 +08:00
import { SvgMessageUrl } from '@vben/icons';
2024-09-10 09:05:16 +08:00
import { $t } from '@vben/locales';
import { useAccessStore, useUserStore } from '@vben/stores';
2024-08-07 08:57:56 +08:00
import { useEventSource } from '@vueuse/core';
import { notification } from 'ant-design-vue';
import dayjs from 'dayjs';
import { defineStore } from 'pinia';
const { apiURL, clientId } = useAppConfig(
import.meta.env,
import.meta.env.PROD,
);
export const useNotifyStore = defineStore(
'app-notify',
() => {
/**
* return才会被持久化
*/
2024-08-07 08:57:56 +08:00
const notificationList = ref<NotificationItem[]>([]);
const userStore = useUserStore();
const userId = computed(() => {
return userStore.userInfo?.userId || '0';
});
const notifications = computed(() => {
return notificationList.value.filter(
(item) => item.userId === userId.value,
);
});
2024-08-07 08:57:56 +08:00
/**
* sse消息
*/
function startListeningMessage() {
const accessStore = useAccessStore();
const token = accessStore.accessToken;
const sseAddr = `${apiURL}/resource/sse?clientid=${clientId}&Authorization=Bearer ${token}`;
const { data } = useEventSource(sseAddr, [], {
autoReconnect: {
delay: 1000,
onFailed() {
2024-09-10 09:05:16 +08:00
console.error('sse重连失败.');
2024-08-07 08:57:56 +08:00
},
retries: 3,
},
});
watch(data, (message) => {
if (!message) return;
console.log(`接收到消息: ${message}`);
notification.success({
description: message,
duration: 3,
2024-09-10 09:05:16 +08:00
message: $t('component.notice.received'),
2024-08-07 08:57:56 +08:00
});
2024-08-07 10:27:53 +08:00
notificationList.value.unshift({
2024-09-27 14:14:29 +08:00
// avatar: `https://api.multiavatar.com/${random(0, 10_000)}.png`, 随机头像
avatar: SvgMessageUrl,
2024-08-07 08:57:56 +08:00
date: dayjs().format('YYYY-MM-DD HH:mm:ss'),
isRead: false,
message,
2024-09-10 09:05:16 +08:00
title: $t('component.notice.title'),
userId: userId.value,
2024-08-07 08:57:56 +08:00
});
2024-10-18 15:02:39 +08:00
// 需要手动置空 vue3在值相同时不会触发watch
2024-08-07 08:57:56 +08:00
data.value = null;
});
}
/**
*
*/
function setAllRead() {
notificationList.value
.filter((item) => item.userId === userId.value)
.forEach((item) => {
item.isRead = true;
});
2024-08-07 08:57:56 +08:00
}
/**
*
* @param item
*/
function setRead(item: NotificationItem) {
!item.isRead && (item.isRead = true);
}
/**
*
*/
function clearAllMessage() {
notificationList.value = notificationList.value.filter(
(item) => item.userId !== userId.value,
);
2024-08-07 08:57:56 +08:00
}
/**
*
* 退
*/
function $reset() {
// notificationList.value = [];
}
/**
*
*/
const showDot = computed(() =>
notificationList.value
.filter((item) => item.userId === userId.value)
.some((item) => !item.isRead),
2024-08-07 08:57:56 +08:00
);
return {
$reset,
clearAllMessage,
notificationList,
notifications,
2024-08-07 08:57:56 +08:00
setAllRead,
setRead,
showDot,
startListeningMessage,
};
},
{
persist: {
2024-09-26 15:32:33 +08:00
pick: ['notificationList'],
2024-08-07 08:57:56 +08:00
},
},
);