Blame view

稻城亚丁小程序/uniapp/src/utils/staticAsset.ts 881 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
32
33
34
35
  import { getApiBase } from "@/config/api";
  
  function normalizeBase(url: string): string {
    return url.trim().replace(/\/+$/, "");
  }
  
  function getStaticBase(): string {
    const fromEnv = typeof import.meta.env.VITE_STATIC_BASE_URL === "string"
      ? normalizeBase(import.meta.env.VITE_STATIC_BASE_URL)
      : "";
    if (fromEnv) {
      return fromEnv;
    }
    const apiBase = normalizeBase(getApiBase());
    return apiBase ? `${apiBase}/assets/map` : "";
  }
  
  /**
   * 将原先 /static/xxx 映射到后端静态目录:
   * https://domain/assets/map/xxx
   */
  export function staticAsset(input: string): string {
    const p = (input || "").trim();
    if (!p) {
      return "";
    }
    if (/^https?:\/\//i.test(p)) {
      return p;
    }
    const file = p
      .replace(/^\/+/, "")
      .replace(/^static\//, "");
    const base = getStaticBase();
    return base ? `${base}/${file}` : `/static/${file}`;
  }