pages_messages.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. import React, { useState } from 'react';
  2. import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
  3. import { Button, Table, Space, Modal, Form, Input, Select, message } from 'antd';
  4. import type { TableProps } from 'antd';
  5. import dayjs from 'dayjs';
  6. import 'dayjs/locale/zh-cn';
  7. import { MessageAPI , UserAPI } from './api/index.ts';
  8. import type { UserMessage } from '../share/types.ts';
  9. import { MessageStatusNameMap , MessageStatus} from '../share/types.ts';
  10. export const MessagesPage = () => {
  11. const queryClient = useQueryClient();
  12. const [form] = Form.useForm();
  13. const [isModalVisible, setIsModalVisible] = useState(false);
  14. const [searchParams, setSearchParams] = useState({
  15. page: 1,
  16. pageSize: 10,
  17. type: undefined,
  18. status: undefined,
  19. search: undefined
  20. });
  21. // 获取消息列表
  22. const { data: messages, isLoading } = useQuery({
  23. queryKey: ['messages', searchParams],
  24. queryFn: () => MessageAPI.getMessages(searchParams),
  25. });
  26. // 获取用户列表
  27. const { data: users } = useQuery({
  28. queryKey: ['users'],
  29. queryFn: () => UserAPI.getUsers({ page: 1, limit: 1000 }),
  30. });
  31. // 获取未读消息数
  32. const { data: unreadCount } = useQuery({
  33. queryKey: ['unreadCount'],
  34. queryFn: () => MessageAPI.getUnreadCount(),
  35. });
  36. // 标记消息为已读
  37. const markAsReadMutation = useMutation({
  38. mutationFn: (id: number) => MessageAPI.markAsRead(id),
  39. onSuccess: () => {
  40. queryClient.invalidateQueries({ queryKey: ['messages'] });
  41. queryClient.invalidateQueries({ queryKey: ['unreadCount'] });
  42. message.success('标记已读成功');
  43. },
  44. });
  45. // 删除消息
  46. const deleteMutation = useMutation({
  47. mutationFn: (id: number) => MessageAPI.deleteMessage(id),
  48. onSuccess: () => {
  49. queryClient.invalidateQueries({ queryKey: ['messages'] });
  50. message.success('删除成功');
  51. },
  52. });
  53. // 发送消息
  54. const sendMessageMutation = useMutation({
  55. mutationFn: (data: any) => MessageAPI.sendMessage(data),
  56. onSuccess: () => {
  57. queryClient.invalidateQueries({ queryKey: ['messages'] });
  58. queryClient.invalidateQueries({ queryKey: ['unreadCount'] });
  59. message.success('发送成功');
  60. setIsModalVisible(false);
  61. form.resetFields();
  62. },
  63. });
  64. const columns: TableProps<UserMessage>['columns'] = [
  65. {
  66. title: '标题',
  67. dataIndex: 'title',
  68. key: 'title',
  69. },
  70. {
  71. title: '类型',
  72. dataIndex: 'type',
  73. key: 'type',
  74. },
  75. {
  76. title: '发送人',
  77. dataIndex: 'sender_name',
  78. key: 'sender_name',
  79. },
  80. {
  81. title: '状态',
  82. dataIndex: 'user_status',
  83. key: 'user_status',
  84. render: (user_status: MessageStatus) => (
  85. <span style={{ color: user_status === MessageStatus.UNREAD ? 'red' : 'green' }}>
  86. {MessageStatusNameMap[user_status]}
  87. </span>
  88. ),
  89. },
  90. {
  91. title: '发送时间',
  92. dataIndex: 'created_at',
  93. key: 'created_at',
  94. render: (date: string) => dayjs(date).format('YYYY-MM-DD HH:mm'),
  95. },
  96. {
  97. title: '操作',
  98. key: 'action',
  99. render: (_: any, record) => (
  100. <Space size="middle">
  101. <Button
  102. type="link"
  103. onClick={() => markAsReadMutation.mutate(record.id)}
  104. disabled={record.user_status === MessageStatus.READ}
  105. >
  106. 标记已读
  107. </Button>
  108. <Button
  109. type="link"
  110. danger
  111. onClick={() => deleteMutation.mutate(record.id)}
  112. >
  113. 删除
  114. </Button>
  115. </Space>
  116. ),
  117. },
  118. ];
  119. const handleSearch = (values: any) => {
  120. setSearchParams({
  121. ...searchParams,
  122. ...values,
  123. page: 1
  124. });
  125. };
  126. const handleTableChange = (pagination: any) => {
  127. setSearchParams({
  128. ...searchParams,
  129. page: pagination.current,
  130. pageSize: pagination.pageSize
  131. });
  132. };
  133. const handleSendMessage = (values: any) => {
  134. sendMessageMutation.mutate(values);
  135. };
  136. return (
  137. <div className="p-4">
  138. <div className="flex justify-between items-center mb-4">
  139. <h1 className="text-2xl font-bold">消息管理</h1>
  140. <div className="flex items-center space-x-4">
  141. {unreadCount && unreadCount.count > 0 && (
  142. <span className="text-red-500">{unreadCount.count}条未读</span>
  143. )}
  144. <Button type="primary" onClick={() => setIsModalVisible(true)}>
  145. 发送消息
  146. </Button>
  147. </div>
  148. </div>
  149. <div className="bg-white p-4 rounded shadow">
  150. <Form layout="inline" onFinish={handleSearch} className="mb-4">
  151. <Form.Item name="type" label="类型">
  152. <Select
  153. style={{ width: 120 }}
  154. allowClear
  155. options={[
  156. { value: 'SYSTEM', label: '系统消息' },
  157. { value: 'NOTICE', label: '公告' },
  158. { value: 'PERSONAL', label: '个人消息' },
  159. ]}
  160. />
  161. </Form.Item>
  162. <Form.Item name="status" label="状态">
  163. <Select
  164. style={{ width: 120 }}
  165. allowClear
  166. options={[
  167. { value: 'UNREAD', label: '未读' },
  168. { value: 'READ', label: '已读' },
  169. ]}
  170. />
  171. </Form.Item>
  172. <Form.Item name="search" label="搜索">
  173. <Input placeholder="输入标题或内容" />
  174. </Form.Item>
  175. <Form.Item>
  176. <Button type="primary" htmlType="submit">
  177. 搜索
  178. </Button>
  179. </Form.Item>
  180. </Form>
  181. <Table
  182. columns={columns}
  183. dataSource={messages?.data}
  184. loading={isLoading}
  185. rowKey="id"
  186. pagination={{
  187. current: searchParams.page,
  188. pageSize: searchParams.pageSize,
  189. total: messages?.pagination?.total,
  190. showSizeChanger: true,
  191. }}
  192. onChange={handleTableChange}
  193. />
  194. </div>
  195. <Modal
  196. title="发送消息"
  197. visible={isModalVisible}
  198. onCancel={() => setIsModalVisible(false)}
  199. footer={null}
  200. width={800}
  201. >
  202. <Form
  203. form={form}
  204. layout="vertical"
  205. onFinish={handleSendMessage}
  206. >
  207. <Form.Item
  208. name="title"
  209. label="标题"
  210. rules={[{ required: true, message: '请输入标题' }]}
  211. >
  212. <Input placeholder="请输入消息标题" />
  213. </Form.Item>
  214. <Form.Item
  215. name="type"
  216. label="消息类型"
  217. rules={[{ required: true, message: '请选择消息类型' }]}
  218. >
  219. <Select
  220. options={[
  221. { value: 'SYSTEM', label: '系统消息' },
  222. { value: 'NOTICE', label: '公告' },
  223. { value: 'PERSONAL', label: '个人消息' },
  224. ]}
  225. />
  226. </Form.Item>
  227. <Form.Item
  228. name="receiver_ids"
  229. label="接收人"
  230. rules={[{ required: true, message: '请选择接收人' }]}
  231. >
  232. <Select
  233. mode="multiple"
  234. placeholder="请选择接收人"
  235. options={users?.data?.map((user: any) => ({
  236. value: user.id,
  237. label: user.username,
  238. }))}
  239. />
  240. </Form.Item>
  241. <Form.Item
  242. name="content"
  243. label="内容"
  244. rules={[{ required: true, message: '请输入消息内容' }]}
  245. >
  246. <Input.TextArea rows={6} placeholder="请输入消息内容" />
  247. </Form.Item>
  248. <Form.Item>
  249. <Button
  250. type="primary"
  251. htmlType="submit"
  252. loading={sendMessageMutation.status === 'pending'}
  253. >
  254. 发送
  255. </Button>
  256. </Form.Item>
  257. </Form>
  258. </Modal>
  259. </div>
  260. );
  261. };