pl-photo.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { ElMessage } from 'element-plus'
  2. import {
  3. downloadAttachmentFile,
  4. normalizeAttachmentUrls,
  5. openAttachmentPreviewInNewTab
  6. } from '@/utils/download-attachment'
  7. /** 取逗号分隔附件中的第一张 URL(相对或绝对) */
  8. export function firstPhotoUrl(raw?: string | null): string {
  9. const normalized = normalizeAttachmentUrls(raw)
  10. if (!normalized) return ''
  11. return normalized.split(',').map((s) => s.trim()).filter(Boolean)[0] || ''
  12. }
  13. /** 拼 MinIO 完整访问地址(与 AttachmentUploadField 一致) */
  14. export function resolvePhotoDisplayUrl(url?: string | null): string {
  15. const trimmed = String(url || '').trim()
  16. if (!trimmed) return ''
  17. if (/^https?:\/\//i.test(trimmed)) return trimmed
  18. const base = String(import.meta.env.VITE_MINIO_BASE_URL || '').replace(/\/$/, '')
  19. return base ? `${base}/${trimmed.replace(/^\//, '')}` : trimmed
  20. }
  21. function photoFileName(url: string) {
  22. return (
  23. String(url || '')
  24. .split('?')[0]
  25. .split('/')
  26. .pop() || '照片'
  27. )
  28. }
  29. /** 列表照片列:新标签页预览首图 */
  30. export async function previewListPhoto(raw?: string | null) {
  31. const fileUrl = firstPhotoUrl(raw)
  32. if (!fileUrl) {
  33. ElMessage.error('无法获取照片地址')
  34. return
  35. }
  36. await openAttachmentPreviewInNewTab(
  37. { fileUrl, fileName: photoFileName(fileUrl) },
  38. { fullFileUrl: resolvePhotoDisplayUrl, kind: 'image' }
  39. )
  40. }
  41. /** 列表照片列:下载首图 */
  42. export async function downloadListPhoto(raw?: string | null) {
  43. const fileUrl = firstPhotoUrl(raw)
  44. if (!fileUrl) {
  45. ElMessage.error('无法获取照片地址')
  46. return
  47. }
  48. try {
  49. await downloadAttachmentFile(
  50. { fileUrl, fileName: photoFileName(fileUrl) },
  51. { fullFileUrl: resolvePhotoDisplayUrl }
  52. )
  53. ElMessage.success('下载成功')
  54. } catch {
  55. ElMessage.error('下载失败,请稍后重试')
  56. }
  57. }