146 lines
4.7 KiB
JavaScript
146 lines
4.7 KiB
JavaScript
import React,{useEffect,useState,useRef} from 'react';
|
||
import { Form, Button, Input, Row, Col, Upload,message,Image } from 'antd';
|
||
import {PaperClipOutlined,DeleteOutlined} from '@ant-design/icons';
|
||
import { formItemLayout, btnItemLayout } from '../../../../components/crud/FormLayoutProps';
|
||
import apiurl from '../../../../service/apiurl';
|
||
const ModalForm = ({ mode, record, onEdit, onSave, onCrudSuccess }) => {
|
||
const url = "http://223.75.53.141:9102/test.by-lyf.tmp"
|
||
const [form] = Form.useForm();
|
||
const [fileList, setFileList] = useState([]) //上传文件列表
|
||
const [loading, setLoading] = useState(false)
|
||
const fileChange = (info) => {
|
||
if (info.file.status === "done") {
|
||
setLoading(false);
|
||
setFileList(info.fileList)
|
||
}
|
||
if (info.file.status === "uploading") {
|
||
setLoading(true);
|
||
}
|
||
if (info.file.status === "error") {
|
||
message.error("文件上传失败")
|
||
setLoading(false);
|
||
}
|
||
setFileList(info.fileList)
|
||
}
|
||
|
||
const beforeUpload = (file) => {
|
||
if (fileList.length == 1) {
|
||
message.error("只能上传一张图片!")
|
||
return false;
|
||
}
|
||
const isJpgOrPng =
|
||
file.type === 'image/jpeg' ||
|
||
file.type === 'image/jpg' ||
|
||
file.type === 'image/png';
|
||
if (!isJpgOrPng) {
|
||
message.error('请上传jpeg/png/jpg格式的图片!');
|
||
}
|
||
return isJpgOrPng
|
||
};
|
||
|
||
const onFinish = async (values) => {
|
||
let oldFiles = fileList.map(item => ({ fileId: item.response?.data?.fileId }))
|
||
values.pic = oldFiles[0];
|
||
delete values.files
|
||
if (mode === 'save') {
|
||
onSave(apiurl.rcgl.byfz.fzxc.picSave,values)
|
||
}
|
||
}
|
||
|
||
const deleteFile = (fileId) => {
|
||
let filterFile = fileList.filter(item => item.response?.data?.fileId !== fileId);
|
||
setFileList(filterFile)
|
||
}
|
||
|
||
return (
|
||
<>
|
||
<Form form={form} {...formItemLayout} onFinish={onFinish} initialValues={record}>
|
||
<Row>
|
||
<Col span={24}>
|
||
<Form.Item
|
||
label="图片标题"
|
||
name="picTitle"
|
||
labelCol={{ span: 3, offset: 0 }}
|
||
wrapperCol={{span:20,offset:0}}
|
||
rules={[{ required: true }]}
|
||
>
|
||
<Input
|
||
style={{ width: '100%' }}
|
||
allowClear
|
||
/>
|
||
</Form.Item>
|
||
</Col>
|
||
|
||
</Row>
|
||
<Row>
|
||
<Col span={24}>
|
||
<Form.Item
|
||
label="图片"
|
||
name="files"
|
||
labelCol={{ span: 3, offset: 0 }}
|
||
wrapperCol={{span:21,offset:0}}
|
||
rules={[{ required: true }]}
|
||
>
|
||
<Upload
|
||
name='file'
|
||
action="/gunshiApp/xyt/termite/pic/file/upload/singleSimple"
|
||
onChange={fileChange}
|
||
fileList={fileList}
|
||
disabled={loading}
|
||
showUploadList={false}
|
||
beforeUpload={beforeUpload}
|
||
>
|
||
{mode == "view" ? null :
|
||
<div style={{display:"flex",alignItems:"center",columnGap:10,color:"#4f85ec",cursor:"pointer"}}>
|
||
<PaperClipOutlined />
|
||
<a style={{ cursor: "pointer" }}>上传图片
|
||
<span style={{ marginLeft:10,color:"#dfdfdf" }}>(支持扩展名:.jpg .png)</span></a>
|
||
</div>
|
||
}
|
||
</Upload>
|
||
</Form.Item>
|
||
</Col>
|
||
</Row>
|
||
<Row gutter={[16]}>
|
||
{
|
||
loading ? <span>文件正在上传中,请等待</span> :
|
||
fileList.length > 0 && fileList.map(file => {
|
||
return (
|
||
<Col span={12}>
|
||
<div className={mode == "view" ? 'file-item' : 'file-item'}>
|
||
<div className='file-description'>
|
||
<Image width={60} src={url +file.response?.data?.filePath} alt='' />
|
||
<span>{file.name}</span>
|
||
</div>
|
||
{mode !== "view" &&
|
||
<div
|
||
className="delete-icon"
|
||
onClick={() => deleteFile(file.response?.data?.fileId)}
|
||
>
|
||
<DeleteOutlined />
|
||
</div>
|
||
}
|
||
</div>
|
||
</Col>
|
||
)
|
||
})
|
||
}
|
||
</Row>
|
||
{
|
||
mode==='view'?null:(
|
||
<>
|
||
<Form.Item {...btnItemLayout}>
|
||
<Button type="primary" htmlType="submit">
|
||
{mode === 'save' ? '提交' : '修改'}
|
||
</Button>
|
||
</Form.Item>
|
||
</>
|
||
)
|
||
}
|
||
</Form>
|
||
</>
|
||
);
|
||
}
|
||
|
||
export default ModalForm;
|