#
Junjie
6 小时以前 a8e8b92a28fd7b48b6eeae2f2f859ba381a39614
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
var app = new Vue({
    el: '#app',
    data: {
        loading: false,
        saving: false,
        items: [],
        predefineColors: [
            '#78FF81',
            '#FA51F6',
            '#C4C400',
            '#30BFFC',
            '#A81DEE',
            '#97B400',
            '#E69138',
            '#B8B8B8',
            '#FF6B6B',
            '#FFD166',
            '#06D6A0',
            '#118AB2'
        ]
    },
    mounted: function () {
        this.reloadData();
    },
    methods: {
        reloadData: function () {
            var that = this;
            this.loading = true;
            $.ajax({
                url: baseUrl + '/watch/stationColor/config/auth',
                headers: { token: localStorage.getItem('token') },
                method: 'GET',
                success: function (res) {
                    that.loading = false;
                    if (res.code === 200) {
                        var items = (res.data && res.data.items) ? res.data.items : [];
                        that.items = items.map(function (item) {
                            return {
                                status: item.status,
                                name: item.name,
                                desc: item.desc,
                                color: that.normalizeColor(item.color || item.defaultColor),
                                defaultColor: that.normalizeColor(item.defaultColor)
                            };
                        });
                    } else if (res.code === 403) {
                        top.location.href = baseUrl + '/';
                    } else {
                        that.$message.error(res.msg || '加载站点颜色配置失败');
                    }
                },
                error: function () {
                    that.loading = false;
                    that.$message.error('加载站点颜色配置失败');
                }
            });
        },
        resetDefaults: function () {
            this.items = this.items.map(function (item) {
                return Object.assign({}, item, {
                    color: item.defaultColor
                });
            });
            this.$message.success('已恢复默认颜色');
        },
        applyDefaultColor: function (item) {
            item.color = item.defaultColor;
        },
        handleColorInput: function (item) {
            var normalized = this.normalizeColor(item.color);
            if (normalized !== String(item.color || '').trim().toUpperCase()) {
                this.$message.warning('颜色格式已自动修正为十六进制');
            }
            item.color = normalized;
        },
        saveConfig: function () {
            var that = this;
            this.saving = true;
            $.ajax({
                url: baseUrl + '/watch/stationColor/config/save/auth',
                headers: { token: localStorage.getItem('token') },
                method: 'POST',
                contentType: 'application/json;charset=UTF-8',
                dataType: 'json',
                data: JSON.stringify({
                    items: this.items.map(function (item) {
                        return {
                            status: item.status,
                            color: that.normalizeColor(item.color)
                        };
                    })
                }),
                success: function (res) {
                    that.saving = false;
                    if (res.code === 200) {
                        that.$message.success('站点颜色配置已保存');
                        that.reloadData();
                    } else if (res.code === 403) {
                        top.location.href = baseUrl + '/';
                    } else {
                        that.$message.error(res.msg || '保存站点颜色配置失败');
                    }
                },
                error: function () {
                    that.saving = false;
                    that.$message.error('保存站点颜色配置失败');
                }
            });
        },
        normalizeColor: function (color) {
            var value = String(color || '').trim();
            if (!value) {
                return '#B8B8B8';
            }
            if (/^#[0-9a-fA-F]{6}$/.test(value)) {
                return value.toUpperCase();
            }
            if (/^#[0-9a-fA-F]{3}$/.test(value)) {
                return ('#' + value.charAt(1) + value.charAt(1) + value.charAt(2) + value.charAt(2) + value.charAt(3) + value.charAt(3)).toUpperCase();
            }
            if (/^0x[0-9a-fA-F]{6}$/.test(value)) {
                return ('#' + value.substring(2)).toUpperCase();
            }
            return '#B8B8B8';
        }
    }
});