UpXlsx.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import { API_upFile } from '@/store/action/layout'
  2. import { fileDomInitialFu } from '@/utils/domShow'
  3. import { MessageFu } from '@/utils/message'
  4. import { notification } from 'antd'
  5. import React, { useCallback, useRef } from 'react'
  6. import { forwardRef, useImperativeHandle } from 'react'
  7. type Props = {
  8. url: string
  9. xlsxResInfoFu: (arr: any[]) => void
  10. ref: any //当前自己的ref,给父组件调用
  11. dirCode: string
  12. ISBNArr: string[] //ISBN编号集合,用来过滤表格
  13. }
  14. function UpXlsx({ url, xlsxResInfoFu, dirCode, ISBNArr }: Props, ref: any) {
  15. const [api, contextHolder] = notification.useNotification()
  16. // 点击上传附件按钮
  17. const myInput = useRef<HTMLInputElement>(null)
  18. // 上传附件的处理函数
  19. const handeUpPhoto = useCallback(
  20. async (e: React.ChangeEvent<HTMLInputElement>) => {
  21. if (e.target.files) {
  22. // 拿到files信息
  23. const filesInfo = e.target.files[0]
  24. // 校验格式
  25. if (!filesInfo.name.endsWith('.xlsx')) {
  26. e.target.value = ''
  27. return MessageFu.warning('只支持.xlsx格式!')
  28. }
  29. // 校验大小
  30. if (filesInfo.size > 5 * 1024 * 1024) {
  31. e.target.value = ''
  32. return MessageFu.warning('最大支持5M!')
  33. }
  34. // 创建FormData对象
  35. const fd = new FormData()
  36. // 把files添加进FormData对象(‘photo’为后端需要的字段)
  37. fd.append('type', 'doc')
  38. fd.append('dirCode', dirCode)
  39. fd.append('file', filesInfo)
  40. fd.append('isDb', 'false')
  41. e.target.value = ''
  42. const res = await API_upFile(fd, url)
  43. try {
  44. if (res.code === 0) {
  45. MessageFu.success('上传成功!')
  46. // const { msg, error } = res.data
  47. // const errorTit = error && error.length ? JSON.stringify(error) : ''
  48. // api.info({
  49. // message: msg,
  50. // description: errorTit,
  51. // duration: 0,
  52. // placement: 'topLeft'
  53. // })
  54. const arrTemp: any[] = res.data || []
  55. const moreTit: number[] = []
  56. const arr = arrTemp.filter((item, ind) => {
  57. const indRes = ind === arrTemp.findIndex(item2 => item.num === item2.num)
  58. if (!indRes) moreTit.push(ind)
  59. return indRes
  60. })
  61. if (moreTit && moreTit.length) {
  62. const moreTitTxt = moreTit.map(v => v + 1)
  63. api.warning({
  64. message: `第${JSON.stringify(moreTitTxt)}行ISBN编号重复,已过滤`,
  65. duration: 0,
  66. placement: 'bottomLeft'
  67. })
  68. }
  69. if (ISBNArr.length + arr.length > 50) {
  70. fileDomInitialFu()
  71. return MessageFu.warning(
  72. `最多支持50条数据,您本次可录入数据条数为${50 - ISBNArr.length}条!`
  73. )
  74. }
  75. const temp: string[] = []
  76. const arrRes: any[] = []
  77. arr.forEach((v: any, i: number) => {
  78. v.id = Date.now() + i
  79. if (ISBNArr.includes(v.num) && v.num !== '') {
  80. temp.push(v.num)
  81. } else {
  82. arrRes.push(v)
  83. }
  84. })
  85. xlsxResInfoFu(arrRes)
  86. if (temp.length) {
  87. api.warning({
  88. message: 'ISBN编号' + JSON.stringify(temp) + '重复,已过滤',
  89. duration: 0,
  90. placement: 'bottomLeft'
  91. })
  92. }
  93. }
  94. fileDomInitialFu()
  95. } catch (error) {
  96. fileDomInitialFu()
  97. }
  98. }
  99. },
  100. [api, dirCode, ISBNArr, url, xlsxResInfoFu]
  101. )
  102. const myInputClickFu = useCallback(() => {
  103. myInput.current?.click()
  104. }, [])
  105. // 可以让父组件调用子组件的方法
  106. useImperativeHandle(ref, () => ({
  107. myInputClickFu
  108. }))
  109. return (
  110. <>
  111. {contextHolder}
  112. <input
  113. id='upInput'
  114. type='file'
  115. accept='.xlsx'
  116. ref={myInput}
  117. onChange={e => handeUpPhoto(e)}
  118. />
  119. </>
  120. )
  121. }
  122. export default forwardRef(UpXlsx)