index.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. import React, { useCallback, useMemo, useState } from 'react'
  2. import { Upload, Button, UploadProps, Select } from 'antd'
  3. import { InboxOutlined } from '@ant-design/icons'
  4. import { MessageFu } from '@/utils/message'
  5. import styles from './index.module.scss'
  6. import { B1Xtype } from '@/pages/B_enterTibet/B1collect/data'
  7. import { baseURL } from '@/utils/http'
  8. import { fileTypeRes } from '@/store/action/layout'
  9. import { getTokenFu } from '@/utils/storage'
  10. import history from '@/utils/history'
  11. import { API_C2dels } from '@/store/action/C2files'
  12. import MyPopconfirm from '../MyPopconfirm'
  13. import { DeleteOutlined } from '@ant-design/icons'
  14. const { Dragger } = Upload
  15. type Props = {
  16. tableList: B1Xtype[]
  17. moduleId: number
  18. isShow: boolean
  19. closeFu: () => void
  20. succFu: (obj: any) => void
  21. }
  22. function ZupFile({ tableList, moduleId, isShow, closeFu, succFu }: Props) {
  23. // 附件列表
  24. const [fileList, setFileList] = useState<any[]>([])
  25. // const timeRef = useRef(-1)
  26. const FileProps: UploadProps = {
  27. name: 'file',
  28. // 支持上传文件夹
  29. directory: true,
  30. multiple: true,
  31. action: baseURL + 'cms/orderCollect/upload',
  32. headers: {
  33. token: getTokenFu()
  34. },
  35. // 上传的额外参数
  36. data: file => ({
  37. dirCode: 'collectUpFiles',
  38. isDb: 'true',
  39. type: fileTypeRes(file.name),
  40. moduleId,
  41. isCompress: 'true'
  42. }),
  43. fileList,
  44. onChange(info: any) {
  45. setFileList([...info.fileList])
  46. const { status } = info.file
  47. // 检查请求状态
  48. const response = info.file.response || {}
  49. if (status !== 'uploading') {
  50. if (response.code !== 0) {
  51. setFileList(info.fileList.filter((v: any) => v.uid !== info.file.uid))
  52. MessageFu.error(`${info.file.name} 上传失败;${response.msg}`)
  53. }
  54. if (response.code === 5001 || response.code === 5002) {
  55. MessageFu.warning('登录失效!')
  56. history.push('/login')
  57. return false
  58. }
  59. // console.log(info.file, info.fileList);
  60. }
  61. if (status === 'done' && response.code === 0) {
  62. // console.log("-----", info);
  63. setFileList(
  64. info.fileList.map((v: any) => ({
  65. ...v,
  66. mySelect:
  67. info.file.uid === v.uid
  68. ? {
  69. id: info.file.response.data.id,
  70. goodsId: undefined,
  71. name: info.file.name
  72. }
  73. : v.mySelect
  74. }))
  75. )
  76. } else if (status === 'error') {
  77. MessageFu.error(`${info.file.name} 上传失败.`)
  78. // 去掉列表中的失败状态文件
  79. setFileList(info.fileList.filter((v: any) => v.uid !== info.file.uid))
  80. }
  81. },
  82. // 自定义列表
  83. itemRender: (originNode, file: any) => {
  84. const obj = file.mySelect || { goodsId: undefined }
  85. return (
  86. <span className='custom-upload-item'>
  87. {originNode}
  88. {file.status === 'done' ? (
  89. <>
  90. <span className='mySelect'>
  91. <Select
  92. getPopupContainer={() => document.querySelector('#A1OMain')!}
  93. style={{ width: 200 }}
  94. placeholder='请选择'
  95. value={obj.goodsId}
  96. onChange={e => selectChangeFu(e, file.uid, file.mySelect.id)}
  97. options={tableList.map(v => ({
  98. value: v.id,
  99. label: v.name
  100. }))}
  101. />
  102. <span hidden={obj.goodsId}>请选择藏品</span>
  103. </span>
  104. <MyPopconfirm
  105. txtK='删除'
  106. onConfirm={async () => {
  107. if (file.percent === 100) {
  108. // console.log("-----还没有发请求删除", file);
  109. const id = file.response.data.id
  110. // 已经上传完成,发请求删除
  111. const res = await API_C2dels([id])
  112. if (res.code === 0) {
  113. MessageFu.success('删除成功!')
  114. setFileList(fileList.filter((v: any) => v.uid !== file.uid))
  115. }
  116. } else {
  117. MessageFu.success('删除成功!')
  118. setFileList(fileList.filter((v: any) => v.uid !== file.uid))
  119. }
  120. }}
  121. Dom={
  122. <div className='mySelectIcon'>
  123. <DeleteOutlined />
  124. </div>
  125. }
  126. />
  127. </>
  128. ) : null}
  129. </span>
  130. )
  131. }
  132. }
  133. // 下拉框改变
  134. const selectChangeFu = useCallback(
  135. (val: number, uid: string, id: number) => {
  136. setFileList(
  137. fileList.map(v => ({
  138. ...v,
  139. mySelect:
  140. v.uid === uid
  141. ? {
  142. ...v.mySelect,
  143. goodsId: v.mySelect.id === id ? val : v.mySelect.goodsId
  144. }
  145. : v.mySelect
  146. }))
  147. )
  148. },
  149. [fileList]
  150. )
  151. // 需要提交的数据
  152. const arrRes = useMemo(() => {
  153. return fileList.map((v: any) => v.mySelect || { goodsId: undefined })
  154. }, [fileList])
  155. // 点击提交
  156. const btnOk = useCallback(async () => {
  157. const obj: any = {}
  158. arrRes.forEach(v => {
  159. if (obj[v.goodsId]) obj[v.goodsId].push(v.id)
  160. else obj[v.goodsId] = [v.id]
  161. })
  162. succFu(obj)
  163. setFileList([])
  164. closeFu()
  165. }, [arrRes, closeFu, succFu])
  166. return (
  167. <div className={styles.ZupFile} hidden={!isShow}>
  168. <div className='ZupFileBox' id='A1OMain'>
  169. <div className='ZupFilell'>
  170. {/* 文件夹上传区域(支持拖拽) */}
  171. <Dragger {...FileProps}>
  172. <p className='ant-upload-drag-icon'>
  173. <InboxOutlined />
  174. </p>
  175. <p className='ant-upload-text'>点击或拖拽文件夹到此区域上传</p>
  176. <p className='ant-upload-hint'>支持上传整个文件夹及子目录文件</p>
  177. </Dragger>
  178. </div>
  179. </div>
  180. {/* 关闭按钮 */}
  181. <div className='ZupFileBtnX'>
  182. <Button
  183. type='primary'
  184. onClick={btnOk}
  185. disabled={fileList.length === 0 || arrRes.some(v => !v.goodsId)}
  186. >
  187. 提交
  188. </Button>
  189. &emsp;
  190. <Button onClick={closeFu}>关闭</Button>
  191. {fileList.length ? (
  192. <>
  193. &emsp;
  194. <MyPopconfirm
  195. txtK='清空'
  196. onConfirm={() => setFileList([])}
  197. Dom={<Button danger>清空数据</Button>}
  198. />
  199. </>
  200. ) : null}
  201. </div>
  202. </div>
  203. )
  204. }
  205. export default ZupFile