tav-ui
Version:
359 lines (354 loc) • 12.7 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var vue = require('vue');
require('../../../locales/index2.js');
var useGlobalConfig = require('../../../hooks/global/useGlobalConfig2.js');
var useMessage = require('../../../hooks/web/useMessage2.js');
require('../../../utils/index2.js');
var hooks = require('./hooks2.js');
var transfer = require('../../../locales/transfer2.js');
var is = require('../../../utils/is2.js');
const { createMessage } = useMessage.useMessage();
class Handler {
constructor(props, instanceEmit) {
this._hyperlinkFormMethods = {};
this._provide = vue.computed(() => useGlobalConfig.useGlobalConfig("components").value?.TaUpload);
this._params = {};
this._showTable = true;
this._refFileList = [];
this._uploadResponse = [];
this._refFileListPushEnd = false;
this._immediate = false;
this._controlInOuter = false;
this._fileFormatter = hooks.useFileFormatter();
this._typeCode = vue.ref();
this._isLoading = vue.ref(false);
this._dataSource = vue.ref([]);
this.currentTypeCodeIsHyperlink = vue.ref(false);
this._apis = {};
this.currentUpload = null;
this.isInvalidRequest = () => this._refFileListPushEnd || this._refFileList.length === 0;
this.getFileActualIds = () => this._uploadResponse.map((el) => el.actualId);
this.getResult = () => this._fileFormatter.formatToApi(this._uploadResponse);
this.fillDataSource = () => {
if (!this._showTable) {
return;
}
this._dataSource.value = [];
this._dataSource.value.push(...this._uploadResponse);
};
this.updateItem = (record, oldFileActualIds) => {
const index = this._uploadResponse.findIndex((el) => el.actualId === oldFileActualIds);
const oldRecord = this._uploadResponse.splice(index, 1, record)[0];
this._fileFormatter.upadteVersion(record);
this.fillDataSource();
this.throwResponse([{ ...record, version: oldRecord.version + 1 }], "update");
};
this.deleteItem = (record) => {
const { actualId } = record;
const index = this._uploadResponse.findIndex((el) => el.actualId === actualId);
const newRecord = this._uploadResponse[index];
const spliceData = () => {
this._uploadResponse.splice(index, 1);
this.fillDataSource();
this.throwResponse([newRecord], "delete");
};
this._isLoading.value = true;
if (!this._immediate) {
spliceData();
setTimeout(() => {
this._isLoading.value = false;
}, 300);
return;
}
this.apis.removeFile(actualId).then(() => {
spliceData();
}).catch(() => {
createMessage.warn(transfer.tavI18n("Tav.file.message.1"));
}).finally(() => this._isLoading.value = false);
};
this.antBeforeUpload = (file) => {
const regexpStr = `[\\\\/:*?"<>|]`;
const fileRegexp = new RegExp(regexpStr, "g");
if (fileRegexp.test(file.name)) {
createMessage.warn(`${file.name} ${transfer.tavI18n("Tav.file.upload.5")}${regexpStr}`);
return false;
} else {
this._refFileList.push(file);
}
};
this.customRequest = () => {
if (this.isInvalidRequest())
return;
const sizeOverflowFiles = this._refFileList.filter((file) => file.size / 1024 / 1024 > 1024);
if (sizeOverflowFiles.length > 0) {
createMessage.warn(`${transfer.tavI18n("Tav.file.upload.4")} ${sizeOverflowFiles.map((file) => `${file.name}:${Math.floor(file.size / 1024 / 1024)}MB`).join()}${transfer.tavI18n("Tav.common.greater")}1GB`);
this.resetFileList();
return;
}
if (this._props.maxCount && this._uploadResponse.length + this._refFileList.length > this._props.maxCount) {
createMessage.warn(`${transfer.tavI18n("Tav.file.message.4")} ${this._props.maxCount}\u4E2A`);
this.resetFileList();
return;
}
this._refFileListPushEnd = true;
this.realUpload();
};
this.resetFileList = () => {
this._refFileList = [];
this._refFileListPushEnd = false;
this._isLoading.value = false;
};
this.realUpload = async () => {
if (!this._typeCode.value) {
createMessage.warn(transfer.tavI18n("Tav.file.message.5"));
this.resetFileList();
return;
}
const { beforeUpload } = this._props;
if (beforeUpload && !await beforeUpload(this._refFileList, this._typeCode.value)) {
this.resetFileList();
return;
}
const formData = new FormData();
this._refFileList.forEach((el) => {
formData.append("files", el);
});
this._params.typeCode = this._typeCode.value;
for (const k in this._params) {
if (!this._immediate && ["businessId", "businessKey"].includes(k))
continue;
if (!this._params[k])
continue;
this._params[k] != void 0 && formData.append(k, this._params[k]);
}
this._isLoading.value = true;
this.currentUpload = this.apis.uploadFile(formData).then(({ data: r }) => {
this._uploadResponse.unshift(...r);
this.throwResponse(r, "upload");
vue.nextTick(() => this.fillDataSource());
r.forEach((el) => {
this._fileFormatter.upadteVersion(el);
});
createMessage.success(transfer.tavI18n("Tav.file.message.6"));
}).catch(() => {
}).finally(() => {
this.resetFileList();
});
};
this.hyperlinkFormRegister = (methods) => {
this._hyperlinkFormMethods = methods;
};
this.preOpenChooseFile = (e) => {
if (!this._typeCode.value) {
createMessage.warn(transfer.tavI18n("Tav.file.message.5"));
e.stopPropagation();
}
};
this.emit = instanceEmit;
this.props = props;
vue.watch(() => this._props.showTable, (val) => {
this._showTable = val;
}, {
immediate: true
});
vue.watch(() => this._props.immediate, (val) => {
this._immediate = val;
}, {
immediate: true
});
vue.watch(() => this._props.params.businessId, (val) => {
this._params.businessId = val;
if (this._controlInOuter)
return;
if (val === void 0) {
this.clearResponse();
return;
}
if (this._props.uploadResponse)
return;
this.backfill();
}, {
immediate: true
});
vue.watch(() => this._props.params.businessKey, (val) => {
this._params.businessKey = val;
if (this._controlInOuter)
return;
if (val === void 0) {
this.clearResponse();
return;
}
if (this._props.uploadResponse)
return;
this.backfill();
}, {
immediate: true,
flush: "pre"
});
vue.watch(() => [
this._props.params.id,
this._props.params.endTime,
this._props.params.typeCode,
this._props.params.startTime,
this._props.params.moduleCode,
this._props.params.businessKey,
this._props.params.searchValue
], ([
idVal,
endTimeVal,
typeCodeVal,
startTimeVal,
moduleCodeVal,
businessKeyVal,
searchValueVal
], [
idPrev,
endTimePrev,
typeCodePrev,
startTimePrev,
moduleCodePrev,
businessKeyPrev,
searchValuePrev
]) => {
idVal !== idPrev && (this._params.id = idVal);
endTimeVal !== endTimePrev && (this._params.endTime = endTimeVal);
typeCodeVal !== typeCodePrev && (this._typeCode.value = typeCodeVal);
startTimeVal !== startTimePrev && (this._params.startTime = startTimeVal);
businessKeyVal !== businessKeyPrev && (this._params.businessKey = businessKeyVal);
moduleCodeVal !== moduleCodePrev && (this._params.moduleCode = moduleCodeVal);
searchValueVal !== searchValuePrev && (this._params.searchValue = searchValueVal);
}, {
flush: "pre"
});
}
set props(v) {
this._props = v;
const { params, controlInOuter } = v;
this._controlInOuter = controlInOuter;
this._typeCode.value = params.typeCode;
vue.watch(() => this._props.uploadResponse, (v2) => {
this.uploadResponse = v2;
if (!this._props.showTable)
return;
this.throwResponse(v2, "init");
}, {
immediate: true
});
for (const key in params) {
this._params[key] = params[key];
}
}
get dataSource() {
return this._dataSource;
}
get typeCode() {
return this._typeCode;
}
get loading() {
return this._isLoading;
}
get typeCodeRecord() {
return this._props.typeCodeRecord ?? this._provide.value?.typeCodeRecord ?? {};
}
get apis() {
if (!this._apis.queryFile) {
const apis = {
queryFile: this._props.queryFile ?? this._provide.value?.queryFile,
removeFile: this._props.removeFile ?? this._provide.value?.removeFile,
uploadFile: this._props.uploadFile ?? this._provide.value?.uploadFile,
updateFile: this._props.updateFile ?? this._provide.value?.updateFile,
uploadHyperlink: this._props.uploadHyperlink ?? this._provide.value?.uploadHyperlink,
download: this._props.download ?? this._provide.value?.download,
updateFileNameAndAddress: this._props.updateFileNameAndAddress ?? this._provide.value?.updateFileNameAndAddress,
updateFileType: this._props.updateFileType ?? this._provide.value?.updateFileType,
queryFileType: this._props.queryFileType ?? this._provide.value?.queryFileType
};
for (const key in apis) {
this._apis[key] = apis[key] && is.isFunction(apis[key]) ? (...args) => apis[key](...args, this._props.AppId) : void 0;
}
if (!(is.isFunction(apis.queryFile) && (this._immediate ? is.isFunction(apis.removeFile) : true) && is.isFunction(apis.uploadFile) && (this._props.showUploadHyperlinkBtn === "unset" || this._props.showUploadHyperlinkBtn === false) ? true : is.isFunction(apis.uploadHyperlink))) {
throw new Error("<queryFile, uploadFile, uploadHyperlink,typeCodeRecord, [removeFile]> \u5FC5\u987B\u5728TaUpload\u6302\u8F7D\u524D\u4ECEapp.vue\u6CE8\u5165, \u6216\u8005\u4F20\u5165\u540C\u540Dprops");
}
}
return this._apis;
}
get getFileFormatter() {
return this._fileFormatter;
}
set paramsName(v) {
this._paramsName = v;
}
set paramsAddress(v) {
this._paramsAddress = v;
}
set uploadResponse(v) {
this._uploadResponse = v ?? [];
this.fillDataSource();
}
getPropsOrProvide(propName) {
return this._props[propName] ?? this._provide.value?.[propName];
}
throwResponse(newRecord, type) {
this.emit("update:fileActualIds", !this._props.immediate && (this._params.businessId || this._params.businessKey) ? this.getResult() : this.getFileActualIds());
this.emit("change", newRecord, this._uploadResponse, type);
}
async backfill() {
if (!this._showTable)
return;
if (this._params.businessId || this._params.businessKey) {
this.clearResponse();
this._isLoading.value = true;
const response = await this.apis.queryFile({
filter: {
...this._params,
typeCode: void 0
},
model: {
page: 1,
limit: 50
}
}).finally(() => this._isLoading.value = false);
this._uploadResponse.push(...response.data.result);
this._fileFormatter.formatToApi(this._uploadResponse);
this.throwResponse(response.data.result, "init");
}
this.fillDataSource();
}
clearResponse() {
this.uploadResponse = [];
this._props.params.typeCode && (this._typeCode.value = this._props.params.typeCode);
this.throwResponse([], "delete");
}
hyperlinkUpload() {
if (!this._typeCode.value) {
createMessage.warn(transfer.tavI18n("Tav.file.message.5"));
this.resetFileList();
return;
}
const payload = {
...this._params,
typeCode: this._typeCode.value,
name: this._paramsName,
address: this._paramsAddress
};
if (!this._immediate) {
Reflect.deleteProperty(payload, "businessId");
Reflect.deleteProperty(payload, "businessKey");
}
this._isLoading.value = true;
this.apis.uploadHyperlink(payload).then(({ data: r }) => {
this._uploadResponse.unshift(r);
this._fileFormatter.upadteVersion(r);
this.throwResponse([r], "upload");
vue.nextTick(() => this.fillDataSource());
createMessage.success(transfer.tavI18n("Tav.file.message.6"));
}).catch(() => {
}).finally(() => {
this._hyperlinkFormMethods.resetFields?.();
this._isLoading.value = false;
});
}
}
exports.Handler = Handler;
//# sourceMappingURL=main2.js.map