通用评论

Upload.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import React from "react";
  2. import { Upload, Icon, Modal, message } 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. };
  48. this.handleCancel = this.handleCancel.bind(this);
  49. this.handlePreview = this.handlePreview.bind(this);
  50. this.handleChange = this.handleChange.bind(this);
  51. this.customRequest = this.customRequest.bind(this);
  52. }
  53. componentDidMount() {
  54. this.props.app.sOssSts();
  55. }
  56. handleCancel() {
  57. this.setState({ previewVisible: false });
  58. }
  59. handlePreview(file) {
  60. this.setState({
  61. previewImage: file.url || file.thumbUrl,
  62. previewVisible: true
  63. });
  64. }
  65. handleChange({ fileList }) {
  66. this.props.onChangeFileList(fileList);
  67. }
  68. customRequest(info) {
  69. const { file } = info;
  70. info.onProgress({ percent: 10 });
  71. let reader = new FileReader();
  72. reader.readAsDataURL(info.file);
  73. reader.onloadend = () => {
  74. info.onProgress({ percent: 20 });
  75. // DRIVER_LICENSE_PATH oss 的存储路径位置
  76. UploadToOss(this.props.app.oss, DRIVER_LICENSE_PATH, file)
  77. .then(data => {
  78. info.onProgress({ percent: 100 });
  79. info.onSuccess();
  80. this.props.onUpload({ path: data.name, uid: file.uid });
  81. })
  82. .catch(e => {
  83. const msg = e.message || ERROR_DEFAULT;
  84. if (this.props.showError) {
  85. message.error(msg);
  86. }
  87. if (this.props.onError) {
  88. this.props.onError(msg, { response: e.response });
  89. }
  90. info.onError(e);
  91. });
  92. };
  93. }
  94. render() {
  95. const { previewVisible, previewImage } = this.state;
  96. const { fileList, maxUpload, multiple } = this.props;
  97. const uploadButton = (
  98. <div>
  99. <Icon type="plus" />
  100. <div className="ant-upload-text">{intl.get("editor.uploadBtn")}</div>
  101. </div>
  102. );
  103. return (
  104. <div>
  105. <Upload
  106. accept="image/jpg,image/jpeg,image/png,image/bmp"
  107. multiple={multiple}
  108. listType="picture-card"
  109. fileList={fileList}
  110. customRequest={this.customRequest}
  111. onPreview={this.handlePreview}
  112. onChange={this.handleChange}
  113. >
  114. {fileList.length >= maxUpload ? null : uploadButton}
  115. </Upload>
  116. <Modal
  117. visible={previewVisible}
  118. footer={null}
  119. onCancel={this.handleCancel}
  120. >
  121. <img alt="upload" style={{ width: "100%" }} src={previewImage} />
  122. </Modal>
  123. </div>
  124. );
  125. }
  126. }
  127. export default Comment(App);