Junjie
16 小时以前 8bfe1168a42d4e3750a15b0c0fb0a7629d6cf91c
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
(function () {
    new Vue({
        el: "#app",
        data: function () {
            return {
                loading: false,
                saving: false,
                running: false,
                tableOptions: [],
                result: null,
                form: {
                    expireDays: 180,
                    mode: "all",
                    tables: []
                }
            };
        },
        computed: {
            resultDetails: function () {
                var vm = this;
                if (!vm.result || !vm.result.detail) {
                    return [];
                }
                return Object.keys(vm.result.detail).map(function (table) {
                    return {
                        table: table,
                        label: vm.resolveTableLabel(table),
                        count: vm.result.detail[table]
                    };
                });
            }
        },
        created: function () {
            this.loadConfig();
        },
        methods: {
            authHeaders: function () {
                return { token: localStorage.getItem("token") };
            },
            handleForbidden: function (res) {
                if (res && Number(res.code) === 403) {
                    top.location.href = baseUrl + "/";
                    return true;
                }
                return false;
            },
            resolveTableLabel: function (table) {
                var index;
                for (index = 0; index < this.tableOptions.length; index += 1) {
                    if (this.tableOptions[index].value === table) {
                        return this.tableOptions[index].label;
                    }
                }
                return table;
            },
            loadConfig: function () {
                var vm = this;
                vm.loading = true;
                $.ajax({
                    url: baseUrl + "/logCleanup/config/auth",
                    method: "GET",
                    headers: vm.authHeaders(),
                    success: function (res) {
                        var tables;
                        vm.loading = false;
                        if (vm.handleForbidden(res)) {
                            return;
                        }
                        if (!res || res.code !== 200) {
                            vm.$message.error((res && res.msg) ? res.msg : "加载日志清理配置失败");
                            return;
                        }
                        vm.form.expireDays = Number((res.data && res.data.expireDays) || 180);
                        tables = (res.data && res.data.tables) || {};
                        vm.tableOptions = Object.keys(tables).map(function (key) {
                            return {
                                value: key,
                                label: tables[key]
                            };
                        });
                    },
                    error: function () {
                        vm.loading = false;
                        vm.$message.error("加载日志清理配置失败");
                    }
                });
            },
            saveConfig: function () {
                var vm = this;
                if (!vm.form.expireDays || Number(vm.form.expireDays) <= 0) {
                    vm.$message.warning("日志保留天数必须大于0");
                    return;
                }
                vm.saving = true;
                $.ajax({
                    url: baseUrl + "/logCleanup/config/save/auth",
                    method: "POST",
                    contentType: "application/json;charset=UTF-8",
                    headers: vm.authHeaders(),
                    data: JSON.stringify({
                        expireDays: Number(vm.form.expireDays)
                    }),
                    success: function (res) {
                        vm.saving = false;
                        if (vm.handleForbidden(res)) {
                            return;
                        }
                        if (!res || res.code !== 200) {
                            vm.$message.error((res && res.msg) ? res.msg : "保存日志清理配置失败");
                            return;
                        }
                        vm.$message.success("保存成功");
                    },
                    error: function () {
                        vm.saving = false;
                        vm.$message.error("保存日志清理配置失败");
                    }
                });
            },
            runCleanup: function () {
                var vm = this;
                var requestBody;
                if (vm.form.mode === "selected" && (!vm.form.tables || !vm.form.tables.length)) {
                    vm.$message.warning("请选择至少一张日志表");
                    return;
                }
                requestBody = {
                    mode: vm.form.mode,
                    tables: vm.form.mode === "selected" ? vm.form.tables : []
                };
                vm.$confirm("确定立即执行日志清理吗?", "提示", {
                    type: "warning",
                    confirmButtonText: "确定",
                    cancelButtonText: "取消"
                }).then(function () {
                    vm.running = true;
                    $.ajax({
                        url: baseUrl + "/logCleanup/run/auth",
                        method: "POST",
                        contentType: "application/json;charset=UTF-8",
                        headers: vm.authHeaders(),
                        data: JSON.stringify(requestBody),
                        success: function (res) {
                            vm.running = false;
                            if (vm.handleForbidden(res)) {
                                return;
                            }
                            if (!res || res.code !== 200) {
                                vm.$message.error((res && res.msg) ? res.msg : "手动清理失败");
                                return;
                            }
                            vm.result = res.data || null;
                            vm.$message.success("手动清理执行完成");
                        },
                        error: function () {
                            vm.running = false;
                            vm.$message.error("手动清理失败");
                        }
                    });
                }).catch(function () {});
            }
        }
    });
})();