miaopanel
1<script setup lang="ts">
2import { computed, inject, onBeforeUnmount, onMounted, reactive, ref } from 'vue'
3import { api } from '../api'
4
5interface Permission {
6 raw: string
7 kind: string
8 value: string
9 risk: 'high' | 'medium' | string
10 label: string
11}
12
13interface PluginVersion {
14 version: string
15 channel: string
16 arch: string
17 size: number
18 sha256: string
19 min_panel: string
20 max_panel: string
21 downloads: number
22 published_at: number
23}
24
25interface PluginItem {
26 slug: string
27 name: string
28 summary: string
29 category: string
30 license: string
31 homepage: string
32 icon: string
33 author: string
34 downloads: number
35 permissions: string[]
36 permission_view: Permission[]
37 latest: PluginVersion | null
38 versions: PluginVersion[]
39 installed: boolean
40 installed_version: string
41 status: string
42 update: boolean
43 available: string
44 revoked: boolean
45 reason: string
46 auto_update: boolean
47}
48
49interface Task {
50 id: string
51 slug: string
52 plugin: string
53 version: string
54 action: string
55 status: 'running' | 'ok' | 'failed'
56 log: string[]
57 error?: string
58 started_at: number
59 ended_at?: number
60}
61
62interface UiField {
63 key: string
64 label?: string
65 type?: string
66 default?: string
67 placeholder?: string
68 help?: string
69 options?: { label?: string; value?: string }[]
70}
71
72const notify = inject<(text: string, kind?: 'ok' | 'error') => void>('notify')!
73
74const plugins = ref<PluginItem[]>([])
75const loading = ref(false)
76const offline = ref(false)
77const offlineReason = ref('')
78const tab = ref<'all' | 'installed' | 'updates'>('all')
79const query = ref('')
80const detail = ref<PluginItem | null>(null)
81const busy = ref('')
82const installForm = reactive({ version: '', confirm: '' })
83const uninstallForm = reactive({ confirm: '', purge: false, force: false })
84const task = ref<Task | null>(null)
85const taskOpen = ref(false)
86const logBox = ref<HTMLElement | null>(null)
87const settingsOpen = ref(false)
88const settingsLoading = ref(false)
89const settingValues = reactive<Record<string, string>>({})
90const settingFields = ref<UiField[]>([])
91const configOpen = ref(false)
92const configBusy = ref(false)
93const configForm = reactive({ repo_url: '', auto_update: false })
94let timer: number | undefined
95
96const filtered = computed(() => {
97 const q = query.value.trim().toLowerCase()
98 return plugins.value.filter((p) => {
99 if (tab.value === 'installed' && !p.installed) return false
100 if (tab.value === 'updates' && !p.update) return false
101 if (!q) return true
102 return (
103 p.name.toLowerCase().includes(q) ||
104 p.slug.includes(q) ||
105 (p.summary || '').toLowerCase().includes(q)
106 )
107 })
108})
109
110const updateCount = computed(() => plugins.value.filter((p) => p.update).length)
111
112async function load(refresh = false) {
113 loading.value = true
114 try {
115 const data = await api.get<{ plugins: PluginItem[]; offline: boolean; error?: string }>(
116 `api/v1/plugins${refresh ? '?refresh=1' : ''}`,
117 )
118 plugins.value = data.plugins || []
119 offline.value = !!data.offline
120 offlineReason.value = data.error || ''
121 } catch (e) {
122 notify((e as Error).message, 'error')
123 } finally {
124 loading.value = false
125 }
126}
127
128function openDetail(p: PluginItem) {
129 detail.value = p
130 installForm.version = p.update && p.available ? p.available : p.latest?.version ?? ''
131 installForm.confirm = ''
132 uninstallForm.confirm = ''
133 uninstallForm.purge = false
134 uninstallForm.force = false
135 settingsOpen.value = false
136}
137
138function closeDetail() {
139 detail.value = null
140}
141
142function openTask(t: Task) {
143 task.value = t
144 taskOpen.value = true
145 scrollLog()
146 if (t.status === 'running') {
147 if (timer) window.clearInterval(timer)
148 timer = window.setInterval(poll, 1000)
149 }
150}
151
152async function poll() {
153 if (!task.value) return
154 try {
155 const next = await api.get<Task>(`api/v1/plugin-tasks/${task.value.id}`)
156 task.value = next
157 scrollLog()
158 if (next.status !== 'running') {
159 stopPoll()
160 await load()
161 }
162 } catch {
163 stopPoll()
164 }
165}
166
167function stopPoll() {
168 if (timer) window.clearInterval(timer)
169 timer = undefined
170}
171
172function closeTask() {
173 taskOpen.value = false
174 stopPoll()
175}
176
177function scrollLog() {
178 requestAnimationFrame(() => {
179 if (logBox.value) logBox.value.scrollTop = logBox.value.scrollHeight
180 })
181}
182
183async function install(p: PluginItem) {
184 const version = installForm.version
185 if (!version) return
186 if (installForm.confirm !== `${p.slug}@${version}`) {
187 notify(`请输入 ${p.slug}@${version} 以确认安装`, 'error')
188 return
189 }
190 const warning = p.revoked
191 ? `该插件已被平台标记:${p.reason}\n仍然安装?`
192 : `安装 ${p.name} ${version}?安装脚本会以 root 权限运行。`
193 if (!window.confirm(warning)) return
194 busy.value = p.slug
195 try {
196 const t = await api.post<Task>(`api/v1/plugins/${p.slug}/install`, {
197 version,
198 confirm: installForm.confirm,
199 })
200 closeDetail()
201 openTask(t)
202 } catch (e) {
203 notify((e as Error).message, 'error')
204 } finally {
205 busy.value = ''
206 }
207}
208
209async function uninstall(p: PluginItem) {
210 if (uninstallForm.confirm !== p.slug) {
211 notify(`请输入 ${p.slug} 以确认卸载`, 'error')
212 return
213 }
214 if (!window.confirm(`卸载 ${p.name}?${uninstallForm.purge ? '插件数据也会被删除。' : ''}`)) return
215 busy.value = p.slug
216 try {
217 const t = await api.post<Task>(`api/v1/plugins/${p.slug}/uninstall`, {
218 confirm: uninstallForm.confirm,
219 purge: uninstallForm.purge,
220 force: uninstallForm.force,
221 })
222 closeDetail()
223 openTask(t)
224 } catch (e) {
225 notify((e as Error).message, 'error')
226 } finally {
227 busy.value = ''
228 }
229}
230
231async function control(p: PluginItem, action: string) {
232 busy.value = p.slug
233 try {
234 await api.post(`api/v1/plugins/${p.slug}/action`, { action })
235 notify(`${p.name}:${action} 完成`)
236 await load()
237 } catch (e) {
238 notify((e as Error).message, 'error')
239 } finally {
240 busy.value = ''
241 }
242}
243
244async function toggleAuto(p: PluginItem) {
245 try {
246 await api.post(`api/v1/plugins/${p.slug}/auto-update`, { auto_update: !p.auto_update })
247 p.auto_update = !p.auto_update
248 notify(p.auto_update ? '已开启自动更新' : '已关闭自动更新')
249 } catch (e) {
250 notify((e as Error).message, 'error')
251 }
252}
253
254async function openSettings(p: PluginItem) {
255 settingsOpen.value = true
256 settingsLoading.value = true
257 settingFields.value = []
258 for (const key of Object.keys(settingValues)) delete settingValues[key]
259 try {
260 const data = await api.get<{ values: Record<string, string>; schema: any }>(
261 `api/v1/plugins/${p.slug}/settings`,
262 )
263 for (const [k, v] of Object.entries(data.values || {})) settingValues[k] = v
264 const fields = data.schema?.fields
265 if (Array.isArray(fields)) {
266 settingFields.value = fields as UiField[]
267 for (const f of settingFields.value) {
268 if (settingValues[f.key] === undefined) settingValues[f.key] = f.default ?? ''
269 }
270 }
271 } catch (e) {
272 notify((e as Error).message, 'error')
273 settingsOpen.value = false
274 } finally {
275 settingsLoading.value = false
276 }
277}
278
279async function saveSettings(p: PluginItem) {
280 try {
281 await api.put(`api/v1/plugins/${p.slug}/settings`, { values: { ...settingValues } })
282 notify('配置已保存')
283 settingsOpen.value = false
284 } catch (e) {
285 notify((e as Error).message, 'error')
286 }
287}
288
289function fmtSize(n: number) {
290 if (!n) return '—'
291 const units = ['B', 'KB', 'MB', 'GB']
292 let v = n
293 let i = 0
294 while (v >= 1024 && i < units.length - 1) {
295 v /= 1024
296 i++
297 }
298 return `${v.toFixed(i === 0 ? 0 : 1)} ${units[i]}`
299}
300
301function riskClass(risk: string) {
302 return risk === 'high' ? 'badge err' : 'badge off'
303}
304
305async function openConfig() {
306 configOpen.value = true
307 try {
308 const data = await api.get<{ repo_url: string; auto_update: boolean }>('api/v1/plugins/config')
309 configForm.repo_url = data.repo_url
310 configForm.auto_update = data.auto_update
311 } catch (e) {
312 notify((e as Error).message, 'error')
313 }
314}
315
316async function saveConfig() {
317 configBusy.value = true
318 try {
319 await api.put('api/v1/plugins/config', {
320 repo_url: configForm.repo_url,
321 auto_update: configForm.auto_update,
322 })
323 notify('目录设置已保存')
324 configOpen.value = false
325 await load(true)
326 } catch (e) {
327 notify((e as Error).message, 'error')
328 } finally {
329 configBusy.value = false
330 }
331}
332
333async function resetPlatformKey() {
334 if (!window.confirm('重置平台签名公钥?下次拉取目录会信任当前服务器提供的公钥,仅应在官方换钥时使用。')) {
335 return
336 }
337 configBusy.value = true
338 try {
339 await api.post('api/v1/plugins/reset-key', {})
340 notify('已重置,重新拉取目录')
341 await load(true)
342 } catch (e) {
343 notify((e as Error).message, 'error')
344 } finally {
345 configBusy.value = false
346 }
347}
348
349onMounted(() => load())
350onBeforeUnmount(stopPoll)
351</script>
352
353<template>
354 <div class="stack">
355 <div class="row">
356 <div class="tabs">
357 <button class="tab" :class="{ active: tab === 'all' }" @click="tab = 'all'">
358 全部插件
359 </button>
360 <button class="tab" :class="{ active: tab === 'installed' }" @click="tab = 'installed'">
361 已安装
362 </button>
363 <button class="tab" :class="{ active: tab === 'updates' }" @click="tab = 'updates'">
364 可更新
365 <span v-if="updateCount" class="badge ok">{{ updateCount }}</span>
366 </button>
367 </div>
368 <div class="spacer"></div>
369 <input v-model="query" class="select" placeholder="搜索插件" style="min-width: 200px" />
370 <button class="btn btn-ghost btn-sm" @click="openConfig">目录设置</button>
371 <button class="btn btn-ghost btn-sm" :disabled="loading" @click="load(true)">
372 刷新目录
373 </button>
374 </div>
375
376 <div v-if="offline" class="card hint note">
377 无法连接插件服务器,当前展示的是本地缓存目录。安装与更新会失败,请检查网络或发布服务器地址。
378 <span v-if="offlineReason" class="muted small">({{ offlineReason }})</span>
379 </div>
380
381 <div v-if="loading && !plugins.length" class="card empty">
382 <p class="muted">正在读取插件目录…</p>
383 </div>
384
385 <div v-else-if="!filtered.length" class="card empty">
386 <p class="muted">
387 {{ tab === 'installed' ? '还没有安装任何第三方插件。' : tab === 'updates' ? '所有插件都是最新版本。' : '没有匹配的插件。' }}
388 </p>
389 <p class="hint">
390 开发者可以在官网注册账号并提交插件,审核通过后会出现在这里。
391 </p>
392 </div>
393
394 <div v-else class="app-grid">
395 <div v-for="p in filtered" :key="p.slug" class="card app-card">
396 <div class="row app-head">
397 <span class="app-icon">{{ (p.name || p.slug).slice(0, 1) }}</span>
398 <div>
399 <h3>{{ p.name }}</h3>
400 <div class="muted small">
401 {{ p.author || '第三方开发者' }} · v{{ p.latest?.version || p.installed_version }}
402 </div>
403 </div>
404 <div class="spacer"></div>
405 <span v-if="p.revoked" class="badge err">已下架</span>
406 <span v-else-if="p.installed" class="badge ok">已安装</span>
407 <span v-if="p.update" class="badge">可更新 {{ p.available }}</span>
408 </div>
409 <p class="hint app-summary">{{ p.summary }}</p>
410 <div class="row" style="flex-wrap: wrap; gap: 6px">
411 <span class="badge">{{ p.category }}</span>
412 <span v-for="perm in p.permission_view.slice(0, 3)" :key="perm.raw" class="badge off">
413 {{ perm.raw }}
414 </span>
415 <span v-if="p.permission_view.length > 3" class="muted small">
416 +{{ p.permission_view.length - 3 }}
417 </span>
418 </div>
419 <div class="row app-actions">
420 <span class="muted small">下载 {{ p.downloads }}</span>
421 <div class="spacer"></div>
422 <button class="btn btn-ghost btn-sm" @click="openDetail(p)">
423 {{ p.installed ? '管理' : '查看' }}
424 </button>
425 <button
426 v-if="p.installed"
427 class="btn btn-ghost btn-sm"
428 :disabled="busy === p.slug"
429 @click="openDetail(p)"
430 >
431 卸载
432 </button>
433 <button
434 v-else
435 class="btn btn-primary btn-sm"
436 :disabled="busy === p.slug || !p.latest"
437 @click="openDetail(p)"
438 >
439 安装
440 </button>
441 </div>
442 </div>
443 </div>
444
445 <div v-if="detail" class="overlay" @click.self="closeDetail">
446 <div class="card modal wide plugin-detail">
447 <div class="row">
448 <div>
449 <h3>{{ detail.name }} <span class="muted">v{{ detail.latest?.version }}</span></h3>
450 <p class="muted small">
451 {{ detail.author }} · {{ detail.category }} · {{ detail.license || '未声明许可证' }} ·
452 下载 {{ detail.downloads }}
453 </p>
454 </div>
455 <div class="spacer"></div>
456 <button class="btn btn-ghost btn-sm" @click="closeDetail">关闭</button>
457 </div>
458
459 <div v-if="detail.revoked" class="card hint note" style="border-color: #fecaca">
460 该插件已被平台下架:{{ detail.reason || '未提供原因' }}。建议卸载。
461 </div>
462
463 <p class="hint">{{ detail.summary }}</p>
464 <p v-if="detail.homepage" class="hint">
465 <a :href="detail.homepage" target="_blank" rel="noreferrer">{{ detail.homepage }}</a>
466 </p>
467
468 <h4>权限声明</h4>
469 <div v-if="detail.permission_view.length" class="stack" style="gap: 6px">
470 <div v-for="perm in detail.permission_view" :key="perm.raw" class="row">
471 <span :class="riskClass(perm.risk)">{{ perm.risk === 'high' ? '高危' : '普通' }}</span>
472 <span class="mono small">{{ perm.raw }}</span>
473 <span class="muted small">{{ perm.label }}</span>
474 </div>
475 </div>
476 <p v-else class="muted small">没有声明额外权限。</p>
477
478 <h4>版本</h4>
479 <div class="table-wrap">
480 <table class="table">
481 <thead>
482 <tr><th>版本</th><th>架构</th><th>大小</th><th>面板要求</th><th>下载</th></tr>
483 </thead>
484 <tbody>
485 <tr v-for="v in detail.versions" :key="`${v.version}-${v.arch}`">
486 <td>{{ v.version }} <span v-if="v.channel === 'beta'" class="badge off">beta</span></td>
487 <td class="mono">{{ v.arch }}</td>
488 <td>{{ fmtSize(v.size) }}</td>
489 <td class="muted small">
490 {{ v.min_panel ? `>= ${v.min_panel}` : '任意' }}
491 {{ v.max_panel ? ` <= ${v.max_panel}` : '' }}
492 </td>
493 <td>{{ v.downloads }}</td>
494 </tr>
495 </tbody>
496 </table>
497 </div>
498
499 <template v-if="!detail.installed">
500 <h4>安装</h4>
501 <div class="grid cols-2">
502 <label class="field">
503 <span>版本</span>
504 <select v-model="installForm.version" class="select">
505 <option v-for="v in detail.versions" :key="v.version" :value="v.version">
506 {{ v.version }} ({{ v.arch }}) {{ fmtSize(v.size) }}
507 </option>
508 </select>
509 </label>
510 <label class="field">
511 <span>确认安装</span>
512 <input
513 v-model="installForm.confirm"
514 class="select"
515 :placeholder="`${detail.slug}@${installForm.version}`"
516 />
517 </label>
518 </div>
519 <p class="hint">
520 安装脚本会以 root 执行。请核对上面的权限声明,并在输入框中输入
521 <code>{{ detail.slug }}@{{ installForm.version }}</code> 以确认。
522 </p>
523 <div class="modal-foot">
524 <button class="btn btn-ghost" @click="closeDetail">取消</button>
525 <button class="btn btn-primary" :disabled="busy === detail.slug" @click="install(detail)">
526 确认安装
527 </button>
528 </div>
529 </template>
530
531 <template v-else>
532 <h4>已安装 v{{ detail.installed_version }}</h4>
533 <div class="row" style="flex-wrap: wrap; gap: 8px">
534 <span class="badge ok">{{ detail.status }}</span>
535 <button class="btn btn-ghost btn-sm" @click="toggleAuto(detail)">
536 {{ detail.auto_update ? '关闭自动更新' : '开启自动更新' }}
537 </button>
538 <button class="btn btn-ghost btn-sm" @click="openSettings(detail)">插件配置</button>
539 <template v-if="detail.update">
540 <button class="btn btn-primary btn-sm" @click="openDetail(detail)">
541 更新到 {{ detail.available }}
542 </button>
543 </template>
544 <button
545 v-if="detail.status === 'installed'"
546 class="btn btn-ghost btn-sm"
547 @click="control(detail, 'restart')"
548 >
549 重启服务
550 </button>
551 </div>
552
553 <div v-if="settingsOpen" class="card" style="margin-top: 10px">
554 <div class="row">
555 <h4>插件配置</h4>
556 <div class="spacer"></div>
557 <button class="btn btn-ghost btn-sm" @click="settingsOpen = false">收起</button>
558 </div>
559 <p v-if="settingsLoading" class="muted small">读取中…</p>
560 <p v-else-if="!settingFields.length" class="muted small">
561 该插件没有提供声明式配置表单,请使用它自带的界面或命令行。
562 </p>
563 <div v-else class="grid cols-2">
564 <label v-for="f in settingFields" :key="f.key" class="field">
565 <span>{{ f.label || f.key }}</span>
566 <select v-if="f.type === 'select'" v-model="settingValues[f.key]" class="select">
567 <option
568 v-for="opt in f.options || []"
569 :key="opt.value || opt.label"
570 :value="opt.value || opt.label"
571 >
572 {{ opt.label || opt.value }}
573 </option>
574 </select>
575 <input
576 v-else
577 v-model="settingValues[f.key]"
578 class="select"
579 :type="f.type === 'password' ? 'password' : 'text'"
580 :placeholder="f.placeholder"
581 />
582 <small v-if="f.help" class="muted">{{ f.help }}</small>
583 </label>
584 </div>
585 <div v-if="settingFields.length" class="modal-foot">
586 <button class="btn btn-primary" @click="saveSettings(detail)">保存配置</button>
587 </div>
588 </div>
589
590 <h4>卸载</h4>
591 <div class="grid cols-2">
592 <label class="field">
593 <span>确认卸载</span>
594 <input v-model="uninstallForm.confirm" class="select" :placeholder="detail.slug" />
595 </label>
596 <label class="field">
597 <span>选项</span>
598 <label class="check"><input v-model="uninstallForm.purge" type="checkbox" /> 同时删除插件数据与配置</label>
599 <label class="check"><input v-model="uninstallForm.force" type="checkbox" /> 卸载脚本失败也继续</label>
600 </label>
601 </div>
602 <div class="modal-foot">
603 <button class="btn btn-ghost" @click="closeDetail">取消</button>
604 <button class="btn btn-danger" :disabled="busy === detail.slug" @click="uninstall(detail)">
605 确认卸载
606 </button>
607 </div>
608 </template>
609 </div>
610 </div>
611
612 <div v-if="configOpen" class="overlay" @click.self="configOpen = false">
613 <div class="card modal">
614 <div class="row">
615 <h3>插件目录设置</h3>
616 <div class="spacer"></div>
617 <button class="btn btn-ghost btn-sm" @click="configOpen = false">关闭</button>
618 </div>
619 <label class="field">
620 <span>发布服务器</span>
621 <input v-model="configForm.repo_url" class="select" placeholder="https://mmmb.fangqihang.cn" />
622 </label>
623 <label class="check">
624 <input v-model="configForm.auto_update" type="checkbox" />
625 自动安装同一主版本的补丁更新(仅限已签名且兼容的版本)
626 </label>
627 <p class="hint">
628 公钥固定后,服务器更换签名密钥需要在此重置;重置只影响后续校验,不会卸载已装插件。
629 </p>
630 <div class="modal-foot">
631 <button class="btn btn-ghost" :disabled="configBusy" @click="resetPlatformKey">
632 重置平台公钥
633 </button>
634 <div class="spacer"></div>
635 <button class="btn btn-primary" :disabled="configBusy" @click="saveConfig">保存</button>
636 </div>
637 </div>
638 </div>
639
640 <div v-if="taskOpen && task" class="overlay" @click.self="closeTask">
641 <div class="card task-modal">
642 <div class="row">
643 <div>
644 <h3>{{ task.plugin }} {{ task.version }} · {{ task.action }}</h3>
645 <span
646 class="badge"
647 :class="task.status === 'ok' ? 'ok' : task.status === 'failed' ? 'err' : 'off'"
648 >
649 {{ task.status === 'running' ? '执行中' : task.status === 'ok' ? '已完成' : '失败' }}
650 </span>
651 </div>
652 <div class="spacer"></div>
653 <button class="btn btn-ghost btn-sm" @click="closeTask">关闭</button>
654 </div>
655 <pre ref="logBox" class="task-log">{{ (task.log || []).join('\n') }}</pre>
656 <p v-if="task.status === 'running'" class="hint">
657 任务在后台运行,关闭窗口不会中断。签名、哈希与脚本输出都会记录在这里。
658 </p>
659 <p v-else-if="task.error" class="error-text">{{ task.error }}</p>
660 </div>
661 </div>
662 </div>
663</template>
664
665<style scoped>
666.plugin-detail {
667 max-width: 860px;
668}
669h4 {
670 margin: 16px 0 8px;
671 font-size: 13px;
672 color: var(--muted);
673 letter-spacing: 0.04em;
674 text-transform: uppercase;
675}
676.check {
677 display: flex;
678 gap: 6px;
679 align-items: center;
680 font-size: 13px;
681}
682</style>