zhangwei
4 天以前 9a416e0c3308badb6135ce921e6307f31e0dd463
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
<template>
    <div class="sys-update-container">
        <div>
            <NoticeBar text="系统更新管理,请慎重操作!" style="margin: 4px" />
        </div>
        <el-container style="height: calc(100vh - 150px);">
            <el-aside v-auth="'sysUpdate:list'" width="220px" class="backup-list">
                <p class="backup-list-description">备份列表</p>
                <el-scrollbar>
                    <div class="backup-items">
                        <div v-for="(backup, index) in state.backups" :key="index" class="backup-item" @mouseenter="hovered = index" @mouseleave="hovered = null">
                            <el-button type="text" :class="{ 'selected-backup': state.selectedBackup === backup, 'hovered-backup': hovered === index }" @click="() => state.selectedBackup = backup">
                                {{ backup.fileName }}
                            </el-button>
                        </div>
                    </div>
                </el-scrollbar>
            </el-aside>
            <el-main v-auth="'sysUpdate:logs'" class="log-terminal-container">
                <div class="toolbar">
                    <el-button-group>
                        <el-button v-auth="'sysUpdate:update'" v-reclick="5000" :disabled="state.isUpdating" @click="handleAction('update')">更新</el-button>
                        <el-button v-auth="'sysUpdate:restore'" v-reclick="5000" :disabled="!canRestore || state.isUpdating || !state.selectedBackup" @click="handleAction('restore')">还原</el-button>
                        <el-button v-auth="'sysUpdate:clear'" v-reclick="5000" :disabled="state.isUpdating" @click="clearLogs">清空</el-button>
                        <el-button v-auth="'sysUpdate:webHookKey'" v-reclick="5000" @click="getWebHookKey">获取密钥</el-button>
                    </el-button-group>
                </div>
                <div class="log-terminal">
                    <div class="terminal-output" ref="terminalOutput">
                        <pre>{{ state.logOutput }}</pre>
                    </div>
                </div>
            </el-main>
        </el-container>
    </div>
</template>
 
<script setup lang="ts">
import { reactive, computed, onMounted, nextTick, ref, onUnmounted } from 'vue';
import { ElMessage, ElMessageBox } from "element-plus";
import { getAPI } from "/@/utils/axios-utils";
import { authAll } from "/@/utils/authFunction";
import { BackupOutput, SysUpdateApi } from "/@/api-services";
import commonFunction from "/@/utils/commonFunction";
import NoticeBar from "/@/components/noticeBar/index.vue";
 
const { copyText } = commonFunction();
const state = reactive({
    selectedBackup: null as BackupOutput | null,
    backups: [] as BackupOutput[],
    isUpdating: false,
    logOutput: '',
});
 
// 计算属性 canRestore
const canRestore = computed(() => !!state.selectedBackup);
 
// 引用元素
const terminalOutput = ref<HTMLElement | null>(null);
 
// 新增的悬停索引变量
const hovered = ref<number | null>(null);
 
let refreshInterval: number;
 
// 获取初始数据
const fetchData = async () => {
    try {
        state.backups = (await getAPI(SysUpdateApi).apiSysUpdateListPost()).data.result ?? [];
        await refreshLog();
    } catch (error) {
        handleError('获取数据失败', error);
    }
};
 
// 刷新日志
const refreshLog = async () => {
    try {
        const response = await getAPI(SysUpdateApi).apiSysUpdateLogsGet();
        state.logOutput = (response.data.result ?? []).join('\n');
        scrollToBottom(); // 更新日志后立即滚动到底部
    } catch (error) {
        handleError('获取日志失败', error);
    }
};
 
// 滚动到底部
const scrollToBottom = () => {
    nextTick(() => {
        if (terminalOutput.value) {
            terminalOutput.value.scrollTop = terminalOutput.value.scrollHeight;
        }
    });
};
 
// 启动/停止日志刷新定时器
const toggleRefreshTimer = (start: boolean) => {
    if (start && !refreshInterval) {
        refreshInterval = window.setInterval(refreshLog, 300);
    } else if (!start && refreshInterval) {
        window.clearInterval(refreshInterval);
        refreshInterval = 0;
    }
};
 
// 处理动作
const handleAction = async (action: 'update' | 'restore') => {
    if (state.isUpdating) return;
 
    state.isUpdating = true;
    toggleRefreshTimer(true);
 
    try {
        switch (action) {
            case 'update':
                await getAPI(SysUpdateApi).apiSysUpdateUpdatePost({ timeout: -1 });
                ElMessage.success('更新成功');
                fetchData();
                break;
            case 'restore':
                ElMessageBox.confirm(`确定要还原到 ${state.selectedBackup?.fileName} ?`, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                }).then(async () => {
                    await getAPI(SysUpdateApi).apiSysUpdateRestorePost({ fileName: state.selectedBackup?.fileName } as any);
                    ElMessage.success('还原成功');
                });
                break;
        }
    } catch (error) {
        handleError(`执行${action}失败`, error);
    } finally {
        toggleRefreshTimer(false);
        state.isUpdating = false;
    }
};
 
// 清空日志
const clearLogs = async () => {
    try {
        state.logOutput = '';
        await getAPI(SysUpdateApi).apiSysUpdateClearGet();
        ElMessage.success('日志已清空');
    } catch (error) {
        handleError('清空日志失败', error);
    }
};
 
// 获取密钥
const getWebHookKey = async () => {
    try {
        const res = await getAPI(SysUpdateApi).apiSysUpdateWebHookKeyGet();
        if (res.data.result) copyText(res.data.result);
    } catch (error) {
        handleError('获取密钥失败', error);
    }
}
 
// 错误处理
const handleError = (message: string, error: any) => {
    ElMessage.error(`${message},请稍后再试。`);
};
 
onMounted(() => {
    if (!authAll(['sysUpdate:list', 'sysUpdate:logs'])) return;
    fetchData();
    toggleRefreshTimer(true);
});
 
onUnmounted(() => {
    if (!authAll(['sysUpdate:list', 'sysUpdate:logs'])) return;
    toggleRefreshTimer(false);
});
</script>
 
<style scoped>
 
.sys-update-container {
    display: flex;
    height: 100%;
    background-color: #f0f2f5;
}
 
.backup-list-description {
    margin-bottom: 10px;
    color: #909399;
}
 
.backup-list {
    background-color: #ffffff;
    padding: 20px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    border-radius: 8px;
    transition: box-shadow 0.3s ease-in-out;
    max-height: 100%;
    overflow: hidden;
}
 
.backup-items {
    max-height: calc(100vh - 40px);
    overflow-y: auto;
    overflow-x: hidden;
}
 
.backup-item {
    margin-bottom: 10px;
    transition: transform 0.2s;
}
 
.backup-item:hover {
    transform: translateX(5px);
}
 
.selected-backup, .hovered-backup {
    font-weight: bold;
    color: #409eff;
}
 
.action-button {
    margin-top: 8px;
    transition: background-color 0.3s;
}
 
.action-button:hover {
    background-color: #ecf5ff;
}
 
.log-terminal-container {
    flex-grow: 1;
    padding: 20px;
    display: flex;
    flex-direction: column;
    background-color: #ffffff;
    border-radius: 8px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    transition: box-shadow 0.3s ease-in-out;
}
 
.toolbar {
    margin-bottom: 5px;
    padding: 5px 10px 5px 10px;
    background-color: #ffffff;
    border-radius: 4px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: flex-start;
    gap: 8px;
}
 
.log-terminal {
    background-color: #2c3e50;
    color: #ecf0f1;
    border-radius: 4px;
    flex-grow: 1;
    position: relative;
    overflow: hidden;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
 
.terminal-output {
    padding: 20px;
    height: 100%;
    overflow-y: auto;
    white-space: pre-wrap;
    word-wrap: break-word;
    font-family: 'Courier New', Courier, monospace;
    font-size: 14px;
    line-height: 1.5;
}
</style>