68 lines
1.9 KiB
TypeScript
68 lines
1.9 KiB
TypeScript
/**
|
||
* @description 图标路径处理工具
|
||
* @author yslg
|
||
* @since 2025-10-27
|
||
*/
|
||
|
||
import { PUBLIC_IMG_PATH } from '@/config';
|
||
|
||
/**
|
||
* 获取图标完整路径
|
||
* @param icon 图标路径或URL
|
||
* @param subPath 子路径(可选,默认为 achievement)
|
||
* @returns 完整的图标URL
|
||
*
|
||
* @example
|
||
* // 仅文件名
|
||
* getIconUrl('v1-icon.svg') // => '/schoolNewsWeb/img/achievement/v1-icon.svg'
|
||
*
|
||
* // 完整相对路径
|
||
* getIconUrl('/img/achievement/v1-icon.svg') // => '/schoolNewsWeb/img/achievement/v1-icon.svg'
|
||
*
|
||
* // 外部URL
|
||
* getIconUrl('https://cdn.com/icon.svg') // => 'https://cdn.com/icon.svg'
|
||
*/
|
||
export function getIconUrl(icon?: string, subPath = 'achievement'): string {
|
||
if (!icon) return '';
|
||
|
||
// 如果是http或https开头,直接返回(外部URL)
|
||
if (icon.startsWith('http://') || icon.startsWith('https://')) {
|
||
return icon;
|
||
}
|
||
|
||
// 如果已经包含完整路径(带 schoolNewsWeb 前缀)
|
||
if (icon.startsWith('/schoolNewsWeb/')) {
|
||
return icon;
|
||
}
|
||
|
||
// 如果包含 /img/ 路径,拼接 schoolNewsWeb 前缀
|
||
if (icon.startsWith('/img/') || icon.startsWith('img/')) {
|
||
const normalizedPath = icon.startsWith('/') ? icon : `/${icon}`;
|
||
return `/schoolNewsWeb${normalizedPath}`;
|
||
}
|
||
|
||
// 否则拼接默认路径
|
||
const fullPath = `${PUBLIC_IMG_PATH}/${subPath}`;
|
||
return icon.startsWith('/') ? `${fullPath}${icon}` : `${fullPath}/${icon}`;
|
||
}
|
||
|
||
/**
|
||
* 获取成就图标URL
|
||
* @param icon 图标路径或文件名
|
||
* @returns 完整的成就图标URL
|
||
*/
|
||
export function getAchievementIconUrl(icon?: string): string {
|
||
return getIconUrl(icon, 'achievement');
|
||
}
|
||
|
||
/**
|
||
* 根据等级获取等级图标URL
|
||
* @param level 等级(1-6)
|
||
* @returns 等级图标URL
|
||
*/
|
||
export function getLevelIconUrl(level = 1): string {
|
||
const validLevel = Math.max(1, Math.min(6, level)); // 限制在1-6之间
|
||
return getIconUrl(`v${validLevel}-icon.svg`, 'achievement');
|
||
}
|
||
|