UNPKG

ming-demo3

Version:
513 lines (441 loc) 19.1 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")); var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")); var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized")); var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _findIndex2 = _interopRequireDefault(require("lodash/findIndex")); var _react = _interopRequireDefault(require("react")); var _baseui = require("@mdf/baseui"); var _label = _interopRequireDefault(require("../basic/label")); var _row = _interopRequireDefault(require("../basic/row")); var _fileList = _interopRequireDefault(require("./fileList")); var _SvgIcon = _interopRequireDefault(require("@mdf/metaui-web/lib/components/common/SvgIcon")); if (process.env.__CLIENT__ === true) { require("./fileList.css"); } var ContentTypeContrast = { ".gif": "image/gif", ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".png": "image/png", ".bmp": "image/x-ms-bmp", ".pdf": "application/pdf", ".txt": "text/plain", ".doc": "application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document", ".xls": "application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", ".p12": "application/x-pkcs12", ".pfx": "", ".zip": "application/x-zip-compressed", ".rar": "" }; var fileUpload = function (_React$Component) { (0, _inherits2["default"])(fileUpload, _React$Component); function fileUpload(props) { var _this; (0, _classCallCheck2["default"])(this, fileUpload); _this = (0, _possibleConstructorReturn2["default"])(this, (0, _getPrototypeOf2["default"])(fileUpload).call(this, props)); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "transferBill2Refer", function (value, bill2ReferKeyFieldMap) { var newValue = []; var fileList = _this.state.fileList; value.forEach(function (item, index) { var referItem = {}; for (var billKey in bill2ReferKeyFieldMap) { referItem[bill2ReferKeyFieldMap[billKey]] = item[billKey]; } if (fileList[index]) { if (fileList[index].name == referItem.name && fileList[index].size == referItem.size) { referItem.id = fileList[index].id; } else { var i = (0, _findIndex2["default"])(fileList, { "name": referItem.name, "size": referItem.size }); if (i != -1) referItem.id = fileList[i].id; } } if (_this.state.sortField) referItem[_this.state.sortField] = item[_this.state.sortField]; newValue.push(referItem); }); return newValue; }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "_fielUploadOnChange", function (info) { var _this$state = _this.state, fileList = _this$state.fileList, config = _this$state.config; var num = (0, _findIndex2["default"])(fileList, { 'id': info.file.uid }); if (info.file.status === 'uploading') { if (!info.file.percent) { cb.utils.loadingControl.end(); _this.uploading = false; fileList[num].percent = 100; } else { fileList[num].percent = info.file.percent; } fileList[num].size = info.file.size; _this.setState({ fileList: fileList, percent: info.file.percent }); } if (info.file.status === 'done') { if (info.file.response.code == 200) { if (config && config.uploadType == 'local') { fileList[num].address = info.file.response.data; } else { fileList[num].address = _this.DocumentServerAddress + info.file.response.data; } _this.setState({ fileList: fileList }); cb.utils.alert("".concat(info.file.name, " \u4E0A\u4F20\u6210\u529F\uFF01"), 'success'); cb.utils.loadingControl.end(); _this.uploading = false; if (_this.props.model) { var bSet = true; fileList.map(function (item) { if (cb.utils.isEmpty(item.address)) bSet = false; }); if (bSet) _this.props.model.setValue(fileList, true); } if (_this.props.doUpload) _this.props.doUpload(fileList); } else { cb.utils.loadingControl.end(); _this.uploading = false; fileList.pop(); _this.setState({ fileList: fileList }); cb.utils.alert("".concat(info.file.name, " \u4E0A\u4F20\u5931\u8D25\uFF01").concat(info.file.response.code, " : ").concat(info.file.response.message), 'error'); } } if (info.file.status === 'error') { cb.utils.alert("".concat(info.file.name, " \u4E0A\u4F20\u5931\u8D25\uFF01").concat(info.file.response.code, " : ").concat(info.file.response.message), 'error'); } }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "_fileListControl", function (fileType) { if (_this.state.fileList.length == 0 && (_this.state.readOnly || _this.state.disabled)) { return _react["default"].createElement("div", { className: "upload-nodata" }, _react["default"].createElement(_baseui.Icon, { type: fileType == "file" ? "noFile" : "noPic" }), _react["default"].createElement("span", null, fileType == "file" ? "暂无附件~" : "暂无图片~")); } var config = _this.state.config; var isDownLoadable = true; if (config && config.isDownLoadable != undefined) { isDownLoadable = config.isDownLoadable; } return _react["default"].createElement(_fileList["default"], { isDownLoadable: isDownLoadable, model: _this.props.model, arrowImg: _this._arrowImg, DelFile: _this._DelFile, colCount: _this.state.colCount, fileType: fileType, fileList: _this.state.fileList, readOnly: _this.state.readOnly || _this.state.disabled, sortField: _this.state.sortField, columns: _this.state.columns, mode: _this.state.mode }); }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "_DelFile", function (index) { if (_this.props.model) _this.props.model.deleteItem(index); }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "_arrowImg", function (arrow, index) {}); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "getfiletype", function (file) { var filetypearr = file.name.split('.'); return filetypearr[filetypearr.length - 1]; }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "_beforeUpload", function (file, uploadFileList) { cb.utils.loadingControl.start(); var _this$state2 = _this.state, fileList = _this$state2.fileList, config = _this$state2.config; var maxSize = 20971520, nowSize = 0; if (config && config.maxSize) maxSize = config.maxSize * 1024 * 1024; fileList && fileList.map(function (item) { nowSize += parseFloat(item.size); }); nowSize += file.size; if (nowSize > maxSize) { cb.utils.alert('总附件大小超出控制!', 'error'); _this.uploading = false; cb.utils.loadingControl.end(); return false; } var _this$state3 = _this.state, fileType = _this$state3.fileType, accept = _this$state3.accept; if (config && config.uploadType && config.uploadType == 'local') { if (fileList.length > 0 || uploadFileList.length > 1) { cb.utils.alert('当前只允许上传一个附件,请删除后重新上传!', 'error'); _this.uploading = false; cb.utils.loadingControl.end(); return false; } } if (config) { if (config.fileType) fileType = config.fileType; if (config.accept) accept = config.accept; } if (accept == '') { if (fileType == 'file') accept = "pdf,doc,xls,docx,xlsx,txt,jpg,png,bmp,gif";else accept = "png,jpg,jpeg,bmp"; } accept = accept.split(','); var filetype = _this.getfiletype(file); if (!filetype || !accept.includes(filetype.toLowerCase()) && !accept.includes('.' + filetype.toLowerCase())) { cb.utils.alert('选择文件格式不正确,请重新选择!', 'error'); _this.uploading = false; cb.utils.loadingControl.end(); return false; } if (config && config.maxQuantity) { if (fileList.length >= config.maxQuantity) { cb.utils.alert('超出上传限制,请删除后重新上传!', 'error'); _this.uploading = false; cb.utils.loadingControl.end(); return false; } if (uploadFileList.length > config.maxQuantity) { cb.utils.alert('选择数量超过上传限制,请重新上传!', 'error'); _this.uploading = false; cb.utils.loadingControl.end(); return false; } } if (!_this.uploading) { _this.uploading = true; } fileList.push({ 'percent': file.percent, 'name': file.name, 'id': file.uid, 'type': file.type }); _this.setState({ fileList: fileList }); }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "downLoadAll", function () { var fileList = _this.state.fileList; if (fileList.length > 0) { fileList.forEach(function (element) { window.open(element.address); }, (0, _assertThisInitialized2["default"])(_this)); } }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "getUploadData", function (file) { var config = _this.state.config; if (!config || !config.folderName) return file; file.folderName = config.folderName; return file; }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "renderBaseUpload", function (action, showName, fileListControl, accept, showUpLoad) { var config = _this.state.config, fileType = null, maxQuantity; if (!config || !config.fileType) { fileType = _this.state.fileType; } else { fileType = config.fileType; } if (config) maxQuantity = config.maxQuantity ? config.maxQuantity : -1; var str = maxQuantity !== -1 ? '不超过' + maxQuantity + '张' : '不限'; if (showUpLoad) { var uploadTips = ""; if (config && config.uploadTips) uploadTips = _react["default"].createElement("span", { className: "fileupload-txt" }, config.uploadTips);else { if (fileType == 'file') uploadTips = _react["default"].createElement("span", { className: "fileupload-txt" }, "\uFF08\u5168\u90E8\u9644\u4EF6\u5927\u5C0F\u4E0D\u53EF\u8D85\u8FC7", _react["default"].createElement("em", null, "20M"), "\uFF0C\u652F\u6301\u683C\u5F0F\uFF1APDF\u3001Word\u3001Excel\u3001Txt\u3001JPG\u3001PNG\u3001BMP\u3001GIF\uFF09"); if (config && config.uploadType == 'local') uploadTips = _react["default"].createElement("span", { className: "fileupload-txt" }, "\uFF08\u5168\u90E8\u9644\u4EF6\u5927\u5C0F\u4E0D\u53EF\u8D85\u8FC7", _react["default"].createElement("em", null, "20M"), "\uFF0C\u652F\u6301\u683C\u5F0F\uFF1AP12\uFF09"); } return _react["default"].createElement(_row["default"], null, _react["default"].createElement("div", { title: _this.state.showTitle, className: "fileupload" }, _react["default"].createElement(_baseui.Upload, { showUploadList: false, data: _this.getUploadData, action: action, onChange: _this._fielUploadOnChange, multiple: _this.state.multiple, accept: accept, beforeUpload: _this._beforeUpload, disabled: !showUpLoad }, _react["default"].createElement("a", { style: { color: _this.props.showNameColor ? _this.props.showNameColor : undefined }, className: "btn-gray" }, _this.state.showSVG ? _react["default"].createElement(_SvgIcon["default"], { type: _this.state.showSVG }) : _react["default"].createElement(_baseui.Icon, { type: "paper-clip" }), showName)), _this.props.hideDesc ? "" : fileType == 'file' ? _react["default"].createElement("span", { className: "fileupload-txt" }, uploadTips) : _react["default"].createElement("span", { className: "fileupload-txt" }, uploadTips)), _react["default"].createElement("div", null, fileListControl)); } else { return _react["default"].createElement(_row["default"], null, _react["default"].createElement("div", null, fileListControl)); } }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "renderUpload", function (action, showName, fileListControl, accept, showUpLoad) { var cShowCaption = _this.props.cShowCaption; var control = _this.renderBaseUpload(action, showName, fileListControl, accept, showUpLoad); var caption = cShowCaption; if ((!_this.state.readOnly || !_this.state.disabled) && _this.state.bIsNull === false && caption) caption = _react["default"].createElement("label", null, _react["default"].createElement(_baseui.Icon, { type: "star" }), caption); return caption ? _react["default"].createElement(_label["default"], { control: control, title: caption }) : control; }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "_getControl", function () { var context = cb.rest.AppContext; var action = '/upload?token=' + context.token; var pics = _this.state.pics; if (pics) action += '&pics=' + pics; var accept = '', showName = '', fileType = '', config = _this.state.config; if (!config || !config.fileType) { fileType = _this.state.fileType; } else { fileType = config.fileType; } if (config && config.uploadType) { if (config.uploadType == 'local') action = '/upload2Local?token=' + context.token; } if (fileType == 'file') { accept = ''; showName = '上传附件'; } else { accept = 'image/gif,image/jpeg,image/png,image/x-ms-bmp'; showName = _this.props.bShowName ? _this.props.showName : '添加图片'; } if (config && config.accept) { var acceptList = config.accept.split(','); accept = ''; acceptList.map(function (item) { var newStr = ContentTypeContrast[item] || ContentTypeContrast['.' + item]; if (newStr && newStr !== '') accept = accept + ',' + newStr; }); } var showUpLoad = true; if (_this.state.mode == 'browse' || _this.state.readOnly || _this.state.disabled) { showUpLoad = false; } var showCaption = _react["default"].createElement("span", null, "[", _react["default"].createElement("a", { onClick: function onClick() { return _this.downLoadAll(); } }, "\u4E0B\u8F7D\u5168\u90E8"), "]"); var fileListControl = _this.state.showUploadList ? _this._fileListControl(fileType) : ''; if (_this.state.fileList.length == 0) showCaption = _react["default"].createElement("span", null); return _this.renderUpload(action, showName, fileListControl, accept, showUpLoad); }); var _config = null, imageSize = '720*400', _pics = null; if (props.cStyle) { try { _config = JSON.parse(props.cStyle); if (_config.imageSize) imageSize = _config.imageSize; if (_config.pics) _pics = JSON.stringify(_config.pics); } catch (e) {} } _this.state = { colCount: props.colCount || 8, fileType: props.fileType || 'file', fileList: [], showUploadList: props.showUploadList || true, multiple: true, accept: '', mode: props.mode || 'edit', readOnly: false, config: _config, imageSize: imageSize, pics: _pics, showSVG: props.showSVG || '', showTitle: props.showTitle || '' }; _this.uploading = false; _this.DocumentServerAddress = 'https://oivs4lxfc.bkt.clouddn.com'; var proxy = cb.rest.DynamicProxy.create({ getFileServerUrl: { url: '/pub/fileupload/getFileServerUrl', method: 'GET', options: { token: true } } }); proxy.getFileServerUrl({}, function (err, result) { if (!err) this.DocumentServerAddress = result; }, (0, _assertThisInitialized2["default"])(_this)); return _this; } (0, _createClass2["default"])(fileUpload, [{ key: "componentDidMount", value: function componentDidMount() { if (this.props.model) this.props.model.addListener(this); } }, { key: "componentWillUnmount", value: function componentWillUnmount() { if (this.props.model) this.props.model.removeListener(this); } }, { key: "setListenerState", value: function setListenerState(params, propertyName) { if (params.sortField) this.state.sortField = params.sortField; var showValue = params.showValue; if (showValue && showValue.length > 0) { params.fileList = this.transferBill2Refer(showValue, params.bill2ReferKeyFieldMap); } this.setState(params); } }, { key: "setValue", value: function setValue(value) { if (!value) value = []; this.setState({ fileList: this.transferBill2Refer(value, this.state.bill2ReferKeyFieldMap) }); } }, { key: "setServerUrl", value: function setServerUrl(url) { this.DocumentServerAddress = url; } }, { key: "render", value: function render() { var control = this._getControl(); return _react["default"].createElement("div", { className: "upload-content clearfix" }, control); } }]); return fileUpload; }(_react["default"].Component); exports["default"] = fileUpload; //# sourceMappingURL=fileUpload.js.map