Skip to content

Let's use AI to Earn!

111

daoyian1ca3cdf93402
diff --git a/.env.development b/.env.development
index f6f06c94..a878652c 100644
--- a/.env.development
+++ b/.env.development
@@ -2,4 +2,4 @@ NEXT_PUBLIC_EVN=dev
#NEXT_PUBLIC_API_URL=http://localhost:7000/api/
-NEXT_PUBLIC_API_URL=https://apitest.aiearn.ai/api/
\ No newline at end of file
+NEXT_PUBLIC_API_URL=https://dev.aitoearn.ai/api/
\ No newline at end of file
diff --git a/src/api/notification.ts b/src/api/notification.ts
new file mode 100644
index 00000000..c5e4ffa1
--- /dev/null
+++ b/src/api/notification.ts
@@ -0,0 +1,55 @@
+import http from "@/utils/request";
+
+export interface NotificationItem {
+ id: string;
+ title: string;
+ content: string;
+ type: "system" | "user" | "material" | "other";
+ isRead: boolean;
+ createdAt: string;
+ updatedAt: string;
+}
+
+export interface NotificationListResponse {
+ list: NotificationItem[];
+ total: number;
+ page: number;
+ pageSize: number;
+}
+
+export interface NotificationDetailResponse {
+ data: NotificationItem;
+}
+
+export interface UnreadCountResponse {
+ count: number;
+}
+
+// 获取用户通知列表
+export const getNotificationList = (params: {
+ page?: number;
+ pageSize?: number;
+ type?: string;
+}) => {
+ return http.get<NotificationListResponse>("notification", { params });
+};
+
+// 获取通知详情
+export const getNotificationDetail = (id: string) => {
+ return http.get<NotificationDetailResponse>(`notification/${id}`);
+};
+
+// 标记通知为已读
+export const markNotificationAsRead = (id: string) => {
+ return http.put(`notification/read`, { id });
+};
+
+// 全部标记为已读
+export const markAllNotificationsAsRead = () => {
+ return http.put("notification/read-all");
+};
+
+// 获取未读数量
+export const getUnreadCount = () => {
+ return http.get<UnreadCountResponse>("notification/unread-count");
+};
\ No newline at end of file
diff --git a/src/app/i18n/locales/en/common.json b/src/app/i18n/locales/en/common.json
index 067cc340..e0d8791c 100644
--- a/src/app/i18n/locales/en/common.json
+++ b/src/app/i18n/locales/en/common.json
@@ -3,6 +3,18 @@
"logout": "Logout",
"unknownUser": "Unknown User",
"login": "Login",
+ "notifications": "Notifications",
+ "noNotifications": "No notifications",
+ "markAllAsRead": "Mark all as read",
+ "markAsRead": "Mark as read",
+ "unreadCount": "Unread messages",
+ "viewAll": "View all",
+ "notificationTypes": {
+ "system": "System Notification",
+ "user": "User Notification",
+ "material": "Material Notification",
+ "other": "Other Notification"
+ },
"websit": {
"termsOfService": {
"title": "Terms of Service",
diff --git a/src/app/i18n/locales/zh-CN/common.json b/src/app/i18n/locales/zh-CN/common.json
index c949f332..d6a03b1b 100644
--- a/src/app/i18n/locales/zh-CN/common.json
+++ b/src/app/i18n/locales/zh-CN/common.json
@@ -3,6 +3,18 @@
"logout": "退出登录",
"unknownUser": "未知用户",
"login": "登录",
+ "notifications": "消息通知",
+ "noNotifications": "暂无消息",
+ "markAllAsRead": "全部标记为已读",
+ "markAsRead": "标记为已读",
+ "unreadCount": "未读消息",
+ "viewAll": "查看全部",
+ "notificationTypes": {
+ "system": "系统通知",
+ "user": "用户通知",
+ "material": "素材通知",
+ "other": "其他通知"
+ },
"websit": {
"termsOfService": {
"title": "用户协议",
diff --git a/src/app/layout/LyaoutHeader.tsx b/src/app/layout/LyaoutHeader.tsx
index 643ac5a3..ff5baa47 100644
--- a/src/app/layout/LyaoutHeader.tsx
+++ b/src/app/layout/LyaoutHeader.tsx
@@ -1,13 +1,13 @@
"use client";
-import { ForwardedRef, forwardRef, memo, useEffect, useRef } from "react";
+import { ForwardedRef, forwardRef, memo, useEffect, useRef, useState } from "react";
import styles from "./styles/lyaoutHeader.module.scss";
import { useUserStore } from "@/store/user";
import Link from "next/link";
import Image from "next/image";
import LayoutNav from "@/app/layout/layoutNav";
import { NoSSR } from "@kwooshung/react-no-ssr";
-import { Button, Dropdown, MenuProps } from "antd";
+import { Button, Dropdown, MenuProps, Badge } from "antd";
import logo from "@/assets/images/logo.png";
import defaultAvatar from "./images/defaultAvatar.jpg";
import {
@@ -17,6 +17,8 @@ import {
} from "@ant-design/icons";
import { useRouter } from "next/navigation";
import { useTransClient } from "@/app/i18n/client";
+import NotificationPanel from "@/components/NotificationPanel";
+import { useNotification } from "@/hooks/useNotification";
export interface ILyaoutHeaderRef {}
@@ -42,6 +44,18 @@ function UserInfo() {
},
{
key: "2",
+ label: (
+ <div
+ onClick={() => {
+ router.push("/notification");
+ }}
+ >
+ 消息通知
+ </div>
+ ),
+ },
+ {
+ key: "3",
label: (
<div
onClick={() => {
@@ -53,7 +67,7 @@ function UserInfo() {
),
},
{
- key: "3",
+ key: "4",
label: (
<div
onClick={() => {
@@ -65,7 +79,7 @@ function UserInfo() {
),
},
{
- key: "4",
+ key: "5",
label: (
<div
onClick={() => {
@@ -106,6 +120,8 @@ const LyaoutHeader = memo(
const layoutHeader = useRef<HTMLDivElement>(null);
const router = useRouter();
const { t } = useTransClient("common");
+ const [notificationVisible, setNotificationVisible] = useState(false);
+ const { unreadCount } = useNotification();
const toggleLanguage = () => {
const newLng = userStore.lang === "zh-CN" ? "en" : "zh-CN";
@@ -130,45 +146,63 @@ const LyaoutHeader = memo(
}, []);
return (
- <div ref={layoutHeader} className={styles.layoutHeader}>
- <div className={styles.layoutHeader_wrapper}>
- <div className={styles["layoutHeader_wrapper-left"]}>
- <h1 className={styles["layoutHeader_wrapper-logo"]}>
- <Link href="/">
- <Image src={logo} alt="logo" width={50} />
- </Link>
- </h1>
- <LayoutNav />
- </div>
+ <>
+ <div ref={layoutHeader} className={styles.layoutHeader}>
+ <div className={styles.layoutHeader_wrapper}>
+ <div className={styles["layoutHeader_wrapper-left"]}>
+ <h1 className={styles["layoutHeader_wrapper-logo"]}>
+ <Link href="/">
+ <Image src={logo} alt="logo" width={50} />
+ </Link>
+ </h1>
+ <LayoutNav />
+ </div>
- <div
- className={styles["layoutHeader_wrapper-right"]}
- suppressHydrationWarning={true}
- >
- <Button
- type="text"
- icon={<GlobalOutlined />}
- onClick={toggleLanguage}
- className={styles.languageButton}
+ <div
+ className={styles["layoutHeader_wrapper-right"]}
+ suppressHydrationWarning={true}
>
- {userStore.lang === "zh-CN" ? "EN" : "中文"}
- </Button>
- <NoSSR>
- {userStore.token ? (
- <UserInfo />
- ) : (
- <Button
- onClick={() => {
- router.push("/login");
- }}
- >
- {t("login")}
- </Button>
- )}
- </NoSSR>
+ <Button
+ type="text"
+ icon={<GlobalOutlined />}
+ onClick={toggleLanguage}
+ className={styles.languageButton}
+ >
+ {userStore.lang === "zh-CN" ? "EN" : "中文"}
+ </Button>
+ <NoSSR>
+ {userStore.token && (
+ <Badge count={unreadCount} size="small">
+ <Button
+ type="text"
+ icon={<BellOutlined />}
+ onClick={() => setNotificationVisible(true)}
+ className={styles.notificationButton}
+ />
+ </Badge>
+ )}
+ {userStore.token ? (
+ <UserInfo />
+ ) : (
+ <Button
+ onClick={() => {
+ router.push("/login");
+ }}
+ >
+ {t("login")}
+ </Button>
+ )}
+ </NoSSR>
+ </div>
</div>
</div>
- </div>
+
+ {/* 通知面板 */}
+ <NotificationPanel
+ visible={notificationVisible}
+ onClose={() => setNotificationVisible(false)}
+ />
+ </>
);
}),
);
diff --git a/src/app/layout/styles/lyaoutHeader.module.scss b/src/app/layout/styles/lyaoutHeader.module.scss
index 3c26f134..34ae7c51 100644
--- a/src/app/layout/styles/lyaoutHeader.module.scss
+++ b/src/app/layout/styles/lyaoutHeader.module.scss
@@ -68,6 +68,46 @@
display: flex;
align-items: center;
gap: 16px;
+
+ .notificationButton {
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ width: 40px;
+ height: 40px;
+ border-radius: 50%;
+ transition: all 0.3s ease;
+
+ &:hover {
+ background-color: #f5f5f5;
+ transform: scale(1.05);
+ }
+
+ .anticon {
+ font-size: 18px;
+ color: #666;
+ }
+ }
+
+ .languageButton {
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ width: 40px;
+ height: 40px;
+ border-radius: 50%;
+ transition: all 0.3s ease;
+
+ &:hover {
+ background-color: #f5f5f5;
+ transform: scale(1.05);
+ }
+
+ .anticon {
+ font-size: 16px;
+ color: #666;
+ }
+ }
}
}
}
diff --git a/src/app/notification/notification.module.scss b/src/app/notification/notification.module.scss
new file mode 100644
index 00000000..fd71bc70
--- /dev/null
+++ b/src/app/notification/notification.module.scss
@@ -0,0 +1,171 @@
+.notificationPage {
+ min-height: 100vh;
+ background-color: #f5f5f5;
+ padding: 20px 0;
+}
+
+.container {
+ max-width: 1200px;
+ margin: 0 auto;
+ padding: 0 20px;
+}
+
+.tabs {
+ background: white;
+ border-radius: 8px;
+ padding: 20px;
+ box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
+
+ :global {
+ .ant-tabs-nav {
+ margin-bottom: 20px;
+ }
+
+ .ant-tabs-tab {
+ font-size: 16px;
+ font-weight: 500;
+ }
+ }
+}
+
+.notificationList {
+ .header {
+ display: flex;
+ justify-content: space-between;
+ align-items: center;
+ margin-bottom: 20px;
+ padding-bottom: 16px;
+ border-bottom: 1px solid #f0f0f0;
+
+ h2 {
+ margin: 0;
+ font-size: 20px;
+ font-weight: 600;
+ color: #333;
+ }
+ }
+}
+
+.notificationItem {
+ padding: 16px 0;
+ border-bottom: 1px solid #f0f0f0;
+ transition: background-color 0.2s;
+
+ &:hover {
+ background-color: #fafafa;
+ }
+
+ &.unread {
+ background-color: #f6ffed;
+ border-left: 4px solid #52c41a;
+ padding-left: 16px;
+ }
+
+ &:last-child {
+ border-bottom: none;
+ }
+}
+
+.notificationTitle {
+ display: flex;
+ align-items: center;
+ gap: 8px;
+ font-weight: 500;
+ font-size: 16px;
+ color: #333;
+}
+
+.notificationContent {
+ width: 100%;
+ margin-top: 8px;
+}
+
+.notificationMeta {
+ display: flex;
+ justify-content: space-between;
+ align-items: center;
+ margin-bottom: 8px;
+ font-size: 12px;
+ color: #666;
+
+ .type {
+ background-color: #f0f0f0;
+ padding: 2px 8px;
+ border-radius: 12px;
+ font-size: 11px;
+ }
+
+ .time {
+ color: #999;
+ }
+}
+
+.content {
+ color: #666;
+ line-height: 1.6;
+ margin-bottom: 8px;
+ font-size: 14px;
+}
+
+.notificationDetail {
+ h3 {
+ margin-bottom: 16px;
+ color: #333;
+ font-size: 18px;
+ font-weight: 600;
+ }
+
+ .meta {
+ display: flex;
+ justify-content: space-between;
+ margin-bottom: 16px;
+ padding: 12px 16px;
+ background-color: #f5f5f5;
+ border-radius: 6px;
+ font-size: 14px;
+ color: #666;
+ }
+
+ .content {
+ line-height: 1.8;
+ color: #333;
+ white-space: pre-wrap;
+ font-size: 14px;
+ }
+}
+
+// 响应式设计
+@media (max-width: 768px) {
+ .container {
+ padding: 0 16px;
+ }
+
+ .tabs {
+ padding: 16px;
+ }
+
+ .notificationList {
+ .header {
+ flex-direction: column;
+ gap: 12px;
+ align-items: flex-start;
+
+ h2 {
+ font-size: 18px;
+ }
+ }
+ }
+
+ .notificationMeta {
+ flex-direction: column;
+ align-items: flex-start;
+ gap: 4px;
+ }
+
+ .notificationDetail {
+ .meta {
+ flex-direction: column;
+ gap: 8px;
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/app/notification/page.tsx b/src/app/notification/page.tsx
new file mode 100644
index 00000000..badaf306
--- /dev/null
+++ b/src/app/notification/page.tsx
@@ -0,0 +1,227 @@
+"use client";
+
+import React, { useState, useEffect } from "react";
+import { List, Button, message, Spin, Empty, Tabs, Badge, Modal } from "antd";
+import { CheckOutlined, CheckCircleOutlined } from "@ant-design/icons";
+import { useTransClient } from "@/app/i18n/client";
+import {
+ getNotificationList,
+ markNotificationAsRead,
+ markAllNotificationsAsRead,
+ type NotificationItem
+} from "@/api/notification";
+import styles from "./notification.module.scss";
+
+const NotificationPage = () => {
+ const { t } = useTransClient("common");
+ const [notifications, setNotifications] = useState<NotificationItem[]>([]);
+ const [loading, setLoading] = useState(false);
+ const [selectedNotification, setSelectedNotification] = useState<NotificationItem | null>(null);
+ const [detailModalVisible, setDetailModalVisible] = useState(false);
+ const [activeTab, setActiveTab] = useState("all");
+
+ // 获取通知列表
+ const fetchNotifications = async (type?: string) => {
+ try {
+ setLoading(true);
+ const params: any = { page: 1, pageSize: 50 };
+ if (type && type !== "all") {
+ params.type = type;
+ }
+ const response = await getNotificationList(params);
+ if (response && response.data) {
+ setNotifications(response.data.list || []);
+ }
+ } catch (error) {
+ message.error("获取通知列表失败");
+ } finally {
+ setLoading(false);
+ }
+ };
+
+ // 标记单个通知为已读
+ const handleMarkAsRead = async (id: string) => {
+ try {
+ await markNotificationAsRead(id);
+ message.success("标记成功");
+ fetchNotifications(activeTab === "all" ? undefined : activeTab);
+ } catch (error) {
+ message.error("标记失败");
+ }
+ };
+
+ // 全部标记为已读
+ const handleMarkAllAsRead = async () => {
+ try {
+ await markAllNotificationsAsRead();
+ message.success("全部标记成功");
+ fetchNotifications(activeTab === "all" ? undefined : activeTab);
+ } catch (error) {
+ message.error("标记失败");
+ }
+ };
+
+ // 查看通知详情
+ const handleViewDetail = (notification: NotificationItem) => {
+ setSelectedNotification(notification);
+ setDetailModalVisible(true);
+ if (!notification.isRead) {
+ handleMarkAsRead(notification.id);
+ }
+ };
+
+ // 格式化时间
+ const formatTime = (timeString: string) => {
+ const date = new Date(timeString);
+ const now = new Date();
+ const diff = now.getTime() - date.getTime();
+ const minutes = Math.floor(diff / (1000 * 60));
+ const hours = Math.floor(diff / (1000 * 60 * 60));
+ const days = Math.floor(diff / (1000 * 60 * 60 * 24));
+
+ if (minutes < 1) return "刚刚";
+ if (minutes < 60) return `${minutes}分钟前`;
+ if (hours < 24) return `${hours}小时前`;
+ if (days < 7) return `${days}天前`;
+ return date.toLocaleDateString();
+ };
+
+ // 获取通知类型文本
+ const getNotificationTypeText = (type: string) => {
+ const typeMap: Record<string, string> = {
+ system: "系统通知",
+ user: "用户通知",
+ material: "素材通知",
+ other: "其他通知"
+ };
+ return typeMap[type] || type;
+ };
+
+ // 处理标签页切换
+ const handleTabChange = (key: string) => {
+ setActiveTab(key);
+ fetchNotifications(key === "all" ? undefined : key);
+ };
+
+ useEffect(() => {
+ fetchNotifications();
+ }, []);
+
+ return (
+ <div className={styles.notificationPage}>
+ <div className={styles.container}>
+ <Tabs
+ activeKey={activeTab}
+ onChange={handleTabChange}
+ items={[
+ {
+ key: "all",
+ label: "全部通知",
+ children: (
+ <div className={styles.notificationList}>
+ <div className={styles.header}>
+ <h2>全部通知</h2>
+ <Button
+ type="primary"
+ icon={<CheckCircleOutlined />}
+ onClick={handleMarkAllAsRead}
+ disabled={!notifications.some(n => !n.isRead)}
+ >
+ 全部标记为已读
+ </Button>
+ </div>
+ <Spin spinning={loading}>
+ {notifications.length > 0 ? (
+ <List
+ dataSource={notifications}
+ renderItem={(item) => (
+ <List.Item
+ className={`${styles.notificationItem} ${!item.isRead ? styles.unread : ""}`}
+ actions={[
+ !item.isRead && (
+ <Button
+ key="mark"
+ type="text"
+ size="small"
+ icon={<CheckOutlined />}
+ onClick={() => handleMarkAsRead(item.id)}
+ >
+ 标记为已读
+ </Button>
+ )
+ ]}
+ >
+ <List.Item.Meta
+ title={
+ <div className={styles.notificationTitle}>
+ <span>{item.title}</span>
+ {!item.isRead && <Badge status="processing" />}
+ </div>
+ }
+ description={
+ <div className={styles.notificationContent}>
+ <div className={styles.notificationMeta}>
+ <span className={styles.type}>{getNotificationTypeText(item.type)}</span>
+ <span className={styles.time}>{formatTime(item.createdAt)}</span>
+ </div>
+ <div className={styles.content}>
+ {item.content.length > 200
+ ? `${item.content.substring(0, 200)}...`
+ : item.content
+ }
+ </div>
+ <Button
+ type="link"
+ size="small"
+ onClick={() => handleViewDetail(item)}
+ >
+ 查看详情
+ </Button>
+ </div>
+ }
+ />
+ </List.Item>
+ )}
+ />
+ ) : (
+ <Empty description="暂无消息" />
+ )}
+ </Spin>
+ </div>
+ ),
+ }
+ ]}
+ className={styles.tabs}
+ />
+ </div>
+
+ {/* 通知详情弹窗 */}
+ <Modal
+ title="通知详情"
+ open={detailModalVisible}
+ onCancel={() => setDetailModalVisible(false)}
+ footer={[
+ <Button key="close" onClick={() => setDetailModalVisible(false)}>
+ 关闭
+ </Button>
+ ]}
+ width={600}
+ >
+ {selectedNotification && (
+ <div className={styles.notificationDetail}>
+ <h3>{selectedNotification.title}</h3>
+ <div className={styles.meta}>
+ <span>类型: {getNotificationTypeText(selectedNotification.type)}</span>
+ <span>时间: {formatTime(selectedNotification.createdAt)}</span>
+ </div>
+ <div className={styles.content}>
+ {selectedNotification.content}
+ </div>
+ </div>
+ )}
+ </Modal>
+ </div>
+ );
+};
+
+export default NotificationPage;
\ No newline at end of file
diff --git a/src/components/NotificationPanel.module.scss b/src/components/NotificationPanel.module.scss
new file mode 100644
index 00000000..4604670c
--- /dev/null
+++ b/src/components/NotificationPanel.module.scss
@@ -0,0 +1,75 @@
+.notificationItem {
+ padding: 12px 0;
+ border-bottom: 1px solid #f0f0f0;
+ cursor: pointer;
+ transition: background-color 0.2s;
+
+ &:hover {
+ background-color: #fafafa;
+ }
+
+ &.unread {
+ background-color: #f6ffed;
+ border-left: 3px solid #52c41a;
+ }
+}
+
+.notificationTitle {
+ display: flex;
+ align-items: center;
+ gap: 8px;
+ font-weight: 500;
+}
+
+.notificationContent {
+ width: 100%;
+}
+
+.notificationMeta {
+ display: flex;
+ justify-content: space-between;
+ align-items: center;
+ margin-bottom: 8px;
+ font-size: 12px;
+ color: #666;
+
+ .type {
+ background-color: #f0f0f0;
+ padding: 2px 6px;
+ border-radius: 4px;
+ }
+
+ .time {
+ color: #999;
+ }
+}
+
+.content {
+ color: #333;
+ line-height: 1.5;
+ margin-bottom: 8px;
+}
+
+.notificationDetail {
+ h3 {
+ margin-bottom: 16px;
+ color: #333;
+ }
+
+ .meta {
+ display: flex;
+ justify-content: space-between;
+ margin-bottom: 16px;
+ padding: 8px 12px;
+ background-color: #f5f5f5;
+ border-radius: 4px;
+ font-size: 14px;
+ color: #666;
+ }
+
+ .content {
+ line-height: 1.6;
+ color: #333;
+ white-space: pre-wrap;
+ }
+}
\ No newline at end of file
diff --git a/src/components/NotificationPanel.tsx b/src/components/NotificationPanel.tsx
new file mode 100644
index 00000000..9a4023c0
--- /dev/null
+++ b/src/components/NotificationPanel.tsx
@@ -0,0 +1,228 @@
+"use client";
+
+import React, { useState, useEffect } from "react";
+import { Badge, Button, List, Modal, message, Spin, Empty } from "antd";
+import { CheckOutlined } from "@ant-design/icons";
+import { useTransClient } from "@/app/i18n/client";
+import {
+ getNotificationList,
+ markNotificationAsRead,
+ markAllNotificationsAsRead,
+ getUnreadCount,
+ type NotificationItem
+} from "@/api/notification";
+import styles from "./NotificationPanel.module.scss";
+
+interface NotificationPanelProps {
+ visible: boolean;
+ onClose: () => void;
+}
+
+const NotificationPanel: React.FC<NotificationPanelProps> = ({ visible, onClose }) => {
+ const { t } = useTransClient("common");
+ const [notifications, setNotifications] = useState<NotificationItem[]>([]);
+ const [loading, setLoading] = useState(false);
+ const [unreadCount, setUnreadCount] = useState(0);
+ const [selectedNotification, setSelectedNotification] = useState<NotificationItem | null>(null);
+ const [detailModalVisible, setDetailModalVisible] = useState(false);
+
+ // 获取通知列表
+ const fetchNotifications = async () => {
+ try {
+ setLoading(true);
+ const response = await getNotificationList({ page: 1, pageSize: 20 });
+ if (response && response.data) {
+ setNotifications(response.data.list || []);
+ }
+ } catch (error) {
+ message.error("获取通知列表失败");
+ } finally {
+ setLoading(false);
+ }
+ };
+
+ // 获取未读数量
+ const fetchUnreadCount = async () => {
+ try {
+ const response = await getUnreadCount();
+ if (response && response.data) {
+ setUnreadCount(response.data.count || 0);
+ }
+ } catch (error) {
+ console.error("获取未读数量失败:", error);
+ }
+ };
+
+ // 标记单个通知为已读
+ const handleMarkAsRead = async (id: string) => {
+ try {
+ await markNotificationAsRead(id);
+ message.success("标记成功");
+ fetchNotifications();
+ fetchUnreadCount();
+ } catch (error) {
+ message.error("标记失败");
+ }
+ };
+
+ // 全部标记为已读
+ const handleMarkAllAsRead = async () => {
+ try {
+ await markAllNotificationsAsRead();
+ message.success("全部标记成功");
+ fetchNotifications();
+ fetchUnreadCount();
+ } catch (error) {
+ message.error("标记失败");
+ }
+ };
+
+ // 查看通知详情
+ const handleViewDetail = (notification: NotificationItem) => {
+ setSelectedNotification(notification);
+ setDetailModalVisible(true);
+ if (!notification.isRead) {
+ handleMarkAsRead(notification.id);
+ }
+ };
+
+ // 格式化时间
+ const formatTime = (timeString: string) => {
+ const date = new Date(timeString);
+ const now = new Date();
+ const diff = now.getTime() - date.getTime();
+ const minutes = Math.floor(diff / (1000 * 60));
+ const hours = Math.floor(diff / (1000 * 60 * 60));
+ const days = Math.floor(diff / (1000 * 60 * 60 * 24));
+
+ if (minutes < 1) return "刚刚";
+ if (minutes < 60) return `${minutes}分钟前`;
+ if (hours < 24) return `${hours}小时前`;
+ if (days < 7) return `${days}天前`;
+ return date.toLocaleDateString();
+ };
+
+ // 获取通知类型文本
+ const getNotificationTypeText = (type: string) => {
+ const typeMap: Record<string, string> = {
+ system: "系统通知",
+ user: "用户通知",
+ material: "素材通知",
+ other: "其他通知"
+ };
+ return typeMap[type] || type;
+ };
+
+ useEffect(() => {
+ if (visible) {
+ fetchNotifications();
+ fetchUnreadCount();
+ }
+ }, [visible]);
+
+ return (
+ <>
+ <Modal
+ title="消息通知"
+ open={visible}
+ onCancel={onClose}
+ footer={[
+ <Button key="markAll" onClick={handleMarkAllAsRead} disabled={unreadCount === 0}>
+ 全部标记为已读
+ </Button>,
+ <Button key="close" onClick={onClose}>
+ 关闭
+ </Button>
+ ]}
+ width={600}
+ destroyOnClose
+ >
+ <Spin spinning={loading}>
+ {notifications.length > 0 ? (
+ <List
+ dataSource={notifications}
+ renderItem={(item) => (
+ <List.Item
+ className={`${styles.notificationItem} ${!item.isRead ? styles.unread : ""}`}
+ actions={[
+ !item.isRead && (
+ <Button
+ key="mark"
+ type="text"
+ size="small"
+ icon={<CheckOutlined />}
+ onClick={() => handleMarkAsRead(item.id)}
+ >
+ 标记为已读
+ </Button>
+ )
+ ]}
+ >
+ <List.Item.Meta
+ title={
+ <div className={styles.notificationTitle}>
+ <span>{item.title}</span>
+ {!item.isRead && <Badge status="processing" />}
+ </div>
+ }
+ description={
+ <div className={styles.notificationContent}>
+ <div className={styles.notificationMeta}>
+ <span className={styles.type}>{getNotificationTypeText(item.type)}</span>
+ <span className={styles.time}>{formatTime(item.createdAt)}</span>
+ </div>
+ <div className={styles.content}>
+ {item.content.length > 100
+ ? `${item.content.substring(0, 100)}...`
+ : item.content
+ }
+ </div>
+ <Button
+ type="link"
+ size="small"
+ onClick={() => handleViewDetail(item)}
+ >
+ 查看详情
+ </Button>
+ </div>
+ }
+ />
+ </List.Item>
+ )}
+ />
+ ) : (
+ <Empty description="暂无消息" />
+ )}
+ </Spin>
+ </Modal>
+
+ {/* 通知详情弹窗 */}
+ <Modal
+ title="通知详情"
+ open={detailModalVisible}
+ onCancel={() => setDetailModalVisible(false)}
+ footer={[
+ <Button key="close" onClick={() => setDetailModalVisible(false)}>
+ 关闭
+ </Button>
+ ]}
+ width={500}
+ >
+ {selectedNotification && (
+ <div className={styles.notificationDetail}>
+ <h3>{selectedNotification.title}</h3>
+ <div className={styles.meta}>
+ <span>类型: {getNotificationTypeText(selectedNotification.type)}</span>
+ <span>时间: {formatTime(selectedNotification.createdAt)}</span>
+ </div>
+ <div className={styles.content}>
+ {selectedNotification.content}
+ </div>
+ </div>
+ )}
+ </Modal>
+ </>
+ );
+};
+
+export default NotificationPanel;
\ No newline at end of file
diff --git a/src/hooks/useNotification.ts b/src/hooks/useNotification.ts
new file mode 100644
index 00000000..10033f6e
--- /dev/null
+++ b/src/hooks/useNotification.ts
@@ -0,0 +1,41 @@
+import { useState, useEffect, useCallback } from "react";
+import { getUnreadCount } from "@/api/notification";
+
+export const useNotification = () => {
+ const [unreadCount, setUnreadCount] = useState(0);
+ const [loading, setLoading] = useState(false);
+
+ const fetchUnreadCount = useCallback(async () => {
+ try {
+ setLoading(true);
+ const response = await getUnreadCount();
+ if (response && response.data) {
+ setUnreadCount(response.data.count || 0);
+ }
+ } catch (error) {
+ console.error("获取未读数量失败:", error);
+ } finally {
+ setLoading(false);
+ }
+ }, []);
+
+ useEffect(() => {
+ // 初始获取
+ fetchUnreadCount();
+
+ // 每5秒获取一次未读数量
+ const interval = setInterval(() => {
+ fetchUnreadCount();
+ }, 5000);
+
+ return () => {
+ clearInterval(interval);
+ };
+ }, [fetchUnreadCount]);
+
+ return {
+ unreadCount,
+ loading,
+ refreshUnreadCount: fetchUnreadCount,
+ };
+};
\ No newline at end of file