fastlion-amis
Version:
一种MIS页面生成工具
270 lines (269 loc) • 16.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileUpload = void 0;
var tslib_1 = require("tslib");
var react_1 = tslib_1.__importDefault(require("react"));
var upload_1 = tslib_1.__importDefault(require("antd/lib/upload"));
var button_1 = tslib_1.__importDefault(require("antd/lib/button"));
var style_1 = require("./style");
var msgsub_1 = tslib_1.__importDefault(require("../../../utils/msgsub"));
require("../../../font/iconfont.css");
var icons_1 = require("../../../../../components/icons");
var helper_1 = require("../../../../../utils/helper");
var utils_1 = require("../../../../../utils/utils");
var FileUpload = /** @class */ (function (_super) {
tslib_1.__extends(FileUpload, _super);
function FileUpload() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.state = {
visible: false,
showfilelist: [],
params: '',
turtyfilelist: [],
};
_this.btn = react_1.default.createRef();
_this.uploadClick = function () {
var _a, _b;
(_b = (_a = _this.btn) === null || _a === void 0 ? void 0 : _a.current) === null || _b === void 0 ? void 0 : _b.click();
};
_this.openDownloadDialog = function (url, saveName) {
var _a, _b, _c, _d;
var baseUrl = ((_c = (_b = (_a = _this.props.env) === null || _a === void 0 ? void 0 : _a.axiosInstance) === null || _b === void 0 ? void 0 : _b.defaults) === null || _c === void 0 ? void 0 : _c.baseURL) || ((_d = _this.props.env) === null || _d === void 0 ? void 0 : _d.ajaxApi) || '';
return new Promise(function (resolve, reject) {
resolve('');
}).then(function (res) {
if (typeof url == 'object' && url instanceof Blob) {
url = URL.createObjectURL(url); // 创建blob地址
}
else if (typeof url == 'object') {
if (typeof url.data == 'object') {
var str_1 = '';
Object.entries(url.data).forEach(function (_a) {
var k = _a[0], v = _a[1];
var target = (0, utils_1.findField)(_this.props.data, k);
target != undefined && (str_1 += "&".concat(k, "=").concat(encodeURIComponent(target)));
});
url = baseUrl + url.url + str_1;
}
}
var aLink = document.createElement('a');
aLink.href = url;
aLink.download = saveName || ''; // HTML5新增的属性,指定保存文件名,可以不要后缀,注意,file:///模式下不会生效
var event;
if (window.MouseEvent)
event = new MouseEvent('click');
else {
event = document.createEvent('MouseEvents');
event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
}
aLink.dispatchEvent(event);
});
};
_this.handleRemove = function (file) {
var _a = _this.state, params = _a.params, showfilelist = _a.showfilelist, turtyfilelist = _a.turtyfilelist;
var _b = _this.props, loading = _b.loading, env = _b.env;
if (loading) {
msgsub_1.default._error('正在上传中,无法执行删除操作', env === null || env === void 0 ? void 0 : env.getModalContainer);
return false;
}
// if (multiple) {
var index = showfilelist.indexOf(file);
var isexist = turtyfilelist.findIndex(function (item) {
return item.name === file.name && item.uid === file.uid && item.url === file.url;
});
var newturtyfilelist = turtyfilelist.slice();
var newFileList = showfilelist.slice();
var newparams = params.slice();
if (index !== -1 && isexist !== -1) {
var example = params.split(',');
newturtyfilelist.splice(isexist, 1);
example.splice(isexist, 1);
newparams = example.join(',');
}
newFileList.splice(index, 1);
_this.setState({
showfilelist: newFileList,
params: newparams,
turtyfilelist: newturtyfilelist
});
return true;
};
_this.uuid = function () {
var temp_url = URL.createObjectURL(new Blob());
var uuid = temp_url.toString();
URL.revokeObjectURL(temp_url);
return uuid.substr(uuid.lastIndexOf("/") + 1);
};
_this.handleUpload = function (config) {
var _a, _b;
var _c = _this.state, showfilelist = _c.showfilelist, turtyfilelist = _c.turtyfilelist;
var _d = _this.props, url = _d.url, env = _d.env, data = _d.data;
var fileSize = (_a = config === null || config === void 0 ? void 0 : config.file) === null || _a === void 0 ? void 0 : _a.size;
var name = (_b = config === null || config === void 0 ? void 0 : config.file) === null || _b === void 0 ? void 0 : _b.name;
if (fileSize > 20 * 1024 * 1024) {
msgsub_1.default._error('文件大小不能超过20M', env === null || env === void 0 ? void 0 : env.getModalContainer);
return;
}
//file是文件字段,并没有记录到数据域上,所以字段名写死
var newData = (0, helper_1.createObject)(data, { file: config.file });
_this.props.setLoading(true);
try {
env.fetcher(url, newData).then(function (res) {
var _a;
var _b, _c, _d, _e, _f, _g, _h, _j;
if (res.status === 0) {
_this.props.setLoading(false);
_this.setState({
showfilelist: tslib_1.__spreadArray(tslib_1.__spreadArray([], showfilelist, true), [
(_a = {
uid: _this.uuid(),
status: 'done',
name: name
},
_a[((_b = res === null || res === void 0 ? void 0 : res.data) === null || _b === void 0 ? void 0 : _b.downLoadUrl) ? 'downLoadUrl' : ''] = (_d = (_c = res === null || res === void 0 ? void 0 : res.data) === null || _c === void 0 ? void 0 : _c.downLoadUrl) !== null && _d !== void 0 ? _d : '',
_a.failNum = (_e = res === null || res === void 0 ? void 0 : res.data) === null || _e === void 0 ? void 0 : _e.failNum,
_a.successNum = (_f = res === null || res === void 0 ? void 0 : res.data) === null || _f === void 0 ? void 0 : _f.successNum,
_a.totalNum = (_g = res === null || res === void 0 ? void 0 : res.data) === null || _g === void 0 ? void 0 : _g.totalNum,
_a)
], false)
});
(_h = _this.props) === null || _h === void 0 ? void 0 : _h.reloadTarget((_j = _this.props) === null || _j === void 0 ? void 0 : _j.reload, {});
msgsub_1.default._success('上传成功', env === null || env === void 0 ? void 0 : env.getModalContainer);
}
else if (res.status === 301) {
_this.props.setLoading(false);
_this.setState({
showfilelist: tslib_1.__spreadArray(tslib_1.__spreadArray([], showfilelist, true), [
{
uid: _this.uuid(),
status: 'error',
name: name,
}
], false)
});
msgsub_1.default._error(res.msg, env === null || env === void 0 ? void 0 : env.getModalContainer);
}
else {
_this.props.setLoading(false);
_this.setState({
showfilelist: tslib_1.__spreadArray(tslib_1.__spreadArray([], showfilelist, true), [
{
uid: _this.uuid(),
status: 'error',
name: name,
}
], false)
});
msgsub_1.default._error('系统异常:' + res.msg, env === null || env === void 0 ? void 0 : env.getModalContainer);
}
// this._reloadBtn && this._reloadBtn?.click();
}).catch(function (_err) {
_this.props.setLoading(false);
msgsub_1.default._info('本次操作时间较长,请稍后再来查看结果', env === null || env === void 0 ? void 0 : env.getModalContainer);
return;
});
}
catch (_err) {
_this.props.setLoading(false);
msgsub_1.default._error('系统异常:' + _err, env === null || env === void 0 ? void 0 : env.getModalContainer);
return;
}
};
return _this;
}
FileUpload.prototype.componentDidMount = function () {
this.props.onRef && this.props.onRef(this);
// this._reloadBtn = document.getElementsByClassName('import-reload-btn')[0];
};
FileUpload.prototype.downFile = function (file) {
var _a, _b, _c;
var env = this.props.env;
var baseUrl = ((_b = (_a = env === null || env === void 0 ? void 0 : env.axiosInstance) === null || _a === void 0 ? void 0 : _a.defaults) === null || _b === void 0 ? void 0 : _b.baseURL) || (env === null || env === void 0 ? void 0 : env.ajaxApi);
try {
if (file === null || file === void 0 ? void 0 : file.downLoadUrl) {
this.openDownloadDialog(baseUrl + (file === null || file === void 0 ? void 0 : file.downLoadUrl), file === null || file === void 0 ? void 0 : file.name);
}
else {
throw new Error('文件下载地址不存在,请检查');
}
}
catch (err) {
msgsub_1.default._error(err, (_c = this.props.env) === null || _c === void 0 ? void 0 : _c.getTopModalContainer);
}
};
FileUpload.prototype.render = function () {
var _this = this;
var _a, _b;
var url = this.props.url;
var showfilelist = this.state.showfilelist;
var env = this.props.env;
var baseUrl = ((_b = (_a = env === null || env === void 0 ? void 0 : env.axiosInstance) === null || _a === void 0 ? void 0 : _a.defaults) === null || _b === void 0 ? void 0 : _b.baseURL) || (env === null || env === void 0 ? void 0 : env.ajaxApi);
var uploadButton = (react_1.default.createElement("div", { style: {
display: 'none',
textAlign: 'center',
cursor: 'pointer',
outline: 'none',
marginTop: '12px',
fontSize: '12px',
color: '#444'
} },
react_1.default.createElement(button_1.default, { ref: this.btn, size: 'middle', style: {
width: '56px',
height: '32px',
background: '#fff',
border: '1px solid #3168f0',
borderRadius: '4px',
color: '#3168f0',
fontWeight: 500,
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
fontSize: '14px !important',
padding: '9px 15px'
}, disabled:
// 判断是否多选
this.props.loading ? true : false }, this.props.loading ? react_1.default.createElement("span", { className: "fa fa-circle-o-notch fa-spin" }) : '上传')));
var uploadProps = {
action: (baseUrl ? baseUrl : '') + (url === null || url === void 0 ? void 0 : url.url),
listType: "text",
disabled: false,
iconRender: function () { return react_1.default.createElement("span", { className: "fa fa-file-text-o" }); },
showUploadList: {
showPreviewIcon: true,
showRemoveIcon: true,
removeIcon: react_1.default.createElement("span", { className: "fa fa-close" })
},
onRemove: this.handleRemove,
onPreview: function (file) { return false; },
customRequest: this.handleUpload,
itemRender: function (originNode, file, fileList, actions) {
return react_1.default.createElement(style_1.WrapperFileItem, tslib_1.__assign({ className: "lion-upload-itemrender--container" }, file),
originNode,
(file === null || file === void 0 ? void 0 : file.status) === 'done' ? react_1.default.createElement("div", { className: "lion-upload-itemrender--notiy" },
(file === null || file === void 0 ? void 0 : file.totalNum) === (file === null || file === void 0 ? void 0 : file.successNum) ?
react_1.default.createElement(icons_1.Icon, { icon: "status-success", className: "icon" }) :
(file === null || file === void 0 ? void 0 : file.totalNum) === (file === null || file === void 0 ? void 0 : file.failNum) ?
react_1.default.createElement(icons_1.Icon, { icon: "status-fail", className: "icon" }) :
react_1.default.createElement(icons_1.Icon, { icon: "status-warning", className: "icon" }),
"\u603B\u884C\u6570", file === null || file === void 0 ? void 0 :
file.totalNum,
"\u884C,\u6210\u529F", file === null || file === void 0 ? void 0 :
file.successNum,
"\u884C,\u5931\u8D25", file === null || file === void 0 ? void 0 :
file.failNum,
"\u884C",
(file === null || file === void 0 ? void 0 : file.downLoadUrl) ? react_1.default.createElement(react_1.default.Fragment, null,
",",
react_1.default.createElement("span", { onClick: function () { _this.downFile(file); }, style: { textDecoration: "underline", color: "#4569d4", display: 'inline' } }, "\u70B9\u51FB\u6B64\u5904"),
"\u53EF\u4E0B\u8F7D\u76F8\u5173\u6587\u4EF6") : null) : null, actions === null || actions === void 0 ? void 0 :
actions.remove);
},
fileList: showfilelist
};
return (react_1.default.createElement(style_1.WrapperFileUpload, null,
react_1.default.createElement(upload_1.default, tslib_1.__assign({ style: { width: '70px', fontSize: '12px' } }, uploadProps), uploadButton)));
};
return FileUpload;
}(react_1.default.PureComponent));
exports.FileUpload = FileUpload;
//# sourceMappingURL=./renderers/Lion/Table/components/import/FileUpload.js.map