通用评论

Upload.js 4.7KB

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