zrlibs
2025-03-20 b0478092cb738ea038216c347835a14da3768baf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<template>
  <div class="skin-dropdown">
    <Poptip v-model="visible" transfer popper-class="skin">
      <a>
        <slot>
          <span class="icon-text">换肤</span>
        </slot>
        <Icon :size="18" color="#fff" type="md-arrow-dropdown"></Icon>
      </a>
      <template #content>
        <ul class="skin-list">
          <li v-for="skin in skinList" :key="skin.label">
            <Tooltip :content="skin.title">
              <a
                href="javascript:;"
                :style="{ 'background-color': skin.color }"
                :class="{ active: curSkin == skin.label }"
                @click="onClick(skin)"
              ></a>
            </Tooltip>
          </li>
        </ul>
      </template>
    </Poptip>
  </div>
</template>
 
<script>
import { localSave, localRead } from "@/libs/util";
import { showError } from "@/libs/util";
const skinKey = "skin";
export default {
  name: "LayoutSkin",
  data() {
    return {
      visible: false,
      linkId: "skin",
      curSkin: "",
      skinList: [
        {
          title: "浅色",
          label: "light",
          color: "#2db7f5",
        },
        {
          title: "深色",
          label: "dark",
          color: "#404040",
        },
        {
          title: "红色",
          label: "red",
          color: "#cb1b59",
        },
        {
          title: "绿色",
          label: "green",
          color: "#3fa329",
        },
        {
          title: "紫色",
          label: "purple",
          color: "#415aca",
        },
        {
          title: "蓝色",
          label: "blue",
          color: "#138ec7",
        },
      ],
    };
  },
  methods: {
    close() {
      this.visible = false;
    },
    async onClick(skin) {
      if (this.curSkin == skin.label) return this.close();
 
      this.curSkin = skin.label;
      localSave(skinKey, skin.label);
      
      await this.changeSkin(skin);
      this.close();
    },
    async changeSkin(skin) {
      var skinLink = this.getSkinLink();
      var linkUrl = this.getLinkUrl(skin);
 
      if (!skin.label) {
        document.head.removeChild(skinLink);
      }
 
      try {
        let res = await (await fetch(linkUrl)).text();
        skinLink.innerHTML = res || "";
      } catch (ex) {
        this.showError(`加载皮肤文件${linkUrl}失败`);
      }
    },
    getSkinLink() {
      let link = document.querySelector("#" + this.linkId);
      if (!link) {
        link = document.createElement("style");
        link.type = "text/css";
        link.id = this.linkId;
        document.head.appendChild(link);
      }
      return link;
    },
    getLinkUrl(skin) {
      return `${window.skinRoot}/${skin.label}.css`;
    },
    loadSkin(skin) {
      this.curSkin = skin.label;
      this.changeSkin(skin);
    },
    showError(ex) {
      showError(this, ex);
    },
  },
  mounted() {
    let skin;
    const curSkin = localRead(skinKey) || "light";
    if (curSkin) skin = this.skinList.find((l) => l.label == curSkin);
    this.loadSkin(skin);
  },
};
</script>
 
<style lang="less" scoped>
.skin-dropdown {
  margin-right: 10px;
  float: right;
  a {
    color: #fff;
  }
}
</style>
<style lang="less">
.skin {
  .ivu-poptip-body-content {
    overflow: hidden;
  }
  .skin-list {
    list-style: none;
    width: 144px;
    &:after {
      content: "";
      display: table;
      clear: both;
    }
    li {
      margin: 2px;
      width: 20px;
      height: 20px;
      float: left;
      a {
        width: 20px;
        height: 20px;
        display: inline-block;
        &.active {
          box-shadow: 0 0 0 2px rgba(45, 140, 240, 0.2);
        }
      }
    }
  }
}
</style>