index.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import { useEffect, useState } from 'react';
  2. import { useSelector, useDispatch } from 'react-redux'
  3. import { Table, Modal, message, Menu, Breadcrumb, Dropdown, Space, Form, Input, Button, Row, Col, Select } from 'antd';
  4. import { DownOutlined, PlusOutlined } from '@ant-design/icons';
  5. import './index.less';
  6. import apiObj from '@api/index';
  7. import { setUnReadNum } from '@reducers/userInfo.js';
  8. const { post, api, xPost } = apiObj;
  9. const { Option } = Select;
  10. function MyMessage() {
  11. const dispatch = useDispatch();
  12. useEffect(() => {
  13. //刷新列表
  14. getTableData();
  15. }, []);
  16. const [size, setSize] = useState(15); //每页显示条数
  17. const [current, setCurrent] = useState(1);//当前页
  18. const [params, setParams] = useState({
  19. pages: 1,
  20. current: 1,
  21. size: 15
  22. });
  23. const [total, setTotal] = useState(0); //每页显示条数
  24. const [dataSource, setDataSource] = useState([]); //列表数据
  25. const [visible, setVisible] = useState(false); //弹窗查看
  26. const [confirmLoading, setConfirmLoading] = useState(false);
  27. const [msgDetail, setMsgDetail] = useState({});
  28. //从state中取出状态、类型列表
  29. const staticInfo = useSelector(state => {
  30. return state.staticInfo;
  31. });
  32. const toUnRead = useSelector(state => { //是否筛选未读
  33. //console.log(43,state)
  34. return state.tabPanes.toUnRead;
  35. });
  36. const { msgTypeList, msgStatusList } = staticInfo;
  37. //获取表格数据
  38. function getTableData(param = {}) {
  39. post(api.listPage, param).then((res) => {
  40. if (res.data.code === 200) {
  41. let data = res.data.data;
  42. setTotal(data.total);
  43. setDataSource(data.records);
  44. }
  45. })
  46. }
  47. //弹窗取消
  48. function handleCancel() {
  49. setVisible(false);
  50. }
  51. //获取未读消息数量
  52. function getNotNoticeCount() {
  53. xPost(api.getNotNoticeCount).then((res) => {
  54. if (res.data.code === 200) {
  55. let count = res.data.data.count;
  56. count = count > 99 ? '99+' : count;
  57. dispatch(setUnReadNum(count));
  58. } else {
  59. //message.warning(res.data.msg || '请求失败');
  60. }
  61. })
  62. }
  63. //查看消息弹窗
  64. function showMessage(id) {
  65. getMsgDetail(id);
  66. setVisible(true);
  67. }
  68. //获取消息详情
  69. function getMsgDetail(id) { //type:1我的通知,0通知管理
  70. xPost(api.getNoticeInfoById, { id: id, type: 1 }).then((res) => {
  71. const { data, code } = res.data;
  72. if (code === 200) {
  73. setMsgDetail(data);
  74. //更新未读消息数量
  75. getNotNoticeCount();
  76. getTableData(params);//刷新列表
  77. } else {
  78. message.warning(res.data.message || '获取详情失败')
  79. }
  80. });
  81. }
  82. //每页显示条数切换
  83. function onSizeChange(current, pageSize) {
  84. params.current = current
  85. params.size = pageSize
  86. setSize(pageSize)
  87. setCurrent(current)
  88. setParams(params)
  89. getTableData(params)
  90. }
  91. //翻页
  92. function onPageChange(page, pageSize) {
  93. params.current = page
  94. params.size = pageSize
  95. setCurrent(page)
  96. setParams(params)
  97. getTableData(params)
  98. }
  99. //表格渲染
  100. function RenderTable() {
  101. const columns = [
  102. { title: '标题', dataIndex: 'title', key: 'title' },
  103. {
  104. title: '类型', width: 150, dataIndex: 'typeName', key: 'typeName'
  105. },
  106. { title: '发送者', dataIndex: 'sender', key: 'sender' },
  107. { title: '发送时间', width: 240, dataIndex: 'gmtCreate', key: 'gmtCreate' },
  108. {
  109. title: '阅读状态', key: 'statusName', dataIndex: 'statusName'
  110. },
  111. {
  112. title: '操作', key: 'operation', render: (row) => {
  113. return (<Space size="middle">
  114. <a onClick={() => showMessage(row.id)}>查看</a>
  115. </Space>)
  116. }
  117. },
  118. ];
  119. return (
  120. <Table
  121. rowKey={record => record.id}
  122. scroll={{ y: 'calc(100vh - 360px)' }}
  123. columns={columns}
  124. dataSource={dataSource}
  125. pagination={{
  126. showSizeChanger: true,
  127. pageSizeOptions: [15, 30, 60, 120],
  128. defaultPageSize: size,
  129. pageNo: 1,
  130. current: current,
  131. size: 'small',
  132. total: total,
  133. showTotal: (total, range) => `第${range[0]}-${range[1]} 条/共 ${total} 条数据`,
  134. onShowSizeChange: (current, pageSize) => onSizeChange(current, pageSize), // 改变每页数量时更新显示
  135. onChange: (page, pageSize) => onPageChange(page, pageSize),//点击页码事件
  136. }}
  137. />
  138. )
  139. }
  140. //筛选项渲染
  141. const [form] = Form.useForm();
  142. const initialValues = { status: toUnRead ? '0' : '' };
  143. const onFinish = (values) => {
  144. getTableData(values);
  145. console.log('筛选项:', values);
  146. };
  147. const onReset = () => {
  148. form.resetFields();
  149. getTableData();
  150. };
  151. return (
  152. <div className='wrapper'>
  153. <div className="filter-box">
  154. <Form form={form} name="control-hooks" initialValues={initialValues} onFinish={onFinish}>
  155. <Row gutter={24}>
  156. <Col span={5} key={1}>
  157. <Form.Item name="type" label="通知类型">
  158. <Select
  159. allowClear
  160. >
  161. {msgTypeList.map((item) => {
  162. return (
  163. <Option value={item.name} key={item.name}>{item.val}</Option>
  164. )
  165. })}
  166. </Select>
  167. </Form.Item>
  168. </Col>
  169. <Col span={5} key={2}>
  170. <Form.Item name="status" label="阅读状态">
  171. <Select
  172. allowClear
  173. >
  174. {msgStatusList.map((item) => {
  175. return (
  176. <Option value={item.name} key={item.name}>{item.val}</Option>
  177. )
  178. })}
  179. </Select>
  180. </Form.Item>
  181. </Col>
  182. <Col span={9} key={3}>
  183. <Form.Item>
  184. <Button type="primary" htmlType="submit">
  185. 查询
  186. </Button>
  187. <Button htmlType="button" onClick={onReset}>
  188. 重置
  189. </Button>
  190. </Form.Item>
  191. </Col>
  192. </Row>
  193. </Form>
  194. </div>
  195. <div className="table">
  196. <div className="table-header">
  197. <h2 className="table-title">我的通知</h2>
  198. </div>
  199. <RenderTable />
  200. </div>
  201. <Modal
  202. title="查看通知"
  203. footer={null}
  204. width={724}
  205. visible={visible}
  206. confirmLoading={confirmLoading}
  207. onCancel={() => setVisible(false)}
  208. >
  209. <div className="message-box">
  210. <p className='msg-title'>{msgDetail.title}</p>
  211. <div className="msg-content" dangerouslySetInnerHTML={{ __html: msgDetail.content }}></div>
  212. <div className="msg-footer">
  213. <span className="msg-type">类型:{msgDetail.typeName}</span>
  214. <span className="msg-sender">发送者:{msgDetail.creatorName}</span>
  215. <span className="msg-time">发送时间:{msgDetail.gmtCreate}</span>
  216. </div>
  217. </div>
  218. <p className='message-oper'><Button type='primary' onClick={handleCancel}>返回</Button></p>
  219. </Modal>
  220. </div>
  221. )
  222. }
  223. export default MyMessage;