import React, { PureComponent } from 'react'; import { Button, Card, Form, message, Upload, Icon, Modal, Row, Col } from 'antd'; import { connect } from 'dva'; import { queryMyData, submitData } from '../api'; import { uploadImage } from '../../utils/wq.img.upload';
import styles from '../../utils/form.less';
const FormItem = Form.Item;
@Form.create() export default class PicturesWall extends PureComponent { constructor(props) { super(props); const { id } = this.props.match.params; this.state = { id, img: undefined, imgList: [], previewVisible: false, previewImage: '', }; }
componentDidMount() { const { id } = this.state; id && this.queryData(); }
queryData() { const { id } = this.state; queryMyData({ id, }) .then(({ ret, data }) => { if (ret == 0 && data && data.list && data.list.length) { const item = data.list[0];
const img = data.img; const imgList = item.img ? [ { uid: '1', name: 'hehe.png', status: 'done', url: img, }, ] : [];
this.setState({ img, imgList, }); } else { return Promise.reject(); } }) .catch(() => { message.error('查询出错,请重试'); }); }
handleCancel = () => this.setState({ previewVisible: false });
handlePreview = (file) => { console.log('smyhvae handlePreview:' + JSON.stringify(file)); this.setState({ previewImage: file.url || file.thumbUrl, previewVisible: true, }); };
handleBeforeUpload = (file) => { console.log('smyhvae handleBeforeUpload file:' + JSON.stringify(file)); console.log('smyhvae handleBeforeUpload file.file:' + JSON.stringify(file.file)); console.log('smyhvae handleBeforeUpload file type:' + JSON.stringify(file.type));
const isJPG = file.type === 'image/jpeg'; const isJPEG = file.type === 'image/jpeg'; const isGIF = file.type === 'image/gif'; const isPNG = file.type === 'image/png'; const isLt2M = file.size / 1024 / 1024 < 1; if (!(isJPG || isJPEG || isPNG)) { Modal.error({ title: '只能上传JPG、JPEG、PNG格式的图片~', }); } else if (!isLt2M) { Modal.error({ title: '图片超过1M限制,不允许上传~', }); } return (isJPG || isJPEG || isPNG) && isLt2M; };
checkImageWH(file) { return new Promise(function (resolve, reject) { let filereader = new FileReader(); filereader.onload = (e) => { let src = e.target.result; const image = new Image(); image.onload = function () { file.width = this.width; file.height = this.height; resolve(); }; image.onerror = reject; image.src = src; }; filereader.readAsDataURL(file); }); }
doImgUpload = (options) => { const { onSuccess, onError, file, onProgress } = options;
const imgItem = { uid: '1', name: 'hehe.png', status: 'uploading', url: '', percent: 99, };
this.setState({ imgList: [imgItem], });
const reader = new FileReader(); reader.readAsDataURL(file);
reader.onload = (file) => { const params = { myBase64: file.target.result, };
uploadImage(params) .then((res) => { console.log('smyhvae doImgUpload:' + JSON.stringify(res)); console.log('smyhvae 图片上传成功:' + res.imageUrl);
const imgItem = { uid: '1', name: 'hehe.png', status: 'done', url: res.imageUrl, imgUrl: res.imageUrl, };
this.setState({ imgList: [imgItem], }); }) .catch((e) => { console.log('smyhvae 图片上传失败:' + JSON.stringify(e || '')); message.error('图片上传失败,请重试'); }); }; };
handleChange = ({ file, fileList }) => { console.log('smyhvae handleChange file:' + JSON.stringify(file)); console.log('smyhvae handleChange fileList:' + JSON.stringify(fileList));
if (file.status == 'removed') { this.setState({ imgList: [], }); } };
submit = (e) => { e.preventDefault();
this.props.form.validateFields((err, fieldsValue) => { if (err) { return; }
const { id, imgList } = this.state;
const tempImgList = imgList.filter((item) => item.status == 'done'); const imgArr = []; tempImgList.forEach((item) => { imgArr.push(item.imgUrl); });
submitData({ id, img: imgArr[0] || '', }) .then((res) => { if (res.ret == 0) { message.success(`${id ? '修改' : '新增'}成功,自动跳转中...`);
} else if (res.ret == 201 || res.ret == 202 || res.ret == 203 || res.ret == 6) { return Promise.reject(res.msg); } else { return Promise.reject(); } }) .catch((e) => { message.error(e || '提交失败,请重试'); }); }); };
render() { const { id, imgList } = this.state; console.log('smyhvae render imgList:' + JSON.stringify(imgList)); const { getFieldDecorator } = this.props.form; const formItemLayout = { labelCol: { span: 3 }, wrapperCol: { span: 10 }, }; const buttonItemLayout = { wrapperCol: { span: 10, offset: 3 }, };
const uploadButton = ( <div> <Icon type="plus" /> <div className="ant-upload-text">Upload</div> </div> );
return ( <Card title={id ? '修改信息' : '新增信息'}> <Form onSubmit={this.submit} layout="horizontal">
{/* 新建图片、编辑图片 */} <FormItem label="图片" {...formItemLayout}> {getFieldDecorator('img', { rules: [{ required: false, message: '请上传图片' }], })( <Upload action="2" customRequest={this.doImgUpload} listType="picture-card" fileList={imgList} onPreview={this.handlePreview} beforeUpload={this.handleBeforeUpload} onChange={this.handleChange} > {imgList.length >= 1 ? null : uploadButton} </Upload> )} </FormItem> <Row> <Col span={3} /> <Col span={18} className={styles.graytext}> 注:图片支持JPG、JPEG、PNG格式,小于1M,最多上传1张 </Col> </Row>
<FormItem {...buttonItemLayout}> <Button type="primary" htmlType="submit"> 提交 </Button> </FormItem> </Form>
{/* 图片点开预览 */} <Modal visible={this.state.previewVisible} footer={null} onCancel={this.handleCancel}> <img alt="example" style={{ width: '100%' }} src={this.state.previewImage} /> </Modal> </Card> ); } }
|