58 lines
1.9 KiB
TypeScript
58 lines
1.9 KiB
TypeScript
import { downloadAttachmentFile } from '@/utils/download-attachment'
|
|
|
|
const minioBaseUrl = (import.meta.env.VITE_MINIO_BASE_URL || '').replace(/\/$/, '')
|
|
|
|
/**
|
|
* 附件存储路径 → 浏览器可访问 URL。
|
|
* 兼容:
|
|
* - 完整 http(s) URL
|
|
* - 同域网关路径 `/minio/{bucket}/objectKey`(上传回写常见形态)
|
|
* - 相对 object key,如 `oa-approval/202608/10/xxx.png`
|
|
*
|
|
* 注意:对已带 `/minio/...` 的路径不得再拼一次 `VITE_MINIO_BASE_URL`,否则会变成
|
|
* `http://host/zkxw/minio/zkxw/...` 导致缩略图破图。
|
|
*/
|
|
export function attachmentFullUrl(url: string) {
|
|
const u = String(url || '').trim()
|
|
if (!u) return ''
|
|
if (/^https?:\/\//i.test(u)) return u
|
|
|
|
const base = minioBaseUrl
|
|
|
|
// `/minio/zkxw/oa-approval/...` → 按环境改写或原样返回
|
|
if (u.startsWith('/minio/')) {
|
|
const parts = u.split('/').filter(Boolean) // minio, bucket, ...key
|
|
const objectKey = parts.length >= 3 ? parts.slice(2).join('/') : ''
|
|
if (objectKey && /^https?:\/\//i.test(base)) {
|
|
// 本地开发:VITE_MINIO_BASE_URL=http://host:port/bucket
|
|
return `${base}/${objectKey}`
|
|
}
|
|
// 生产/同域反代:路径本身即可访问
|
|
if (!base || u === base || u.startsWith(`${base}/`)) return u
|
|
if (objectKey) return `${base}/${objectKey}`
|
|
return u
|
|
}
|
|
|
|
if (base && (u === base || u.startsWith(`${base}/`))) return u
|
|
if (!base) return u
|
|
return `${base}/${u.replace(/^\//, '')}`
|
|
}
|
|
|
|
export function useAttachmentActions() {
|
|
function handleAttachmentPreview(file: Record<string, unknown>) {
|
|
const url = attachmentFullUrl(String(file.fileUrl || ''))
|
|
if (url) window.open(url, '_blank')
|
|
}
|
|
|
|
function handleAttachmentDownload(file: Record<string, unknown>) {
|
|
downloadAttachmentFile(file, {
|
|
fullFileUrl: (u: string) => attachmentFullUrl(u)
|
|
})
|
|
}
|
|
|
|
return {
|
|
handleAttachmentPreview,
|
|
handleAttachmentDownload
|
|
}
|
|
}
|