You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.8 KiB
JavaScript

// 导出获取图片URL的函数
export const getImageUrl = (path) => {
// 如果path不存在或不是字符串直接返回空字符串
if (!path || typeof path !== 'string') {
console.error('getImageUrl: path 参数无效或为空', path);
return '';
}
// 如果是绝对URLhttp://或https://开头),直接返回
if (path.startsWith('http://') || path.startsWith('https://')) {
return path;
}
// 否则处理为本地资源
// 使用Vite支持的动态导入方式
try {
// 根据不同的图片路径返回对应的导入URL
// 注意:这种方式需要为每个图片路径单独添加映射
// 您可以根据实际需要扩展这个映射表
const imageMap = {
'img/honor1.png': new URL('../assets/img/honor1.png', import.meta.url).href,
'img/honor2.png': new URL('../assets/img/honor2.png', import.meta.url).href,
'img/honor3.png': new URL('../assets/img/honor3.png', import.meta.url).href,
'img/dynamic1.png': new URL('../assets/img/dynamic1.png', import.meta.url).href,
'img/dynamic2.png': new URL('../assets/img/dynamic2.png', import.meta.url).href,
// 可以根据需要添加更多图片路径映射
};
// 如果找到对应的映射返回映射的URL
if (imageMap[path]) {
return imageMap[path];
} else {
// 如果没有找到映射,尝试使用默认方式
console.warn('图片路径未在映射表中找到:', path);
return new URL(`../assets/${path}`, import.meta.url).href;
}
} catch (error) {
// 如果构建失败,打印错误信息并返回空字符串
console.error('图片加载失败:', error, '路径:', path);
return '';
}
}