UNPKG

knk

Version:

react components based on react

119 lines 3.98 kB
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck"; import _createClass from "@babel/runtime/helpers/esm/createClass"; import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized"; import _inherits from "@babel/runtime/helpers/esm/inherits"; import _createSuper from "@babel/runtime/helpers/esm/createSuper"; import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { Spin, message } from 'antd'; import { serialize } from '../../../common/tool'; import fetchJsonp from 'fetch-jsonp'; function getUploadDownloadInfo(key, path) { var pathUrl = path; if (key) { pathUrl += (/\?/.test(path) ? '&' : '?') + serialize({ key: key }); } return fetchJsonp(pathUrl, { jsonpCallback: 'jsonpCallback' }).then(function (response) { return response.json(); }); } var RmsFilesDown = /*#__PURE__*/function (_Component) { _inherits(RmsFilesDown, _Component); var _super = _createSuper(RmsFilesDown); function RmsFilesDown(props) { var _this; _classCallCheck(this, RmsFilesDown); _this = _super.call(this, props); _this.state = { redirectUrl: '', loading: true }; _this.open = true; _this.handleFetchDownUrl = _this.handleFetchDownUrl.bind(_assertThisInitialized(_this)); return _this; } _createClass(RmsFilesDown, [{ key: "componentDidUpdate", value: function componentDidUpdate(preProps) { var url = this.props.url; if (url !== preProps.url) { this.handleFetchDownUrl(); } } }, { key: "componentDidMount", value: function componentDidMount() { this.handleFetchDownUrl(); } }, { key: "componentWillUnmount", value: function componentWillUnmount() { this.open = false; } }, { key: "render", value: function render() { var _this$state = this.state, redirectUrl = _this$state.redirectUrl, loading = _this$state.loading; var _this$props = this.props, content = _this$props.content, showTip = _this$props.showTip; return /*#__PURE__*/React.createElement(Spin, { spinning: loading, tip: showTip || '' }, redirectUrl ? /*#__PURE__*/React.createElement("a", { href: redirectUrl, target: "__blank", download: true }, content) : null); } }, { key: "handleFetchDownUrl", value: function handleFetchDownUrl() { var _this2 = this; var _this$props2 = this.props, url = _this$props2.url, filename = _this$props2.filename, uploadTokenURI = _this$props2.uploadTokenURI; var apiPath = "".concat(uploadTokenURI, "/getDownloadParamJsonp"); if (!url.match(/\?key=(.+)/)) { this.setState({ loading: false, redirectUrl: url }); return; } var decodeUrl = decodeURIComponent(url.match(/\?key=(.+)/)[1]); getUploadDownloadInfo(decodeUrl, apiPath).then(function (data) { var file = data.data; var fileType = file.mimetype.split('/')[1]; var downName = /\..+/.test(filename) ? filename : "".concat(filename, ".").concat(fileType); // 如果传入的filename已经包含后缀名 if (_this2.open) { _this2.setState({ loading: false, redirectUrl: file.mimetype.split('/')[0] === 'image' ? "".concat(file.downloadUrl, "?attname=").concat(encodeURIComponent(downName)) : file.downloadUrl }); } }).catch(function (err) { _this2.setState({ loading: false }); message.error("\u83B7\u53D6\u4E0B\u8F7D\u5730\u5740\u5931\u8D25:".concat(err.message)); }); } }]); return RmsFilesDown; }(Component); RmsFilesDown.propTypes = { url: PropTypes.string.isRequired, // 文件地址 content: PropTypes.object.isRequired, filename: PropTypes.string.isRequired, showTip: PropTypes.string }; export default RmsFilesDown;