SFileUpload2.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. const n = `<template>
  2. <div class="s-file-upload-common" :class="[\`is-\${listType}\`, { 'is-readonly': isReadonly }]">
  3. <div class="head" v-if="label">
  4. <div class="label">{{ label }}</div>
  5. </div>
  6. <!-- 同步 portal demo PictureUpload.vue:draggable 拖拽排序 + hover 工具条(预览/下载/删除/拖拽) -->
  7. <draggable
  8. v-model="fileList"
  9. @start="dragStart"
  10. @end="dragEnd"
  11. handle=".move"
  12. :class="isCard ? 'img-list-wrap' : 'file-list-wrap'"
  13. :style="picStyle"
  14. :disabled="!canDrag"
  15. item-key="uid"
  16. >
  17. <template #item="{ element: item, index }">
  18. <div
  19. :key="item.uid || index"
  20. :class="isCard ? 'img-item-wrap' : 'file-row'"
  21. @mouseover="handleMouseEnter(item, index)"
  22. @mouseleave="handleMouseLeave(item, index)"
  23. >
  24. <!-- card:图片展示原图,其他类型展示文件类型 -->
  25. <template v-if="isCard">
  26. <img
  27. v-if="isImageItem(item)"
  28. class="upload-img-wrap"
  29. :src="fileUrl(item)"
  30. :alt="fileName(item)"
  31. />
  32. <div v-else class="file-thumb">
  33. <component :is="fileIcon(item)" class="file-thumb-icon" />
  34. <span class="file-thumb-ext">{{ fileExt(item) || 'FILE' }}</span>
  35. </div>
  36. <div v-show="item.showTool" class="tool-wrap">
  37. <div class="left">
  38. <EyeOutlined
  39. v-if="showPreview"
  40. class="tool-icon-wrap"
  41. :title="$l('预览')"
  42. @click.stop="preview(item, index)"
  43. />
  44. <DownloadOutlined
  45. v-if="showDownload"
  46. class="tool-icon-wrap"
  47. :title="$l('下载')"
  48. @click.stop="download(item)"
  49. />
  50. <DeleteOutlined
  51. v-if="canRemove"
  52. class="tool-icon-wrap"
  53. :title="$l('删除')"
  54. @click.stop="handleRemove(index, item)"
  55. />
  56. </div>
  57. <div class="right move">
  58. <DragOutlined v-if="canDrag" />
  59. </div>
  60. </div>
  61. </template>
  62. <!-- list:文件类型 + 文件名称 + 预览/下载/删除 -->
  63. <template v-else>
  64. <component :is="fileIcon(item)" class="file-row-icon" />
  65. <div class="file-row-ext">{{ fileExt(item) || 'FILE' }}</div>
  66. <div class="file-row-name" :title="fileName(item)">{{ fileName(item) }}</div>
  67. <div class="file-row-actions">
  68. <EyeOutlined
  69. v-if="showPreview"
  70. class="file-action"
  71. :title="$l('预览')"
  72. @click.stop="preview(item)"
  73. />
  74. <DownloadOutlined
  75. v-if="showDownload"
  76. class="file-action"
  77. :title="$l('下载')"
  78. @click.stop="download(item)"
  79. />
  80. <DeleteOutlined
  81. v-if="canRemove"
  82. class="file-action"
  83. :title="$l('删除')"
  84. @click.stop="handleRemove(index, item)"
  85. />
  86. <DragOutlined v-if="canDrag" class="file-action move" :title="$l('拖动排序')" />
  87. </div>
  88. </template>
  89. </div>
  90. </template>
  91. <template #footer>
  92. <div
  93. v-if="fileList.length < maxFileCount && isAdd && !isReadonly"
  94. :class="isCard ? 'img-add-wrap' : 'file-add-wrap'"
  95. @click="addPic"
  96. >
  97. <PlusOutlined :class="isCard ? 'plus-icon' : 'plus-icon plus-icon-small'" />
  98. <span v-if="!isCard" class="file-add-text">{{ $l('上传文件') }}</span>
  99. <input
  100. :accept="accept || 'image/*'"
  101. :multiple="true"
  102. type="file"
  103. :disabled="disabled"
  104. ref="fileInput"
  105. style="display: none"
  106. @change="fileChange"
  107. />
  108. </div>
  109. </template>
  110. </draggable>
  111. <s-file-preview v-model:value="previewVisible" :list="fileList" :active="active" />
  112. </div>
  113. </template>
  114. <script setup>
  115. import { ref, computed, watch } from 'vue'
  116. import {
  117. EyeOutlined,
  118. DeleteOutlined,
  119. DragOutlined,
  120. PlusOutlined,
  121. DownloadOutlined,
  122. FileOutlined,
  123. FilePdfOutlined,
  124. FileWordOutlined,
  125. FileExcelOutlined,
  126. FilePptOutlined,
  127. FileZipOutlined,
  128. FileTextOutlined,
  129. FileMarkdownOutlined,
  130. FileImageOutlined,
  131. VideoCameraOutlined,
  132. AudioOutlined
  133. } from '@ant-design/icons-vue'
  134. import draggable from 'vuedraggable'
  135. import SFilePreview from './SFilePreview.vue'
  136. import { batchFileSaveApi, deleteFileApi } from '../../api/upload'
  137. const emit = defineEmits(['change', 'handChange', 'dragChange', 'download'])
  138. const props = defineProps({
  139. /**
  140. * 文件列表(同 PictureUpload 的 data)
  141. * @list
  142. */
  143. list: {
  144. type: Array,
  145. default: () => {
  146. return []
  147. }
  148. },
  149. /**
  150. * 标题
  151. * @label
  152. */
  153. label: {
  154. type: String,
  155. default: ''
  156. },
  157. /**
  158. * 只读(同 PictureUpload 的 disabled)
  159. * @readonly
  160. */
  161. readonly: {
  162. type: Boolean,
  163. default: false
  164. },
  165. /**
  166. * 详情模式:只读展示,不显示新增入口、不可删除拖拽,仅保留预览与下载
  167. * @detailMode
  168. */
  169. detailMode: {
  170. type: Boolean,
  171. default: false
  172. },
  173. /**
  174. * 是否显示新增(isAdd=false 等价 PictureUpload 的 ishiddenAdd=true:隐藏添加框且不可删除/拖拽)
  175. * @isAdd
  176. */
  177. isAdd: {
  178. type: Boolean,
  179. default: true
  180. },
  181. /**
  182. * 文件类型
  183. * @accept
  184. */
  185. accept: {
  186. type: String,
  187. default: 'image/*'
  188. },
  189. /**
  190. * 展示形态:card 卡片(图片显示原图,其他类型显示文件类型);list 列表(文件类型 + 文件名 + 操作按钮)
  191. * @listType
  192. */
  193. listType: {
  194. type: String,
  195. default: 'card'
  196. },
  197. /**
  198. * 是否显示预览按钮
  199. * @showPreview
  200. */
  201. showPreview: {
  202. type: Boolean,
  203. default: true
  204. },
  205. /**
  206. * 是否显示下载按钮
  207. * @showDownload
  208. */
  209. showDownload: {
  210. type: Boolean,
  211. default: true
  212. },
  213. /**
  214. * 是否显示删除按钮(仍需 isAdd 且非只读)
  215. * @showRemove
  216. */
  217. showRemove: {
  218. type: Boolean,
  219. default: true
  220. },
  221. // 最多可上传数量(同 PictureUpload 的 maxSize)
  222. maxFileCount: {
  223. type: Number,
  224. default: 5
  225. },
  226. // 单文件大小上限,单位 KB(同 PictureUpload 的 fileSize)
  227. fileSize: {
  228. type: Number,
  229. default: 200
  230. },
  231. // FTP 上传目录(同 PictureUpload 的 path),不传时回退 businessTableName
  232. path: {
  233. type: String,
  234. default: undefined
  235. },
  236. // 是否隐藏拖拽排序图标(同 PictureUpload 的 noDrag)
  237. noDrag: {
  238. type: Boolean,
  239. default: false
  240. },
  241. // 图片列表容器样式(同 PictureUpload 的 picStyle)
  242. picStyle: {
  243. type: Object,
  244. default: () => {}
  245. },
  246. // 兼容保留:不传 path 时作为 FTP 目录名
  247. businessTableName: {
  248. type: String,
  249. default: 'sapit-ui-plus-components-upload'
  250. },
  251. // 业务主键,随上传一起提交用于关联业务数据
  252. businessKey: {
  253. type: String,
  254. default: undefined
  255. },
  256. // 兼容保留(PictureUpload 无上传列表概念,仅声明避免外部传参告警)
  257. showUploadList: {
  258. type: Boolean,
  259. default: true
  260. },
  261. // 已废弃:由 showRemove 取代,仅保留声明避免外部传参告警
  262. showRemoveIcon: {
  263. type: Boolean,
  264. default: false
  265. }
  266. })
  267. const disabled = computed(() => props.readonly || props.detailMode)
  268. const isReadonly = computed(() => props.readonly || props.detailMode)
  269. const isCard = computed(() => props.listType !== 'list')
  270. const canDrag = computed(() => props.isAdd && !isReadonly.value && !props.noDrag)
  271. const canRemove = computed(() => props.isAdd && !isReadonly.value && props.showRemove)
  272. const ftpPath = () => props.path || props.businessTableName
  273. // --- 文件类型识别(用于 card 的原图/图标切换、list 的图标与扩展名) ---
  274. const IMAGE_EXTS = ['png', 'jpg', 'jpeg', 'gif', 'webp', 'bmp', 'svg', 'ico', 'avif', 'tif', 'tiff']
  275. const FILE_ICON_MAP = {
  276. pdf: FilePdfOutlined,
  277. doc: FileWordOutlined,
  278. docx: FileWordOutlined,
  279. wps: FileWordOutlined,
  280. rtf: FileWordOutlined,
  281. xls: FileExcelOutlined,
  282. xlsx: FileExcelOutlined,
  283. csv: FileExcelOutlined,
  284. ppt: FilePptOutlined,
  285. pptx: FilePptOutlined,
  286. zip: FileZipOutlined,
  287. rar: FileZipOutlined,
  288. '7z': FileZipOutlined,
  289. gz: FileZipOutlined,
  290. tar: FileZipOutlined,
  291. txt: FileTextOutlined,
  292. log: FileTextOutlined,
  293. json: FileTextOutlined,
  294. xml: FileTextOutlined,
  295. md: FileMarkdownOutlined,
  296. png: FileImageOutlined,
  297. jpg: FileImageOutlined,
  298. jpeg: FileImageOutlined,
  299. gif: FileImageOutlined,
  300. webp: FileImageOutlined,
  301. bmp: FileImageOutlined,
  302. svg: FileImageOutlined,
  303. ico: FileImageOutlined,
  304. mp4: VideoCameraOutlined,
  305. mov: VideoCameraOutlined,
  306. avi: VideoCameraOutlined,
  307. mkv: VideoCameraOutlined,
  308. webm: VideoCameraOutlined,
  309. mp3: AudioOutlined,
  310. wav: AudioOutlined,
  311. flac: AudioOutlined,
  312. aac: AudioOutlined,
  313. m4a: AudioOutlined
  314. }
  315. const fileUrl = (item = {}) => item.imageUrl || item.url || item.uid || ''
  316. // 文件名优先取上传时记录的原始名,回显数据缺失时按 URL 末段兜底
  317. const nameFromUrl = (url = '') => {
  318. const clean = String(url).split('?')[0].split('#')[0]
  319. const base = clean.substring(clean.lastIndexOf('/') + 1)
  320. try {
  321. return decodeURIComponent(base)
  322. } catch (e) {
  323. return base
  324. }
  325. }
  326. const fileName = (item = {}) => item.name || nameFromUrl(fileUrl(item)) || window.$l('未命名文件')
  327. const fileExt = (item = {}) => {
  328. const name = fileName(item)
  329. const i = name.lastIndexOf('.')
  330. return i > -1 ? name.slice(i + 1).toLowerCase() : ''
  331. }
  332. const isImageItem = (item = {}) => {
  333. const type = item.type || item.mimeType || ''
  334. if (type) return String(type).indexOf('image/') === 0
  335. return IMAGE_EXTS.includes(fileExt(item))
  336. }
  337. const fileIcon = (item = {}) => FILE_ICON_MAP[fileExt(item)] || FileOutlined
  338. // 文件列表:项结构同步 PictureUpload(sort/imageUrl),并兼容基座业务契约(uid/url 供提交与回显预览)
  339. const fileList = ref([])
  340. const previewVisible = ref(false)
  341. const active = ref(0)
  342. // 监听(同 PictureUpload:immediate + deep,直接替换引用)
  343. watch(
  344. () => props.list,
  345. (newVal) => {
  346. fileList.value = newVal || []
  347. },
  348. { immediate: true, deep: true }
  349. )
  350. const emitChange = () => {
  351. emit('change', fileList.value)
  352. emit('handChange', fileList.value)
  353. }
  354. // 拖拽(同 PictureUpload:结束后按新顺序写回 sort)
  355. const dragStart = (evt) => {}
  356. const dragEnd = (evt) => {
  357. if (fileList.value && fileList.value.length > 1) {
  358. fileList.value.forEach((f, i) => {
  359. f.sort = i
  360. })
  361. }
  362. emit('dragChange', fileList.value)
  363. emitChange()
  364. }
  365. const addPic = () => {
  366. if (fileInput.value) fileInput.value.click()
  367. }
  368. const fileInput = ref(null)
  369. const fileChange = (e) => {
  370. const files = e.target.files
  371. if (files && files.length && fileList.value.length + files.length > props.maxFileCount) {
  372. window.$message.warning(
  373. window.$l('上传图片,最多可上传') + props.maxFileCount + window.$l('张')
  374. )
  375. return
  376. }
  377. if (files && files.length) {
  378. for (let i = 0; i < files.length; i++) {
  379. if (files[i].size > props.fileSize * 1024) {
  380. window.$message.warning(\`\${window.$l('上传图片大小不能大于')}\${props.fileSize}KB\`)
  381. return
  382. }
  383. }
  384. }
  385. const originFiles = Array.from(files || [])
  386. batchFileSaveApi(ftpPath(), files)
  387. .then((res) => {
  388. if (res && res.length) {
  389. for (let i = 0; i < res.length; i++) {
  390. // uid/url 兼容基座业务契约(uid 供提交、url 供旧回显数据预览)
  391. // name 记录原始文件名,供 list 形态展示
  392. fileList.value.push({
  393. sort: i,
  394. name: (originFiles[i] && originFiles[i].name) || nameFromUrl(res[i].imageUrl),
  395. imageUrl: res[i].imageUrl,
  396. uid: res[i].imageUrl,
  397. url: res[i].imageUrl
  398. })
  399. }
  400. }
  401. emitChange()
  402. })
  403. .finally(() => {
  404. // 清空 input value,保证连续选择同一文件也能触发 change
  405. if (fileInput.value) fileInput.value.value = ''
  406. })
  407. }
  408. const handleMouseEnter = (record) => {
  409. // hover 工具条只用于 card 形态,list 形态按钮常显
  410. if (!isCard.value) return
  411. record.showTool = true
  412. }
  413. const handleMouseLeave = (record) => {
  414. if (!isCard.value) return
  415. record.showTool = false
  416. }
  417. const handleRemove = (indexToRemove, record) => {
  418. fileList.value.splice(indexToRemove, 1)
  419. // 同步 PictureUpload:删除时同步删除 FTP 文件(imageUrl 为完整 URL)
  420. deleteFileApi(ftpPath(), record.imageUrl || record.uid)
  421. emitChange()
  422. }
  423. // 预览:图片走弹层大图,其他类型交给浏览器新窗口打开(pdf/txt 等可直接预览)
  424. const preview = (record = {}) => {
  425. const url = fileUrl(record)
  426. if (!url) return
  427. if (!isImageItem(record)) {
  428. window.open(url, '_blank', 'noopener')
  429. return
  430. }
  431. // 弹窗按列表下标定位,引用比对失败时按 URL 兜底,避免打开到错误的图
  432. let idx = fileList.value.findIndex((i) => i === record)
  433. if (idx < 0) idx = fileList.value.findIndex((i) => fileUrl(i) === url)
  434. active.value = idx > -1 ? idx : 0
  435. previewVisible.value = true
  436. }
  437. // 下载:先取 blob 保证文件名生效;跨域不可读时退回新窗口打开
  438. const download = async (record = {}) => {
  439. const url = fileUrl(record)
  440. if (!url) return
  441. emit('download', record)
  442. const name = fileName(record)
  443. const saveByHref = (href, useBlobUrl) => {
  444. const a = document.createElement('a')
  445. a.href = href
  446. a.download = name
  447. if (!useBlobUrl) {
  448. a.target = '_blank'
  449. a.rel = 'noopener'
  450. }
  451. document.body.appendChild(a)
  452. a.click()
  453. document.body.removeChild(a)
  454. }
  455. try {
  456. const res = await fetch(url)
  457. if (!res.ok) throw new Error('fetch failed')
  458. const blob = await res.blob()
  459. const blobUrl = URL.createObjectURL(blob)
  460. saveByHref(blobUrl, true)
  461. setTimeout(() => URL.revokeObjectURL(blobUrl), 1000)
  462. } catch (err) {
  463. saveByHref(url, false)
  464. }
  465. }
  466. <\/script>
  467. <style lang="less" scoped>
  468. .s-file-upload-common {
  469. .head {
  470. display: flex;
  471. justify-content: flex-start;
  472. align-items: center;
  473. margin: 10px;
  474. .label {
  475. font-weight: 500;
  476. font-size: 15px;
  477. margin-right: 30px;
  478. }
  479. }
  480. }
  481. .plus-icon {
  482. font-size: 30px;
  483. color: var(--s-border-color-base);
  484. }
  485. .plus-icon-small {
  486. font-size: 16px;
  487. }
  488. .img-list-wrap {
  489. padding: 10px;
  490. display: flex;
  491. justify-content: flex-start;
  492. align-items: center;
  493. row-gap: 10px;
  494. column-gap: 10px;
  495. flex-wrap: wrap;
  496. .img-item-wrap {
  497. position: relative;
  498. width: 100px;
  499. height: 100px;
  500. border-radius: 8px;
  501. border: 1px solid var(--s-border-color-base);
  502. padding: 5px;
  503. box-sizing: border-box;
  504. .upload-img-wrap {
  505. width: 90px;
  506. height: 90px;
  507. border-radius: 8px;
  508. object-fit: cover;
  509. }
  510. .file-thumb {
  511. width: 100px;
  512. height: 100px;
  513. border-radius: 8px;
  514. box-sizing: border-box;
  515. display: flex;
  516. flex-direction: column;
  517. justify-content: center;
  518. align-items: center;
  519. gap: 6px;
  520. background: var(--s-color-bg);
  521. color: var(--s-color);
  522. .file-thumb-icon {
  523. font-size: 32px;
  524. }
  525. .file-thumb-ext {
  526. font-size: 12px;
  527. text-transform: uppercase;
  528. color: var(--s-text-color-secondary);
  529. }
  530. }
  531. }
  532. .img-add-wrap {
  533. cursor: pointer;
  534. width: 100px;
  535. height: 100px;
  536. display: flex;
  537. justify-content: center;
  538. align-items: center;
  539. border: 1px dashed var(--s-border-color-base);
  540. border-radius: 8px;
  541. &:hover {
  542. border-color: var(--s-color);
  543. }
  544. }
  545. .tool-wrap {
  546. width: 100%;
  547. height: 30px;
  548. line-height: 30px;
  549. cursor: pointer;
  550. padding: 0 10px;
  551. box-sizing: border-box;
  552. background-color: var(--s-mask-color);
  553. color: var(--s-text-color-inverse);
  554. transition: 0.45s;
  555. position: relative;
  556. bottom: 2rem;
  557. left: 0;
  558. z-index: 2;
  559. display: flex;
  560. justify-content: space-between;
  561. align-items: center;
  562. border-bottom-left-radius: 10px;
  563. border-bottom-right-radius: 10px;
  564. .left {
  565. display: flex;
  566. align-items: center;
  567. gap: 8px;
  568. }
  569. }
  570. }
  571. /* list 形态:文件类型 + 文件名称 + 预览/下载/删除 */
  572. .file-list-wrap {
  573. padding: 10px;
  574. display: flex;
  575. flex-direction: column;
  576. row-gap: 8px;
  577. .file-row {
  578. display: flex;
  579. justify-content: space-between;
  580. align-items: center;
  581. column-gap: 10px;
  582. height: 40px;
  583. padding: 0 12px;
  584. box-sizing: border-box;
  585. border: 1px solid var(--s-border-color-base);
  586. border-radius: 6px;
  587. transition: background-color 0.2s;
  588. &:hover {
  589. background: var(--s-bg-color-hover);
  590. }
  591. .file-row-icon {
  592. font-size: 18px;
  593. color: var(--s-color);
  594. }
  595. .file-row-ext {
  596. width: 56px;
  597. height: 20px;
  598. font-size: 12px;
  599. display: flex;
  600. justify-content: flex-start;
  601. align-items: center;
  602. text-transform: uppercase;
  603. color: var(--s-text-color-secondary);
  604. overflow: hidden;
  605. text-overflow: ellipsis;
  606. white-space: nowrap;
  607. }
  608. .file-row-name {
  609. height: 20px;
  610. display: flex;
  611. justify-content: flex-start;
  612. align-items: center;
  613. flex: 1;
  614. color: var(--s-text-color);
  615. overflow: hidden;
  616. text-overflow: ellipsis;
  617. white-space: nowrap;
  618. word-break: break-all;
  619. }
  620. .file-row-actions {
  621. height: 20px;
  622. display: flex;
  623. align-items: center;
  624. column-gap: 12px;
  625. .file-action {
  626. font-size: 16px;
  627. color: var(--s-text-color-secondary);
  628. cursor: pointer;
  629. transition: color 0.2s;
  630. &:hover {
  631. color: var(--s-color);
  632. }
  633. &.move {
  634. cursor: move;
  635. }
  636. }
  637. }
  638. }
  639. .file-add-wrap {
  640. display: flex;
  641. align-items: center;
  642. justify-content: center;
  643. column-gap: 6px;
  644. height: 40px;
  645. cursor: pointer;
  646. border: 1px dashed var(--s-border-color-base);
  647. border-radius: 6px;
  648. color: var(--s-text-color-secondary);
  649. transition: 0.2s;
  650. &:hover {
  651. color: var(--s-color);
  652. border-color: var(--s-color);
  653. }
  654. .file-add-text {
  655. font-size: 13px;
  656. }
  657. }
  658. }
  659. .tool-icon-wrap {
  660. cursor: pointer;
  661. }
  662. </style>
  663. `;
  664. export {
  665. n as default
  666. };