UNPKG

fastlion-amis

Version:

一种MIS页面生成工具

884 lines (883 loc) 113 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LionUploadRenderer = exports.SimpleUploadTableCellRenderer = void 0; var tslib_1 = require("tslib"); var react_1 = tslib_1.__importStar(require("react")); var Item_1 = require("../../../Form/Item"); // import 'cropperjs/dist/cropper.css'; var Cropper = react_1.default.lazy(function () { return Promise.resolve().then(function () { return new Promise(function(resolve){require(['react-cropper'], function(ret) {resolve(tslib_1.__importStar(ret));})}); }); }); var react_dropzone_1 = tslib_1.__importDefault(require("react-dropzone")); require("blueimp-canvastoblob"); var find_1 = tslib_1.__importDefault(require("lodash/find")); var helper_1 = require("../../../../utils/helper"); var icons_1 = require("../../../../components/icons"); var Button_1 = tslib_1.__importDefault(require("../../../../components/Button")); var antd_1 = require("antd"); var attr_accept_1 = tslib_1.__importDefault(require("attr-accept")); var InputFile_1 = require("../../../Form/InputFile"); var Image_1 = tslib_1.__importDefault(require("../../../Image")); var tpl_builtin_1 = require("../../../../utils/tpl-builtin"); var tpl_1 = require("../../../../utils/tpl"); var isPlainObject_1 = tslib_1.__importDefault(require("lodash/isPlainObject")); var merge_1 = tslib_1.__importDefault(require("lodash/merge")); var utils_1 = require("../../utils/utils"); var spark_md5_1 = tslib_1.__importDefault(require("spark-md5")); var axios_1 = tslib_1.__importDefault(require("axios")); var image_conversion_1 = require("image-conversion"); var icons_2 = require("@ant-design/icons"); var antd_2 = require("antd"); var CellPopover_1 = tslib_1.__importDefault(require("../../Table/LionCellImg/CellPopover")); var LodopFuncs_1 = require("../../../../utils/print/LodopFuncs"); var LabelPrint_1 = require("../../LabelPrint"); var shell_1 = require("../../../../utils/shell"); var PopUp_1 = tslib_1.__importDefault(require("../../../../components/PopUp")); var MoveUpload_1 = require("../../../../components/Lion/MoveUpload"); var factory_1 = require("../../../../factory"); var sub_1 = require("../../../../utils/sub"); /** * Image 图片上传控件 * 文档:https://baidu.gitee.io/amis/docs/components/form/image */ // 图片缓存 var ImgCache = {}; var defaultQuality = 0.7; var defaultThreshold = 2 * 1024; // 注册缓存清理事件 sub_1.EventSub.on(sub_1.EventEnum.ClearMappingAcahe, function (InstanceName) { if (!InstanceName) return; delete ImgCache[InstanceName]; }); var preventEvent = function (e) { return e.stopPropagation(); }; var isUrlImg = function (file) { if (!file.name && !file.size && file.addr.startsWith('http')) return true; return false; }; var LionUpload = /** @class */ (function (_super) { tslib_1.__extends(LionUpload, _super); function LionUpload(props) { var _this_1 = this; var _a, _b, _c, _d; _this_1 = _super.call(this, props) || this; _this_1.state = { uploading: false, locked: false, files: [], // visible: false, // uploading: false, // params: '', // init: false, // formItemName: '', // removeIcon: true, // loading: false, // preUploading: false, //预处理 chunksSize: 0, // currentChunks: 0, // 当前上传的队列个数 当前还剩下多少个分片没上传 // uploadPercent: -1, // 上传率 // preUploadPercent: -1, // 预处理率 // uploadRequest: false, // 上传请求,即进行第一个过程中 // uploaded: false, // 表示文件是否上传成功 uploadParams: {}, arrayBufferData: [], upload_cmd: 'check_exist', upload_name: '', // upload_field: '', file_type: '', // fileSize: 0, isOpened: false, maxTagCount: 0, minTagCount: 0, }; _this_1.baseURL = ''; _this_1.files = []; _this_1.fileUploadCancelExecutors = []; _this_1.dropzone = react_1.default.createRef(); _this_1.dropzoneimg = react_1.default.createRef(); _this_1.frameImageRef = react_1.default.createRef(); _this_1.current = null; _this_1.unmounted = false; // 自lion-input-file迁徙而来的上传方法2 _this_1.handleFile = function (config) { return new Promise(function (resolve, reject) { return tslib_1.__awaiter(_this_1, void 0, void 0, function () { function loadNext() { var start = currentChunk * chunkSize, end = ((start + chunkSize) >= file.size) ? file.size : start + chunkSize; chunkFileReader.readAsArrayBuffer(blobSlice.call(file, start, end)); } var fileSize, file, blobSlice, _this, chunkSize, chunks, currentChunk, file_type, file_name, spark, chunkFileReader, params, arrayBufferData, md5; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: fileSize = config === null || config === void 0 ? void 0 : config.size; file = config; blobSlice = File.prototype.slice; _this = this; chunkSize = 1024 * 1024 * 1; chunks = Math.ceil(file.size / chunkSize); currentChunk = 0; file_type = config === null || config === void 0 ? void 0 : config.type; file_name = config === null || config === void 0 ? void 0 : config.name; spark = new spark_md5_1.default.ArrayBuffer(); chunkFileReader = new FileReader(); params = { chunks: [], file: {} }; arrayBufferData = []; params.file.fileName = file.name || this.state.cropFileName; params.file.fileSize = file.size; return [4 /*yield*/, this.getFileMd5(file)]; case 1: md5 = _a.sent(); params.file.fileMd5 = md5; params.file.id = config.id; chunkFileReader.onload = function (e) { // 对每一片分片进行md5加密 spark.append(e.target.result); // 每一个分片需要包含的信息 var obj = { chunk: currentChunk + 1, start: currentChunk * chunkSize, end: ((currentChunk * chunkSize + chunkSize) >= file.size) ? file.size : currentChunk * chunkSize + chunkSize, chunkMd5: spark.end(), chunks: chunks }; // 每一次分片onload,currentChunk都需要增加,以便来计算分片的次数 currentChunk++; params.chunks.push(obj); // 将每一块分片的arrayBuffer存储起来,用来partUpload var tmp = { chunk: obj.chunk, currentBuffer: e.target.result }; arrayBufferData.push(tmp); if (currentChunk < chunks) { // 当前切片总数没有达到总数时 loadNext(); } else { //记录所有chunks的长度 params.file.fileChunks = params.chunks.length; // 表示预处理结束,将上传的参数,arrayBuffer的数据存储起来 _this.setState({ // preUploading: false, uploadParams: params, arrayBufferData: arrayBufferData, chunksSize: chunks, // preUploadPercent: 100, file_type: file_type, upload_name: params.file.fileName, fileSize: fileSize }, function () { resolve(); }); } }; chunkFileReader.onerror = function () { console.warn('oops, something went wrong.'); }; loadNext(); return [2 /*return*/]; } }); }); }); }; // 自lion-input-file迁徙而来的上传方法3 _this_1.handlePartUpload = function (originalFile, cb, onProgress) { var _a = _this_1.state, upload_cmd = _a.upload_cmd, upload_name = _a.upload_name, chunksSize = _a.chunksSize, file_type = _a.file_type, uploadParams = _a.uploadParams; var uploadList = uploadParams.chunks, file = uploadParams.file; var fileMd5 = file.fileMd5; var _b = _this_1.props, url = _b.url, receiver = _b.receiver, env = _b.env, name = _b.name; var count = 0; // 遍历uploadList try { // @ts-ignore Promise.allSettled(uploadList.map(function (_uploadItem) { return new Promise(function (resolve, reject) { var chunkMd5 = _uploadItem.chunkMd5, chunk = _uploadItem.chunk, start = _uploadItem.start, end = _uploadItem.end; var formData = new FormData(), //新建一个Blob对象,将对应分片的arrayBuffer加入Blob中 blob = new Blob([_this_1.state.arrayBufferData[chunk - 1].currentBuffer], { type: 'application/octet-stream' }); // 上传的参数 var params = "fileMd5=".concat(fileMd5, "&chunkMd5=").concat(chunkMd5, "&chunk=").concat(chunk, "&start=").concat(start, "&end=").concat(end, "&chunks=").concat(_this_1.state.arrayBufferData.length); // 通过formData将上传参数传入 formData.append('upload_cmd', upload_cmd); formData.append('upload_field', String(name)); formData.append('file_name', upload_name); formData.append('file_md5', fileMd5); formData.append('chunk_index', chunk); formData.append('chunk_total', String(chunksSize)); formData.append('chunk_size', String(end - start)); formData.append('type', file_type); formData.append('chunk_file', blob); // 搞不懂为什么,这样在壳上可以解密 shell_1.Shell.hasShell() && shell_1.Shell.uploadFile({ path: url || receiver, url: url || receiver, formData: formData, }, function () { }, function () { }); env.fetcher({ url: url || receiver, data: formData, method: 'post' }).then(function (res) { if (res.status === 0) { count++; onProgress(count / chunksSize); resolve(res === null || res === void 0 ? void 0 : res.data); } else { reject({ _err: res === null || res === void 0 ? void 0 : res.msg, _data: formData }); } }).catch(function (e) { return reject(e); }); }); })).then(function (_res) { var _chunk_success_count = 0; var _chunk_error_arr = []; _res.map(function (_upload_res) { if ((_upload_res === null || _upload_res === void 0 ? void 0 : _upload_res.status) === 'fulfilled') { _chunk_success_count++; } else { _chunk_error_arr.push(_upload_res.reason._data); } }); if (_chunk_success_count === chunksSize) { var formData = new FormData(); // 通过formData将上传参数传入,发起文件分片合并通知 formData.append('upload_cmd', 'chunk_merge'); formData.append('upload_field', String(name)); formData.append('file_name', upload_name); formData.append('file_md5', fileMd5); // 搞不懂为什么,这样在壳上可以解密 shell_1.Shell.hasShell() && shell_1.Shell.uploadFile({ path: url || receiver, url: url || receiver, formData: formData, }, function () { }, function () { }); env.fetcher({ url: url || receiver, data: formData, method: 'post' }).then(function (_merge_res) { if ((_merge_res === null || _merge_res === void 0 ? void 0 : _merge_res.status) === 0) { var obj = tslib_1.__assign(tslib_1.__assign({}, _merge_res === null || _merge_res === void 0 ? void 0 : _merge_res.data), { state: 'uploaded' }); obj.value = obj.value || obj.url; cb(null, originalFile, obj); } else { cb(_merge_res === null || _merge_res === void 0 ? void 0 : _merge_res.msg, originalFile); } }).catch(function (_file_merge_res) { cb('上传失败', originalFile); return; }); } else { _chunk_error_arr.map(function (_chunk_error_item) { env.fetcher({ url: url, data: _chunk_error_item, method: 'post' }).then(function (_error_res) { }); }); throw new Error('chunk file upload error!'); } }).catch(function (_err) { cb(_err, originalFile); return; }); } catch (error) { cb(error, originalFile); return; } }; // 打印 _this_1.handleLabelPrint = function (item) { var list = item; _this_1.setState({ ctx: tslib_1.__assign(tslib_1.__assign({}, _this_1.state.ctx), { items: [list], rows: [list], selectedItems: [list], unSelectedItems: [] }) }, function () { var LODOP = (0, LodopFuncs_1.getLodop)(); if (LODOP) { _this_1.setState({ _print_visible: true }); } }); }; // 判断是否是pdf,图片,视频 _this_1.handleElementCategory = function (item) { if ((0, utils_1.isImg)(item === null || item === void 0 ? void 0 : item.name) || (0, utils_1.isPdf)(item === null || item === void 0 ? void 0 : item.name)) { return true; } else { return false; } }; // 进度条 _this_1.handprogress = function (name) { var _a = _this_1.state.content[name], percent = _a.percent, size = _a.size; var speed = _this_1.state.content; var value = Number(((percent / size) * 100).toFixed(0)); speed[name] = tslib_1.__assign(tslib_1.__assign({}, _this_1.state.content[name]), { percent: ++percent, pent: value !== Infinity ? value : 0 }); _this_1.setState({ content: speed }, function () { if (Number(((percent / size) * 100).toFixed(0)) == 100) { speed[name] = tslib_1.__assign(tslib_1.__assign({}, _this_1.state.content[name]), { percent: 0, pent: 0 }); _this_1.setState({ content: speed }); } }); }; // 获取文件大小判断是否分流还有分流多大 _this_1.chunks = function (url, chunkSize, name) { if (chunkSize === void 0) { chunkSize = 524288; } return tslib_1.__awaiter(_this_1, void 0, void 0, function () { var staging, chunk, datacontent, contentSize_1, previewvalue, speed_1; var _this_1 = this; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: staging = url.split("?").pop(); chunk = staging.split("=").pop(); if (!(chunk === "1")) return [3 /*break*/, 2]; return [4 /*yield*/, fetch(url, { method: "HEAD" })]; case 1: datacontent = (_a.sent()).headers.get("Content-Length"); contentSize_1 = parseInt(datacontent); previewvalue = this.state.content; speed_1 = this.state.content; previewvalue[name] = { "size": 0, "percent": 0, "pent": 0 }; this.setState({ content: previewvalue }, function () { speed_1[name] = tslib_1.__assign(tslib_1.__assign({}, _this_1.state.content[name]), { size: Number((contentSize_1 / chunkSize).toFixed(0)) }); _this_1.setState({ content: speed_1 }); }); return [2 /*return*/, contentSize_1]; case 2: return [2 /*return*/, false]; } }); }); }; //0相册 1 拍照 _this_1.getImages = function (type) { var _a = _this_1.props, _b = _a.maxLength, maxLength = _b === void 0 ? 0 : _b, multiple = _a.multiple; var files = _this_1.state.files; var count = maxLength - files.length; // maxLength大于0证明有配置,此时才需要验证 if (count <= 0 && maxLength) { antd_1.message.warning({ content: "\u4E0A\u4F20\u4E2A\u6570\u5DF2\u8FBE\u4E0A\u9650".concat(maxLength, "\u4E2A") }); return; } var list = []; var fail = function (res) { antd_1.message.error(res.msg); }; var info = function (res) { if ((res === null || res === void 0 ? void 0 : res.code) === -200) { antd_1.message.warning((res === null || res === void 0 ? void 0 : res.msg) || '拍照失败'); } else { list.push(res.data); } }; var success = function (res) { list.push(res.data); _this_1.onUpload(list); }; shell_1.Shell.getAttachment({ type: type }, info, success, fail); }; _this_1.onUpload = function (md5s) { var info = md5s.map(function (item) { var blob = _this_1.base64ToBlob(item.dataurl); var file = new File([blob], item.filename, { type: 'image/png' }); return file; }); _this_1.handleDrop(info); }; _this_1.base64ToBlob = function (base64Data) { var byteString = atob(base64Data.split(',')[1]); var ab = new ArrayBuffer(byteString.length); var ia = new Uint8Array(ab); for (var i = 0; i < byteString.length; i++) { ia[i] = byteString.charCodeAt(i); } var blob = new Blob([ab], { type: 'image/png' }); return blob; }; _this_1.filetext = function (fileName) { // if (/(.pdf)/i.test(fileName)) { // return <Icon icon={'file-pdf'} className="icon"></Icon> // } else if (/(.ppt)/i.test(fileName)) { // return <Icon icon={'file-ppt'} className="icon"></Icon> // } else if (/(.jpe?)g|(.png)|(.svg)|(.gif)/i.test(fileName)) { // const [iconName, iconColor] = getMediaIcon(fileName); // return <Icon icon={iconName} color={iconColor} className="icon" symbol></Icon> // } else if (/(.xlsx)|(.xls)/i.test(fileName)) { // return <Icon icon={'file-xls'} className="icon"></Icon> // } else if (/(.docx)|(.doc)/i.test(fileName)) { // return <Icon icon={'file-doc'} className="icon"></Icon> // } else { // return <Icon icon={'file-file'} className="icon"></Icon> // } var _a = (0, utils_1.getMediaIcon)(fileName), iconName = _a[0], iconColor = _a[1]; return react_1.default.createElement(icons_1.Icon, { icon: iconName, color: iconColor, className: "icon", symbol: true }); }; _this_1.addContent = function () { return (react_1.default.createElement("div", { className: 'add-content-container' }, react_1.default.createElement("div", { className: 'add-content-body' }, react_1.default.createElement("div", { className: 'add-content-body-show' }, "\u5C06\u56FE\u7247\u62D6\u62FD\u6216\u590D\u5236\u81F3\u6B64\u5373\u53EF\u6DFB\u52A0"), react_1.default.createElement("div", { className: 'add-content-divider' }), react_1.default.createElement("div", { className: 'add-content-body-input' }, react_1.default.createElement("div", { className: 'add-content-url' }, react_1.default.createElement(antd_2.Input, { value: _this_1.state.pictureUrl, onChange: function (e) { _this_1.setState({ pictureUrl: e.target.value }); }, placeholder: '\u8F93\u5165\u56FE\u7247\u5730\u5740' })), react_1.default.createElement("div", { className: 'add-content-commit' }, react_1.default.createElement(Button_1.default, { onClick: function () { return tslib_1.__awaiter(_this_1, void 0, void 0, function () { var newFiles; var _this_1 = this; return tslib_1.__generator(this, function (_a) { newFiles = this.state.files.concat(); newFiles.push({ value: this.state.pictureUrl, state: "init", chunkDownload: false, preview: this.state.pictureUrl, size: null, addr: this.state.pictureUrl, thumbnailAddr: this.state.pictureUrl, previewAddr: this.state.pictureUrl, isNotImg: false, downloadSrc: this.state.pictureUrl }); this.setState({ files: newFiles }, function () { _this_1.files = newFiles; _this_1.onChange(); _this_1.setState({ pictureUrl: '' }); }); return [2 /*return*/]; }); }); }, disabled: !_this_1.state.pictureUrl }, "\u786E\u8BA4")), react_1.default.createElement("div", { className: 'add-content-add' }, react_1.default.createElement(Button_1.default, { onClick: function () { return (0, helper_1.isMobile)() ? _this_1.setState({ isOpened: true }) : _this_1.handleSelect(); } }, "\u6DFB\u52A0\u672C\u5730\u56FE\u7247")))))); }; _this_1.baseURL = ((_c = (_b = (_a = 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 = props.env) === null || _d === void 0 ? void 0 : _d.ajaxApi) || ''; // this.baseURL = 'https://sasdev.sanfu.com/saas_dev' // this.baseURL = 'https://bwt.sanfu.com/saas' // this.baseURL = 'https://bwt.sanfu.com/fs-saas' _this_1.state = tslib_1.__assign(tslib_1.__assign({}, _this_1.state), { crop: _this_1.buildCrop(props), frameImageWidth: 0, selectList: [], setvideo: false, structure: {}, content: {} }); _this_1.fillCell = !!props.formItem && !!props.fillCell && !!props.readonly; _this_1.errImg = 'data:image/png;base64,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'; _this_1.sendFile = _this_1.sendFile.bind(_this_1); _this_1.removeFile = _this_1.removeFile.bind(_this_1); _this_1.handleDrop = _this_1.handleDrop.bind(_this_1); _this_1.handleClick = _this_1.handleClick.bind(_this_1); _this_1.handleClick = _this_1.handleClick.bind(_this_1); _this_1.handleCrop = _this_1.handleCrop.bind(_this_1); _this_1.handleDropRejected = _this_1.handleDropRejected.bind(_this_1); _this_1.cancelCrop = _this_1.cancelCrop.bind(_this_1); _this_1.rotatableCrop = _this_1.rotatableCrop.bind(_this_1); _this_1.handleFrameImageLoaded = _this_1.handleFrameImageLoaded.bind(_this_1); _this_1.startUpload = _this_1.startUpload.bind(_this_1); _this_1.stopUpload = _this_1.stopUpload.bind(_this_1); _this_1.toggleUpload = _this_1.toggleUpload.bind(_this_1); _this_1.tick = _this_1.tick.bind(_this_1); _this_1.onChange = _this_1.onChange.bind(_this_1); _this_1.addFiles = _this_1.addFiles.bind(_this_1); _this_1.handleSelect = _this_1.handleSelect.bind(_this_1); _this_1.handlePaste = _this_1.handlePaste.bind(_this_1); _this_1.syncAutoFill = _this_1.syncAutoFill.bind(_this_1); _this_1.initFiles = _this_1.initFiles.bind(_this_1); _this_1.formatfileAddr = _this_1.formatfileAddr.bind(_this_1); _this_1.updateFiles = _this_1.updateFiles.bind(_this_1); _this_1.previewImage = _this_1.previewImage.bind(_this_1); _this_1.handprogress.bind = _this_1.handprogress.bind(_this_1); _this_1.chunks.bind = _this_1.chunks.bind(_this_1); return _this_1; } LionUpload.formatFileSize = function (size, units) { if (units === void 0) { units = [' B', ' KB', ' M', ' G']; } size = parseInt(size, 10) || 0; while (size > 1024 && units.length > 1) { size /= 1024; units.shift(); } return size.toFixed(2) + units[0]; }; LionUpload.sizeInfo = function (width, height, __) { if (!width) { return __('Image.height', { height: height }); } else if (!height) { return __('Image.width', { width: width }); } return __('Image.size', { width: width, height: height }); }; LionUpload.prototype.componentDidMount = function () { // this.syncAutoFill(); // 初始化 ImgCache[this.props.crudName] = ImgCache[this.props.crudName] || {}; this.updateFiles(); this.calcuate(); }; LionUpload.prototype.calcuate = function () { var _this_1 = this; var cx = this.props.classnames; var files = this.state.files; this.setState({ maxTagCount: undefined }, function () { return tslib_1.__awaiter(_this_1, void 0, void 0, function () { var finalFiles, maxWidth, tagWidth, showCount, children, minTagCount, i, e; var _a, _b; return tslib_1.__generator(this, function (_c) { switch (_c.label) { case 0: finalFiles = files.filter(function (item) { return !item.err; }); maxWidth = this.dropzoneimg.current.clientWidth - (finalFiles && finalFiles.length > 1 ? 25 : 0); tagWidth = 0; showCount = 0; return [4 /*yield*/, ((_a = this.dropzoneimg.current) === null || _a === void 0 ? void 0 : _a.getElementsByClassName(cx('ImageControl-img-wrapper')))]; case 1: children = _c.sent(); minTagCount = Math.floor(maxWidth / (((_b = children === null || children === void 0 ? void 0 : children[0]) === null || _b === void 0 ? void 0 : _b.offsetWidth) + 12)); if (children && children.length > 0) { for (i = 0; i < children.length; i++) { e = children[i]; // if (tagWidth > maxWidth) { // e.style.display = "none" // } if (tagWidth <= maxWidth) { showCount++; } //如果是最后一个不需要使用这个length tagWidth += e.offsetWidth + ((i < children.length - 1) ? 12 : 0); if (tagWidth > maxWidth && !(0, helper_1.isMobile)()) { e.style.display = "none"; } // if (e.style.display != 'none') { // showCount++ // } } this.setState({ maxTagCount: showCount, minTagCount: minTagCount }); } return [2 /*return*/]; } }); }); }); }; LionUpload.prototype.updateFiles = function () { var _this_1 = this; this.initFiles().then(function (files) { _this_1.setState({ files: (_this_1.files = files.filter(function (item) { return !item.err; })) }, _this_1.syncAutoFill); }); }; LionUpload.prototype.componentDidUpdate = function (prevProps) { var _a, _b, _c, _d, _e, _f; var props = this.props; if (prevProps.value !== props.value && this.emitValue !== props.value) { if ((props.isFieldTable || props.tableType == 'input-table') && ((_a = props === null || props === void 0 ? void 0 : props.value) === null || _a === void 0 ? void 0 : _a.value) && ((_b = prevProps === null || prevProps === void 0 ? void 0 : prevProps.value) === null || _b === void 0 ? void 0 : _b.value) && props.value.value === prevProps.value.value) { } else { this.updateFiles(); } this.calcuate(); } if (((_d = (_c = prevProps === null || prevProps === void 0 ? void 0 : prevProps.value) === null || _c === void 0 ? void 0 : _c.info) === null || _d === void 0 ? void 0 : _d.length) !== ((_f = (_e = props === null || props === void 0 ? void 0 : props.value) === null || _e === void 0 ? void 0 : _e.info) === null || _f === void 0 ? void 0 : _f.length)) { this.calcuate(); } if (prevProps.crop !== props.crop) { this.setState({ crop: this.buildCrop(props) }); } }; LionUpload.prototype.componentWillUnmount = function () { this.unmounted = true; }; // 增加缓存 LionUpload.prototype.getBatchAddrInfo = function (files) { var _this_1 = this; var requests = files.map(function (item) { return tslib_1.__awaiter(_this_1, void 0, void 0, function () { var res, _a; return tslib_1.__generator(this, function (_b) { switch (_b.label) { case 0: if (!this.props.crudName) return [3 /*break*/, 2]; if (ImgCache[this.props.crudName][item.thumbnailAddr]) return [2 /*return*/, ImgCache[this.props.crudName][item.thumbnailAddr] // 表格模式下,不再同时发起xhr和img请求图片 // const getThumbnailAddr = () => new Promise((res) => { // Axios(item.thumbnailAddr).then(res).catch(res) // }) // const res = await getThumbnailAddr() ]; return [4 /*yield*/, new Promise(function (resolve) { return resolve(tslib_1.__assign(tslib_1.__assign({}, item), { notNull: true })); })]; case 1: res = _b.sent(); ImgCache[this.props.crudName][item.thumbnailAddr] = res; return [2 /*return*/, res]; case 2: if (!Array.isArray(item.thumbnailAddr)) return [3 /*break*/, 4]; return [4 /*yield*/, new Promise(function (resolve) { return resolve(tslib_1.__assign(tslib_1.__assign({}, item), { notNull: true })); })]; case 3: _a = _b.sent(); return [3 /*break*/, 5]; case 4: _a = (0, axios_1.default)(item.thumbnailAddr); _b.label = 5; case 5: // 如果预览地址是Array模式,就直接返回对应的icon的item图,否则就去请求对应预览图 return [2 /*return*/, _a]; } }); }); }); return Promise.allSettled(requests); }; LionUpload.prototype.analyzingAddrInfo = function (files, allResponse) { var _a = this.props, name = _a.name, value = _a.value, store = _a.store; var info = tslib_1.__spreadArray([], value.info, true); var list = files.concat(); var hasErrorFile = false; allResponse.forEach(function (item, index) { var _a, _b, _c, _d; if (item.status === 'fulfilled') { var result = item === null || item === void 0 ? void 0 : item.value; if (result.status === 200 || result.notNull) { list[index].notNull = true; if ((_a = result === null || result === void 0 ? void 0 : result.data) === null || _a === void 0 ? void 0 : _a.status) { list[index].errorMsg = result.data.msg; list[index].state = 'error'; } else { // list[index].thumbnailAddr = result.data } } } if (item.status === 'rejected') { if (((_d = (_c = (_b = item.reason) === null || _b === void 0 ? void 0 : _b.response) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.status) == 404) { info.splice(index, 1); hasErrorFile = true; } // list[index].errorMsg = 'rejected' // list[index].state = 'error' // list[index].notNull = true } }); if (name && hasErrorFile) store === null || store === void 0 ? void 0 : store.changeValue(name, tslib_1.__assign(tslib_1.__assign({}, value), { info: info })); return list.filter(function (item) { return item.notNull; }); }; LionUpload.prototype.initFiles = function () { var _this_1 = this; var _a = this.props, value = _a.value, preAjax = _a.preAjax, data = _a.data; var files = []; if (value) { var fileValue = value.value, fileInfo = value.info; if (fileValue && Array.isArray(fileInfo) && fileInfo.length) { var fileValueArr_1 = fileValue.split(','); files = fileInfo.map(function (item, index) { return _this_1.formatfileAddr(Object.assign({ state: 'init', value: fileValueArr_1[index], taskId: data.TASK_ID }, item)); }); } } this.setState({ selectList: files.filter(function (item) { return !item.err; }).map(function (_, index) { return index; }) }); // files = testdata return new Promise(function (resolve) { if (preAjax && files.length) { _this_1.getBatchAddrInfo(files).then(function (allResponse) { var list = _this_1.analyzingAddrInfo(files, allResponse); resolve(list); }); } else { resolve(files); } }); }; LionUpload.prototype.buildCrop = function (props) { var crop = props.crop; var __ = this.props.translate; if (crop && props.multiple) { props.env && props.env.alert && props.env.alert(__('Image.configError')); return null; } if (crop === true) { crop = {}; } if (crop) { crop = tslib_1.__assign({ aspectRatio: undefined, guides: true, dragMode: 'move', viewMode: 1, rotatable: true, scalable: true }, crop); } return crop; }; LionUpload.prototype.handleDropRejected = function (rejectedFiles, evt) { if (evt.type !== 'change' && evt.type !== 'drop') { return; } var _a = this.props, multiple = _a.multiple, env = _a.env, accept = _a.accept, __ = _a.translate; var files = rejectedFiles.map(function (fileRejection) { return (tslib_1.__assign(tslib_1.__assign({}, fileRejection.file), { state: 'invalid', id: (0, helper_1.guid)(), name: fileRejection.file.name })); }); env.alert(__('File.invalidType', { files: files.map(function (file) { return "\u300C".concat(file.name, "\u300D"); }).join(' '), accept: accept })); }; // 开始上传 LionUpload.prototype.startUpload = function (retry) { if (retry === void 0) { retry = false; } if (this.state.uploading) { return; } this.setState({ uploading: true, locked: true, files: (this.files = this.files.map(function (file) { if (retry && file.state === 'error') { file.state = 'pending'; file.progress = 0; } return file; })) }, this.tick); }; LionUpload.prototype.toggleUpload = function () { return this.state.uploading ? this.stopUpload() : this.startUpload(); }; LionUpload.prototype.stopUpload = function () { if (!this.state.uploading) { return; } this.setState({ uploading: false }); }; // 统一处理下,省的后续的组件重复去拼地址 LionUpload.prototype.formatfileAddr = function (fileData) { var _a, _b, _c, _d; if (!fileData) return; var obj = JSON.parse(JSON.stringify(fileData)); if (obj.previewAddr) { obj.previewAddr = (((_a = obj.previewAddr) === null || _a === void 0 ? void 0 : _a.startsWith('http')) ? '' : this.baseURL) + obj.previewAddr; } // 非图片格式去本地拿图标 if ((0, utils_1.isImg)(obj.name)) { obj.thumbnailAddr = (((_b = obj.thumbnailAddr) === null || _b === void 0 ? void 0 : _b.startsWith('http')) ? '' : this.baseURL) + (obj.thumbnailAddr || (obj === null || obj === void 0 ? void 0 : obj.url) || (obj === null || obj === void 0 ? void 0 : obj.addr)); // 给个标识,留给样式调整空间,以及调用不同的预览组件 obj.isNotImg = false; } else { obj.thumbnailAddr = (0, utils_1.getMediaIcon)(obj.name); obj.isNotImg = true; } var taskId = obj.taskId; var url = (_c = ((obj === null || obj === void 0 ? void 0 : obj.url) || (obj === null || obj === void 0 ? void 0 : obj.addr))) !== null && _c !== void 0 ? _c : ''; var dealUrl = taskId ? url.includes('?') ? "".concat(url, "&taskId=").concat(taskId) : "".concat(url, "?taskId=").concat(taskId) : url; obj.downloadSrc = (((_d = obj.addr) === null || _d === void 0 ? void 0 : _d.startsWith('http')) ? '' : this.baseURL) + dealUrl; return obj; }; LionUpload.prototype.tick = function () { var _this_1 = this; if (this.current || !this.state.uploading) { return; } var env = this.props.env; var __ = this.props.translate; var file = (0, find_1.default)(this.files, function (item) { return item.state === 'pending'; }); if (file) { this.current = file; file.state = 'uploading'; this.setState({ files: (this.files = this.files.concat()) }, function () { return _this_1.sendFile(file, function (error, file, obj) { var files = _this_1.files.concat(); var idx = files.indexOf(file); if (!~idx) { return; } var newFile = file; if (error) { newFile.state = file.state !== 'uploading' ? file.state : 'error'; newFile.error = error; if (!_this_1.props.multiple && newFile.state === 'invalid') { files.splice(idx, 1); _this_1.current = null; return _this_1.setState({ files: (_this_1.files = files), error: error }, _this_1.tick); } env.notify('error', error || __('File.errorRetry')); } else { newFile = _this_1.formatfileAddr(tslib_1.__assign({ name: file.name || _this_1.state.cropFileName, size: file.size }, obj)); } files.splice(idx, 1, newFile); _this_1.current = null; _this_1.setState({ files: (_this_1.files = files) }, _this_1.tick); }, function (progress) { var files = _this_1.files.concat(); var idx = files.indexOf(file); if (!~idx) { return; } // file 是个非 File 对象,先不copy了直接改。 file.progress = progress; _this_1.setState({ files: (_this_1.files = files) }); }); }); } else { this.setState({ uploading: false, locked: false }, function () { _this_1.onChange(!!_this_1.resolve); if (_this_1.resolve) { _this_1.resolve(_this_1.files.some(function (file) { return file.state === 'error'; }) ? __('File.errorRetry') : null); _this_1.resolve = undefined; } }); } }; LionUpload.prototype.removeFile = function (file, index) { var files = this.files.concat(); var fileIndex = files.indexOf(file); this.removeFileCanelExecutor(file, true); files.splice(fileIndex, 1); var isUploading = this.current === file; if (isUploading) { this.current = null; } this.setState({ files: (this.files = files) }, isUploading ? this.tick : this.onChange); }; LionUpload.prototype.previewImage = function (file, index, e) { var _this_1 = this; var isImage = this.props.isImage; e.preventDefault(); // if (isCon(file.name) && !isImage) { // Shell.hasShell() ? // Shell.handleExternalUrl({ // "externalUrl": file.downloadSrc, // "fileName": file.name, // "handleType": "open" // }) // : // this.setState({ // setvideo: true, // structure: { // "type": "video", // "src": file.downloadSrc, // "poster": file.thumbnailAddr, // "aspectRatio": "16:9" // } // }) // } else { var onImageEnlarge = this.props.onImageEnlarge; if (onImageEnlarge) { var files = this.files; if (!isImage && file.isNotImg && file.size > 10 * 1024 * 1024) { antd_1.message.info('大于10M的文件请下载后查看'); } else { onImageEnlarge({