cropBlock.jsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import React, {
  2. Component
  3. } from 'react';
  4. import CropBox from './cropBox.jsx';
  5. import './crop.css';
  6. import {Icon,message} from 'antd';
  7. class CropBlock extends Component {
  8. constructor(props) {
  9. super(props);
  10. this.state = {
  11. urlArr: this.props.imgArr ? this.props.imgArr : [],
  12. number: this.props.number ? this.props.number : '',
  13. uploadData: this.props.uploadData ? this.props.uploadData : {},
  14. aspectRatio: this.props.aspectRatio ? this.props.aspectRatio : '',
  15. url: this.props.url ? this.props.url : '',
  16. close: true,
  17. }
  18. this.getUrl = this.getUrl.bind(this);
  19. this.addImg = this.addImg.bind(this);
  20. this.delImg = this.delImg.bind(this);
  21. this.getAllImg = this.getAllImg.bind(this);
  22. }
  23. getUrl(url) {
  24. let urlArr2 = this.state.urlArr;
  25. urlArr2.push(url);
  26. this.setState({
  27. urlArr: urlArr2,
  28. close: true
  29. })
  30. this.getAllImg(urlArr2);
  31. }
  32. addImg() {
  33. if(!this.state.number || (this.state.urlArr.length < this.state.number)) {
  34. this.setState({
  35. close: false
  36. })
  37. }else {
  38. message.warning("最多上传"+this.state.number+"张图片!");
  39. }
  40. }
  41. closeFun(state){
  42. this.setState({
  43. close:state
  44. })
  45. }
  46. delImg(index) {
  47. let urlArr2 = this.state.urlArr;
  48. urlArr2.splice(index, 1);
  49. this.setState({
  50. urlArr: urlArr2,
  51. close: true
  52. })
  53. this.getAllImg(urlArr2);
  54. }
  55. getAllImg(urlArr) {
  56. if(this.props.getAllImg) {
  57. this.props.getAllImg(urlArr);
  58. }
  59. }
  60. componentWillReceiveProps(data){
  61. if(data.urlArr.length>0){
  62. this.setState({
  63. urlArr:data.urlArr
  64. })
  65. }
  66. }
  67. render() {
  68. let imgList = this.state.urlArr.map((src, index) =>
  69. <div key = {index} className="crop-img-block">
  70. <img alt = "图片" className = "crop-img" title ="" src = {globalConfig.avatarHost + '/upload'+src} />
  71. <span className="del"><Icon type='delete' onClick = {this.delImg.bind(this, index)}/></span>
  72. </div>
  73. )
  74. return (
  75. <div className = "crop-block">
  76. {imgList}
  77. {this.state.urlArr.length < this.state.number&&<div className="crop-add-img" onClick = {this.addImg}>
  78. <span>+</span>
  79. <p>点击上传</p>
  80. </div>}
  81. <CropBox closeFun={this.closeFun.bind(this)} getUrl = {this.getUrl} uploadData = {this.state.uploadData} aspectRatio = {this.state.aspectRatio} url = {this.state.url} close = {this.state.close}/>
  82. </div>
  83. )
  84. }
  85. }
  86. export default CropBlock;
  87. /*
  88. //调用
  89. <Crop
  90. number = {5}
  91. aspectRatio = {1/1}
  92. url ={globalConfig.context + '/api/admin/banners/uploadPicture'}
  93. uploadData = {{sign: 'banners'}}
  94. urlArr = {[]}
  95. />
  96. */