通用评论

Upload.js 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _modal = require("antd/es/modal");
  6. var _modal2 = _interopRequireDefault(_modal);
  7. var _upload = require("antd/es/upload");
  8. var _upload2 = _interopRequireDefault(_upload);
  9. var _icon = require("antd/es/icon");
  10. var _icon2 = _interopRequireDefault(_icon);
  11. var _message2 = require("antd/es/message");
  12. var _message3 = _interopRequireDefault(_message2);
  13. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  14. require("antd/es/modal/style/css");
  15. require("antd/es/upload/style/css");
  16. require("antd/es/icon/style/css");
  17. require("antd/es/message/style/css");
  18. var _react = require("react");
  19. var _react2 = _interopRequireDefault(_react);
  20. var _dayjs = require("dayjs");
  21. var _dayjs2 = _interopRequireDefault(_dayjs);
  22. var _shortid = require("shortid");
  23. var _shortid2 = _interopRequireDefault(_shortid);
  24. var _constant = require("../../constant");
  25. var _Comment = require("../../Comment");
  26. var _Comment2 = _interopRequireDefault(_Comment);
  27. require("./Upload.css");
  28. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  29. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  30. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  31. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  32. // import styles from "./Upload.less";
  33. var client = function client(oss) {
  34. return new window.OSS.Wrapper({
  35. accessKeyId: oss.access_key_id,
  36. accessKeySecret: oss.access_key_secret,
  37. stsToken: oss.security_token,
  38. endpoint: _constant.OSS_ENDPOINT, //常量,你可以自己定义
  39. bucket: _constant.OSS_BUCKET
  40. });
  41. };
  42. var uploadPath = function uploadPath(path, file) {
  43. return path + "/" + (0, _dayjs2.default)().format("YYYYMMDD") + "/" + _shortid2.default.generate() + "." + file.type.split("/")[1];
  44. };
  45. var UploadToOss = function UploadToOss(oss, path, file) {
  46. var url = uploadPath(path, file);
  47. return new Promise(function (resolve, reject) {
  48. client(oss).multipartUpload(url, file).then(function (data) {
  49. resolve(data);
  50. }).catch(function (error) {
  51. reject(error);
  52. });
  53. });
  54. };
  55. var App = function (_React$Component) {
  56. _inherits(App, _React$Component);
  57. function App(props) {
  58. _classCallCheck(this, App);
  59. var _this = _possibleConstructorReturn(this, (App.__proto__ || Object.getPrototypeOf(App)).call(this, props));
  60. _this.state = {
  61. previewVisible: false,
  62. previewImage: ""
  63. };
  64. _this.handleCancel = _this.handleCancel.bind(_this);
  65. _this.handlePreview = _this.handlePreview.bind(_this);
  66. _this.handleChange = _this.handleChange.bind(_this);
  67. _this.customRequest = _this.customRequest.bind(_this);
  68. return _this;
  69. }
  70. _createClass(App, [{
  71. key: "componentDidMount",
  72. value: function componentDidMount() {
  73. this.props.app.sOssSts();
  74. }
  75. }, {
  76. key: "handleCancel",
  77. value: function handleCancel() {
  78. this.setState({ previewVisible: false });
  79. }
  80. }, {
  81. key: "handlePreview",
  82. value: function handlePreview(file) {
  83. this.setState({
  84. previewImage: file.url || file.thumbUrl,
  85. previewVisible: true
  86. });
  87. }
  88. }, {
  89. key: "handleChange",
  90. value: function handleChange(_ref) {
  91. var fileList = _ref.fileList;
  92. this.props.onChangeFileList(fileList);
  93. }
  94. }, {
  95. key: "customRequest",
  96. value: function customRequest(info) {
  97. var _this2 = this;
  98. var file = info.file;
  99. info.onProgress({ percent: 10 });
  100. var reader = new FileReader();
  101. reader.readAsDataURL(info.file);
  102. reader.onloadend = function () {
  103. info.onProgress({ percent: 20 });
  104. // DRIVER_LICENSE_PATH oss 的存储路径位置
  105. UploadToOss(_this2.props.app.oss, _constant.DRIVER_LICENSE_PATH, file).then(function (data) {
  106. info.onProgress({ percent: 100 });
  107. info.onSuccess();
  108. _this2.props.onUpload({ path: data.name, uid: file.uid });
  109. }).catch(function (e) {
  110. _message3.default.error(e.message || _constant.ERROR_DEFAULT);
  111. info.onError(e);
  112. });
  113. };
  114. }
  115. }, {
  116. key: "render",
  117. value: function render() {
  118. var _state = this.state,
  119. previewVisible = _state.previewVisible,
  120. previewImage = _state.previewImage;
  121. var fileList = this.props.fileList;
  122. var uploadButton = _react2.default.createElement(
  123. "div",
  124. null,
  125. _react2.default.createElement(_icon2.default, { type: "plus" }),
  126. _react2.default.createElement(
  127. "div",
  128. { className: "ant-upload-text" },
  129. "\u4E0A\u4F20"
  130. )
  131. );
  132. return _react2.default.createElement(
  133. "div",
  134. null,
  135. _react2.default.createElement(
  136. _upload2.default,
  137. {
  138. accept: "image/jpg,image/jpeg,image/png,image/bmp",
  139. listType: "picture-card",
  140. fileList: fileList,
  141. customRequest: this.customRequest,
  142. onPreview: this.handlePreview,
  143. onChange: this.handleChange
  144. },
  145. fileList.length >= _constant.MAX_UPLOAD_NUMBER ? null : uploadButton
  146. ),
  147. _react2.default.createElement(
  148. _modal2.default,
  149. {
  150. visible: previewVisible,
  151. footer: null,
  152. onCancel: this.handleCancel
  153. },
  154. _react2.default.createElement("img", { alt: "upload", style: { width: "100%" }, src: previewImage })
  155. )
  156. );
  157. }
  158. }]);
  159. return App;
  160. }(_react2.default.Component);
  161. exports.default = (0, _Comment2.default)(App);
  162. //# sourceMappingURL=Upload.js.map