UpXlsx.tsx 4.2 KB

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