feat: 前端基础设施更新 - API模块、路由、状态管理和工具类
- 新增 address/admin/favorite/review API 模块 - 更新已有 API 模块适配后端接口变更 - 新增 admin 类型定义和工具函数 - 添加静态资源文件 - 更新路由配置和守卫逻辑 - 更新 Vite 配置和依赖锁文件
This commit is contained in:
42
flash-sale-frontend/src/utils/image.ts
Normal file
42
flash-sale-frontend/src/utils/image.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import defaultProductImage from '@/assets/default-product.svg'
|
||||
|
||||
export const DEFAULT_PRODUCT_IMAGE = defaultProductImage
|
||||
|
||||
const ABSOLUTE_URL_PATTERN = /^(https?:)?\/\//i
|
||||
const SPECIAL_URL_PATTERN = /^(data:|blob:)/i
|
||||
|
||||
const normalizeBaseUrl = (value?: string) => {
|
||||
if (!value) return ''
|
||||
return value.endsWith('/') ? value.slice(0, -1) : value
|
||||
}
|
||||
|
||||
export const resolveImageUrl = (value?: string | null) => {
|
||||
if (!value || !String(value).trim()) {
|
||||
return DEFAULT_PRODUCT_IMAGE
|
||||
}
|
||||
|
||||
const imageUrl = String(value).trim()
|
||||
if (ABSOLUTE_URL_PATTERN.test(imageUrl) || SPECIAL_URL_PATTERN.test(imageUrl)) {
|
||||
return imageUrl
|
||||
}
|
||||
|
||||
const baseUrl = normalizeBaseUrl(import.meta.env.VITE_API_BASE_URL)
|
||||
if (!baseUrl) {
|
||||
return imageUrl.startsWith('/') ? imageUrl : `/${imageUrl}`
|
||||
}
|
||||
|
||||
return imageUrl.startsWith('/') ? `${baseUrl}${imageUrl}` : `${baseUrl}/${imageUrl}`
|
||||
}
|
||||
|
||||
export const applyFallbackImage = (event: Event) => {
|
||||
const target = event.target as HTMLImageElement | null
|
||||
if (!target) return
|
||||
|
||||
if (target.dataset.fallbackApplied === 'true') {
|
||||
return
|
||||
}
|
||||
|
||||
target.dataset.fallbackApplied = 'true'
|
||||
target.onerror = null
|
||||
target.src = DEFAULT_PRODUCT_IMAGE
|
||||
}
|
||||
Reference in New Issue
Block a user