|
@@ -9,6 +9,7 @@
|
|
|
:closeFunc="handleCloseFunc"
|
|
|
:maskClosable="false"
|
|
|
:keyboard="false"
|
|
|
+ class="upload-modal"
|
|
|
wrapClassName="upload-modal"
|
|
|
:okButtonProps="getOkButtonProps"
|
|
|
:cancelButtonProps="{ disabled: isUploadingRef }"
|
|
@@ -31,6 +32,7 @@
|
|
|
:accept="getStringAccept"
|
|
|
:multiple="multiple"
|
|
|
:before-upload="beforeUpload"
|
|
|
+ :show-upload-list="false"
|
|
|
class="upload-modal-toolbar__btn"
|
|
|
>
|
|
|
<a-button type="primary">
|
|
@@ -54,7 +56,7 @@
|
|
|
import { basicProps } from './props';
|
|
|
import { createTableColumns, createActionColumn } from './data';
|
|
|
// utils
|
|
|
- import { checkFileType, checkImgType, getBase64WithFile } from './helper';
|
|
|
+ import { checkImgType, getBase64WithFile } from './helper';
|
|
|
import { buildUUID } from '/@/utils/uuid';
|
|
|
import { isFunction } from '/@/utils/is';
|
|
|
import { warn } from '/@/utils/log';
|
|
@@ -84,7 +86,7 @@
|
|
|
const { t } = useI18n();
|
|
|
const [register, { closeModal }] = useModalInner();
|
|
|
|
|
|
- const { getAccept, getStringAccept, getHelpText } = useUploadType({
|
|
|
+ const { getStringAccept, getHelpText } = useUploadType({
|
|
|
acceptRef: accept,
|
|
|
helpTextRef: helpText,
|
|
|
maxNumberRef: maxNumber,
|
|
@@ -123,19 +125,20 @@
|
|
|
// 上传前校验
|
|
|
function beforeUpload(file: File) {
|
|
|
const { size, name } = file;
|
|
|
- const { maxSize } = props;
|
|
|
- const accept = unref(getAccept);
|
|
|
+ const { maxSize, accept } = props;
|
|
|
+ const type = name.split('.').pop() || '';
|
|
|
+ console.log('beforeUpload', type, name);
|
|
|
+ if (accept && accept.length > 0 && !accept.includes(type.toLowerCase())) {
|
|
|
+ createMessage.error(t('component.upload.accept', [accept.join(',')]));
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ console.log('file', type, props);
|
|
|
// 设置最大值,则判断
|
|
|
if (maxSize && file.size / 1024 / 1024 >= maxSize) {
|
|
|
createMessage.error(t('component.upload.maxSizeMultiple', [maxSize]));
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- // 设置类型,则判断
|
|
|
- if (accept.length > 0 && !checkFileType(file, accept)) {
|
|
|
- createMessage.error!(t('component.upload.acceptUpload', [accept.join(',')]));
|
|
|
- return false;
|
|
|
- }
|
|
|
const commonItem = {
|
|
|
uuid: buildUUID(),
|
|
|
file,
|
|
@@ -177,13 +180,33 @@
|
|
|
// imageList: [thumbUrl],
|
|
|
// });
|
|
|
// }
|
|
|
-
|
|
|
+ /**
|
|
|
+ * 将中文符号转换成英文符号
|
|
|
+ */
|
|
|
+ function chineseChar2englishChar(chineseChar) {
|
|
|
+ // 将单引号‘’都转换成',将双引号“”都转换成"
|
|
|
+ var str = chineseChar.replace(/\’|\‘/g, "'").replace(/\“|\”/g, '"');
|
|
|
+ // 将中括号【】转换成[],将大括号{}转换成{}
|
|
|
+ str = str
|
|
|
+ .replace(/\【/g, '[')
|
|
|
+ .replace(/\】/g, ']')
|
|
|
+ .replace(/\{/g, '{')
|
|
|
+ .replace(/\}/g, '}');
|
|
|
+ // 将逗号,转换成,,将:转换成:
|
|
|
+ str = str.replace(/,/g, ',').replace(/:/g, ':');
|
|
|
+ return str;
|
|
|
+ }
|
|
|
async function uploadApiByItem(item: FileItem) {
|
|
|
+ console.log('item', props, item);
|
|
|
const { api, afterFetch } = props;
|
|
|
if (!api || !isFunction(api)) {
|
|
|
return warn('upload api must exist and be a function');
|
|
|
}
|
|
|
try {
|
|
|
+ let isZh = false;
|
|
|
+ if (/.*[\u4e00-\u9fa5]+.*$/.test(item.name)) {
|
|
|
+ isZh = true;
|
|
|
+ }
|
|
|
item.status = UploadResultStatus.UPLOADING;
|
|
|
const { data } = await props.api?.(
|
|
|
{
|
|
@@ -192,7 +215,7 @@
|
|
|
},
|
|
|
file: item.file,
|
|
|
name: props.name,
|
|
|
- filename: props.filename,
|
|
|
+ filename: isZh ? item.uuid + '.' + item.type : props.filename,
|
|
|
},
|
|
|
function onUploadProgress(progressEvent: ProgressEvent) {
|
|
|
const complete = ((progressEvent.loaded / progressEvent.total) * 100) | 0;
|
|
@@ -201,11 +224,11 @@
|
|
|
);
|
|
|
item.status = UploadResultStatus.SUCCESS;
|
|
|
item.responseData = data;
|
|
|
-
|
|
|
+
|
|
|
if (afterFetch && isFunction(afterFetch)) {
|
|
|
item.responseData = (await afterFetch(data)) || data;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return {
|
|
|
success: true,
|
|
|
error: null,
|
|
@@ -219,10 +242,39 @@
|
|
|
};
|
|
|
}
|
|
|
}
|
|
|
+ //上传文件流
|
|
|
+ async function uploadFileFlow(item: FileItem) {
|
|
|
+ console.log('item', props, item);
|
|
|
+ const { afterFetch } = props;
|
|
|
+ try {
|
|
|
+ let isZh = false;
|
|
|
+ if (/.*[\u4e00-\u9fa5]+.*$/.test(item.name)) {
|
|
|
+ isZh = true;
|
|
|
+ }
|
|
|
+ item.status = UploadResultStatus.UPLOADING;
|
|
|
|
|
|
+ item.status = UploadResultStatus.SUCCESS;
|
|
|
+
|
|
|
+ if (afterFetch && isFunction(afterFetch)) {
|
|
|
+ item.responseData = (await afterFetch(item)) || item;
|
|
|
+ }
|
|
|
+
|
|
|
+ return {
|
|
|
+ success: true,
|
|
|
+ error: null,
|
|
|
+ };
|
|
|
+ } catch (e) {
|
|
|
+ console.log(e);
|
|
|
+ item.status = UploadResultStatus.ERROR;
|
|
|
+ return {
|
|
|
+ success: false,
|
|
|
+ error: e,
|
|
|
+ };
|
|
|
+ }
|
|
|
+ }
|
|
|
// 点击开始上传
|
|
|
async function handleStartUpload() {
|
|
|
- const { maxNumber } = props;
|
|
|
+ const { maxNumber,fileFlow } = props;
|
|
|
if ((fileListRef.value.length + props.previewFileList?.length ?? 0) > maxNumber) {
|
|
|
return createMessage.warning(t('component.upload.maxNumber', [maxNumber]));
|
|
|
}
|
|
@@ -233,22 +285,27 @@
|
|
|
fileListRef.value.filter((item) => item.status !== UploadResultStatus.SUCCESS) || [];
|
|
|
const data = await Promise.all(
|
|
|
uploadFileList.map((item) => {
|
|
|
+ if(fileFlow){
|
|
|
+ return uploadFileFlow(item);
|
|
|
+ }else{
|
|
|
return uploadApiByItem(item);
|
|
|
+ }
|
|
|
}),
|
|
|
);
|
|
|
isUploadingRef.value = false;
|
|
|
// 生产环境:抛出错误
|
|
|
const errorList = data.filter((item: any) => !item.success);
|
|
|
if (errorList.length > 0) throw errorList;
|
|
|
- } catch (e) {
|
|
|
+ } catch (error) {
|
|
|
+ console.log(error);
|
|
|
isUploadingRef.value = false;
|
|
|
- throw e;
|
|
|
+ throw error;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 点击保存
|
|
|
function handleOk() {
|
|
|
- const { maxNumber } = props;
|
|
|
+ const { maxNumber,fileFlow } = props;
|
|
|
|
|
|
if (fileListRef.value.length > maxNumber) {
|
|
|
return createMessage.warning(t('component.upload.maxNumber', [maxNumber]));
|
|
@@ -260,8 +317,13 @@
|
|
|
|
|
|
for (const item of fileListRef.value) {
|
|
|
const { status, responseData } = item;
|
|
|
+ console.log('responseData', item);
|
|
|
if (status === UploadResultStatus.SUCCESS && responseData) {
|
|
|
- fileList.push(responseData.url);
|
|
|
+ if(fileFlow){
|
|
|
+ fileList.push(responseData.name);
|
|
|
+ }else{
|
|
|
+ fileList.push(responseData.url);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
// 存在一个上传成功的即可保存
|
|
@@ -302,6 +364,7 @@
|
|
|
handleCloseFunc,
|
|
|
getIsSelectFile,
|
|
|
getUploadBtnText,
|
|
|
+ chineseChar2englishChar,
|
|
|
t,
|
|
|
};
|
|
|
},
|