| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668 |
- const n = `<template>
- <div class="s-file-upload-common" :class="[\`is-\${listType}\`, { 'is-readonly': isReadonly }]">
- <div class="head" v-if="label">
- <div class="label">{{ label }}</div>
- </div>
- <!-- 同步 portal demo PictureUpload.vue:draggable 拖拽排序 + hover 工具条(预览/下载/删除/拖拽) -->
- <draggable
- v-model="fileList"
- @start="dragStart"
- @end="dragEnd"
- handle=".move"
- :class="isCard ? 'img-list-wrap' : 'file-list-wrap'"
- :style="picStyle"
- :disabled="!canDrag"
- item-key="uid"
- >
- <template #item="{ element: item, index }">
- <div
- :key="item.uid || index"
- :class="isCard ? 'img-item-wrap' : 'file-row'"
- @mouseover="handleMouseEnter(item, index)"
- @mouseleave="handleMouseLeave(item, index)"
- >
- <!-- card:图片展示原图,其他类型展示文件类型 -->
- <template v-if="isCard">
- <img
- v-if="isImageItem(item)"
- class="upload-img-wrap"
- :src="fileUrl(item)"
- :alt="fileName(item)"
- />
- <div v-else class="file-thumb">
- <component :is="fileIcon(item)" class="file-thumb-icon" />
- <span class="file-thumb-ext">{{ fileExt(item) || 'FILE' }}</span>
- </div>
- <div v-show="item.showTool" class="tool-wrap">
- <div class="left">
- <EyeOutlined
- v-if="showPreview"
- class="tool-icon-wrap"
- :title="$l('预览')"
- @click.stop="preview(item, index)"
- />
- <DownloadOutlined
- v-if="showDownload"
- class="tool-icon-wrap"
- :title="$l('下载')"
- @click.stop="download(item)"
- />
- <DeleteOutlined
- v-if="canRemove"
- class="tool-icon-wrap"
- :title="$l('删除')"
- @click.stop="handleRemove(index, item)"
- />
- </div>
- <div class="right move">
- <DragOutlined v-if="canDrag" />
- </div>
- </div>
- </template>
- <!-- list:文件类型 + 文件名称 + 预览/下载/删除 -->
- <template v-else>
- <component :is="fileIcon(item)" class="file-row-icon" />
- <span class="file-row-ext">{{ fileExt(item) || 'FILE' }}</span>
- <span class="file-row-name" :title="fileName(item)">{{ fileName(item) }}</span>
- <span class="file-row-actions">
- <EyeOutlined
- v-if="showPreview"
- class="file-action"
- :title="$l('预览')"
- @click.stop="preview(item)"
- />
- <DownloadOutlined
- v-if="showDownload"
- class="file-action"
- :title="$l('下载')"
- @click.stop="download(item)"
- />
- <DeleteOutlined
- v-if="canRemove"
- class="file-action"
- :title="$l('删除')"
- @click.stop="handleRemove(index, item)"
- />
- <DragOutlined v-if="canDrag" class="file-action move" :title="$l('拖动排序')" />
- </span>
- </template>
- </div>
- </template>
- <template #footer>
- <div
- v-if="fileList.length < maxFileCount && isAdd && !isReadonly"
- :class="isCard ? 'img-add-wrap' : 'file-add-wrap'"
- @click="addPic"
- >
- <PlusOutlined :class="isCard ? 'plus-icon' : 'plus-icon plus-icon-small'" />
- <span v-if="!isCard" class="file-add-text">{{ $l('上传文件') }}</span>
- <input
- :accept="accept || 'image/*'"
- :multiple="true"
- type="file"
- :disabled="disabled"
- ref="fileInput"
- style="display: none"
- @change="fileChange"
- />
- </div>
- </template>
- </draggable>
- <s-file-preview v-model:value="previewVisible" :list="fileList" :active="active" />
- </div>
- </template>
- <script setup>
- import { ref, computed, watch } from 'vue'
- import {
- EyeOutlined,
- DeleteOutlined,
- DragOutlined,
- PlusOutlined,
- DownloadOutlined,
- FileOutlined,
- FilePdfOutlined,
- FileWordOutlined,
- FileExcelOutlined,
- FilePptOutlined,
- FileZipOutlined,
- FileTextOutlined,
- FileMarkdownOutlined,
- FileImageOutlined,
- VideoCameraOutlined,
- AudioOutlined
- } from '@ant-design/icons-vue'
- import draggable from 'vuedraggable'
- import SFilePreview from './SFilePreview.vue'
- import { batchFileSaveApi, deleteFileApi } from '../../api/upload'
- const emit = defineEmits(['change', 'handChange', 'dragChange', 'download'])
- const props = defineProps({
- /**
- * 文件列表(同 PictureUpload 的 data)
- * @list
- */
- list: {
- type: Array,
- default: () => {
- return []
- }
- },
- /**
- * 标题
- * @label
- */
- label: {
- type: String,
- default: ''
- },
- /**
- * 只读(同 PictureUpload 的 disabled)
- * @readonly
- */
- readonly: {
- type: Boolean,
- default: false
- },
- /**
- * 详情模式:只读展示,不显示新增入口、不可删除拖拽,仅保留预览与下载
- * @detailMode
- */
- detailMode: {
- type: Boolean,
- default: false
- },
- /**
- * 是否显示新增(isAdd=false 等价 PictureUpload 的 ishiddenAdd=true:隐藏添加框且不可删除/拖拽)
- * @isAdd
- */
- isAdd: {
- type: Boolean,
- default: true
- },
- /**
- * 文件类型
- * @accept
- */
- accept: {
- type: String,
- default: 'image/*'
- },
- /**
- * 展示形态:card 卡片(图片显示原图,其他类型显示文件类型);list 列表(文件类型 + 文件名 + 操作按钮)
- * @listType
- */
- listType: {
- type: String,
- default: 'card'
- },
- /**
- * 是否显示预览按钮
- * @showPreview
- */
- showPreview: {
- type: Boolean,
- default: true
- },
- /**
- * 是否显示下载按钮
- * @showDownload
- */
- showDownload: {
- type: Boolean,
- default: true
- },
- /**
- * 是否显示删除按钮(仍需 isAdd 且非只读)
- * @showRemove
- */
- showRemove: {
- type: Boolean,
- default: true
- },
- // 最多可上传数量(同 PictureUpload 的 maxSize)
- maxFileCount: {
- type: Number,
- default: 5
- },
- // 单文件大小上限,单位 KB(同 PictureUpload 的 fileSize)
- fileSize: {
- type: Number,
- default: 200
- },
- // FTP 上传目录(同 PictureUpload 的 path),不传时回退 businessTableName
- path: {
- type: String,
- default: undefined
- },
- // 是否隐藏拖拽排序图标(同 PictureUpload 的 noDrag)
- noDrag: {
- type: Boolean,
- default: false
- },
- // 图片列表容器样式(同 PictureUpload 的 picStyle)
- picStyle: {
- type: Object,
- default: () => {}
- },
- // 兼容保留:不传 path 时作为 FTP 目录名
- businessTableName: {
- type: String,
- default: 'sapit-ui-plus-components-upload'
- },
- // 业务主键,随上传一起提交用于关联业务数据
- businessKey: {
- type: String,
- default: undefined
- },
- // 兼容保留(PictureUpload 无上传列表概念,仅声明避免外部传参告警)
- showUploadList: {
- type: Boolean,
- default: true
- },
- // 已废弃:由 showRemove 取代,仅保留声明避免外部传参告警
- showRemoveIcon: {
- type: Boolean,
- default: false
- }
- })
- const disabled = computed(() => props.readonly || props.detailMode)
- const isReadonly = computed(() => props.readonly || props.detailMode)
- const isCard = computed(() => props.listType !== 'list')
- const canDrag = computed(() => props.isAdd && !isReadonly.value && !props.noDrag)
- const canRemove = computed(() => props.isAdd && !isReadonly.value && props.showRemove)
- const ftpPath = () => props.path || props.businessTableName
- // --- 文件类型识别(用于 card 的原图/图标切换、list 的图标与扩展名) ---
- const IMAGE_EXTS = ['png', 'jpg', 'jpeg', 'gif', 'webp', 'bmp', 'svg', 'ico', 'avif', 'tif', 'tiff']
- const FILE_ICON_MAP = {
- pdf: FilePdfOutlined,
- doc: FileWordOutlined,
- docx: FileWordOutlined,
- wps: FileWordOutlined,
- rtf: FileWordOutlined,
- xls: FileExcelOutlined,
- xlsx: FileExcelOutlined,
- csv: FileExcelOutlined,
- ppt: FilePptOutlined,
- pptx: FilePptOutlined,
- zip: FileZipOutlined,
- rar: FileZipOutlined,
- '7z': FileZipOutlined,
- gz: FileZipOutlined,
- tar: FileZipOutlined,
- txt: FileTextOutlined,
- log: FileTextOutlined,
- json: FileTextOutlined,
- xml: FileTextOutlined,
- md: FileMarkdownOutlined,
- png: FileImageOutlined,
- jpg: FileImageOutlined,
- jpeg: FileImageOutlined,
- gif: FileImageOutlined,
- webp: FileImageOutlined,
- bmp: FileImageOutlined,
- svg: FileImageOutlined,
- ico: FileImageOutlined,
- mp4: VideoCameraOutlined,
- mov: VideoCameraOutlined,
- avi: VideoCameraOutlined,
- mkv: VideoCameraOutlined,
- webm: VideoCameraOutlined,
- mp3: AudioOutlined,
- wav: AudioOutlined,
- flac: AudioOutlined,
- aac: AudioOutlined,
- m4a: AudioOutlined
- }
- const fileUrl = (item = {}) => item.imageUrl || item.url || item.uid || ''
- // 文件名优先取上传时记录的原始名,回显数据缺失时按 URL 末段兜底
- const nameFromUrl = (url = '') => {
- const clean = String(url).split('?')[0].split('#')[0]
- const base = clean.substring(clean.lastIndexOf('/') + 1)
- try {
- return decodeURIComponent(base)
- } catch (e) {
- return base
- }
- }
- const fileName = (item = {}) => item.name || nameFromUrl(fileUrl(item)) || window.$l('未命名文件')
- const fileExt = (item = {}) => {
- const name = fileName(item)
- const i = name.lastIndexOf('.')
- return i > -1 ? name.slice(i + 1).toLowerCase() : ''
- }
- const isImageItem = (item = {}) => {
- const type = item.type || item.mimeType || ''
- if (type) return String(type).indexOf('image/') === 0
- return IMAGE_EXTS.includes(fileExt(item))
- }
- const fileIcon = (item = {}) => FILE_ICON_MAP[fileExt(item)] || FileOutlined
- // 文件列表:项结构同步 PictureUpload(sort/imageUrl),并兼容基座业务契约(uid/url 供提交与回显预览)
- const fileList = ref([])
- const previewVisible = ref(false)
- const active = ref(0)
- // 监听(同 PictureUpload:immediate + deep,直接替换引用)
- watch(
- () => props.list,
- (newVal) => {
- fileList.value = newVal || []
- },
- { immediate: true, deep: true }
- )
- const emitChange = () => {
- emit('change', fileList.value)
- emit('handChange', fileList.value)
- }
- // 拖拽(同 PictureUpload:结束后按新顺序写回 sort)
- const dragStart = (evt) => {}
- const dragEnd = (evt) => {
- if (fileList.value && fileList.value.length > 1) {
- fileList.value.forEach((f, i) => {
- f.sort = i
- })
- }
- emit('dragChange', fileList.value)
- emitChange()
- }
- const addPic = () => {
- if (fileInput.value) fileInput.value.click()
- }
- const fileInput = ref(null)
- const fileChange = (e) => {
- const files = e.target.files
- if (files && files.length && fileList.value.length + files.length > props.maxFileCount) {
- window.$message.warning(
- window.$l('上传图片,最多可上传') + props.maxFileCount + window.$l('张')
- )
- return
- }
- if (files && files.length) {
- for (let i = 0; i < files.length; i++) {
- if (files[i].size > props.fileSize * 1024) {
- window.$message.warning(\`\${window.$l('上传图片大小不能大于')}\${props.fileSize}KB\`)
- return
- }
- }
- }
- const originFiles = Array.from(files || [])
- batchFileSaveApi(ftpPath(), files)
- .then((res) => {
- if (res && res.length) {
- for (let i = 0; i < res.length; i++) {
- // uid/url 兼容基座业务契约(uid 供提交、url 供旧回显数据预览)
- // name 记录原始文件名,供 list 形态展示
- fileList.value.push({
- sort: i,
- name: (originFiles[i] && originFiles[i].name) || nameFromUrl(res[i].imageUrl),
- imageUrl: res[i].imageUrl,
- uid: res[i].imageUrl,
- url: res[i].imageUrl
- })
- }
- }
- emitChange()
- })
- .finally(() => {
- // 清空 input value,保证连续选择同一文件也能触发 change
- if (fileInput.value) fileInput.value.value = ''
- })
- }
- const handleMouseEnter = (record) => {
- // hover 工具条只用于 card 形态,list 形态按钮常显
- if (!isCard.value) return
- record.showTool = true
- }
- const handleMouseLeave = (record) => {
- if (!isCard.value) return
- record.showTool = false
- }
- const handleRemove = (indexToRemove, record) => {
- fileList.value.splice(indexToRemove, 1)
- // 同步 PictureUpload:删除时同步删除 FTP 文件(imageUrl 为完整 URL)
- deleteFileApi(ftpPath(), record.imageUrl || record.uid)
- emitChange()
- }
- // 预览:图片走弹层大图,其他类型交给浏览器新窗口打开(pdf/txt 等可直接预览)
- const preview = (record = {}) => {
- const url = fileUrl(record)
- if (!url) return
- if (!isImageItem(record)) {
- window.open(url, '_blank', 'noopener')
- return
- }
- // 弹窗按列表下标定位,引用比对失败时按 URL 兜底,避免打开到错误的图
- let idx = fileList.value.findIndex((i) => i === record)
- if (idx < 0) idx = fileList.value.findIndex((i) => fileUrl(i) === url)
- active.value = idx > -1 ? idx : 0
- previewVisible.value = true
- }
- // 下载:先取 blob 保证文件名生效;跨域不可读时退回新窗口打开
- const download = async (record = {}) => {
- const url = fileUrl(record)
- if (!url) return
- emit('download', record)
- const name = fileName(record)
- const saveByHref = (href, useBlobUrl) => {
- const a = document.createElement('a')
- a.href = href
- a.download = name
- if (!useBlobUrl) {
- a.target = '_blank'
- a.rel = 'noopener'
- }
- document.body.appendChild(a)
- a.click()
- document.body.removeChild(a)
- }
- try {
- const res = await fetch(url)
- if (!res.ok) throw new Error('fetch failed')
- const blob = await res.blob()
- const blobUrl = URL.createObjectURL(blob)
- saveByHref(blobUrl, true)
- setTimeout(() => URL.revokeObjectURL(blobUrl), 1000)
- } catch (err) {
- saveByHref(url, false)
- }
- }
- <\/script>
- <style lang="less" scoped>
- .s-file-upload-common {
- .head {
- display: flex;
- justify-content: flex-start;
- align-items: center;
- margin: 10px;
- .label {
- font-weight: 500;
- font-size: 15px;
- margin-right: 30px;
- }
- }
- }
- .plus-icon {
- font-size: 30px;
- color: var(--s-border-color-base);
- }
- .plus-icon-small {
- font-size: 16px;
- }
- .img-list-wrap {
- padding: 10px;
- display: flex;
- justify-content: flex-start;
- align-items: center;
- row-gap: 10px;
- column-gap: 10px;
- flex-wrap: wrap;
- .img-item-wrap {
- position: relative;
- width: 100px;
- height: 100px;
- border-radius: 8px;
- border: 1px solid var(--s-border-color-base);
- padding: 5px;
- box-sizing: border-box;
- .upload-img-wrap {
- width: 90px;
- height: 90px;
- border-radius: 8px;
- object-fit: cover;
- }
- .file-thumb {
- width: 100px;
- height: 100px;
- border-radius: 8px;
- box-sizing: border-box;
- display: flex;
- flex-direction: column;
- justify-content: center;
- align-items: center;
- gap: 6px;
- background: var(--s-color-bg);
- color: var(--s-color);
- .file-thumb-icon {
- font-size: 32px;
- }
- .file-thumb-ext {
- font-size: 12px;
- text-transform: uppercase;
- color: var(--s-text-color-secondary);
- }
- }
- }
- .img-add-wrap {
- cursor: pointer;
- width: 100px;
- height: 100px;
- display: flex;
- justify-content: center;
- align-items: center;
- border: 1px dashed var(--s-border-color-base);
- border-radius: 8px;
- &:hover {
- border-color: var(--s-color);
- }
- }
- .tool-wrap {
- width: 100%;
- height: 30px;
- line-height: 30px;
- cursor: pointer;
- padding: 0 10px;
- box-sizing: border-box;
- background-color: var(--s-mask-color);
- color: var(--s-text-color-inverse);
- transition: 0.45s;
- position: relative;
- bottom: 2rem;
- left: 0;
- z-index: 2;
- display: flex;
- justify-content: space-between;
- align-items: center;
- border-bottom-left-radius: 10px;
- border-bottom-right-radius: 10px;
- .left {
- display: flex;
- align-items: center;
- gap: 8px;
- }
- }
- }
- /* list 形态:文件类型 + 文件名称 + 预览/下载/删除 */
- .file-list-wrap {
- padding: 10px;
- display: flex;
- flex-direction: column;
- row-gap: 8px;
- .file-row {
- display: flex;
- justify-content: space-between;
- align-items: center;
- column-gap: 10px;
- height: 40px;
- padding: 0 12px;
- box-sizing: border-box;
- border: 1px solid var(--s-border-color-base);
- border-radius: 6px;
- transition: background-color 0.2s;
- &:hover {
- background: var(--s-bg-color-hover);
- }
- .file-row-icon {
- font-size: 18px;
- color: var(--s-color);
- }
- .file-row-ext {
- width: 56px;
- height: 20px;
- font-size: 12px;
- display: flex;
- justify-content: flex-start;
- align-items: center;
- text-transform: uppercase;
- color: var(--s-text-color-secondary);
- overflow: hidden;
- text-overflow: ellipsis;
- white-space: nowrap;
- }
- .file-row-name {
- height: 20px;
- display: flex;
- justify-content: flex-start;
- align-items: center;
- flex: 1;
- color: var(--s-text-color);
- overflow: hidden;
- text-overflow: ellipsis;
- white-space: nowrap;
- word-break: break-all;
- }
- .file-row-actions {
- height: 20px;
- display: flex;
- align-items: center;
- column-gap: 12px;
- .file-action {
- font-size: 16px;
- color: var(--s-text-color-secondary);
- cursor: pointer;
- transition: color 0.2s;
- &:hover {
- color: var(--s-color);
- }
- &.move {
- cursor: move;
- }
- }
- }
- }
- .file-add-wrap {
- display: flex;
- align-items: center;
- justify-content: center;
- column-gap: 6px;
- height: 40px;
- cursor: pointer;
- border: 1px dashed var(--s-border-color-base);
- border-radius: 6px;
- color: var(--s-text-color-secondary);
- transition: 0.2s;
- &:hover {
- color: var(--s-color);
- border-color: var(--s-color);
- }
- .file-add-text {
- font-size: 13px;
- }
- }
- }
- .tool-icon-wrap {
- cursor: pointer;
- }
- </style>
- `;
- export {
- n as default
- };
|