Blame view

稻城亚丁小程序/uniapp/src/utils/mediaUrl.ts 787 Bytes
bc518174   王天杨   提交两个项目文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  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;
  }