仰望星辰工作室

miaopanel

miaopanel/ web/src/components/docker/BindDomainModal.vue 5.2 KB · 172 行 原始文件
1<script setup lang="ts">
2import { computed, inject, ref } from 'vue'
3import { api } from '../../api'
4import type { DockerBinding, DockerContainer } from '../../types'
5
6const props = defineProps<{ container: DockerContainer; nginxReady: boolean }>()
7const emit = defineEmits<{ (e: 'close'): void; (e: 'changed'): void }>()
8const notify = inject<(text: string, kind?: 'ok' | 'error') => void>('notify')!
9
10const published = computed(() => props.container.ports.filter((p) => p.published && p.host_port > 0))
11const hostPort = ref(published.value[0]?.host_port ?? 0)
12const domains = ref('')
13const busy = ref(false)
14const bindings = ref<DockerBinding[]>([...props.container.bindings])
15const unbinding = ref<number | null>(null)
16
17async function submit() {
18 if (busy.value) return
19 if (!props.nginxReady) {
20 notify('请先在应用商店安装 nginx,再绑定域名', 'error')
21 return
22 }
23 if (!hostPort.value) {
24 notify('该容器没有已发布的端口,请先用 -p 映射端口', 'error')
25 return
26 }
27 const list = domains.value
28 .split(/[\s,,]+/)
29 .map((d) => d.trim())
30 .filter(Boolean)
31 if (list.length === 0) {
32 notify('请至少填写一个域名', 'error')
33 return
34 }
35 busy.value = true
36 try {
37 await api.post('api/v1/docker/bindings', {
38 container_id: props.container.id,
39 host_port: hostPort.value,
40 domains: list,
41 })
42 notify('域名绑定成功,已创建反向代理站点')
43 emit('changed')
44 emit('close')
45 } catch (e) {
46 notify((e as Error).message, 'error')
47 } finally {
48 busy.value = false
49 }
50}
51
52async function unbind(binding: DockerBinding) {
53 if (unbinding.value !== null) return
54 if (!window.confirm(`解绑 ${binding.domains.join('、')}?对应反代站点会被删除。`)) return
55 unbinding.value = binding.site_id
56 try {
57 await api.del(`api/v1/docker/bindings/${binding.site_id}`)
58 bindings.value = bindings.value.filter((b) => b.site_id !== binding.site_id)
59 notify('已解绑')
60 emit('changed')
61 } catch (e) {
62 notify((e as Error).message, 'error')
63 } finally {
64 unbinding.value = null
65 }
66}
67</script>
68
69<template>
70 <div class="overlay" @click.self="emit('close')">
71 <div class="modal" style="width: min(620px, 100%)">
72 <div class="modal-head">
73 <h3>绑定域名 · {{ container.name }}</h3>
74 <button class="icon-btn" title="关闭" @click="emit('close')">×</button>
75 </div>
76
77 <p class="hint">
78 面板会为选中的宿主端口创建一个 nginx 反向代理站点,之后可在「网站」页继续申请 SSL、
79 调整 WAF 与域名。
80 </p>
81
82 <div v-if="!nginxReady" class="alert danger" style="margin-bottom: 12px">
83 尚未安装 nginx,无法创建反向代理。请先到应用商店安装 nginx。
84 </div>
85
86 <template v-if="published.length === 0">
87 <div class="alert danger">
88 该容器没有发布到宿主机的端口。请使用 <code>-p 宿主端口:容器端口</code> 重新创建容器。
89 </div>
90 </template>
91
92 <template v-else>
93 <label class="field">
94 宿主端口
95 <select v-model.number="hostPort" class="select">
96 <option v-for="p in published" :key="p.host_port + p.protocol" :value="p.host_port">
97 {{ p.host_ip || '0.0.0.0' }}:{{ p.host_port }} → 容器 {{ p.container_port }}/{{ p.protocol }}
98 </option>
99 </select>
100 </label>
101
102 <label class="field">
103 域名(每行一个,也可用逗号分隔)
104 <textarea
105 v-model="domains"
106 class="textarea mono"
107 rows="3"
108 placeholder="panel.example.com"
109 :disabled="busy"
110 ></textarea>
111 </label>
112 </template>
113
114 <div v-if="bindings.length" class="bind-list">
115 <div class="bind-title">已绑定</div>
116 <div v-for="b in bindings" :key="b.site_id" class="bind-row">
117 <div>
118 <b class="mono">{{ b.domains.join('、') }}</b>
119 <div class="hint">:{{ b.host_port }} → {{ b.container_port }} · 站点 #{{ b.site_id }}</div>
120 </div>
121 <button
122 class="btn btn-ghost btn-sm"
123 :disabled="unbinding === b.site_id"
124 @click="unbind(b)"
125 >
126 <span v-if="unbinding === b.site_id" class="spin"></span>
127 解绑
128 </button>
129 </div>
130 </div>
131
132 <div class="modal-foot">
133 <div class="spacer"></div>
134 <button class="btn btn-ghost" :disabled="busy" @click="emit('close')">取消</button>
135 <button
136 class="btn btn-primary"
137 :disabled="busy || !nginxReady || published.length === 0"
138 @click="submit"
139 >
140 <span v-if="busy" class="spin"></span>
141 绑定域名
142 </button>
143 </div>
144 </div>
145 </div>
146</template>
147
148<style scoped>
149.bind-list {
150 margin-top: 12px;
151 border-top: 1px solid var(--line);
152 padding-top: 10px;
153}
154.bind-title {
155 font-size: 12px;
156 color: var(--muted);
157 margin-bottom: 6px;
158}
159.bind-row {
160 display: flex;
161 align-items: center;
162 gap: 10px;
163 padding: 8px 10px;
164 border-radius: 10px;
165 background: #f7fcfb;
166 margin-bottom: 6px;
167}
168.bind-row > div {
169 min-width: 0;
170 flex: 1;
171}
172</style>