import { baseUrl } from "@/config/env" const isAbsoluteUrl = (path) => typeof path === "string" && /^https?:\/\//i.test(path) const getApiBase = () => (baseUrl || "").replace(/\/+$/, "") /** * 去掉 VITE_BASE_URL 多余前缀,支持重复前缀,得到相对路径,如 /profile/2026/... */ export const toRelativeFilePath = (path) => { if (typeof path !== "string" || !path) return "" if (isAbsoluteUrl(path)) return path const prefix = getApiBase() let p = path.startsWith("/") ? path : `/${path}` if (prefix) { const normalizedPrefix = prefix.startsWith("/") ? prefix : `/${prefix}` while (p === normalizedPrefix || p.startsWith(`${normalizedPrefix}/`)) { p = p.slice(normalizedPrefix.length) || "/" if (!p.startsWith("/")) p = `/${p}` } } return p } /** * 拼接为前端可访问路径,如 /profile/2026/06/12/xxx.pdf */ export const toFullFileUrl = (path) => { if (typeof path !== "string" || !path) return "" if (isAbsoluteUrl(path)) return path const prefix = getApiBase() const relative = toRelativeFilePath(path) if (!prefix) return relative return prefix + relative }