/** * 静态资源URL工具类 * 用于获取后端提供的静态资源URL */ // 配置基础URL const config = { // 后端API基础URL,从环境变量或配置文件读取 baseUrl: process.env.NODE_ENV === 'development' ? 'http://localhost:20080' : 'https://your-domain.com', // 静态资源基础路径 staticBasePath: '/static/resource/uniapp/' } /** * 获取静态资源的完整URL * @param {string} resourcePath - 资源相对路径,如:'icon-img/home.png' * @returns {string} 完整的资源URL */ export function getStaticResourceUrl(resourcePath) { if (!resourcePath) { console.warn('staticResource: resourcePath is empty') return '' } // 移除开头的 '/' 或 'static/' const cleanPath = resourcePath.replace(/^(\/|static\/)?/, '') return config.baseUrl + config.staticBasePath + cleanPath } /** * 批量获取静态资源URL * @param {Array} resourcePaths - 资源路径数组 * @returns {Object} 路径到URL的映射对象 */ export function getBatchStaticResourceUrls(resourcePaths) { if (!Array.isArray(resourcePaths)) { console.warn('staticResource: resourcePaths must be an array') return {} } const results = {} resourcePaths.forEach(path => { results[path] = getStaticResourceUrl(path) }) return results } /** * 获取图标资源URL的便捷方法 * @param {string} iconName - 图标文件名,如:'home.png' * @returns {string} 图标URL */ export function getIconUrl(iconName) { return getStaticResourceUrl(`icon-img/${iconName}`) } /** * 获取评分图标URL * @param {string} rateName - 评分图标名,如:'none.png' * @returns {string} 评分图标URL */ export function getRateIconUrl(rateName) { return getStaticResourceUrl(`rate/${rateName}`) } /** * 预定义的常用图标URL */ export const COMMON_ICONS = { // 导航图标 HOME: () => getIconUrl('home.png'), HOME_ACTIVE: () => getIconUrl('home-active.png'), PROFILE: () => getIconUrl('profile.png'), PROFILE_ACTIVE: () => getIconUrl('profile-active.png'), // 功能图标 EMPTY: () => getIconUrl('empty.png'), DELETE: () => getIconUrl('delete.png'), LOCATION: () => getIconUrl('ding_wei.png'), NOTICE: () => getIconUrl('notice.png'), WARNING: () => getIconUrl('warn.png'), LOADING: () => getIconUrl('loading_white.png'), // 用户相关 AVATAR: () => getIconUrl('tou.png'), UPLOAD: () => getIconUrl('uploadImg.png'), // 其他 EXPIRED: () => getIconUrl('guoqi.png'), USED: () => getIconUrl('used.png'), WECHAT: () => getIconUrl('weixin.png'), // 评分 RATE_NONE: () => getRateIconUrl('none.png') } /** * 更新配置 * @param {Object} newConfig - 新的配置对象 */ export function updateConfig(newConfig) { Object.assign(config, newConfig) } /** * 获取当前配置 * @returns {Object} 当前配置 */ export function getConfig() { return { ...config } } // 默认导出主要函数 export default { getStaticResourceUrl, getBatchStaticResourceUrls, getIconUrl, getRateIconUrl, COMMON_ICONS, updateConfig, getConfig }