|
|
@@ -6,6 +6,7 @@
|
|
|
:action="uploadUrl"
|
|
|
:fileList="fileList"
|
|
|
:disabled="disabled"
|
|
|
+ :item-render="previewItemRender"
|
|
|
v-bind="bindProps"
|
|
|
@remove="onRemove"
|
|
|
@change="onFileChange"
|
|
|
@@ -26,7 +27,7 @@
|
|
|
</template>
|
|
|
|
|
|
<script lang="ts" setup>
|
|
|
- import { ref, reactive, computed, watch, nextTick, createApp,unref } from 'vue';
|
|
|
+ import { ref, reactive, computed, watch, nextTick, createApp, unref, h, isVNode } from 'vue';
|
|
|
import { Icon } from '/@/components/Icon';
|
|
|
import { getToken } from '/@/utils/auth';
|
|
|
import { uploadUrl } from '/@/api/common/api';
|
|
|
@@ -69,6 +70,8 @@
|
|
|
disabled: propTypes.bool.def(false),
|
|
|
// 替换前一个文件,用于超出最大数量依然允许上传
|
|
|
replaceLastOne: propTypes.bool.def(false),
|
|
|
+ // 是否开启附件预览(pdf、图片,仅对非图片模式生效)
|
|
|
+ filePreview: propTypes.bool.def(true),
|
|
|
});
|
|
|
|
|
|
const headers = getHeaders();
|
|
|
@@ -80,6 +83,8 @@
|
|
|
const isMaxCount = computed(() => props.maxCount > 0 && fileList.value.length >= props.maxCount);
|
|
|
// 当前是否是上传图片模式
|
|
|
const isImageMode = computed(() => props.fileType === UploadTypeEnum.image);
|
|
|
+ // 附件预览模式下自定义列表项渲染,为 pdf、图片文件增加「预览」入口
|
|
|
+ const previewItemRender = computed(() => (props.filePreview && !isImageMode.value ? renderPreviewItem : undefined));
|
|
|
// 上传按钮是否禁用
|
|
|
const buttonDisabled = computed(()=>{
|
|
|
if(props.disabled === true){
|
|
|
@@ -360,7 +365,146 @@
|
|
|
if (isImageMode.value) {
|
|
|
createImgPreview({ imageList: [file.url], maskClosable: true });
|
|
|
} else {
|
|
|
- window.open(file.url);
|
|
|
+ previewAttachFile(file);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取文件名称(用于判断文件类型)
|
|
|
+ function getFileTypeName(file) {
|
|
|
+ return (file && (file.name || file.url || (file.response && file.response.message))) || '';
|
|
|
+ }
|
|
|
+
|
|
|
+ // 是否为 pdf 文件
|
|
|
+ function isPdfFile(file) {
|
|
|
+ return /\.pdf(\?.*)?$/i.test(getFileTypeName(file));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 是否为图片文件
|
|
|
+ function isImageFile(file) {
|
|
|
+ return /\.(png|jpe?g|gif|bmp|webp|svg)(\?.*)?$/i.test(getFileTypeName(file));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取文件访问地址
|
|
|
+ function getFileUrl(file) {
|
|
|
+ if (file && file.url) {
|
|
|
+ return file.url;
|
|
|
+ }
|
|
|
+ const path = file && file.response && file.response.message;
|
|
|
+ return path ? getFileAccessHttpUrl(path) : '';
|
|
|
+ }
|
|
|
+
|
|
|
+ // 附件预览:图片使用图片预览框,pdf 使用新标签页预览,其它类型直接新窗口打开
|
|
|
+ function previewAttachFile(file) {
|
|
|
+ const url = getFileUrl(file);
|
|
|
+ if (!url) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (isImageFile(file)) {
|
|
|
+ createImgPreview({ imageList: [url], maskClosable: true });
|
|
|
+ } else if (isPdfFile(file)) {
|
|
|
+ previewPdfFile(url);
|
|
|
+ } else {
|
|
|
+ window.open(url, '_blank');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 自定义上传列表项:pdf、图片文件增加「预览」「下载」入口,其他类型文件增加「下载」入口
|
|
|
+ function renderPreviewItem(originNode: any, file: any) {
|
|
|
+ // 兼容 itemRender 参数为对象的形式
|
|
|
+ if (originNode && typeof originNode === 'object' && !isVNode(originNode) && originNode.originNode) {
|
|
|
+ file = originNode.file;
|
|
|
+ originNode = originNode.originNode;
|
|
|
+ }
|
|
|
+ // 上传中不显示预览/下载入口
|
|
|
+ if (file && file.status === 'uploading') {
|
|
|
+ return originNode;
|
|
|
+ }
|
|
|
+ const children = [originNode];
|
|
|
+ if (isPdfFile(file) || isImageFile(file)) {
|
|
|
+ children.push(
|
|
|
+ h(
|
|
|
+ 'a',
|
|
|
+ {
|
|
|
+ class: 'attachment-preview-link',
|
|
|
+ title: '预览(preview)',
|
|
|
+ onClick: (e) => {
|
|
|
+ e.preventDefault();
|
|
|
+ e.stopPropagation();
|
|
|
+ previewAttachFile(file);
|
|
|
+ },
|
|
|
+ },
|
|
|
+ h(Icon, { icon: 'ant-design:eye-outlined' })
|
|
|
+ )
|
|
|
+ );
|
|
|
+ }
|
|
|
+ children.push(
|
|
|
+ h(
|
|
|
+ 'a',
|
|
|
+ {
|
|
|
+ class: 'attachment-preview-link',
|
|
|
+ title: '下载(download)',
|
|
|
+ onClick: (e) => {
|
|
|
+ e.preventDefault();
|
|
|
+ e.stopPropagation();
|
|
|
+ downloadAttachFile(file);
|
|
|
+ },
|
|
|
+ },
|
|
|
+ h(Icon, { icon: 'ant-design:download-outlined' })
|
|
|
+ )
|
|
|
+ );
|
|
|
+ return h('div', { class: 'attachment-preview-item-wrapper' }, children);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 下载附件:以 blob 方式触发浏览器下载,跨域失败时回退为新窗口打开
|
|
|
+ async function downloadAttachFile(file) {
|
|
|
+ const url = getFileUrl(file);
|
|
|
+ if (!url) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ const res = await fetch(url);
|
|
|
+ if (!res.ok) {
|
|
|
+ throw new Error(`status: ${res.status}`);
|
|
|
+ }
|
|
|
+ const blob = await res.blob();
|
|
|
+ const blobUrl = URL.createObjectURL(blob);
|
|
|
+ const a = document.createElement('a');
|
|
|
+ a.href = blobUrl;
|
|
|
+ a.download = decodeURIComponent(getFileName(url));
|
|
|
+ document.body.appendChild(a);
|
|
|
+ a.click();
|
|
|
+ a.remove();
|
|
|
+ setTimeout(() => URL.revokeObjectURL(blobUrl), 60000);
|
|
|
+ } catch (e) {
|
|
|
+ // 跨域等场景下直接新窗口打开(由浏览器处理下载)
|
|
|
+ window.open(url, '_blank');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // pdf 在新标签页中预览(浏览器自带预览能力)
|
|
|
+ async function previewPdfFile(url) {
|
|
|
+ if (!url) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ const win = window.open('', '_blank');
|
|
|
+ if (!win) {
|
|
|
+ createMessage.warning('预览窗口被浏览器拦截,请允许本站弹出窗口');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ win.document.title = 'PDF 预览';
|
|
|
+ try {
|
|
|
+ // 以 blob 方式加载,避免服务端返回 attachment 时变成下载
|
|
|
+ const res = await fetch(url);
|
|
|
+ if (!res.ok) {
|
|
|
+ throw new Error(`status: ${res.status}`);
|
|
|
+ }
|
|
|
+ const blob = await res.blob();
|
|
|
+ const blobUrl = URL.createObjectURL(new Blob([blob], { type: 'application/pdf' }));
|
|
|
+ win.location.href = blobUrl;
|
|
|
+ setTimeout(() => URL.revokeObjectURL(blobUrl), 60000);
|
|
|
+ } catch (e) {
|
|
|
+ // 跨域等场景下直接新窗口打开
|
|
|
+ win.location.href = url;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -414,6 +558,27 @@
|
|
|
.ant-upload-text-icon {
|
|
|
color: @primary-color;
|
|
|
}
|
|
|
+ /* 附件预览入口(pdf、图片) */
|
|
|
+ .attachment-preview-item-wrapper {
|
|
|
+ display: flex;
|
|
|
+ align-items: center;
|
|
|
+
|
|
|
+ > .ant-upload-list-item {
|
|
|
+ flex: 1 1 auto;
|
|
|
+ min-width: 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ .attachment-preview-link {
|
|
|
+ flex: none;
|
|
|
+ display: inline-flex;
|
|
|
+ align-items: center;
|
|
|
+ margin-left: 8px;
|
|
|
+ font-size: 14px;
|
|
|
+ cursor: pointer;
|
|
|
+ /* 详情(禁用)状态外层 fieldset 为 pointer-events:none,这里恢复可点击 */
|
|
|
+ pointer-events: auto;
|
|
|
+ }
|
|
|
+ }
|
|
|
.ant-upload-list-item {
|
|
|
.upload-actions-container {
|
|
|
position: absolute;
|