BatchImport.jsx 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import React, { useRef, useState } from 'react'
  2. import { Button, Spin, Table, Icon, notification } from 'antd'
  3. import router from 'umi/router'
  4. import { fetch, apis } from '@/utils/request'
  5. const { Column } = Table
  6. const spaceStyle = { marginLeft: '16px' }
  7. const buildingDownloadExcel = fetch(apis.buildingOwnerInfo.buildingDownloadExcel)
  8. const uploadBuildingExcel = fetch(apis.buildingOwnerInfo.uploadBuildingExcel)
  9. const submitBuildingExcel = fetch(apis.buildingOwnerInfo.submitBuildingExcel)
  10. export default props => {
  11. const [loading, setLoading] = useState(false)
  12. const [dataList, setDataList] = useState([])
  13. const [pagenavi, setPagenavi] = useState({ pageSize: 10, total: 0 })
  14. const excelRef = useRef()
  15. const downloadExcel = () => {
  16. buildingDownloadExcel().then(res => {
  17. const url = window.URL.createObjectURL(new Blob([res]))
  18. const link = document.createElement('a')
  19. link.href = url
  20. link.setAttribute('download', '业主资料库.xlsx')
  21. link.click()
  22. })
  23. }
  24. const importExcel = () => {
  25. const input = document.createElement('input')
  26. input.setAttribute('type', 'file')
  27. input.addEventListener('input', e => {
  28. const files = e.target.files
  29. if (files && files[0]) {
  30. excelRef.current = files[0]
  31. const formData = new FormData()
  32. formData.append('file', excelRef.current)
  33. setLoading(true)
  34. uploadBuildingExcel({ data: formData }).then(res => {
  35. setDataList(res.list)
  36. setPagenavi({
  37. ...pagenavi,
  38. pageSize: res.size,
  39. total: res.total
  40. })
  41. setLoading(false)
  42. }).catch(() => {
  43. setLoading(false)
  44. })
  45. }
  46. })
  47. input.click()
  48. }
  49. const submitExcel = () => {
  50. if (!excelRef.current) {
  51. notification.warning({ message: '没有选择文件' })
  52. return
  53. }
  54. const formData = new FormData()
  55. formData.append('file', excelRef.current)
  56. setLoading(true)
  57. submitBuildingExcel({ data: formData }).then(res => {
  58. notification.success({ message: '提交成功' })
  59. setLoading(false)
  60. }).catch(() => {
  61. setLoading(false)
  62. })
  63. }
  64. return (
  65. <div>
  66. <div style={{ marginBottom: '24px' }}>
  67. <Button type="primary" onClick={importExcel} style={spaceStyle}><Icon type="upload" />选取文件并预览</Button>
  68. <Button type="danger" onClick={submitExcel} style={spaceStyle}>提交</Button>
  69. <Button style={spaceStyle} onClick={() => router.go(-1)}>取消</Button>
  70. <Button type="link" style={spaceStyle} onClick={downloadExcel}><Icon type="download" />下载模板</Button>
  71. </div>
  72. <Table dataSource={dataList} pagination={pagenavi} loading={loading}>
  73. <Column key="phaseName" title="期/区" dataIndex="phaseName" />
  74. <Column key="buildingName" title="栋" dataIndex="buildingName" />
  75. <Column key="unitName" title="单元" dataIndex="unitName" />
  76. <Column key="levelName" title="楼层" dataIndex="levelName" />
  77. <Column key="roomNoName" title="户号" dataIndex="roomNoName" />
  78. <Column key="ownerName" title="户主姓名" dataIndex="ownerName" />
  79. {/* <Column key="roleName" title="身份" dataIndex="roleName" /> */}
  80. <Column key="ownerTel" title="手机号码" dataIndex="ownerTel" />
  81. </Table>
  82. </div>
  83. )
  84. }