Files
ruoyi-plus-vben5/apps/web-antd/src/components/upload/src/file-upload.vue
dap 3582807910 refactor: 迁移requestClient到alovaInstance并移除旧版上传组件
重构项目中所有使用requestClient的API调用,替换为alovaInstance
移除已废弃的旧版上传组件及相关代码
调整上传组件类型定义以适配antdv-next更新
优化上传逻辑,移除不必要的进度事件和取消信号
更新类型定义文件,迁移axios配置到alova类型
2026-01-19 21:58:07 +08:00

151 lines
3.7 KiB
Vue

<!--
不再支持url 统一使用ossId
去除使用`file-type`库进行文件类型检测 在Safari无法使用
-->
<script setup lang="ts">
import type { UploadProps } from 'antdv-next';
import type { BaseUploadProps, UploadEmits } from './props';
import { computed } from 'vue';
import { $t, I18nT } from '@vben/locales';
import { InboxOutlined, UploadOutlined } from '@ant-design/icons-vue';
import { Upload } from 'antdv-next';
import { uploadApi } from '#/api';
import { defaultFileAcceptExts, defaultFilePreview } from './helper';
import { useUpload } from './hook';
interface FileUploadProps extends BaseUploadProps {
/**
* 同antdv的listType 但是排除picture-card
* 文件上传不适合用picture-card显示
* @default text
*/
listType?: Exclude<UploadProps['listType'], 'picture-card'>;
}
const props = withDefaults(defineProps<FileUploadProps>(), {
api: () => uploadApi,
removeOnError: true,
showSuccessMsg: true,
removeConfirm: false,
accept: defaultFileAcceptExts.join(','),
data: () => undefined,
maxCount: 1,
maxSize: 5,
disabled: false,
helpMessage: true,
preview: defaultFilePreview,
enableDragUpload: false,
directory: false,
abortOnUnmounted: true,
listType: 'text',
});
const emit = defineEmits<UploadEmits>();
/** 返回不同的上传组件 */
const CurrentUploadComponent = computed(() => {
if (props.enableDragUpload) {
return Upload.Dragger;
}
return Upload;
});
// 双向绑定 ossId
const ossIdList = defineModel<string | string[]>('value', {
default: () => [],
});
const {
customRequest,
acceptStr,
handleChange,
handleRemove,
beforeUpload,
innerFileList,
} = useUpload(props, emit, ossIdList, 'file');
</script>
<!--
Upload.Dragger只会影响样式
使用普通Upload也是支持拖拽上传的
-->
<template>
<div>
<CurrentUploadComponent
v-model:file-list="innerFileList"
:accept="accept"
:list-type="listType"
:disabled="disabled"
:directory="directory"
:max-count="maxCount"
:progress="{ showInfo: true }"
:multiple="multiple"
:before-upload="beforeUpload"
:custom-request="customRequest"
@preview="preview"
@change="handleChange"
@remove="handleRemove"
>
<div v-if="!enableDragUpload && innerFileList?.length < maxCount">
<a-button :disabled="disabled">
<UploadOutlined />
{{ $t('component.upload.upload') }}
</a-button>
</div>
<div v-if="enableDragUpload">
<p class="ant-upload-drag-icon">
<InboxOutlined />
</p>
<p class="ant-upload-text">
{{ $t('component.upload.clickOrDrag') }}
</p>
</div>
</CurrentUploadComponent>
<slot name="helpMessage" v-bind="{ maxCount, disabled, maxSize, accept }">
<I18nT
v-if="helpMessage"
scope="global"
keypath="component.upload.uploadHelpMessage"
tag="div"
class="mt-2 text-[14px] leading-[1.5] text-black/45 dark:text-white/45"
:class="{ 'upload-text__disabled': disabled }"
>
<template #size>
<span
class="mx-1 font-medium text-primary"
:class="{ 'upload-text__disabled': disabled }"
>
{{ maxSize }}MB
</span>
</template>
<template #ext>
<span
class="mx-1 font-medium text-primary"
:class="{ 'upload-text__disabled': disabled }"
>
{{ acceptStr }}
</span>
</template>
</I18nT>
</slot>
</div>
</template>
<style lang="scss">
// 禁用的样式和antd保持一致
.upload-text__disabled {
color: rgb(50 54 57 / 25%);
cursor: not-allowed;
&:where(.dark, .dark *) {
color: rgb(242 242 242 / 25%);
}
}
</style>