Browse Source

🚀 企业认证

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

35
src/components/Upload/src/BasicUpload.vue

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

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

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

10
src/components/Upload/src/useUpload.ts

@ -2,11 +2,11 @@ import { Ref, unref, computed } from 'vue';
import { useI18n } from '/@/hooks/web/useI18n'; import { useI18n } from '/@/hooks/web/useI18n';
const { t } = useI18n(); const { t } = useI18n();
export function useUploadType({ export function useUploadType({
acceptRef, acceptRef,
helpTextRef, helpTextRef,
maxNumberRef, maxNumberRef,
maxSizeRef, maxSizeRef,
}: { }: {
acceptRef: Ref<string[]>; acceptRef: Ref<string[]>;
helpTextRef: Ref<string>; helpTextRef: Ref<string>;
maxNumberRef: Ref<number>; maxNumberRef: Ref<number>;

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

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

Loading…
Cancel
Save