1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
| 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> ); } }
|