仰望星辰工作室

starmusic-mobile

starmusic-mobile/ components/lx-icon/lx-icon.vue 2.0 KB · 77 行 原始文件
1<template>
2 <image
3 class="lx-icon"
4 :src="iconSrc"
5 :style="{ width: size + 'rpx', height: size + 'rpx' }"
6 mode="aspectFit"
7 />
8</template>
9
10<script>
11/**
12 * 通用 SVG 图标组件
13 * App 端原生渲染不支持内联 <svg> 标签,因此改用 <image> 加载
14 * 预生成的静态 SVG 文件(static/icons/{name}-{color}.svg),颜色通过文件名映射。
15 * 新增颜色时需同步 static/icons 下的文件(可运行 scripts/gen-icon-svgs.js 重新生成)。
16 * color="theme" 表示跟随当前主题色(文件为 {name}-theme-{主题id}.svg)。
17 * 用法:<lx-icon name="play" :size="48" color="theme" />
18 */
19import { getThemeIconKey } from '../../common/theme.js'
20import event from '../../common/event.js'
21
22const COLOR_MAP = {
23 '#ffffff': 'white',
24 '#fff': 'white',
25 '#303133': 'dark',
26 '#606266': 'gray',
27 '#909399': 'gray2',
28 '#c0c4cc': 'light',
29 '#31c27c': 'green',
30 '#67c23a': 'ok',
31 '#f56c6c': 'fail',
32 '#ff4d4f': 'red',
33}
34
35export default {
36 name: 'LxIcon',
37 props: {
38 name: { type: String, default: '' },
39 size: { type: Number, default: 48 },
40 color: { type: String, default: '#303133' },
41 },
42 data() {
43 return {
44 // 当前主题对应的图标后缀(color="theme" 时使用)
45 themeIconKey: getThemeIconKey(),
46 }
47 },
48 created() {
49 this._offIconTheme = event.on('settingUpdated', (keys) => {
50 if (!keys || keys.indexOf('common.theme') >= 0) this.themeIconKey = getThemeIconKey()
51 })
52 },
53 beforeUnmount() {
54 if (this._offIconTheme) {
55 this._offIconTheme()
56 this._offIconTheme = null
57 }
58 },
59 computed: {
60 iconSrc() {
61 const color = (this.color || '').toLowerCase()
62 if (color === 'theme') {
63 return `/static/icons/${this.name || 'play'}-${this.themeIconKey}.svg`
64 }
65 const colorKey = COLOR_MAP[color] || 'dark'
66 return `/static/icons/${this.name || 'play'}-${colorKey}.svg`
67 },
68 },
69}
70</script>
71
72<style lang="scss" scoped>
73.lx-icon {
74 display: inline-block;
75 flex-shrink: 0;
76}
77</style>