仰望星辰工作室

miaopanel

miaopanel/ web/src/views/Sites.vue 10.7 KB · 369 行 原始文件
1<script setup lang="ts">
2import { computed, inject, onMounted, ref } from 'vue'
3import { api } from '../api'
4import { navigate, route } from '../router'
5import type { Site } from '../types'
6import SiteCreateModal from '../components/SiteCreateModal.vue'
7import ConfirmDialog from '../components/ConfirmDialog.vue'
8import SiteDetail from './SiteDetail.vue'
9
10const notify = inject<(text: string, kind?: 'ok' | 'error' | 'warn') => void>('notify')!
11
12const sites = ref<Site[]>([])
13const loading = ref(false)
14const busy = ref(0)
15const query = ref('')
16const showCreate = ref(false)
17const nginxReady = ref(true)
18const deleting = ref<Site | null>(null)
19const deleteBusy = ref(false)
20const deleteError = ref('')
21
22const detailId = computed(() => (route.value.view === 'sites' ? route.value.id ?? null : null))
23const appKinds = ['go', 'node', 'proxy']
24
25const kinds: { value: string; label: string }[] = [
26 { value: 'static', label: '静态站点' },
27 { value: 'php', label: 'PHP' },
28 { value: 'go', label: 'Go 应用' },
29 { value: 'node', label: 'Node 应用' },
30 { value: 'proxy', label: '反向代理' },
31]
32
33function kindLabel(kind: string) {
34 return kinds.find((k) => k.value === kind)?.label ?? kind
35}
36
37const filtered = computed(() => {
38 const q = query.value.trim().toLowerCase()
39 if (!q) return sites.value
40 return sites.value.filter(
41 (s) => s.name.toLowerCase().includes(q) || s.domains.some((d) => d.toLowerCase().includes(q)),
42 )
43})
44
45function isApp(site: Site) {
46 return appKinds.includes(site.kind)
47}
48
49function liveState(site: Site): { text: string; cls: string } {
50 const live = site.live_status ?? ''
51 if (live === 'preview') return { text: 'nginx 未安装', cls: 'err' }
52 if (!isApp(site)) {
53 return live === 'inactive'
54 ? { text: '已停止', cls: 'off' }
55 : { text: '运行中', cls: 'ok' }
56 }
57 switch (live) {
58 case 'active':
59 return { text: '运行中', cls: 'ok' }
60 case 'activating':
61 case 'reloading':
62 return { text: '启动中', cls: '' }
63 case 'failed':
64 return { text: '异常', cls: 'err' }
65 case 'inactive':
66 return { text: '已停止', cls: 'off' }
67 case 'unsupported':
68 return { text: '系统不支持', cls: 'err' }
69 default:
70 return live === ''
71 ? { text: site.status === 'running' ? '运行中' : '已停止', cls: site.status === 'running' ? 'ok' : 'off' }
72 : { text: '状态未知', cls: 'off' }
73 }
74}
75
76function httpPort(site: Site) {
77 return site.options.listen_port || 80
78}
79
80function appPort(site: Site) {
81 return isApp(site) ? site.port : 0
82}
83
84function siteURL(site: Site): string {
85 const host = site.domains.find((d) => !d.startsWith('*.')) ?? site.domains[0] ?? ''
86 if (!host) return ''
87 // nginx always terminates TLS on 443; the custom listener port is HTTP only.
88 if (site.ssl_enabled) return `https://${host}`
89 const port = httpPort(site)
90 const suffix = port && port !== 80 ? `:${port}` : ''
91 return `http://${host}${suffix}`
92}
93
94async function load() {
95 loading.value = true
96 try {
97 sites.value = await api.get<Site[]>('api/v1/sites')
98 const meta = await api.get<{ nginx: boolean }>('api/v1/system/meta')
99 nginxReady.value = meta.nginx
100 } catch (e) {
101 notify((e as Error).message, 'error')
102 } finally {
103 loading.value = false
104 }
105}
106
107function askDelete(site: Site) {
108 deleteError.value = ''
109 deleting.value = site
110}
111
112async function action(site: Site, act: string) {
113 busy.value = site.id
114 try {
115 await api.post(`api/v1/sites/${site.id}/action`, { action: act })
116 notify(`已${act === 'start' ? '启动' : act === 'stop' ? '停止' : '重启'} ${site.name}`)
117 await load()
118 } catch (e) {
119 notify((e as Error).message, 'error')
120 } finally {
121 busy.value = 0
122 }
123}
124
125function openSite(site: Site) {
126 const url = siteURL(site)
127 if (!url) {
128 notify('该站点还没有域名', 'warn')
129 return
130 }
131 window.open(url, '_blank', 'noopener')
132}
133
134async function copy(text: string, label = '已复制') {
135 if (!text) return
136 try {
137 await navigator.clipboard.writeText(text)
138 notify(label)
139 } catch {
140 notify('浏览器不允许访问剪贴板', 'error')
141 }
142}
143
144async function confirmDelete(payload: { option: string }) {
145 const site = deleting.value
146 if (!site) return
147 deleteBusy.value = true
148 deleteError.value = ''
149 try {
150 const removeFiles = payload.option === 'remove'
151 await api.del(`api/v1/sites/${site.id}?remove_files=${removeFiles ? 1 : 0}`)
152 notify(`站点 ${site.name} 已删除${removeFiles ? '(含网站目录)' : ''}`)
153 deleting.value = null
154 await load()
155 } catch (e) {
156 deleteError.value = (e as Error).message
157 } finally {
158 deleteBusy.value = false
159 }
160}
161
162function onCreated(site?: Site) {
163 load()
164 if (site?.id) navigate('sites', { id: site.id })
165}
166
167onMounted(load)
168</script>
169
170<template>
171 <SiteDetail v-if="detailId" :id="detailId" />
172
173 <template v-else>
174 <div v-if="!nginxReady" class="alert warn nginx-warn">
175 <div>
176 <b>nginx 尚未安装</b>:新建站点会写入配置并显示“运行中”,但网站实际上无法访问。
177 </div>
178 <button class="btn btn-primary btn-sm" @click="navigate('apps')">去应用商店安装 nginx</button>
179 </div>
180
181 <div class="card">
182 <div class="row">
183 <div>
184 <h2>网站列表</h2>
185 <p class="hint">静态、PHP、Go、Node 与反向代理站点统一管理</p>
186 </div>
187 <div class="spacer"></div>
188 <input
189 v-model="query"
190 class="input site-search"
191 placeholder="搜索站点或域名"
192 aria-label="搜索站点"
193 />
194 <button class="btn btn-ghost btn-sm" :disabled="loading" @click="load">
195 <span v-if="loading" class="spin"></span>
196 刷新
197 </button>
198 <button class="btn btn-primary btn-sm" @click="showCreate = true">新建站点</button>
199 </div>
200
201 <div class="table-wrap">
202 <table v-if="filtered.length" class="table">
203 <thead>
204 <tr>
205 <th>站点</th>
206 <th>类型</th>
207 <th>端口</th>
208 <th>状态</th>
209 <th>SSL</th>
210 <th>WAF</th>
211 <th class="ops-col">操作</th>
212 </tr>
213 </thead>
214 <tbody>
215 <tr v-for="site in filtered" :key="site.id">
216 <td>
217 <div class="site-title">
218 <button class="link-name" @click="navigate('sites', { id: site.id })">
219 {{ site.name }}
220 </button>
221 <button
222 v-if="siteURL(site)"
223 class="icon-btn mini"
224 title="打开站点"
225 @click="openSite(site)"
226 >
227 ↗
228 </button>
229 <button
230 v-if="siteURL(site)"
231 class="icon-btn mini"
232 title="复制访问地址"
233 @click="copy(siteURL(site), '访问地址已复制')"
234 >
235 ⧉
236 </button>
237 </div>
238 <div class="mono muted domains">{{ site.domains.join(' ') }}</div>
239 </td>
240 <td><span class="badge">{{ kindLabel(site.kind) }}</span></td>
241 <td class="mono">
242 <div>{{ httpPort(site) }}</div>
243 <div v-if="appPort(site)" class="muted tiny">应用 {{ appPort(site) }}</div>
244 </td>
245 <td>
246 <span class="badge" :class="liveState(site).cls">{{ liveState(site).text }}</span>
247 </td>
248 <td>
249 <span class="badge" :class="site.ssl_enabled ? 'ok' : 'off'">
250 {{ site.ssl_enabled ? '已启用' : '未启用' }}
251 </span>
252 </td>
253 <td>
254 <span class="badge" :class="site.options.waf ? 'ok' : 'off'">
255 {{ site.options.waf ? '开启' : '关闭' }}
256 </span>
257 </td>
258 <td>
259 <div class="row ops">
260 <button class="btn btn-sm btn-ghost" @click="navigate('sites', { id: site.id })">
261 详情
262 </button>
263 <button
264 v-if="isApp(site)"
265 class="btn btn-sm btn-ghost"
266 :disabled="busy === site.id"
267 @click="action(site, liveState(site).text === '运行中' ? 'stop' : 'start')"
268 >
269 {{ liveState(site).text === '运行中' ? '停止' : '启动' }}
270 </button>
271 <button class="btn btn-sm btn-danger" @click="askDelete(site)">删除</button>
272 </div>
273 </td>
274 </tr>
275 </tbody>
276 </table>
277 <div v-else-if="!loading" class="empty">
278 <p>还没有站点。建站前请确认已安装 nginx,并让域名解析到这台服务器。</p>
279 <button class="btn btn-primary btn-sm" @click="showCreate = true">新建第一个站点</button>
280 </div>
281 <div v-else class="empty">正在加载…</div>
282 </div>
283 </div>
284
285 <SiteCreateModal
286 v-if="showCreate"
287 @close="showCreate = false"
288 @created="onCreated"
289 @open-apps="navigate('apps')"
290 />
291
292 <ConfirmDialog
293 v-if="deleting"
294 :title="`删除站点 ${deleting.name}`"
295 message="删除后 nginx 配置、systemd 单元和该站点的证书记录都会被移除。"
296 confirm-text="删除站点"
297 danger
298 :options="[
299 { value: 'keep', label: '仅删除站点配置', description: '网站目录与文件完整保留,之后可以重新接入' },
300 {
301 value: 'remove',
302 label: '同时删除网站目录',
303 description: '文件将被永久删除,无法恢复',
304 danger: true,
305 },
306 ]"
307 :require-text-map="{ remove: deleting.name }"
308 :busy="deleteBusy"
309 :error="deleteError"
310 @close="deleting = null"
311 @confirm="confirmDelete"
312 />
313 </template>
314</template>
315
316<style scoped>
317.site-search {
318 width: 200px;
319}
320.site-title {
321 display: flex;
322 align-items: center;
323 gap: 4px;
324}
325.link-name {
326 border: none;
327 background: none;
328 padding: 0;
329 color: inherit;
330 font: inherit;
331 font-weight: 600;
332 cursor: pointer;
333}
334.link-name:hover {
335 color: #0f766e;
336 text-decoration: underline;
337}
338.icon-btn.mini {
339 font-size: 12px;
340 padding: 2px 4px;
341 color: var(--muted);
342}
343.domains {
344 margin-top: 2px;
345 font-size: 11.5px;
346}
347.tiny {
348 font-size: 11px;
349}
350.ops {
351 gap: 4px;
352 flex-wrap: wrap;
353}
354.ops-col {
355 width: 220px;
356}
357.nginx-warn {
358 display: flex;
359 align-items: center;
360 justify-content: space-between;
361 gap: 12px;
362 flex-wrap: wrap;
363}
364@media (max-width: 720px) {
365 .site-search {
366 width: 100%;
367 }
368}
369</style>