通用评论

Upload.js 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 _reactIntlUniversal = require("react-intl-universal");
  25. var _reactIntlUniversal2 = _interopRequireDefault(_reactIntlUniversal);
  26. var _constant = require("../../constant");
  27. var _Comment = require("../../Comment");
  28. var _Comment2 = _interopRequireDefault(_Comment);
  29. require("./Upload.css");
  30. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  31. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  32. 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; }
  33. 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; }
  34. var client = function client(oss) {
  35. return new window.OSS.Wrapper({
  36. accessKeyId: oss.access_key_id,
  37. accessKeySecret: oss.access_key_secret,
  38. stsToken: oss.security_token,
  39. endpoint: _constant.OSS_ENDPOINT, //常量,你可以自己定义
  40. bucket: _constant.OSS_BUCKET
  41. });
  42. };
  43. var uploadPath = function uploadPath(path, file) {
  44. return path + "/" + (0, _dayjs2.default)().format("YYYYMMDD") + "/" + _shortid2.default.generate() + "." + file.type.split("/")[1];
  45. };
  46. var UploadToOss = function UploadToOss(oss, path, file) {
  47. var url = uploadPath(path, file);
  48. return new Promise(function (resolve, reject) {
  49. client(oss).multipartUpload(url, file).then(function (data) {
  50. resolve(data);
  51. }).catch(function (error) {
  52. reject(error);
  53. });
  54. });
  55. };
  56. var App = function (_React$Component) {
  57. _inherits(App, _React$Component);
  58. function App(props) {
  59. _classCallCheck(this, App);
  60. var _this = _possibleConstructorReturn(this, (App.__proto__ || Object.getPrototypeOf(App)).call(this, props));
  61. _this.state = {
  62. previewVisible: false,
  63. previewImage: ""
  64. };
  65. _this.handleCancel = _this.handleCancel.bind(_this);
  66. _this.handlePreview = _this.handlePreview.bind(_this);
  67. _this.handleChange = _this.handleChange.bind(_this);
  68. _this.customRequest = _this.customRequest.bind(_this);
  69. return _this;
  70. }
  71. _createClass(App, [{
  72. key: "componentDidMount",
  73. value: function componentDidMount() {
  74. this.props.app.sOssSts();
  75. }
  76. }, {
  77. key: "handleCancel",
  78. value: function handleCancel() {
  79. this.setState({ previewVisible: false });
  80. }
  81. }, {
  82. key: "handlePreview",
  83. value: function handlePreview(file) {
  84. this.setState({
  85. previewImage: file.url || file.thumbUrl,
  86. previewVisible: true
  87. });
  88. }
  89. }, {
  90. key: "handleChange",
  91. value: function handleChange(_ref) {
  92. var fileList = _ref.fileList;
  93. this.props.onChangeFileList(fileList);
  94. }
  95. }, {
  96. key: "customRequest",
  97. value: function customRequest(info) {
  98. var _this2 = this;
  99. var file = info.file;
  100. info.onProgress({ percent: 10 });
  101. var reader = new FileReader();
  102. reader.readAsDataURL(info.file);
  103. reader.onloadend = function () {
  104. info.onProgress({ percent: 20 });
  105. // DRIVER_LICENSE_PATH oss 的存储路径位置
  106. UploadToOss(_this2.props.app.oss, _constant.DRIVER_LICENSE_PATH, file).then(function (data) {
  107. info.onProgress({ percent: 100 });
  108. info.onSuccess();
  109. _this2.props.onUpload({ path: data.name, uid: file.uid });
  110. }).catch(function (e) {
  111. var msg = e.message || _constant.ERROR_DEFAULT;
  112. if (_this2.props.showError) {
  113. _message3.default.error(msg);
  114. }
  115. if (_this2.props.onError) {
  116. _this2.props.onError(msg, { response: e.response });
  117. }
  118. info.onError(e);
  119. });
  120. };
  121. }
  122. }, {
  123. key: "render",
  124. value: function render() {
  125. var _state = this.state,
  126. previewVisible = _state.previewVisible,
  127. previewImage = _state.previewImage;
  128. var _props = this.props,
  129. fileList = _props.fileList,
  130. maxUpload = _props.maxUpload,
  131. multiple = _props.multiple;
  132. var uploadButton = _react2.default.createElement(
  133. "div",
  134. null,
  135. _react2.default.createElement(_icon2.default, { type: "plus" }),
  136. _react2.default.createElement(
  137. "div",
  138. { className: "ant-upload-text" },
  139. _reactIntlUniversal2.default.get("editor.uploadBtn")
  140. )
  141. );
  142. return _react2.default.createElement(
  143. "div",
  144. null,
  145. _react2.default.createElement(
  146. _upload2.default,
  147. {
  148. accept: "image/jpg,image/jpeg,image/png,image/bmp",
  149. multiple: multiple,
  150. listType: "picture-card",
  151. fileList: fileList,
  152. customRequest: this.customRequest,
  153. onPreview: this.handlePreview,
  154. onChange: this.handleChange
  155. },
  156. fileList.length >= maxUpload ? null : uploadButton
  157. ),
  158. _react2.default.createElement(
  159. _modal2.default,
  160. {
  161. className: "upload-img-preview",
  162. visible: previewVisible,
  163. footer: null,
  164. onCancel: this.handleCancel
  165. },
  166. _react2.default.createElement("img", { alt: "upload", style: { width: "100%" }, src: previewImage })
  167. )
  168. );
  169. }
  170. }]);
  171. return App;
  172. }(_react2.default.Component);
  173. exports.default = (0, _Comment2.default)(App);
  174. //# sourceMappingURL=Upload.js.map