fuchengshen
2022-06-20 9d6c67cd2105f09762e619706bcb48613f0aa2ab
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<template>
    <div class="cl-theme">
        <li @click="open">
            <icon-svg :size="18" name="icon-theme" />
        </li>
 
        <!-- 系统设置 -->
        <el-drawer v-model="drawer.visible" title="系统设置" size="300px">
            <div class="cl-theme__container">
                <div class="cl-theme__color is-card">
                    <p>主题</p>
 
                    <ul>
                        <el-tooltip
                            v-for="(item, name) in themeList"
                            :key="name"
                            :content="item.label"
                            placement="top"
                        >
                            <li
                                :style="{
                                    backgroundColor: item.color
                                }"
                                @click="setTheme(item)"
                            >
                                <i
                                    v-show="item.color == form.theme.color"
                                    class="el-icon-check"
                                ></i>
                            </li>
                        </el-tooltip>
                    </ul>
                </div>
 
                <div class="cl-theme__switch is-card">
                    <p>内容区域</p>
 
                    <ul>
                        <li v-if="!browser.isMini">
                            <span>显示一级菜单栏</span>
                            <el-switch
                                v-model="form.conf.showAMenu"
                                size="mini"
                                @change="onAMenuChange"
                            />
                        </li>
                        <li>
                            <span>显示路由导航栏</span>
                            <el-switch v-model="form.conf.showRouteNav" size="mini" />
                        </li>
                        <li>
                            <span>显示页面进程栏</span>
                            <el-switch v-model="form.conf.showProcess" size="mini" />
                        </li>
                    </ul>
                </div>
 
                <div class="cl-theme__append">
                    <slot></slot>
                </div>
 
                <div class="cl-theme__tips" v-if="isDev">
                    <el-alert
                        type="warning"
                        :closable="false"
                        show-icon
                        title="手动修改配置文件可设置为默认主题、布局。"
                    />
 
                    <el-button
                        round
                        type="primary"
                        size="small"
                        style="width: 100%"
                        :disabled="!form.theme.url"
                        @click="openDesc"
                        >修改说明</el-button
                    >
                </div>
            </div>
        </el-drawer>
 
        <!-- 修改说明 -->
        <cl-dialog
            v-model="desc.visible"
            title="修改说明"
            width="800px"
            :props="{
                'append-to-body': true
            }"
        >
            <ul class="cl-theme__desc">
                <li>
                    <p class="cl-theme__desc-label">修改主题色:</p>
                    <cl-codemirror v-model="desc.color" />
                </li>
 
                <li>
                    <p class="cl-theme__desc-label">修改应用配置:</p>
                    <cl-codemirror v-model="desc.conf" />
                </li>
            </ul>
        </cl-dialog>
    </div>
</template>
 
<script>
import { mapGetters } from "vuex";
import { isDev } from "/@/config/env";
import { isArray, cloneDeep } from "/@/cool/utils";
 
export default {
    name: "cl-theme",
 
    props: {
        list: Array
    },
 
    data() {
        return {
            drawer: {
                visible: false
            },
            desc: {
                visible: false,
                color: "",
                conf: ""
            },
            themes: [
                {
                    label: "钴蓝",
                    name: "blue",
                    color: "#4165d7"
                },
                {
                    label: "极黑",
                    name: "black",
                    color: "#2f3447"
                },
                {
                    label: "果绿",
                    name: "green",
                    color: "#51C21A"
                },
                {
                    label: "酱紫",
                    name: "purple",
                    color: "#d0378d"
                }
            ],
            isDev,
            form: {
                name: "",
                conf: {
                    showAMenu: false,
                    showRouteNav: true,
                    showProcess: true
                },
                theme: {
                    color: "",
                    url: ""
                }
            }
        };
    },
 
    computed: {
        ...mapGetters(["app", "modules", "browser"]),
 
        themeList() {
            return isArray(this.list) ? this.list : this.themes;
        }
    },
 
    watch: {
        app: {
            immediate: true,
            handler(val) {
                this.form = cloneDeep(val);
            }
        },
 
        form: {
            deep: true,
            handler(val) {
                this.$store.commit("UPDATE_APP", val);
            }
        }
    },
 
    methods: {
        open() {
            this.drawer.visible = true;
        },
 
        close() {
            this.drawer.visible = false;
        },
 
        onAMenuChange() {
            setTimeout(() => {
                this.$store.commit("SET_MENU_LIST");
            }, 0);
        },
 
        // 设置主题
        setTheme({ name, color, label }) {
            if (this.form.theme.color == color) {
                return false;
            }
 
            this.$message.success(`切换主题:${label}`);
 
            const theme = document.getElementById("theme-style");
            const style = theme || document.createElement("link");
 
            style.href = `${this.modules.theme.options.sourceUrl || "/theme/"}${name}.css`;
 
            if (!theme) {
                style.type = "text/css";
                style.rel = "stylesheet";
                style.id = "theme-style";
 
                document.getElementsByTagName("head").item(0).appendChild(style);
            }
 
            // 设置主题色和路径
            this.form.theme.color = color;
            this.form.theme.url = style.href;
 
            // 设置 css 变量
            document.getElementsByTagName("body")[0].style.setProperty("--color-primary", color);
        },
 
        // 打开修改说明
        openDesc() {
            this.desc.visible = true;
 
            this.desc.color = `
                // src/assets/css/common.scss
                $primary: ${this.form.theme.color};
            `;
 
            this.desc.conf = `
                // src/config/env.js
                export const app = {
                    conf: ${JSON.stringify(this.form.conf)},
                    theme: {
                        url: "${this.form.theme.url}"
                    }
                }
            `;
        }
    }
};
</script>
 
<style lang="scss">
.cl-theme {
    .el-drawer {
        &__header {
            margin-bottom: 20px;
        }
 
        &__body {
            height: calc(100% - 63px);
            padding: 0 5px;
        }
    }
 
    &__container {
        height: 100%;
        overflow: auto;
 
        &::-webkit-scrollbar {
            width: 6px;
        }
 
        &::-webkit-scrollbar-thumb {
            border-radius: 6px;
            background-color: rgba(144, 147, 153, 0.3);
        }
    }
 
    .is-card {
        padding: 20px 0;
        margin: 0 20px 20px 20px;
        border-bottom: 1px solid #f7f7f7;
 
        & > p {
            font-size: 15px;
            font-weight: bold;
            margin-bottom: 10px;
        }
    }
 
    &__switch {
        ul {
            width: 100%;
 
            li {
                display: flex;
                justify-content: space-between;
                align-items: center;
                height: 40px;
                list-style: none;
 
                span {
                    font-size: 13px;
                }
            }
        }
    }
 
    &__color {
        ul {
            display: flex;
            flex-wrap: wrap;
            margin-top: 10px;
 
            li {
                list-style: none;
                height: 20px;
                width: 20px;
                border-radius: 3px;
                margin-right: 10px;
                margin-top: 10px;
                text-align: center;
                color: #fff;
                line-height: 20px;
 
                &:hover {
                    opacity: 0.7;
                }
            }
        }
    }
 
    &__tips {
        padding: 10px 20px;
        margin-bottom: 50px;
 
        .el-button {
            margin-top: 20px;
        }
    }
 
    &__desc {
        padding: 10px;
 
        &-label {
            margin-bottom: 10px;
        }
 
        li {
            list-style: none;
            margin-bottom: 20px;
        }
    }
}
</style>