Blame view

泰额版/Food Labeling Management App UniApp/src/utils/resolveMediaUrl.ts 1.35 KB
59e51671   “wangming”   1
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
  import { buildApiUrl, getStaticMediaOrigin } from './apiBase'
  
  /** 相对路径拼后端根(含 H5 开发时的静态资源域名);绝对 URL / data URL 原样返回 */
  export function resolveMediaUrlForApp(stored: string | null | undefined): string {
    const s = (stored ?? '').trim()
    if (!s) return ''
    if (s.startsWith('data:')) return s
    if (/^https?:\/\//i.test(s)) return s
    const path = s.startsWith('/') ? s : `/${s}`
    const mediaOrigin = getStaticMediaOrigin()
    if (mediaOrigin) return `${mediaOrigin.replace(/\/$/, '')}${path}`
    return buildApiUrl(path)
  }
  
  /**
   * 判断模板里存的字符串是否应按「图片」加载(含平台上传后的 /picture/ 路径)。
   * 用于 QRCODE 等元素:data 可能是「要编码的 URL 文本」,也可能是「已上传的二维码图」路径。
   */
  export function storedValueLooksLikeImagePath(stored: string | null | undefined): boolean {
    const s = (stored ?? '').trim()
    if (!s) return false
    const lower = s.toLowerCase()
    if (lower.startsWith('data:image/')) return true
    if (lower.startsWith('/picture/') || lower.startsWith('/static/')) return true
    if (/\.(png|jpe?g|gif|webp|bmp)(\?|#|$)/i.test(s)) return true
    if (/^https?:\/\//i.test(s)) {
      if (/\/picture\//i.test(s)) return true
      if (/\.(png|jpe?g|gif|webp|bmp)(\?|#|$)/i.test(s)) return true
      return false
    }
    return false
  }