通用评论

Upload.js 4.6KB

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