站内消息支持三种类型,admin发送,mobile订阅

This commit is contained in:
yourname
2025-05-15 12:06:17 +00:00
parent 2f171d4335
commit f3042583df
4 changed files with 143 additions and 11 deletions

View File

@@ -8,7 +8,7 @@ import 'dayjs/locale/zh-cn';
import { MessageAPI , UserAPI } from './api/index.ts';
import type { UserMessage } from '../share/types.ts';
import { MessageStatusNameMap , MessageStatus} from '../share/types.ts';
import { MessageStatusNameMap , MessageStatus, MessageType } from '../share/types.ts';
import { useAuth } from "./hooks_sys.tsx";
export const MessagesPage = () => {
@@ -222,9 +222,9 @@ export const MessagesPage = () => {
style={{ width: 120 }}
allowClear
options={[
{ value: 'SYSTEM', label: '系统消息' },
{ value: 'NOTICE', label: '公告' },
{ value: 'PERSONAL', label: '个人消息' },
{ value: MessageType.SYSTEM, label: '系统消息' },
{ value: MessageType.ANNOUNCE, label: '公告' },
{ value: MessageType.PRIVATE, label: '个人消息' },
]}
/>
</Form.Item>
@@ -233,8 +233,8 @@ export const MessagesPage = () => {
style={{ width: 120 }}
allowClear
options={[
{ value: 'UNREAD', label: '未读' },
{ value: 'READ', label: '已读' },
{ value: MessageStatus.UNREAD, label: '未读' },
{ value: MessageStatus.READ, label: '已读' },
]}
/>
</Form.Item>
@@ -290,9 +290,9 @@ export const MessagesPage = () => {
>
<Select
options={[
{ value: 'SYSTEM', label: '系统消息' },
{ value: 'NOTICE', label: '公告' },
{ value: 'PERSONAL', label: '个人消息' },
{ value: MessageType.SYSTEM, label: '系统消息' },
{ value: MessageType.ANNOUNCE, label: '公告' },
{ value: MessageType.PRIVATE, label: '个人消息' },
]}
/>
</Form.Item>

View File

@@ -39,12 +39,17 @@ export const NotificationsPage = () => {
// 订阅消息频道
newSocket.on('connect', () => {
// 订阅个人频道
newSocket.emit('message:subscribe', `user_${user.id}`);
// 订阅系统频道
newSocket.emit('message:subscribe', 'system');
// 订阅公告频道
newSocket.emit('message:subscribe', 'announce');
setIsSubscribed(true);
});
// 处理实时消息
newSocket.on('message:broadcasted', (newMessage) => {
const handleNewMessage = (newMessage: any) => {
queryClient.setQueryData(['messages'], (oldData: any) => {
if (!oldData) return oldData;
return {
@@ -61,7 +66,12 @@ export const NotificationsPage = () => {
count: oldData.count + 1
};
});
});
};
// 处理广播消息
newSocket.on('message:broadcasted', handleNewMessage);
// 处理频道推送消息
newSocket.on('message:received', handleNewMessage);
// 错误处理
newSocket.on('error', (error) => {
@@ -71,6 +81,8 @@ export const NotificationsPage = () => {
return () => {
if (newSocket) {
newSocket.emit('message:unsubscribe', `user_${user.id}`);
newSocket.emit('message:unsubscribe', 'system');
newSocket.emit('message:unsubscribe', 'announce');
newSocket.disconnect();
}
};