Files
FiDA_Front/src/views/home/components/style.ts

40 lines
1.1 KiB
TypeScript
Raw Normal View History

2026-02-26 13:04:52 +08:00
const sofaImages = import.meta.glob('@/assets/images/sofa/*.png', { eager: true })
const deskImages = import.meta.glob('@/assets/images/desk/*.png', { eager: true })
const chairImages = import.meta.glob('@/assets/images/chair/*.png', { eager: true })
const imagesMaps = {
sofa: sofaImages,
desk: deskImages,
chair: chairImages
}
function slugify(str) {
return str
.toLowerCase()
.trim()
.replace(/['"]/g, '') // 去掉撇号
.replace(/[^a-z0-9\s-]+/g, '') // 只保留字母数字空格-
.replace(/\s+/g, '-') // 空格转 -
.replace(/-+/g, '-')
}
/**
* URL
* @param { 'sofa' | 'desk' | 'chair' } type
* @param { string } style // 必须和 styleList 里的字符串完全一致
* @returns { string | null } <img :src=""> URL
*/
export function getStyleImage(types, style) {
2026-02-26 16:48:08 +08:00
if (!types) types = 'Sofa'
2026-02-26 13:04:52 +08:00
const type = types.toLowerCase()
const map = imagesMaps[type]
2026-02-26 16:48:08 +08:00
2026-02-26 13:04:52 +08:00
const fileName = `${slugify(style)}.png`
const key = `/src/assets/images/${type}/${fileName}`
if (map[key]) {
return map[key].default
}
return ''
}