mediaUrl.ts
787 Bytes
import { getApiBase } from "@/config/api";
/** 将接口返回的图片路径转为可访问 URL(App 等端相对路径无站点根,需拼 API 域名) */
export function resolveMediaUrl(path: string): string {
const p = typeof path === "string" ? path.trim() : "";
if (!p) {
return "";
}
if (p.startsWith("http://") || p.startsWith("https://")) {
return p;
}
// 包内静态资源,不要拼接口域名
if (p.startsWith("/static/")) {
return p;
}
const base = getApiBase();
if (!base) {
if (p.startsWith("uploads/")) {
return `/${p}`;
}
return p;
}
const root = base.replace(/\/+$/, "");
if (p.startsWith("/")) {
return `${root}${p}`;
}
if (p.startsWith("uploads/")) {
return `${root}/${p}`;
}
return p;
}