Skip to content

Let's use AI to Earn!

通知相关

daoyianbfdf3299016a
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..daab8992
--- /dev/null
+++ b/src/app/notification/page.tsx
@@ -0,0 +1,239 @@
+"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 { useUserStore } from "@/store/user";
+import {
+ getNotificationList,
+ markNotificationAsRead,
+ markAllNotificationsAsRead,
+ type NotificationItem
+} from "@/api/notification";
+import styles from "./notification.module.scss";
+
+const NotificationPage = () => {
+ const { t } = useTransClient("common");
+ const token = useUserStore((state) => state.token);
+ 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) => {
+ // 如果没有登录信息,不发送请求
+ if (!token) {
+ setNotifications([]);
+ return;
+ }
+
+ 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) => {
+ if (!token) return;
+
+ try {
+ await markNotificationAsRead(id);
+ message.success("标记成功");
+ fetchNotifications(activeTab === "all" ? undefined : activeTab);
+ } catch (error) {
+ message.error("标记失败");
+ }
+ };
+
+ // 全部标记为已读
+ const handleMarkAllAsRead = async () => {
+ if (!token) return;
+
+ 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();
+ }, [token]);
+
+ 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) || !token}
+ >
+ 全部标记为已读
+ </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.tsx b/src/components/NotificationPanel.tsx
index 9a4023c0..0314c9e9 100644
--- a/src/components/NotificationPanel.tsx
+++ b/src/components/NotificationPanel.tsx
@@ -4,6 +4,7 @@ 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 { useUserStore } from "@/store/user";
import {
getNotificationList,
markNotificationAsRead,
@@ -20,6 +21,7 @@ interface NotificationPanelProps {
const NotificationPanel: React.FC<NotificationPanelProps> = ({ visible, onClose }) => {
const { t } = useTransClient("common");
+ const token = useUserStore((state) => state.token);
const [notifications, setNotifications] = useState<NotificationItem[]>([]);
const [loading, setLoading] = useState(false);
const [unreadCount, setUnreadCount] = useState(0);
@@ -28,6 +30,12 @@ const NotificationPanel: React.FC<NotificationPanelProps> = ({ visible, onClose
// 获取通知列表
const fetchNotifications = async () => {
+ // 如果没有登录信息,不发送请求
+ if (!token) {
+ setNotifications([]);
+ return;
+ }
+
try {
setLoading(true);
const response = await getNotificationList({ page: 1, pageSize: 20 });
@@ -43,6 +51,12 @@ const NotificationPanel: React.FC<NotificationPanelProps> = ({ visible, onClose
// 获取未读数量
const fetchUnreadCount = async () => {
+ // 如果没有登录信息,不发送请求
+ if (!token) {
+ setUnreadCount(0);
+ return;
+ }
+
try {
const response = await getUnreadCount();
if (response && response.data) {
@@ -118,7 +132,7 @@ const NotificationPanel: React.FC<NotificationPanelProps> = ({ visible, onClose
fetchNotifications();
fetchUnreadCount();
}
- }, [visible]);
+ }, [visible, token]);
return (
<>
diff --git a/src/hooks/useNotification.ts b/src/hooks/useNotification.ts
index 10033f6e..f0b2d69e 100644
--- a/src/hooks/useNotification.ts
+++ b/src/hooks/useNotification.ts
@@ -1,11 +1,19 @@
import { useState, useEffect, useCallback } from "react";
import { getUnreadCount } from "@/api/notification";
+import { useUserStore } from "@/store/user";
export const useNotification = () => {
const [unreadCount, setUnreadCount] = useState(0);
const [loading, setLoading] = useState(false);
+ const token = useUserStore((state) => state.token);
const fetchUnreadCount = useCallback(async () => {
+ // 如果没有登录信息,不发送请求
+ if (!token) {
+ setUnreadCount(0);
+ return;
+ }
+
try {
setLoading(true);
const response = await getUnreadCount();
@@ -17,16 +25,16 @@ export const useNotification = () => {
} finally {
setLoading(false);
}
- }, []);
+ }, [token]);
useEffect(() => {
// 初始获取
fetchUnreadCount();
- // 每5秒获取一次未读数量
+ // 每10秒获取一次未读数量
const interval = setInterval(() => {
fetchUnreadCount();
- }, 5000);
+ }, 10000);
return () => {
clearInterval(interval);