Skip to content

Commit

Permalink
调整后的ImageUpload组件无法正常使用且无法使用单个上传[#3344] (#3345)
Browse files Browse the repository at this point in the history
* Update typing.ts

* 默认上传单个

* fix(ImageUpload): 修正图片单个上传&返回结果未进行处理
  • Loading branch information
1455668754 authored Nov 27, 2023
1 parent 97b76ea commit 8b516b7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
37 changes: 31 additions & 6 deletions src/components/Upload/src/components/ImageUpload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
<script lang="ts" setup>
import { ref, toRefs, watch } from 'vue';
import { PlusOutlined } from '@ant-design/icons-vue';
import { Upload, Modal } from 'ant-design-vue';
import type { UploadProps, UploadFile } from 'ant-design-vue';
import type { UploadFile, UploadProps } from 'ant-design-vue';
import { Modal, Upload } from 'ant-design-vue';
import { UploadRequestOption } from 'ant-design-vue/lib/vc-upload/interface';
import { useMessage } from '@/hooks/web/useMessage';
import { isArray, isFunction, isObject, isString } from '@/utils/is';
Expand All @@ -36,6 +36,7 @@
import { useUploadType } from '../hooks/useUpload';
import { uploadContainerProps } from '../props';
import { isImgTypeByName } from '../helper';
import { UploadResultStatus } from '@/components/Upload/src/types/typing';
defineOptions({ name: 'ImageUpload' });
Expand All @@ -46,6 +47,7 @@
const { t } = useI18n();
const { createMessage } = useMessage();
const { accept, helpText, maxNumber, maxSize } = toRefs(props);
const isInnerOperate = ref<boolean>(false);
const { getStringAccept } = useUploadType({
acceptRef: accept,
helpTextRef: helpText,
Expand All @@ -63,8 +65,18 @@
watch(
() => props.value,
(v) => {
if (v && isArray(v)) {
fileList.value = v.map((item, i) => {
if (isInnerOperate.value) {
isInnerOperate.value = false;
return;
}
if (v) {
let value: string[] = [];
if (isArray(v)) {
value = v;
} else {
value.push(v);
}
fileList.value = value.map((item, i) => {
if (item && isString(item)) {
return {
uid: -i + '',
Expand Down Expand Up @@ -107,7 +119,9 @@
if (fileList.value) {
const index = fileList.value.findIndex((item) => item.uid === file.uid);
index !== -1 && fileList.value.splice(index, 1);
emit('change', fileList.value);
const value = getValue();
isInnerOperate.value = true;
emit('change', value);
emit('delete', file);
}
};
Expand Down Expand Up @@ -152,12 +166,23 @@
filename: props.filename,
});
info.onSuccess!(res.data);
emit('change', fileList.value);
const value = getValue();
isInnerOperate.value = true;
emit('change', value);
} catch (e: any) {
console.log(e);
info.onError!(e);
}
}
function getValue() {
const list = (fileList.value || [])
.filter((item) => item?.status === UploadResultStatus.DONE)
.map((item: any) => {
return item?.url || item?.response?.url;
});
return props.multiple ? list : list.length > 0 ? list[0] : '';
}
</script>

<style lang="less">
Expand Down
4 changes: 2 additions & 2 deletions src/components/Upload/src/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const basicProps = {
// 最大数量的文件,Infinity不限制
maxNumber: {
type: Number as PropType<number>,
default: Infinity,
default: 1,
},
// 根据后缀,或者其他
accept: {
Expand All @@ -41,7 +41,7 @@ export const basicProps = {
},
multiple: {
type: Boolean as PropType<boolean>,
default: true,
default: false,
},
uploadParams: {
type: Object as PropType<any>,
Expand Down
1 change: 1 addition & 0 deletions src/components/Upload/src/types/typing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BasicColumn } from '@/components/Table';
import { UploadApiResult } from '@/api/sys/model/uploadModel';

export enum UploadResultStatus {
DONE = 'done',
SUCCESS = 'success',
ERROR = 'error',
UPLOADING = 'uploading',
Expand Down

0 comments on commit 8b516b7

Please sign in to comment.