better-staridc-MNBT
1<template>
2 <div class="td-page">
3 <div class="td-page-head">
4 <div>
5 <h3 class="td-page-title"><i class="mdi mdi-server"></i> 我的云服务器</h3>
6 <p class="td-page-subtitle">查看已开通的主机,可执行开机/关机/重启/重置密码,管理详情跳转独立页面</p>
7 </div>
8 <t-button variant="outline" @click="router.push('/zjmf-shop')">
9 <i class="mdi mdi-cart"></i> 前往选购
10 </t-button>
11 </div>
12
13 <div v-if="hosts.length" class="zjmf-asset-grid">
14 <div class="zjmf-asset td-card" v-for="h in hosts" :key="h.id">
15 <div class="zjmf-asset-head">
16 <div class="zjmf-asset-icon"><i class="mdi mdi-cloud-outline"></i></div>
17 <div class="zjmf-asset-info">
18 <strong>{{ h.name }}</strong>
19 <span class="zjmf-asset-node">{{ cycleText(h.cycle) }}</span>
20 </div>
21 <t-tag :theme="statusTheme(h.status)" variant="light">
22 {{ statusText(h.status) }}
23 </t-tag>
24 </div>
25
26 <div class="zjmf-asset-meta">
27 <div class="meta-item">
28 <span>供应商</span>
29 <b>{{ h.supplier_name || '—' }}</b>
30 </div>
31 <div class="meta-item">
32 <span>主机账号</span>
33 <b class="mono">{{ h.username || '—' }}</b>
34 </div>
35 <div class="meta-item">
36 <span>上游主机 ID</span>
37 <b>{{ h.up_host_id > 0 ? h.up_host_id : '—' }}</b>
38 </div>
39 <div class="meta-item">
40 <span>到期时间</span>
41 <b :class="{ expired: isExpired(h) }">{{ h.renew_date || '—' }}</b>
42 </div>
43 <div class="meta-item">
44 <span>开通时间</span>
45 <b>{{ h.created_at || '—' }}</b>
46 </div>
47 </div>
48
49 <div class="zjmf-asset-actions">
50 <t-button
51 theme="primary"
52 variant="outline"
53 size="small"
54 :href="reserveUrl('hosts/' + h.id)"
55 target="_blank"
56 rel="noopener"
57 >
58 <i class="mdi mdi-cog-outline"></i> 管理详情
59 </t-button>
60 <t-button
61 variant="outline"
62 size="small"
63 theme="success"
64 :disabled="!canAction(h, 'on')"
65 :loading="h._acting === 'on'"
66 @click="onAction(h, 'on')"
67 >
68 <i class="mdi mdi-play"></i> 开机
69 </t-button>
70 <t-button
71 variant="outline"
72 size="small"
73 theme="warning"
74 :disabled="!canAction(h, 'off')"
75 :loading="h._acting === 'off'"
76 @click="onAction(h, 'off')"
77 >
78 <i class="mdi mdi-stop"></i> 关机
79 </t-button>
80 <t-button
81 variant="outline"
82 size="small"
83 :disabled="!canAction(h, 'reboot')"
84 :loading="h._acting === 'reboot'"
85 @click="onAction(h, 'reboot')"
86 >
87 <i class="mdi mdi-restart"></i> 重启
88 </t-button>
89 <t-button
90 variant="outline"
91 size="small"
92 :loading="h._acting === 'reset_password'"
93 @click="onResetPassword(h)"
94 >
95 <i class="mdi mdi-key-change"></i> 重置密码
96 </t-button>
97 </div>
98 </div>
99 </div>
100
101 <t-empty v-else description="暂无云服务器,前往商城选购" style="padding: 60px 0">
102 <t-button theme="primary" @click="router.push('/zjmf-shop')">去选购商品</t-button>
103 </t-empty>
104
105 <!-- 重置密码对话框 -->
106 <t-dialog
107 v-model:visible="pwdVisible"
108 :header="'重置密码:' + (currentHost?.name || '')"
109 :confirm-btn="{ content: '确认重置', theme: 'danger', loading: pwdSubmitting }"
110 :cancel-btn="'取消'"
111 :on-confirm="onConfirmPwd"
112 >
113 <p style="font-size: 13px; color: var(--td-text-secondary); margin: 0 0 12px">
114 新密码将立即生效,请妥善保存。
115 </p>
116 <t-input v-model="newPassword" type="password" placeholder="输入新密码" clearable />
117 </t-dialog>
118 </div>
119</template>
120
121<script setup>
122import { ref, onMounted } from 'vue'
123import { useRouter } from 'vue-router'
124import { MessagePlugin, DialogPlugin } from 'tdesign-vue-next'
125import { getZjmfHosts, zjmfHostAction } from '@/account/api/plugins'
126
127const router = useRouter()
128const boot = window.__TD_BOOT__ || {}
129
130const hosts = ref([])
131
132// 重置密码
133const pwdVisible = ref(false)
134const currentHost = ref(null)
135const newPassword = ref('')
136const pwdSubmitting = ref(false)
137
138function statusText(status) {
139 return { active: '运行中', suspend: '已暂停', pending: '待开通', terminated: '已终止', unknown: '未知' }[status] || status
140}
141
142function cycleText(cycle) {
143 const map = {
144 Monthly: '月付',
145 Quarterly: '季付',
146 SemiAnnually: '半年付',
147 Annually: '年付',
148 Biennially: '两年付',
149 Triennially: '三年付',
150 }
151 return map[cycle] || cycle || ''
152}
153
154function statusTheme(status) {
155 if (status === 'active') return 'success'
156 if (status === 'suspend') return 'warning'
157 if (status === 'pending') return 'warning'
158 if (status === 'terminated') return 'danger'
159 return 'default'
160}
161
162function isExpired(h) {
163 return !!h.renew_date && h.renew_date < new Date().toISOString().slice(0, 10)
164}
165
166/** 操作可用性:运行中才能关机/重启,已暂停/未知才能开机 */
167function canAction(h, action) {
168 if (h.up_host_id <= 0) return false
169 const active = h.status === 'active'
170 if (action === 'on') return !active
171 if (action === 'off') return active
172 if (action === 'reboot') return active
173 return true
174}
175
176function reserveUrl(path) {
177 const base = boot.routeBase || '/index.php?_r='
178 return base + 'reserve/' + String(path).replace(/^\/+/, '')
179}
180
181function onAction(h, action) {
182 const labels = { on: '开机', off: '关机', reboot: '重启' }
183 const dialog = DialogPlugin.confirm({
184 header: labels[action],
185 body: `确定要执行「${labels[action]}」操作吗?`,
186 confirmBtn: { content: '确认', theme: action === 'off' ? 'danger' : 'primary' },
187 onConfirm: async () => {
188 dialog.destroy()
189 h._acting = action
190 const res = await zjmfHostAction(h.id, action)
191 h._acting = ''
192 if (!res.ok) {
193 MessagePlugin.error(res.message || '操作失败')
194 return
195 }
196 MessagePlugin.success('操作成功')
197 },
198 onClose: () => dialog.destroy(),
199 })
200}
201
202function onResetPassword(h) {
203 currentHost.value = h
204 newPassword.value = ''
205 pwdVisible.value = true
206}
207
208async function onConfirmPwd() {
209 const pwd = newPassword.value.trim()
210 if (!pwd) {
211 MessagePlugin.warning('请输入新密码')
212 return false
213 }
214 pwdSubmitting.value = true
215 const res = await zjmfHostAction(currentHost.value.id, 'reset_password', pwd)
216 pwdSubmitting.value = false
217 if (!res.ok) {
218 MessagePlugin.error(res.message || '重置失败')
219 return false
220 }
221 pwdVisible.value = false
222 MessagePlugin.success('密码已重置,请及时保存')
223 return true
224}
225
226async function load() {
227 const res = await getZjmfHosts()
228 if (res.ok) hosts.value = res.data.hosts || []
229}
230
231onMounted(load)
232</script>
233
234<style scoped>
235.zjmf-asset-grid {
236 display: grid;
237 grid-template-columns: repeat(2, 1fr);
238 gap: 16px;
239}
240.zjmf-asset {
241 padding: 18px;
242}
243.zjmf-asset-head {
244 display: flex;
245 align-items: center;
246 gap: 12px;
247}
248.zjmf-asset-icon {
249 width: 42px;
250 height: 42px;
251 border-radius: 10px;
252 background: var(--td-brand-light);
253 color: var(--td-brand);
254 display: grid;
255 place-items: center;
256 font-size: 22px;
257 flex-shrink: 0;
258}
259.zjmf-asset-info {
260 flex: 1;
261 min-width: 0;
262 display: flex;
263 flex-direction: column;
264}
265.zjmf-asset-info strong {
266 font-size: 15px;
267 color: var(--td-text);
268 font-weight: 600;
269 white-space: nowrap;
270 overflow: hidden;
271 text-overflow: ellipsis;
272}
273.zjmf-asset-node {
274 font-size: 12px;
275 color: var(--td-text-secondary);
276 margin-top: 2px;
277}
278.zjmf-asset-meta {
279 margin-top: 14px;
280 border-top: 1px dashed var(--td-border);
281 padding-top: 12px;
282}
283.meta-item {
284 display: flex;
285 align-items: center;
286 justify-content: space-between;
287 font-size: 13px;
288 padding: 4px 0;
289}
290.meta-item span {
291 color: var(--td-text-placeholder);
292}
293.meta-item b {
294 color: var(--td-text);
295 font-weight: 500;
296}
297.meta-item b.expired {
298 color: var(--td-error);
299}
300.meta-item .mono {
301 font-family: Consolas, Monaco, monospace;
302}
303.zjmf-asset-actions {
304 display: flex;
305 gap: 8px;
306 margin-top: 14px;
307 flex-wrap: wrap;
308}
309
310@media (max-width: 860px) {
311 .zjmf-asset-grid {
312 grid-template-columns: 1fr;
313 }
314}
315</style>