通用评论

Upload.js 4.7KB

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