NotificationCenter.vue 7.39 KB
<script setup lang="ts">
import type { GlobalState } from "@/qiankun/communication";

import { computed, onMounted, onUnmounted, ref } from "vue";

import { communicationManager } from "@/qiankun/communication";
import { Bell } from "@element-plus/icons-vue";
// 显示面板
const showPanel = ref(false);

// 通知列表
const notifications = ref<GlobalState["notifications"]>([]);

// 未读数量
const unreadCount = computed(() => notifications.value.length);

// 切换面板显示
const togglePanel = () => {
	showPanel.value = !showPanel.value;
};

// 获取图标
const getIcon = (type: string) => {
	const icons: Record<string, string> = {
		success: "✅",
		warning: "⚠️",
		error: "❌",
		info: "ℹ️",
	};
	return icons[type] || "ℹ️";
};

// 格式化时间
const formatTime = (timestamp: number) => {
	const now = Date.now();
	const diff = now - timestamp;

	if (diff < 60_000) return "刚刚";
	if (diff < 3_600_000) return `${Math.floor(diff / 60_000)}分钟前`;
	if (diff < 86_400_000) return `${Math.floor(diff / 3_600_000)}小时前`;
	return new Date(timestamp).toLocaleString("zh-CN", {
		month: "2-digit",
		day: "2-digit",
		hour: "2-digit",
		minute: "2-digit",
	});
};

// 移除通知
const removeNotification = (id: string) => {
	communicationManager.removeNotification(id);
};

// 清空所有通知
const clearAll = () => {
	communicationManager.clearNotifications();
	showPanel.value = false;
};

// 监听全局状态变化
let unsubscribe: (() => void) | null = null;

onMounted(() => {
	// 初始化通知列表
	const state = communicationManager.getGlobalState();
	notifications.value = state.notifications || [];

	// 监听状态变化
	unsubscribe = communicationManager.onStateChange((state) => {
		notifications.value = state.notifications || [];
	});
});

onUnmounted(() => {
	if (unsubscribe) {
		unsubscribe();
	}
});
</script>

<template>
    <div class="notification-center">
        <!-- 通知图标按钮 -->
        <div class="notification-badge" @click="togglePanel">
            <!-- <span class="icon">🔔</span> -->
            <span v-if="unreadCount > 0" class="badge">{{ unreadCount }}</span>
            <el-icon size="16" style="margin-right: 6px">
                <Bell />
            </el-icon>
            <span>消息通知</span>
        </div>

        <!-- 通知面板 -->
        <transition name="slide">
            <div v-if="showPanel" class="notification-panel">
                <div class="panel-header">
                    <h3>通知中心</h3>
                    <button v-if="notifications.length > 0"
                            class="clear-btn"
                            @click="clearAll">
                        清空
                    </button>
                </div>

                <div class="notification-list">
                    <div v-for="notification in notifications"
                         :key="notification.id"
                         class="notification-item"
                         :class="[`type-${notification.type}`]">
                        <div class="notification-icon">
                            {{ getIcon(notification.type) }}
                        </div>
                        <div class="notification-content">
                            <div class="notification-title">
                                {{ notification.title }}
                            </div>
                            <div class="notification-message">
                                {{ notification.message }}
                            </div>
                            <div class="notification-meta">
                                <span v-if="notification.from" class="from">来自: {{ notification.from }}</span>
                                <span class="time">
                                    {{
                                        formatTime(notification.timestamp)
                                    }}
                                </span>
                            </div>
                        </div>
                        <button class="close-btn"
                                @click="removeNotification(notification.id)">
                            ✕
                        </button>
                    </div>

                    <div v-if="notifications.length === 0" class="empty-state">
                        <div class="empty-icon">
                            📭
                        </div>
                        <div class="empty-text">
                            暂无通知
                        </div>
                    </div>
                </div>
            </div>
        </transition>
    </div>
</template>

<style scoped>
.notification-center {
	position: relative;
}

.notification-badge {
	position: relative;
	display: flex;
	align-items: center;
	justify-content: center;
	font-size: 12px;
	cursor: pointer;
	border-radius: 50%;
	transition: background-color 0.2s;
}

.notification-badge:hover {
	background-color: rgb(0 0 0 / 5%);
}

.badge {
	position: absolute;
	top: 4px;
	right: 4px;
	display: flex;
	align-items: center;
	justify-content: center;
	min-width: 18px;
	height: 18px;
	padding: 0 4px;
	font-size: 12px;
	color: white;
	background-color: #ff4d4f;
	border-radius: 9px;
}

.notification-panel {
	position: absolute;
	top: 50px;
	right: 0;
	z-index: 1000;
	display: flex;
	flex-direction: column;
	width: 400px;
	max-height: 600px;
	background: white;
	border-radius: 8px;
	box-shadow: 0 4px 20px rgb(0 0 0 / 15%);
}

.panel-header {
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: 16px 20px;
	border-bottom: 1px solid #f0f0f0;
}

.panel-header h3 {
	margin: 0;
	font-size: 16px;
	font-weight: 600;
}

.clear-btn {
	padding: 4px 12px;
	font-size: 14px;
	cursor: pointer;
	background: transparent;
	border: 1px solid #d9d9d9;
	border-radius: 4px;
	transition: all 0.2s;
}

.clear-btn:hover {
	color: #1890ff;
	border-color: #1890ff;
}

.notification-list {
	max-height: 500px;
	overflow-y: auto;
}

.notification-item {
	display: flex;
	gap: 12px;
	padding: 16px 20px;
	border-bottom: 1px solid #f0f0f0;
	transition: background-color 0.2s;
}

.notification-item:hover {
	background-color: #f5f5f5;
}

.notification-icon {
	flex-shrink: 0;
	font-size: 24px;
}

.notification-content {
	flex: 1;
	min-width: 0;
}

.notification-title {
	margin-bottom: 4px;
	font-size: 14px;
	font-weight: 600;
	color: #262626;
}

.notification-message {
	margin-bottom: 8px;
	font-size: 14px;
	line-height: 1.5;
	color: #595959;
}

.notification-meta {
	display: flex;
	gap: 12px;
	font-size: 12px;
	color: #8c8c8c;
}

.close-btn {
	flex-shrink: 0;
	width: 24px;
	height: 24px;
	font-size: 16px;
	color: #8c8c8c;
	cursor: pointer;
	background: transparent;
	border: none;
	border-radius: 4px;
	transition: all 0.2s;
}

.close-btn:hover {
	color: #262626;
	background-color: rgb(0 0 0 / 6%);
}

.empty-state {
	padding: 60px 20px;
	text-align: center;
}

.empty-icon {
	margin-bottom: 12px;
	font-size: 48px;
}

.empty-text {
	font-size: 14px;
	color: #8c8c8c;
}

/* 不同类型的通知样式 */
.type-success .notification-icon {
	color: #52c41a;
}

.type-warning .notification-icon {
	color: #faad14;
}

.type-error .notification-icon {
	color: #ff4d4f;
}

.type-info .notification-icon {
	color: #1890ff;
}

/* 过渡动画 */
.slide-enter-active,
.slide-leave-active {
	transition: all 0.3s ease;
}

.slide-enter-from {
	opacity: 0;
	transform: translateY(-10px);
}

.slide-leave-to {
	opacity: 0;
	transform: translateY(-10px);
}
</style>