import React from "react"; import { Button, Upload as AntdUpload, message } from "antd"; import { UploadOutlined } from "@ant-design/icons"; import HcserviceV3 from "./../../tmpl/hcservice-v3"; import { EControlProps, FileValue } from "../../tmpl/interface"; import Super from "../../tmpl/superagent"; import Units from "../../units"; import { LocaleContext } from "../../locale/LocaleProvider"; interface UploadProps extends EControlProps { maxCount: number } interface UploadState { fileList: any[], } export default class MultiFileUpload extends React.PureComponent< UploadProps, UploadState > { state = { fileList: null }; static contextType = LocaleContext; context: React.ContextType; handleRemove = (info) => { const { fileList } = this.state; const { onChange } = this.props; if (fileList && fileList.length > 1) { let tFileList = []; for (let f of fileList) { if (f.uid != info.uid) { tFileList.push(f); } } //this.setState({fileList:fileList}) onChange(JSON.stringify(tFileList)); //this.setState({fileList:tFileList}); } else { onChange(JSON.stringify({ valid: "delete" })); this.setState({ fileList: [] }); } }; handleChange = async (info) => { console.log("file change:", info); let fileList: any[] = []; let status = undefined; if (info.fileList) { info.fileList.forEach(a => { if (a.status === undefined || a.status == "uploading") { status = "uploading"; } }) if (!status) { status = "done"; } } if (status == "uploading") { if (info.fileList) { info.fileList.forEach(a => { if (a.status === undefined || a.status == "done") { fileList.push(a); } }) } if (info.fileList) { info.fileList.forEach(a => { if (a.status === "uploading") { let newFile1: FileValue = { valid: "new", uid: a.uid, status: a.status, fileKey: "", fileName: a.name, }; fileList.push(newFile1); } }) } this.setState({ fileList }) } else if (status == "done") { if (info.fileList) { info.fileList.forEach(a => { if (!a.originFileObj) {//已保存的数据 fileList.push(a); } else { let newFile: FileValue = { valid: "new", status: a.status, uid: a.uid, fileKey: a.response.fileKey, fileName: a.name, }; fileList.push(newFile); } }) } const { onChange } = this.props; onChange(JSON.stringify(fileList)); } }; beforeUpload = (file) => { const { translate } = this.context; const isLt5M = file.size / 1024 / 1024 < 200; if (!isLt5M) { message.error(translate("${超过200M限制 不允许上传~}"), 5); return false; } else { return true; } }; componentDidMount() { const { value } = this.props; let fileList = this.transvalue(value); this.setState({ fileList }) } componentDidUpdate(prevProps: Readonly, prevState: Readonly, snapshot?: any) { const { value: preValue } = prevProps; const { value } = this.props; if (value && value != preValue) { let fileList = this.transvalue(value); this.setState({ fileList }) } } transvalue = (value) => { const { serverKey } = this.props; let value_1 = value; let fileList = []; if (typeof value_1 == "string") { value_1 = JSON.parse(value); if (value_1 instanceof Array) { value_1.forEach(a => { if (a.valid == "new") { fileList.push({ valid: "new", url: a.url, fileKey: a.fileKey, status: "done", uid: a.uid, name: a.fileName ? a.fileName : a.name, }) } else if (a.uid) { fileList.push({ atRatCode: a.atRatCode, valid: a.valid, uid: a.uid, name: a.name, fileKey: a.base ? a.base.bytesInfoVO.code : a.fileKey, status: "done", url: a.url, }); } else { fileList.push({ atRatCode: a.atRatCode, valid: a.valid, uid: a.base ? a.base.bytesInfoVO.code : a.fileKey, name: a.base.fileName, fileKey: a.base ? a.base.bytesInfoVO.code : a.fileKey, status: "done", url: encodeURI(Units.joinPath( Units.joinPath(Units.api(), "/v3/files"), a.base.path )) + `?@token=${Units.serverHydrocarbonToken(serverKey)}&@programToken=${Units.programCode(serverKey)}&@applicationToken=${Units.applicationCode(serverKey)}&disposition=attachment`, }); } }) } else if (value_1.valid === true) { fileList.push({ atRatCode: value_1.atRatCode, uid: value_1.base.bytesInfoVO.code, name: value_1.base.fileName, fileKey: value_1.base.bytesInfoVO.code, status: "done", url: encodeURI(Units.joinPath( Units.joinPath(Units.api(), "/v3/files"), value_1.base.path )) + `?@token=${Units.serverHydrocarbonToken(serverKey)}&@programToken=${Units.programCode(serverKey)}&@applicationToken=${Units.applicationCode(serverKey)}&disposition=attachment`, }); } } return fileList; } render() { const { id, itemType, disabled, serverKey, fieldConfig } = this.props; const { showUploadList = true, uploadText } = fieldConfig || {}; const { translate } = this.context; const { fileList } = this.state; let maxCount = this.props.maxCount ? this.props.maxCount : 1; return (
HcserviceV3.postFileAction(serverKey, file)} beforeUpload={this.beforeUpload} onChange={this.handleChange} onRemove={this.handleRemove} showUploadList={showUploadList} > {fileList && fileList.length >= maxCount ? ( "" ) : ( )}
); } }