Browse Source

🚀 企业认证

master
wangxiang 2 years ago
parent
commit
16f91db94f
  1. 21
      src/components/Upload/src/BasicUpload.vue
  2. 68
      src/components/Upload/src/UploadModal.vue
  3. 2
      src/components/Upload/src/useUpload.ts
  4. 7
      src/views/system/file/index.vue

21
src/components/Upload/src/BasicUpload.vue

@ -1,8 +1,10 @@ @@ -1,8 +1,10 @@
<template>
<div>
<Space>
<a-button type="primary" preIcon="carbon:cloud-upload" @click="openUploadModal">
{{ t('component.upload.upload') }}
<a-button type="primary"
preIcon="carbon:cloud-upload"
@click="openUploadModal"
>{{ t('component.upload.upload') }}
</a-button>
<Tooltip v-if="showPreview" placement="bottom">
<template #title>
@ -19,17 +21,13 @@ @@ -19,17 +21,13 @@
</a-button>
</Tooltip>
</Space>
<UploadModal
v-bind="bindValue"
<UploadModal v-bind="bindValue"
:previewFileList="fileList"
@register="registerUploadModal"
@change="handleChange"
@delete="handleDelete"
@success="handleSuccess"
/>
<UploadPreviewModal
:value="fileList"
<UploadPreviewModal :value="fileList"
@register="registerPreviewModal"
@list-change="handlePreviewChange"
@delete="handlePreviewDelete"
@ -53,7 +51,6 @@ @@ -53,7 +51,6 @@
components: { UploadModal, Space, UploadPreviewModal, Icon, Tooltip },
props: uploadContainerProps,
emits: ['change', 'delete', 'success', 'preview-delete', 'update:value'],
setup(props, { emit, attrs }) {
const { t } = useI18n();
// modal
@ -97,10 +94,6 @@ @@ -97,10 +94,6 @@
emit('change', fileList.value);
}
function handleSuccess() {
emit('success', closeUploadModal);
}
function handleDelete(record: Recordable) {
emit('delete', record);
}
@ -110,9 +103,9 @@ @@ -110,9 +103,9 @@
}
return {
closeUploadModal,
registerUploadModal,
openUploadModal,
handleSuccess,
handleChange,
handlePreviewChange,
registerPreviewModal,

68
src/components/Upload/src/UploadModal.vue

@ -69,24 +69,18 @@ @@ -69,24 +69,18 @@
props: {
...basicProps,
previewFileList: {
type: Array as PropType<FileItem[]>,
type: Array as PropType<string[]>,
default: () => [],
},
},
emits: ['change', 'register', 'delete', 'success'],
setup(props, { emit }) {
const state = reactive<{ fileList: FileItem[] }>({
fileList: [],
});
//
const { t } = useI18n();
const isUploadingRef = ref(false);
const fileListRef = ref<FileItem[]>([]);
const { accept, helpText, maxNumber, maxSize } = toRefs(props);
const { t } = useI18n();
const [register, { closeModal }] = useModalInner();
const { createMessage } = useMessage();
const { getStringAccept, getHelpText } = useUploadType({
acceptRef: accept,
helpTextRef: helpText,
@ -94,28 +88,16 @@ @@ -94,28 +88,16 @@
maxSizeRef: maxSize,
});
const { createMessage } = useMessage();
const getIsSelectFile = computed(() => {
return (
fileListRef.value.length > 0 &&
!fileListRef.value.every((item) => item.status === UploadResultStatus.SUCCESS)
);
});
const getIsSelectFile = computed(() =>
(fileListRef.value.length > 0 && !fileListRef.value.every((item) => item.status === UploadResultStatus.SUCCESS)));
const getOkButtonProps = computed(() => {
const someSuccess = fileListRef.value.some(
(item) => item.status === UploadResultStatus.SUCCESS,
);
return {
disabled: isUploadingRef.value || fileListRef.value.length === 0 || !someSuccess,
};
const someSuccess = fileListRef.value.some(item => item.status === UploadResultStatus.SUCCESS);
return { disabled: isUploadingRef.value || fileListRef.value.length === 0 || !someSuccess };
});
const getUploadBtnText = computed(() => {
const someError = fileListRef.value.some(
(item) => item.status === UploadResultStatus.ERROR,
);
const someError = fileListRef.value.some(item => item.status === UploadResultStatus.ERROR);
return isUploadingRef.value
? t('component.upload.uploading')
: someError
@ -147,10 +129,7 @@ @@ -147,10 +129,7 @@
getBase64WithFile(file).then(({ result: thumbUrl }) => {
fileListRef.value = [
...unref(fileListRef),
{
thumbUrl,
...commonItem,
},
{ thumbUrl, ...commonItem }
];
});
} else {
@ -168,16 +147,12 @@ @@ -168,16 +147,12 @@
async function uploadApiByItem(item: FileItem) {
const { api } = props;
if (!api || !isFunction(api)) {
if (!api || !isFunction(api))
return warn('upload api must exist and be a function');
}
try {
item.status = UploadResultStatus.UPLOADING;
const { data } = await props.api?.(
{
data: {
...(props.uploadParams || {}),
},
const { data } = await props.api?.({
data: {...(props.uploadParams || {})},
file: item.file,
name: props.name,
filename: props.filename,
@ -194,7 +169,6 @@ @@ -194,7 +169,6 @@
error: null,
};
} catch (e) {
console.log(e);
item.status = UploadResultStatus.ERROR;
return {
success: false,
@ -212,20 +186,13 @@ @@ -212,20 +186,13 @@
try {
isUploadingRef.value = true;
//
const uploadFileList =
fileListRef.value.filter((item) => item.status !== UploadResultStatus.SUCCESS) || [];
const data = await Promise.all(
uploadFileList.map((item) => {
return uploadApiByItem(item);
}),
);
const uploadFileList = fileListRef.value.filter((item) => item.status !== UploadResultStatus.SUCCESS) || [];
const result = await Promise.all(uploadFileList.map((item) => uploadApiByItem(item)));
isUploadingRef.value = false;
// :
const errorList = data.filter((item: any) => !item.success);
const errorList = result.filter((item: any) => !item.success);
if (errorList.length > 0) throw errorList;
useTimeoutFn(() => {
emit('success');
}, 300);
//
!props.showUploadSaveBtn && handleOk();
} catch (e) {
isUploadingRef.value = false;
throw e;
@ -280,7 +247,6 @@ @@ -280,7 +247,6 @@
getOkButtonProps,
beforeUpload,
fileListRef,
state,
isUploadingRef,
handleStartUpload,
handleOk,

2
src/components/Upload/src/useUpload.ts

@ -6,7 +6,7 @@ export function useUploadType({ @@ -6,7 +6,7 @@ export function useUploadType({
helpTextRef,
maxNumberRef,
maxSizeRef,
}: {
}: {
acceptRef: Ref<string[]>;
helpTextRef: Ref<string>;
maxNumberRef: Ref<number>;

7
src/views/system/file/index.vue

@ -13,7 +13,7 @@ @@ -13,7 +13,7 @@
:api="commonUpload"
:accept="['image/*']"
multiple
@success="handleUploadSuccess"
@change="handleUploadSave"
/>
<a-button v-auth="['file_del']"
type="primary"
@ -107,10 +107,9 @@ @@ -107,10 +107,9 @@
state.multiple = !rowSelection.length;
}
/** 处理上传成功回调 */
function handleUploadSuccess(closeUploadModal: Fn) {
/** 处理上传成功保存回调 */
function handleUploadSave(fileList: string[]) {
state.fileList = [];
closeUploadModal();
handleRefreshTable();
}

Loading…
Cancel
Save