通用评论

Upload.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import React from "react";
  2. import { Upload, Icon, Modal, message, Spin } from "antd";
  3. import intl from "react-intl-universal";
  4. import { DRIVER_LICENSE_PATH, ERROR_DEFAULT } from "../../constant";
  5. import Comment from "../../Comment";
  6. import "./Upload.css";
  7. class App extends React.Component {
  8. constructor(props) {
  9. super(props);
  10. this.state = {
  11. previewVisible: false,
  12. previewImage: "",
  13. loading: false
  14. };
  15. this.handleCancel = this.handleCancel.bind(this);
  16. this.handlePreview = this.handlePreview.bind(this);
  17. this.handleChange = this.handleChange.bind(this);
  18. this.customRequest = this.customRequest.bind(this);
  19. this.handleCloseClick = this.handleCloseClick.bind(this);
  20. this.onImgLoad = this.onImgLoad.bind(this);
  21. }
  22. componentDidMount() {
  23. this.props.app.sOssSts();
  24. if (this.props.onRef) {
  25. this.props.onRef(this);
  26. }
  27. }
  28. onImgLoad() {
  29. this.setState({
  30. loading: false
  31. });
  32. }
  33. handleCancel() {
  34. this.setState({ previewVisible: false });
  35. }
  36. handlePreview(file) {
  37. this.setState({
  38. previewImage: file.url || file.thumbUrl,
  39. previewVisible: true
  40. });
  41. }
  42. handleChange({ fileList }) {
  43. this.props.onChangeFileList(fileList);
  44. }
  45. customRequest(info) {
  46. const { file } = info;
  47. info.onProgress({ percent: 10 });
  48. let reader = new FileReader();
  49. reader.readAsDataURL(info.file);
  50. reader.onloadend = () => {
  51. info.onProgress({ percent: 20 });
  52. // DRIVER_LICENSE_PATH oss 的存储路径位置
  53. this.props.app
  54. .UploadToOss(this.props.app.oss, DRIVER_LICENSE_PATH, file)
  55. .then(data => {
  56. info.onProgress({ percent: 100 });
  57. info.onSuccess(data);
  58. this.props.onUpload({ path: data.name, uid: file.uid });
  59. })
  60. .catch(e => {
  61. const msg = e.message || ERROR_DEFAULT;
  62. if (this.props.showError) {
  63. message.error(msg);
  64. }
  65. if (this.props.onError) {
  66. this.props.onError(msg, { response: e.response });
  67. }
  68. info.onError(e);
  69. });
  70. };
  71. }
  72. handleCloseClick(index) {
  73. let newFileList = this.props.fileList;
  74. newFileList.splice(index, 1);
  75. this.props.onChangeFileList(newFileList);
  76. }
  77. render() {
  78. const { previewVisible, previewImage } = this.state;
  79. const { fileList, maxUpload, multiple } = this.props;
  80. const uploadButton = (
  81. <div>
  82. <Icon type="plus" />
  83. <div className="ant-upload-text">{intl.get("editor.uploadBtn")}</div>
  84. </div>
  85. );
  86. return (
  87. <div>
  88. <Upload
  89. accept="image/jpg,image/jpeg,image/png,image/bmp"
  90. multiple={multiple}
  91. listType="picture-card"
  92. showUploadList={{ showPreviewIcon: true, showRemoveIcon: false }}
  93. fileList={fileList}
  94. customRequest={this.customRequest}
  95. onPreview={this.handlePreview}
  96. onChange={this.handleChange}
  97. >
  98. {fileList.length >= maxUpload ? null : uploadButton}
  99. </Upload>
  100. {fileList.map((file, index) => {
  101. return (
  102. <div
  103. className="upload-close-icon"
  104. onClick={this.handleCloseClick.bind(this, index)}
  105. key={file.uid}
  106. style={{
  107. left: `${(index % 3) * 112 + 98}px`,
  108. top: `${Math.floor(index / 3) * 112 + 16}px`
  109. }}
  110. />
  111. );
  112. })}
  113. <Modal
  114. wrapClassName="upload-img-preview"
  115. visible={previewVisible}
  116. footer={null}
  117. onCancel={this.handleCancel}
  118. >
  119. <Spin spinning={this.state.loading}>
  120. <img
  121. alt="upload"
  122. style={{ width: "100%" }}
  123. src={previewImage}
  124. onLoad={this.onImgLoad}
  125. onError={this.onImgLoad}
  126. />
  127. </Spin>
  128. </Modal>
  129. </div>
  130. );
  131. }
  132. }
  133. export default Comment(App);