Files
hms/apps/web/src/utils/media.ts
iven a6ec8129c9 refactor(web,health): 消除硬编码路径 — 统一 resolveMediaUrl + 动态 base_url
1. 新增 resolveMediaUrl() 工具函数,统一处理 storage_path 前缀和 JWT token
2. MediaLibrary 和 MediaPicker 改用 resolveMediaUrl,消除重复逻辑
3. banner_handler 不再硬编码 localhost:3000,改为从 Host header 动态构建 base_url
2026-05-10 20:00:39 +08:00

15 lines
596 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 将后端返回的 storage_path / thumbnail_path 转换为可访问的前端 URL。
*
* 后端存储路径格式: "./uploads/{tenant_id}/{filename}" 或 "/uploads/..."
* 前端统一使用相对路径 "/uploads/...",由 Vitedev或 nginxprod代理到后端。
*
* 如需认证,自动附加 ?token= 参数。
*/
export function resolveMediaUrl(rawPath: string | null | undefined): string {
if (!rawPath) return '';
const base = rawPath.replace(/^\.\//, '/');
const token = localStorage.getItem('access_token');
return token ? `${base}?token=${token}` : base;
}