364 lines
11 KiB
Vue
364 lines
11 KiB
Vue
<!--
|
||
覆盖 FormCreate 内置 upload:picture-card 下非图片不再用 <img> 破图,
|
||
改为文档/视频图标 + 文件名(对齐业务附件卡片语义)。
|
||
-->
|
||
<template>
|
||
<div class="_fc-upload _oa-fc-upload">
|
||
<el-upload
|
||
ref="uploadRef"
|
||
:key="fileCount"
|
||
v-bind="uploadAttrs"
|
||
:list-type="resolvedListType"
|
||
:class="{ '_fc-exceed': isExceed }"
|
||
:file-list="fileList"
|
||
:http-request="doHttpRequest"
|
||
:on-preview="handlePreview"
|
||
:on-change="handleChange"
|
||
:on-remove="handleRemove"
|
||
>
|
||
<slot>
|
||
<el-icon v-if="!isTextOrPictureList">
|
||
<Upload />
|
||
</el-icon>
|
||
<el-button v-else type="primary">
|
||
{{ uploadButtonText }}
|
||
</el-button>
|
||
</slot>
|
||
|
||
<template v-if="useCustomFileSlot" #file="{ file }">
|
||
<div class="oa-fc-upload-card" :class="{ 'is-file': !isOaUploadImageFile(file) }">
|
||
<template v-if="file.status === 'uploading'">
|
||
<el-progress
|
||
type="circle"
|
||
:percentage="Number(file.percentage || 0)"
|
||
:stroke-width="6"
|
||
:width="70"
|
||
/>
|
||
</template>
|
||
<template v-else-if="isOaUploadImageFile(file)">
|
||
<img class="el-upload-list__item-thumbnail" :src="file.url" alt="" />
|
||
</template>
|
||
<template v-else-if="isOaUploadVideoFile(file)">
|
||
<div class="oa-fc-upload-card__icon oa-fc-upload-card__icon--video">
|
||
<el-icon :size="28"><VideoPlay /></el-icon>
|
||
</div>
|
||
<div class="oa-fc-upload-card__name" :title="getOaUploadDisplayName(file)">
|
||
{{ getOaUploadDisplayName(file) }}
|
||
</div>
|
||
</template>
|
||
<template v-else>
|
||
<div class="oa-fc-upload-card__icon oa-fc-upload-card__icon--doc">
|
||
<el-icon :size="28"><Document /></el-icon>
|
||
</div>
|
||
<div class="oa-fc-upload-card__name" :title="getOaUploadDisplayName(file)">
|
||
{{ getOaUploadDisplayName(file) }}
|
||
</div>
|
||
</template>
|
||
|
||
<label v-if="file.status === 'success'" class="el-upload-list__item-status-label">
|
||
<el-icon class="el-icon--upload-success el-icon--check"><Check /></el-icon>
|
||
</label>
|
||
|
||
<span v-if="file.status !== 'uploading'" class="el-upload-list__item-actions">
|
||
<span
|
||
v-if="isOaUploadImageFile(file)"
|
||
class="el-upload-list__item-preview"
|
||
title="预览"
|
||
@click="handlePreview(file)"
|
||
>
|
||
<el-icon><ZoomIn /></el-icon>
|
||
</span>
|
||
<span
|
||
class="el-upload-list__item-preview"
|
||
title="下载"
|
||
@click.stop="handleDownload(file)"
|
||
>
|
||
<el-icon><Download /></el-icon>
|
||
</span>
|
||
<span
|
||
v-if="!isDisabled"
|
||
class="el-upload-list__item-delete"
|
||
title="删除"
|
||
@click="removeFile(file)"
|
||
>
|
||
<el-icon><Delete /></el-icon>
|
||
</span>
|
||
</span>
|
||
</div>
|
||
</template>
|
||
</el-upload>
|
||
|
||
<el-dialog
|
||
v-model="previewVisible"
|
||
append-to-body
|
||
:modal="previewMask"
|
||
:title="modalTitle || '预览'"
|
||
@close="previewVisible = false"
|
||
>
|
||
<img style="width: 100%" :src="previewImage" alt="" />
|
||
</el-dialog>
|
||
</div>
|
||
</template>
|
||
|
||
<script lang="ts">
|
||
import { defineComponent } from 'vue'
|
||
import type { UploadFile, UploadProps, UploadRequestOptions, UploadUserFile } from 'element-plus'
|
||
import { ElMessage } from 'element-plus'
|
||
import {
|
||
Check,
|
||
Delete,
|
||
Document,
|
||
Download,
|
||
Upload,
|
||
VideoPlay,
|
||
ZoomIn
|
||
} from '@element-plus/icons-vue'
|
||
import { attachmentFullUrl } from '@/composables/useAttachmentActions'
|
||
import { downloadAttachmentFile } from '@/utils/download-attachment'
|
||
import {
|
||
getOaUploadDisplayName,
|
||
isOaUploadImageFile,
|
||
isOaUploadVideoFile
|
||
} from '@/utils/oa-upload-file-type'
|
||
|
||
function toArray(value: unknown): unknown[] {
|
||
if (value == null || value === '') return []
|
||
return Array.isArray(value) ? value : [value]
|
||
}
|
||
|
||
function getFileName(file: string): string {
|
||
return String(file).split('/').pop() || String(file)
|
||
}
|
||
|
||
function parseFile(file: unknown, i: number): UploadUserFile & { is_string?: boolean } {
|
||
if (file && typeof file === 'object') {
|
||
return file as UploadUserFile
|
||
}
|
||
const url = String(file || '')
|
||
return {
|
||
url,
|
||
name: getFileName(url),
|
||
uid: i,
|
||
status: 'success',
|
||
is_string: true
|
||
}
|
||
}
|
||
|
||
function parseUpload(file: UploadUserFile): UploadUserFile & {
|
||
file?: UploadUserFile
|
||
value?: UploadUserFile
|
||
is_string?: boolean
|
||
} {
|
||
const withMeta = file as UploadUserFile & { is_string?: boolean }
|
||
return { ...withMeta, file: withMeta, value: withMeta }
|
||
}
|
||
|
||
export default defineComponent({
|
||
name: 'fcUpload',
|
||
inheritAttrs: false,
|
||
components: { Check, Delete, Document, Download, Upload, VideoPlay, ZoomIn },
|
||
formCreateParser: {
|
||
toFormValue(value: unknown) {
|
||
return toArray(value)
|
||
},
|
||
toValue(formValue: unknown[], ctx: { prop?: { props?: { limit?: number } } }) {
|
||
return ctx.prop?.props?.limit === 1 ? formValue[0] || '' : formValue
|
||
}
|
||
},
|
||
props: {
|
||
previewMask: { type: [Boolean, Object] as any, default: undefined },
|
||
onPreview: { type: Function, default: undefined },
|
||
httpRequest: { type: Function, default: undefined },
|
||
modalTitle: { type: String, default: '' },
|
||
listType: { type: String, default: '' },
|
||
uploadText: { type: String, default: '' },
|
||
formCreateInject: { type: Object, default: undefined },
|
||
modelValue: { type: [Array, String, Object] as any, default: undefined }
|
||
},
|
||
emits: ['update:modelValue', 'change', 'remove', 'fc.el'],
|
||
data() {
|
||
return {
|
||
previewVisible: false,
|
||
previewImage: '',
|
||
fileList: [] as Array<UploadUserFile & { is_string?: boolean; value?: unknown }>
|
||
}
|
||
},
|
||
computed: {
|
||
resolvedListType(): UploadProps['listType'] {
|
||
return (this.listType as UploadProps['listType']) || 'picture-card'
|
||
},
|
||
isTextOrPictureList(): boolean {
|
||
return this.resolvedListType === 'text' || this.resolvedListType === 'picture'
|
||
},
|
||
useCustomFileSlot(): boolean {
|
||
return this.resolvedListType === 'picture-card' || this.resolvedListType === 'picture'
|
||
},
|
||
fileCount(): number {
|
||
return toArray(this.modelValue).length
|
||
},
|
||
isExceed(): boolean {
|
||
const limit = Number((this.$attrs as any).limit)
|
||
if (!limit) return false
|
||
return limit <= this.fileCount
|
||
},
|
||
isDisabled(): boolean {
|
||
return !!(this.$attrs as any).disabled
|
||
},
|
||
uploadButtonText(): string {
|
||
const t = this.formCreateInject?.t?.('clickToUpload')
|
||
return t || this.uploadText || '点击上传'
|
||
},
|
||
uploadAttrs(): Record<string, unknown> {
|
||
const attrs = { ...(this.$attrs as Record<string, unknown>) }
|
||
delete attrs.onPreview
|
||
delete attrs['on-preview']
|
||
return attrs
|
||
}
|
||
},
|
||
watch: {
|
||
modelValue: {
|
||
immediate: true,
|
||
handler(n: unknown) {
|
||
this.fileList = toArray(n).map(parseFile).map(parseUpload)
|
||
}
|
||
}
|
||
},
|
||
methods: {
|
||
isOaUploadImageFile,
|
||
isOaUploadVideoFile,
|
||
getOaUploadDisplayName,
|
||
handlePreview(file: UploadFile) {
|
||
if (typeof this.onPreview === 'function') {
|
||
this.onPreview(file)
|
||
return
|
||
}
|
||
if (this.resolvedListType === 'text' || !isOaUploadImageFile(file)) {
|
||
if (file?.url) window.open(file.url, '_blank')
|
||
return
|
||
}
|
||
this.previewImage = file.url || ''
|
||
this.previewVisible = true
|
||
},
|
||
async handleDownload(file: UploadFile) {
|
||
const url = String(file?.url || '').trim()
|
||
const fileName = getOaUploadDisplayName(file)
|
||
if (!url) {
|
||
ElMessage.warning('文件地址无效,无法下载')
|
||
return
|
||
}
|
||
// 尚未落到服务端地址时,用本地 raw 触发下载
|
||
if (url.startsWith('blob:') && file.raw) {
|
||
const objectUrl = URL.createObjectURL(file.raw)
|
||
try {
|
||
const a = document.createElement('a')
|
||
a.href = objectUrl
|
||
a.download = fileName
|
||
a.rel = 'noopener'
|
||
document.body.appendChild(a)
|
||
a.click()
|
||
document.body.removeChild(a)
|
||
} finally {
|
||
URL.revokeObjectURL(objectUrl)
|
||
}
|
||
return
|
||
}
|
||
await downloadAttachmentFile(
|
||
{ fileUrl: url, fileName },
|
||
{ fullFileUrl: (u) => attachmentFullUrl(u) }
|
||
)
|
||
},
|
||
update(list: Array<UploadUserFile & { is_string?: boolean; value?: unknown; url?: string }>) {
|
||
const files = list
|
||
.map((v) => (v.is_string ? v.url : (v.value as any) || v.url))
|
||
.filter((url) => url !== undefined)
|
||
this.$emit('update:modelValue', files)
|
||
},
|
||
handleChange(file: UploadFile, list: UploadFile[]) {
|
||
this.$emit('change', file, list)
|
||
if (file.status === 'success') {
|
||
this.update(list as any)
|
||
}
|
||
},
|
||
handleRemove(file: UploadFile, list: UploadFile[]) {
|
||
this.$emit('remove', file, list)
|
||
this.update(list as any)
|
||
},
|
||
removeFile(file: UploadFile) {
|
||
const upload = this.$refs.uploadRef as { handleRemove?: (f: UploadFile) => void } | undefined
|
||
upload?.handleRemove?.(file)
|
||
},
|
||
doHttpRequest(option: UploadRequestOptions) {
|
||
if (typeof this.httpRequest === 'function') {
|
||
return this.httpRequest(option)
|
||
}
|
||
const payload: any = option
|
||
payload.source = 'upload'
|
||
this.formCreateInject?.api?.fetch?.(payload)
|
||
}
|
||
},
|
||
mounted() {
|
||
this.$emit('fc.el', this.$refs.uploadRef)
|
||
}
|
||
})
|
||
</script>
|
||
|
||
<style scoped lang="scss">
|
||
.oa-fc-upload-card {
|
||
width: 100%;
|
||
height: 100%;
|
||
display: flex;
|
||
flex-direction: column;
|
||
align-items: center;
|
||
justify-content: center;
|
||
box-sizing: border-box;
|
||
overflow: hidden;
|
||
|
||
.el-upload-list__item-thumbnail {
|
||
width: 100%;
|
||
height: 100%;
|
||
object-fit: cover;
|
||
display: block;
|
||
}
|
||
|
||
&.is-file {
|
||
padding: 8px 6px;
|
||
background: #f8fafc;
|
||
}
|
||
}
|
||
|
||
.oa-fc-upload-card__icon {
|
||
width: 48px;
|
||
height: 48px;
|
||
border-radius: 10px;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
flex-shrink: 0;
|
||
|
||
&--video {
|
||
background: #ede9fe;
|
||
color: #7c3aed;
|
||
}
|
||
|
||
&--doc {
|
||
background: #e0f2fe;
|
||
color: #0284c7;
|
||
}
|
||
}
|
||
|
||
.oa-fc-upload-card__name {
|
||
margin-top: 6px;
|
||
width: 100%;
|
||
padding: 0 2px;
|
||
font-size: 11px;
|
||
line-height: 1.3;
|
||
color: #334155;
|
||
text-align: center;
|
||
overflow: hidden;
|
||
display: -webkit-box;
|
||
-webkit-box-orient: vertical;
|
||
-webkit-line-clamp: 2;
|
||
word-break: break-all;
|
||
}
|
||
</style>
|