123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import React, { useRef } from 'react';
  2. import { history } from 'umi';
  3. import moment from 'moment';
  4. import { Button, Popconfirm, message, Modal } from 'antd';
  5. import { PlusOutlined } from '@ant-design/icons';
  6. import { PageHeaderWrapper } from '@ant-design/pro-layout';
  7. import PageTable from '@/components/PageTable';
  8. import { getNoticeList, deleteNotice, updateNotice } from '@/services/notice';
  9. const formatterTime = (val) => {
  10. return val && val !== '-' ? moment(val).format('YYYY-MM-DD') : '-';
  11. };
  12. export default (props) => {
  13. const actionRef = useRef();
  14. const gotoEdit = (id) => {
  15. const queryStr = id ? `?id=${id}` : '';
  16. history.push(`./notice/edit.jsx${queryStr}`);
  17. };
  18. const handleDelete = (id) => {
  19. deleteNotice(id).then(() => {
  20. message.success('删除成功');
  21. actionRef.current.reload();
  22. }).catch((err) => {
  23. console.log(err.message)
  24. });
  25. };
  26. //列表切换通知状态方法
  27. const handleOK = (record, data) => {
  28. const title = record.status
  29. ? '您确定要将该通知状态变更为未发布吗? 变更后该通知将不在移动端显示'
  30. : '您确定要将该通知状态变更为发布吗? 发布后该通知可以在移动端显示';
  31. Modal.confirm({
  32. title: title,
  33. okText: '确认',
  34. cancelText: '取消',
  35. onOk() {
  36. updateNotice(record.noticeId, { ...record, status: record.status === 1 ? 0 : 1 })
  37. .then((res) => {
  38. message.success('操作成功');
  39. actionRef.current.reload();
  40. })
  41. .catch((err) => {
  42. console.log(err.message)
  43. });
  44. },
  45. });
  46. };
  47. const actions = () => [
  48. <Button key="add" type="primary" icon={<PlusOutlined />} onClick={() => gotoEdit()}>
  49. 新增
  50. </Button>,
  51. ];
  52. const columns = [
  53. {
  54. title: '标题',
  55. dataIndex: 'title',
  56. key: 'title',
  57. },
  58. {
  59. title: '权重',
  60. dataIndex: 'weight',
  61. key: 'weight',
  62. search: false,
  63. width: 100,
  64. },
  65. {
  66. title: '创建时间',
  67. dataIndex: 'createDate',
  68. key: 'createDate',
  69. render: formatterTime,
  70. search: false,
  71. width: 100,
  72. },
  73. {
  74. title: '发布状态',
  75. dataIndex: 'status',
  76. key: 'status',
  77. search: false,
  78. width: 100,
  79. render: (_, record) => {
  80. return record.status === 1 ? '发布' : '未发布';
  81. },
  82. },
  83. {
  84. title: '操作',
  85. valueType: 'option',
  86. width: 240,
  87. render: (_, record) => [
  88. <Button style={{ padding: 0 }} type="link" key={1} onClick={() => handleOK(record)}>
  89. {record.status === 0 ? '发布' : '取消发布'}
  90. </Button>,
  91. <Button style={{ padding: 0 }} type="link" key={2} onClick={() => gotoEdit(record.noticeId)}>
  92. 编辑
  93. </Button>,
  94. <Popconfirm
  95. key={3}
  96. title="您是否确认删除 ?"
  97. onConfirm={() => handleDelete(record.noticeId)}
  98. okText="确定"
  99. cancelText="取消"
  100. >
  101. <Button style={{ padding: 0 }} type="link">
  102. 删除
  103. </Button>
  104. </Popconfirm>,
  105. ],
  106. },
  107. ];
  108. return (
  109. <PageHeaderWrapper>
  110. <PageTable
  111. request={getNoticeList}
  112. toolBarRender={actions}
  113. actionRef={actionRef}
  114. columns={columns}
  115. rowKey="noticeId"
  116. options={false}
  117. scroll={{ x: 1000 }}
  118. />
  119. </PageHeaderWrapper>
  120. );
  121. };