BatchImport.jsx 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 uploadBuildingTreeExcel = fetch(apis.buildingOwnerInfo.uploadBuildingTreeExcel)
  8. const submitBuildingTreeExcel = fetch(apis.buildingOwnerInfo.submitBuildingTreeExcel)
  9. export default props => {
  10. const [loading, setLoading] = useState(false)
  11. const [dataList, setDataList] = useState([])
  12. const [pagenavi, setPagenavi] = useState({ pageSize: 10, total: 0 })
  13. const excelRef = useRef()
  14. const downloadExcel = () => {
  15. const link = document.createElement('a')
  16. link.href = "http://jingcheng-h5temp.oss-cn-shanghai.aliyuncs.com/%E6%A5%BC%E6%A0%8B%E5%BA%93%E6%A8%A1%E6%9D%BF.xlsx"
  17. link.setAttribute('download', '楼栋库模板.xlsx')
  18. link.click()
  19. }
  20. const importExcel = () => {
  21. const input = document.createElement('input')
  22. input.setAttribute('type', 'file')
  23. input.addEventListener('input', e => {
  24. const files = e.target.files
  25. if (files && files[0]) {
  26. excelRef.current = files[0]
  27. const formData = new FormData()
  28. formData.append('file', excelRef.current)
  29. setLoading(true)
  30. uploadBuildingTreeExcel({ data: formData }).then(res => {
  31. setDataList(res.list)
  32. setPagenavi({
  33. ...pagenavi,
  34. pageSize: res.size,
  35. total: res.total
  36. })
  37. setLoading(false)
  38. }).catch(() => {
  39. setLoading(false)
  40. })
  41. }
  42. })
  43. input.click()
  44. }
  45. const submitExcel = () => {
  46. if (!excelRef.current) {
  47. notification.warning({ message: '没有选择文件' })
  48. return
  49. }
  50. const formData = new FormData()
  51. formData.append('file', excelRef.current)
  52. setLoading(true)
  53. submitBuildingTreeExcel({ data: formData }).then(res => {
  54. notification.success({ message: '提交成功' })
  55. setLoading(false)
  56. }).catch(() => {
  57. setLoading(false)
  58. })
  59. }
  60. return (
  61. <div>
  62. <div style={{ marginBottom: '24px' }}>
  63. <Button type="primary" onClick={importExcel}><Icon type="upload" />选取文件并预览</Button>
  64. <Button type="danger" onClick={submitExcel} style={spaceStyle}>提交</Button>
  65. <Button style={spaceStyle} onClick={() => router.go(-1)}>取消</Button>
  66. <Button type="link" onClick={downloadExcel} style={spaceStyle}><Icon type="download" />下载模板</Button>
  67. </div>
  68. <Table dataSource={dataList} pagination={pagenavi} loading={loading}>
  69. <Column key="phaseName" title="期/区" dataIndex="phaseName" />
  70. <Column key="buildingName" title="栋" dataIndex="buildingName" />
  71. <Column key="unitName" title="单元" dataIndex="unitName" />
  72. <Column key="levelName" title="楼层" dataIndex="levelName" />
  73. <Column key="roomNoName" title="户号" dataIndex="roomNoName" />
  74. </Table>
  75. </div>
  76. )
  77. }