通用评论

Upload.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. if (this.props.onRef) {
  59. this.props.onRef(this);
  60. }
  61. }
  62. onImgLoad() {
  63. this.setState({
  64. loading: false
  65. });
  66. }
  67. handleCancel() {
  68. this.setState({ previewVisible: false });
  69. }
  70. handlePreview(file) {
  71. this.setState({
  72. previewImage: file.url || file.thumbUrl,
  73. previewVisible: true
  74. });
  75. }
  76. handleChange({ fileList }) {
  77. this.props.onChangeFileList(fileList);
  78. }
  79. customRequest(info) {
  80. const { file } = info;
  81. info.onProgress({ percent: 10 });
  82. let reader = new FileReader();
  83. reader.readAsDataURL(info.file);
  84. reader.onloadend = () => {
  85. info.onProgress({ percent: 20 });
  86. // DRIVER_LICENSE_PATH oss 的存储路径位置
  87. UploadToOss(this.props.app.oss, DRIVER_LICENSE_PATH, file)
  88. .then(data => {
  89. info.onProgress({ percent: 100 });
  90. info.onSuccess(data);
  91. this.props.onUpload({ path: data.name, uid: file.uid });
  92. })
  93. .catch(e => {
  94. const msg = e.message || ERROR_DEFAULT;
  95. if (this.props.showError) {
  96. message.error(msg);
  97. }
  98. if (this.props.onError) {
  99. this.props.onError(msg, { response: e.response });
  100. }
  101. info.onError(e);
  102. });
  103. };
  104. }
  105. handleCloseClick(index) {
  106. let newFileList = this.props.fileList;
  107. newFileList.splice(index, 1);
  108. this.props.onChangeFileList(newFileList);
  109. }
  110. render() {
  111. const { previewVisible, previewImage } = this.state;
  112. const { fileList, maxUpload, multiple } = this.props;
  113. const uploadButton = (
  114. <div>
  115. <Icon type="plus" />
  116. <div className="ant-upload-text">{intl.get("editor.uploadBtn")}</div>
  117. </div>
  118. );
  119. return (
  120. <div>
  121. <Upload
  122. accept="image/jpg,image/jpeg,image/png,image/bmp"
  123. multiple={multiple}
  124. listType="picture-card"
  125. showUploadList={{ showPreviewIcon: true, showRemoveIcon: false }}
  126. fileList={fileList}
  127. customRequest={this.customRequest}
  128. onPreview={this.handlePreview}
  129. onChange={this.handleChange}
  130. >
  131. {fileList.length >= maxUpload ? null : uploadButton}
  132. </Upload>
  133. {fileList.map((file, index) => {
  134. return (
  135. <div
  136. className="upload-close-icon"
  137. onClick={this.handleCloseClick.bind(this, index)}
  138. key={file.uid}
  139. style={{
  140. left: `${(index % 3) * 112 + 98}px`,
  141. top: `${Math.floor(index / 3) * 112 + 16}px`
  142. }}
  143. />
  144. );
  145. })}
  146. <Modal
  147. wrapClassName="upload-img-preview"
  148. visible={previewVisible}
  149. footer={null}
  150. onCancel={this.handleCancel}
  151. >
  152. <Spin spinning={this.state.loading}>
  153. <img
  154. alt="upload"
  155. style={{ width: "100%" }}
  156. src={previewImage}
  157. onLoad={this.onImgLoad}
  158. onError={this.onImgLoad}
  159. />
  160. </Spin>
  161. </Modal>
  162. </div>
  163. );
  164. }
  165. }
  166. export default Comment(App);