仰望星辰工作室

miaopanel

miaopanel/ web/src/views/Apps.vue 11.2 KB · 439 行 原始文件
1<script setup lang="ts">
2import { inject, nextTick, onBeforeUnmount, onMounted, reactive, ref, watch } from 'vue'
3import { api } from '../api'
4import { route, updateQuery } from '../router'
5import ServicePanel from '../components/ServicePanel.vue'
6
7interface Version {
8 version: string
9 label: string
10 source: string
11}
12
13interface Installed {
14 version: string
15 path: string
16 active: boolean
17 managed: boolean
18}
19
20interface AppItem {
21 id: string
22 name: string
23 summary: string
24 category: string
25 note: string
26 versions: Version[]
27 installed: Installed[] | null
28 running: boolean
29 supported: boolean
30}
31
32interface Task {
33 id: string
34 app: string
35 app_name: string
36 version: string
37 action: string
38 status: 'running' | 'ok' | 'failed'
39 log: string[]
40 error?: string
41 started_at: number
42 ended_at?: number
43}
44
45const notify = inject<(text: string, kind?: 'ok' | 'error') => void>('notify')!
46
47const apps = ref<AppItem[]>([])
48const loading = ref(false)
49const busy = ref('')
50const selected = reactive<Record<string, string>>({})
51const task = ref<Task | null>(null)
52const taskOpen = ref(false)
53const recent = ref<Task[]>([])
54const managing = ref<AppItem | null>(null)
55const logBox = ref<HTMLElement | null>(null)
56
57// Only services with a tuning/status page get the manage entry.
58const manageable = (id: string) => ['nginx', 'mysql', 'mariadb'].includes(id)
59let timer: number | undefined
60let openedRunning = false
61
62const icons: Record<string, string> = {
63 web: 'N',
64 database: 'DB',
65 runtime: '</>',
66 tool: 'T',
67}
68
69async function load(refresh = false) {
70 loading.value = true
71 try {
72 const list = await api.get<AppItem[]>(`api/v1/apps${refresh ? '?refresh=1' : ''}`)
73 apps.value = list
74 for (const app of list) {
75 if (!selected[app.id]) selected[app.id] = app.versions[0]?.version ?? ''
76 }
77 recent.value = await api.get<Task[]>('api/v1/apps/tasks?limit=6')
78 } catch (e) {
79 notify((e as Error).message, 'error')
80 } finally {
81 loading.value = false
82 }
83}
84
85function installedOf(app: AppItem) {
86 return app.installed ?? []
87}
88
89async function install(app: AppItem) {
90 const version = selected[app.id]
91 if (!version) return
92 const warning = app.id === 'nginx'
93 ? `安装 ${app.name} ${version} 会替换当前 Web 服务器并重启,站点约中断 1 秒。继续?`
94 : app.category === 'database'
95 ? `安装 ${app.name} ${version}?MySQL 与 MariaDB 互斥(同一 3306 与数据目录),请确认另一个数据库已卸载。`
96 : `安装 ${app.name} ${version}?耗时可能从几十秒到几分钟。`
97 if (!window.confirm(warning)) return
98 busy.value = app.id
99 try {
100 const t = await api.post<Task>(`api/v1/apps/${app.id}/install`, { version })
101 openTask(t)
102 } catch (e) {
103 notify((e as Error).message, 'error')
104 } finally {
105 busy.value = ''
106 }
107}
108
109async function uninstall(app: AppItem, version: string) {
110 const warning = app.id === 'nginx'
111 ? `卸载 nginx ${version} 会停止 Web 服务,所有站点将无法访问。仍然卸载?`
112 : app.category === 'database'
113 ? `卸载 ${app.name} ${version}?数据库数据目录 /var/lib/mysql 会保留。`
114 : `卸载 ${app.name} ${version}?`
115 if (!window.confirm(warning)) return
116 busy.value = app.id
117 try {
118 const t = await api.post<Task>(`api/v1/apps/${app.id}/uninstall`, { version })
119 openTask(t)
120 } catch (e) {
121 notify((e as Error).message, 'error')
122 } finally {
123 busy.value = ''
124 }
125}
126
127function openTask(t: Task) {
128 task.value = t
129 taskOpen.value = true
130 openedRunning = t.status === 'running'
131 scrollLog()
132 if (openedRunning) {
133 if (timer) window.clearInterval(timer)
134 timer = window.setInterval(poll, 1000)
135 }
136}
137
138async function poll() {
139 if (!task.value) return
140 try {
141 const t = await api.get<Task>(`api/v1/apps/tasks/${task.value.id}`)
142 task.value = t
143 scrollLog()
144 if (t.status !== 'running') {
145 stopPoll()
146 if (openedRunning) {
147 if (t.status === 'ok') {
148 notify(`${t.app_name} ${t.version} ${t.action === 'install' ? '安装完成' : '已卸载'}`)
149 } else {
150 notify(t.error || '任务失败,请查看日志', 'error')
151 }
152 }
153 openedRunning = false
154 await load(true)
155 }
156 } catch {
157 stopPoll()
158 }
159}
160
161function stopPoll() {
162 if (timer) {
163 window.clearInterval(timer)
164 timer = undefined
165 }
166}
167
168function closeTask() {
169 taskOpen.value = false
170 stopPoll()
171 openedRunning = false
172 if (route.value.query.get('task')) updateQuery({ task: undefined })
173}
174
175function scrollLog() {
176 nextTick(() => {
177 if (logBox.value) logBox.value.scrollTop = logBox.value.scrollHeight
178 })
179}
180
181// The task center links here with ?task=<id>; restoring the modal makes the
182// background job reachable from any page.
183async function openFromRoute() {
184 const id = route.value.query.get('task')
185 if (!id) return
186 try {
187 const t = await api.get<Task>(`api/v1/apps/tasks/${id}`)
188 openTask(t)
189 } catch {
190 notify('任务不存在或日志已被清理', 'error')
191 }
192}
193
194watch(() => route.value.query.toString(), openFromRoute)
195
196onMounted(async () => {
197 await load()
198 await openFromRoute()
199})
200onBeforeUnmount(stopPoll)
201</script>
202
203<template>
204 <div class="card">
205 <div class="row">
206 <div>
207 <h2>应用商店</h2>
208 <p class="hint">按需安装运行时与服务,可选择版本。安装任务在后台执行,可实时查看日志。</p>
209 </div>
210 <div class="spacer"></div>
211 <button class="btn btn-ghost btn-sm" :disabled="loading" @click="load(true)">刷新</button>
212 </div>
213 <p class="hint" style="margin: 8px 0 0">
214 建议顺序:先安装 nginx,再按站点类型安装 PHP / Node / Go / Python,最后按需安装数据库与 phpMyAdmin。
215 安装任务在面板进程内执行,期间请勿重启面板或服务器。
216 </p>
217 <div v-if="recent.length" class="recent">
218 <span class="muted">最近任务:</span>
219 <button
220 v-for="t in recent"
221 :key="t.id"
222 class="btn btn-sm btn-ghost"
223 :class="{ 'task-failed': t.status === 'failed' }"
224 @click="openTask(t)"
225 >
226 {{ t.app_name }} {{ t.version }} ·
227 {{ t.status === 'running' ? '执行中' : t.status === 'ok' ? '完成' : '失败' }}
228 </button>
229 </div>
230 </div>
231
232 <div class="app-grid">
233 <div v-for="app in apps" :key="app.id" class="card app-card">
234 <div class="row app-head">
235 <span class="app-icon" :class="app.category">{{ icons[app.category] || 'APP' }}</span>
236 <div>
237 <h3>{{ app.name }}</h3>
238 <span class="badge" :class="app.supported ? 'ok' : 'off'">
239 {{ app.supported ? '支持当前系统' : '当前系统不支持' }}
240 </span>
241 <span v-if="app.running" class="badge ok" style="margin-left: 6px">运行中</span>
242 </div>
243 <div class="spacer"></div>
244 </div>
245 <p class="hint app-summary">{{ app.summary }}</p>
246
247 <div v-if="installedOf(app).length" class="installed">
248 <div class="installed-title">已安装</div>
249 <div v-for="item in installedOf(app)" :key="item.version" class="installed-row">
250 <span class="mono">{{ item.version }}</span>
251 <span v-if="item.active" class="badge ok">当前</span>
252 <span class="muted path mono" :title="item.path">{{ item.path }}</span>
253 <button
254 v-if="item.managed"
255 class="btn btn-sm btn-danger"
256 :disabled="busy === app.id"
257 @click="uninstall(app, item.version)"
258 >
259 卸载
260 </button>
261 <span v-else class="badge off">系统自带</span>
262 </div>
263 </div>
264 <div v-else class="empty-inline muted">尚未安装</div>
265
266 <div class="app-actions">
267 <select v-model="selected[app.id]" class="select" :disabled="!app.supported || busy === app.id">
268 <option v-for="v in app.versions" :key="v.version" :value="v.version">
269 {{ v.label }} · {{ v.source }}
270 </option>
271 </select>
272 <button
273 v-if="manageable(app.id) && installedOf(app).length"
274 class="btn btn-ghost btn-sm"
275 @click="managing = app"
276 >
277 管理
278 </button>
279 <button
280 class="btn btn-primary btn-sm"
281 :disabled="!app.supported || busy === app.id"
282 @click="install(app)"
283 >
284 {{ busy === app.id ? '提交中…' : '安装' }}
285 </button>
286 </div>
287 <p v-if="app.note" class="hint note">{{ app.note }}</p>
288 </div>
289 </div>
290
291 <div v-if="!loading && apps.length === 0" class="card empty">应用目录为空</div>
292
293 <div v-if="taskOpen && task" class="overlay" @click.self="closeTask">
294 <div class="card task-modal">
295 <div class="row">
296 <div>
297 <h3>{{ task.action === 'install' ? '安装' : '卸载' }} {{ task.app_name }} {{ task.version }}</h3>
298 <span class="badge" :class="task.status === 'ok' ? 'ok' : task.status === 'failed' ? 'err' : 'off'">
299 {{ task.status === 'running' ? '执行中' : task.status === 'ok' ? '已完成' : '失败' }}
300 </span>
301 </div>
302 <div class="spacer"></div>
303 <button class="btn btn-ghost btn-sm" @click="closeTask">关闭</button>
304 </div>
305 <pre ref="logBox" class="task-log">{{ (task.log || []).join('\n') }}</pre>
306 <p v-if="task.status === 'running'" class="hint">任务在后台运行,关闭窗口不会中断安装。</p>
307 <p v-else-if="task.error" class="error-text">{{ task.error }}</p>
308 </div>
309 </div>
310
311 <ServicePanel
312 v-if="managing"
313 :app-id="managing.id"
314 :name="managing.name"
315 @close="managing = null"
316 />
317</template>
318
319<style scoped>
320.recent {
321 display: flex;
322 flex-wrap: wrap;
323 align-items: center;
324 gap: 6px;
325 margin-top: 10px;
326 font-size: 12px;
327}
328
329.task-failed {
330 color: #dc2626;
331}
332
333.app-grid {
334 display: grid;
335 grid-template-columns: repeat(auto-fill, minmax(340px, 1fr));
336 gap: 14px;
337 margin-top: 14px;
338}
339
340.app-card {
341 display: flex;
342 flex-direction: column;
343 gap: 8px;
344}
345
346.app-head h3 {
347 margin: 0 0 4px;
348 font-size: 15px;
349}
350
351.app-icon {
352 width: 40px;
353 height: 40px;
354 border-radius: 12px;
355 display: grid;
356 place-items: center;
357 font-weight: 700;
358 font-size: 12px;
359 color: #fff;
360 background: #38bdf8;
361 flex: none;
362}
363
364.app-icon.web { background: #34d399; }
365.app-icon.database { background: #fb923c; }
366.app-icon.runtime { background: #818cf8; }
367.app-icon.tool { background: #f472b6; }
368
369.app-summary {
370 min-height: 34px;
371 margin: 0;
372}
373
374.installed {
375 border-top: 1px dashed var(--line, #e5e7eb);
376 padding-top: 8px;
377}
378
379.installed-title {
380 font-size: 12px;
381 color: #6b7280;
382 margin-bottom: 4px;
383}
384
385.installed-row {
386 display: flex;
387 align-items: center;
388 gap: 8px;
389 margin-bottom: 4px;
390}
391
392.path {
393 flex: 1;
394 overflow: hidden;
395 text-overflow: ellipsis;
396 white-space: nowrap;
397 font-size: 11px;
398}
399
400.empty-inline {
401 font-size: 12px;
402}
403
404.app-actions {
405 display: flex;
406 gap: 8px;
407 margin-top: auto;
408}
409
410.app-actions .select {
411 flex: 1;
412}
413
414.note {
415 margin: 0;
416 font-size: 11.5px;
417}
418
419.task-modal {
420 width: min(860px, 92vw);
421}
422
423.task-log {
424 margin: 12px 0 6px;
425 padding: 12px;
426 height: 380px;
427 overflow: auto;
428 border-radius: 10px;
429 background: #0f1720;
430 color: #d7e2ee;
431 font: 12px/1.6 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
432 white-space: pre-wrap;
433}
434
435.error-text {
436 color: #dc2626;
437 font-size: 12px;
438}
439</style>