import { ref } from "vue"; export type IconItem = { type: 'iconfont'|'image'|'svg'|'none', value: string, rawSvg?: string, fontFamily?: string, }; export type IconMap = Record; //图标集 const iconMap = {} as IconMap; export const IconUtils = { iconCount: ref(0), /** * 设置 Icon 组件的图标名称映射。 * 如果已存在同名数据,则会覆盖之前的。 * * key是图标的名字,value 可以是以下几种情况: * * 是一个 iconfont:name 字符,则会渲染为字体形式的图标,通过 : 分割,前面是字体名称,后面是图标名称。 * * 是一个 `data:***` 或者 http/https URL,则会尝试使用 Image 渲染为图片 * * 是一个 `<svg` 开头的字符串,会渲染为 svg * * 否则,作为默认字体 iconfont 渲染为字体形式的图标。 */ configIconMap(map: Record) { for (const key in map) { let result : IconItem; const v = map[key]; if (v.startsWith('http') || v.startsWith('data:') || v.startsWith('/')) { result = { type: 'image', value: v, } } else if (v.startsWith(' { uni.request({ url: urlOrJson, method: 'GET', success: (res) => { this.configIconMap(typeof res.data === 'string' ? JSON.parse(res.data) : res.data); resolve(); }, fail: (err) => { console.error('加载默认图标失败', err); reject(err); }, }); }); } else { this.configIconMap(JSON.parse(urlOrJson)); } }, } function toDataSvg(str: string) { if (!str.includes('xmlns')) str = str.replace("