// src/utils/imagePreloader.js import { imageCacheManager } from '@/utils/imageCacheManager' export const preloadImages = async (sources) => { let urls = [] // 支持数组和对象形式的图片源 if (Array.isArray(sources)) { urls = sources } else if (typeof sources === 'object') { urls = Object.values(sources) } // 创建预加载 Promise 数组 const preloadPromises = urls.map((url) => { if (imageCacheManager.isBuildAsset(url)) { // 对于可缓存的图片,使用 loadImage 方法 return imageCacheManager.loadImage(url) } else { // 对于外部链接,创建一个 Promise 来加载图片 return new Promise((resolve, reject) => { const img = new Image() img.onload = () => resolve(img) img.onerror = (error) => reject(error) img.src = url }) } }) // 等待所有预加载完成 try { await Promise.all(preloadPromises) console.log('图片预加载完成') } catch (error) { console.error('预加载图片失败:', error) throw error // 重新抛出错误,让调用方可以处理 } }