index.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { contractStore } from '@/store/mobx'
  2. import { apiDelFile, apiGetFileList } from '@/utils/common/api'
  3. import consts from '@/utils/consts'
  4. import { dayjsFormat } from '@/utils/util'
  5. import { Popconfirm } from 'antd'
  6. import Table, { ColumnsType } from 'antd/lib/table'
  7. import { observer } from 'mobx-react'
  8. import React, { useEffect, useState } from 'react'
  9. interface iFileState {
  10. id: string
  11. filename: string
  12. filepath: string
  13. accountName: string
  14. createTime: string
  15. }
  16. const File: React.FC<{ type: 'income' | 'expenditure' }> = ({ type }) => {
  17. const [ data, setData ] = useState<Array<iFileState>>([])
  18. const [ total, setTotal ] = useState<number>(0)
  19. const [ id, setId ] = useState<string>('')
  20. const [ pagination, setPagination ] = useState({
  21. pageNo: 1,
  22. pageSize: 7
  23. })
  24. useEffect(() => {
  25. if (contractStore.contract.id) {
  26. if (contractStore.contract.id !== id) {
  27. setId(contractStore.contract.id)
  28. initData()
  29. } else if (contractStore.shouldUpdate && contractStore.shouldUpdate === '3') {
  30. initData(pagination.pageNo, pagination.pageSize)
  31. }
  32. contractStore.shouldUpdate && (contractStore.changeUpdate(''))
  33. }
  34. }, [ contractStore.contract.id, contractStore.shouldUpdate ])
  35. const initData = async (pageNo: number = 1, pageSize: number = 7) => {
  36. setPagination({ ...pagination, pageNo, pageSize })
  37. const { code = -1, data = [], total = 0 } = await apiGetFileList(type === 'income' ? consts.DATA_TYPE.CONTRACT_RETURN : consts.DATA_TYPE.CONTRACT_PAID, contractStore.contract.id, pageNo, pageSize)
  38. if (code === consts.RET_CODE.SUCCESS) {
  39. setData(data)
  40. setTotal(total)
  41. }
  42. }
  43. const deleteFile = async (id: string) => {
  44. const { code = -1 } = await apiDelFile(id)
  45. if (code === consts.RET_CODE.SUCCESS) {
  46. const newData = data.filter((file: iFileState) => file.id !== id)
  47. setData(newData)
  48. }
  49. }
  50. const columns: ColumnsType<iFileState> = [
  51. {
  52. dataIndex: 'sort',
  53. // eslint-disable-next-line react/display-name
  54. render: (_: any, record: iFileState, idx: number) => <span>{idx + 1}</span>,
  55. width: 5
  56. },
  57. {
  58. title: '名称',
  59. dataIndex: 'filename',
  60. // eslint-disable-next-line react/display-name
  61. render: (text: string, record: iFileState) => <a href={consts.OSS_PATH.REVIEW + record.filepath} target="_blank" rel="noopener noreferrer">{text}</a>,
  62. width: 50
  63. },
  64. {
  65. title: '上传者',
  66. dataIndex: 'accountName',
  67. width: 10
  68. },
  69. {
  70. title: '上传时间',
  71. dataIndex: 'createTime',
  72. // eslint-disable-next-line react/display-name
  73. render: (time: string) => <span>{dayjsFormat(time, 'YYYY-MM-DD')}</span>,
  74. width: 20
  75. },
  76. {
  77. title: '操作',
  78. dataIndex: 'opreate',
  79. // eslint-disable-next-line react/display-name
  80. render: (_: string, record: iFileState) => {
  81. return <div>
  82. <a className="pi-mg-right-5" download href={consts.OSS_PATH.DOWNLOAD + record.filepath}>下载</a>
  83. <Popconfirm title="确认删除?" cancelText="取消" okText="确认" okButtonProps={{ danger: true }} onConfirm={() => deleteFile(record.id)}>
  84. <span className="pi-link-red pi-mg-left-5">删除</span>
  85. </Popconfirm>
  86. </div>
  87. },
  88. width: 15
  89. }
  90. ]
  91. return (
  92. <Table
  93. dataSource={data}
  94. columns={columns}
  95. bordered
  96. rowKey={record => record.id}
  97. pagination={{
  98. hideOnSinglePage: true,
  99. size: "small",
  100. pageSize: 7,
  101. onChange: (page, pageSize) => initData(page, pageSize),
  102. total
  103. }}
  104. />
  105. )
  106. }
  107. export default observer(File)