Files
shanxia2.0/web-admin/src/utils/pl-photo.ts
T
2026-08-08 20:43:48 +08:00

63 lines
1.8 KiB
TypeScript

import { ElMessage } from 'element-plus'
import {
downloadAttachmentFile,
normalizeAttachmentUrls,
openAttachmentPreviewInNewTab
} from '@/utils/download-attachment'
/** 取逗号分隔附件中的第一张 URL(相对或绝对) */
export function firstPhotoUrl(raw?: string | null): string {
const normalized = normalizeAttachmentUrls(raw)
if (!normalized) return ''
return normalized.split(',').map((s) => s.trim()).filter(Boolean)[0] || ''
}
/** 拼 MinIO 完整访问地址(与 AttachmentUploadField 一致) */
export function resolvePhotoDisplayUrl(url?: string | null): string {
const trimmed = String(url || '').trim()
if (!trimmed) return ''
if (/^https?:\/\//i.test(trimmed)) return trimmed
const base = String(import.meta.env.VITE_MINIO_BASE_URL || '').replace(/\/$/, '')
return base ? `${base}/${trimmed.replace(/^\//, '')}` : trimmed
}
function photoFileName(url: string) {
return (
String(url || '')
.split('?')[0]
.split('/')
.pop() || '照片'
)
}
/** 列表照片列:新标签页预览首图 */
export async function previewListPhoto(raw?: string | null) {
const fileUrl = firstPhotoUrl(raw)
if (!fileUrl) {
ElMessage.error('无法获取照片地址')
return
}
await openAttachmentPreviewInNewTab(
{ fileUrl, fileName: photoFileName(fileUrl) },
{ fullFileUrl: resolvePhotoDisplayUrl, kind: 'image' }
)
}
/** 列表照片列:下载首图 */
export async function downloadListPhoto(raw?: string | null) {
const fileUrl = firstPhotoUrl(raw)
if (!fileUrl) {
ElMessage.error('无法获取照片地址')
return
}
try {
await downloadAttachmentFile(
{ fileUrl, fileName: photoFileName(fileUrl) },
{ fullFileUrl: resolvePhotoDisplayUrl }
)
ElMessage.success('下载成功')
} catch {
ElMessage.error('下载失败,请稍后重试')
}
}