仰望星辰工作室

better-staridc-MNBT

better-staridc-MNBT/ templates/tdesign/spa/src/account/views/DockerShopView.vue 9.4 KB · 361 行 原始文件
Z zfhsh first commit 2 个月前
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-docker"></i> Docker 商城</h3>
6 <p class="td-page-subtitle">选择适合你的 Docker 容器套餐,支付后自动开通</p>
7 </div>
8 <t-button variant="outline" @click="router.push('/docker-assets')">
9 <i class="mdi mdi-docker"></i> 我的 Docker
10 </t-button>
11 </div>
12
13 <div v-if="plans.length" class="plan-grid">
14 <div class="plan-card td-card" v-for="plan in plans" :key="plan.id">
15 <div class="plan-head">
16 <div class="plan-name">{{ plan.name }}</div>
17 <span v-if="plan.category" class="plan-category">{{ plan.category }}</span>
18 </div>
19 <p class="plan-desc">{{ plan.description || '暂无介绍' }}</p>
20
21 <ul class="plan-specs">
22 <li><i class="mdi mdi-chip"></i><span>CPU 核心</span><b>{{ cpuText(plan) }}</b></li>
23 <li><i class="mdi mdi-memory"></i><span>内存</span><b>{{ memText(plan) }}</b></li>
24 <li><i class="mdi mdi-harddisk"></i><span>磁盘</span><b>{{ diskText(plan) }}</b></li>
25 <li><i class="mdi mdi-shield-lock-outline"></i><span>代理</span><b>{{ proxyText(plan) }}</b></li>
26 <li><i class="mdi mdi-server-network"></i><span>开通节点</span><b>{{ plan.node?.name || '—' }}</b></li>
27 </ul>
28
29 <div class="plan-foot">
30 <div class="plan-price">
31 <template v-if="plan.periods.length">
32 <span class="price-num">¥{{ centsToYuan(minPrice(plan)) }}</span>
33 <span class="price-unit">起</span>
34 </template>
35 <span v-else class="price-empty">未设置价格</span>
36 </div>
37 <t-button theme="primary" size="small" :disabled="!plan.periods.length" @click="openOrder(plan)">
38 {{ plan.periods.length ? '立即购买' : '暂不可购' }}
39 </t-button>
40 </div>
41 </div>
42 </div>
43
44 <t-empty v-else description="暂无可购买的 Docker 套餐" style="padding: 60px 0" />
45
46 <!-- 下单对话框 -->
47 <t-dialog
48 v-model:visible="dialogVisible"
49 :header="'购买:' + (currentPlan?.name || '')"
50 :confirm-btn="{ content: '确认购买', theme: 'primary', loading: submitting }"
51 :cancel-btn="'取消'"
52 :on-confirm="onConfirmOrder"
53 >
54 <div class="order-body">
55 <div class="order-label">购买周期</div>
56 <div class="period-grid">
57 <label
58 v-for="p in currentPeriods"
59 :key="p"
60 class="period-item"
61 :class="{ active: period === p }"
62 >
63 <input type="radio" name="period" :value="p" v-model="period" />
64 <span class="period-name">{{ periodLabel(p) }}</span>
65 <span class="period-price">¥{{ centsToYuan(currentPlan.prices[p]) }}</span>
66 </label>
67 </div>
68
69 <template v-if="isFreePlan">
70 <div class="free-tip"><i class="mdi mdi-check-circle-outline"></i> 免费套餐,下单后立即开通</div>
71 </template>
72 <template v-else>
73 <div class="order-label" style="margin-top: 16px">支付方式</div>
74 <div class="period-grid">
75 <label
76 v-for="m in methods"
77 :key="m.plugin + '__' + m.method"
78 class="period-item"
79 :class="{ active: payType === m.plugin + '__' + m.method }"
80 >
81 <input type="radio" name="paytype" :value="m.plugin + '__' + m.method" v-model="payType" />
82 <span class="period-name">{{ m.display_name || m.plugin + ' / ' + m.method }}</span>
83 </label>
84 </div>
85 <p v-if="!methods.length" class="no-methods">暂无可用的支付方式</p>
86 </template>
87 </div>
88 </t-dialog>
89 </div>
90</template>
91
92<script setup>
93import { ref, computed, onMounted } from 'vue'
94import { useRouter } from 'vue-router'
95import { MessagePlugin } from 'tdesign-vue-next'
96import { getDockerShopPlans, getDockerShopMethods, createDockerShopOrder, centsToYuan, goPay } from '@/account/api/plugins'
97
98const router = useRouter()
99
100const PERIODS = {
101 month: '月付',
102 quarter: '季付',
103 half_year: '半年付',
104 year: '年付',
105 two_year: '两年付',
106 three_year: '三年付',
107}
108
109const plans = ref([])
110const methods = ref([])
111
112// 下单状态
113const dialogVisible = ref(false)
114const currentPlan = ref(null)
115const period = ref('')
116const payType = ref('')
117const submitting = ref(false)
118
119const currentPeriods = computed(() => currentPlan.value?.periods || [])
120const isFreePlan = computed(() => {
121 const plan = currentPlan.value
122 if (!plan) return false
123 return currentPeriods.value.every((p) => !plan.prices[p])
124})
125
126function periodLabel(p) {
127 return PERIODS[p] || p
128}
129
130function minPrice(plan) {
131 const prices = (plan.periods || []).map((p) => plan.prices[p] || 0).filter((v) => v > 0)
132 return prices.length ? Math.min(...prices) : 0
133}
134
135function cpuText(plan) {
136 const v = Number(plan.base_plan?.cpu_max)
137 return v > 0 ? v + ' 核' : '—'
138}
139
140function memText(plan) {
141 const mb = Number(plan.base_plan?.mem_max)
142 if (!mb) return '—'
143 return mb >= 1024 ? (mb / 1024) + ' GB' : mb + ' MB'
144}
145
146function diskText(plan) {
147 const mb = Number(plan.base_plan?.disk_max)
148 if (!mb) return '—'
149 return mb >= 1024 ? (mb / 1024) + ' GB' : mb + ' MB'
150}
151
152function proxyText(plan) {
153 const v = Number(plan.base_plan?.proxy_max)
154 return v > 0 ? v + ' 个' : '不限'
155}
156
157function openOrder(plan) {
158 currentPlan.value = plan
159 period.value = plan.periods?.[0] || ''
160 payType.value = methods.value[0] ? methods.value[0].plugin + '__' + methods.value[0].method : ''
161 dialogVisible.value = true
162}
163
164async function onConfirmOrder() {
165 if (!currentPlan.value) return
166 if (!isFreePlan.value && !payType.value) {
167 MessagePlugin.warning('请选择支付方式')
168 return false
169 }
170 submitting.value = true
171 const res = await createDockerShopOrder(currentPlan.value.id, period.value, payType.value)
172 submitting.value = false
173 if (!res.ok) {
174 MessagePlugin.error(res.message || '创建订单失败')
175 return false
176 }
177 if (!goPay(res)) {
178 MessagePlugin.success('订单创建成功')
179 dialogVisible.value = false
180 }
181 return true
182}
183
184async function load() {
185 const [plansRes, methodsRes] = await Promise.all([getDockerShopPlans(), getDockerShopMethods()])
186 if (plansRes.ok) plans.value = plansRes.data.plans || []
187 if (methodsRes.ok) methods.value = methodsRes.data.methods || []
188}
189
190onMounted(load)
191</script>
192
193<style scoped>
194.plan-grid {
195 display: grid;
196 grid-template-columns: repeat(3, 1fr);
197 gap: 16px;
198}
199.plan-card {
200 padding: 20px;
201 display: flex;
202 flex-direction: column;
203 transition: transform var(--td-dur) var(--td-ease),
204 box-shadow var(--td-dur) var(--td-ease);
205}
206.plan-card:hover {
207 transform: translateY(-3px);
208 box-shadow: var(--td-shadow-md);
209}
210.plan-head {
211 display: flex;
212 align-items: center;
213 justify-content: space-between;
214 gap: 8px;
215}
216.plan-name {
217 font-size: 16px;
218 font-weight: 700;
219 color: var(--td-text);
220}
221.plan-category {
222 font-size: 12px;
223 color: var(--td-brand);
224 background: var(--td-brand-light);
225 padding: 2px 8px;
226 border-radius: 10px;
227 white-space: nowrap;
228}
229.plan-desc {
230 margin: 8px 0 14px;
231 font-size: 13px;
232 color: var(--td-text-secondary);
233 line-height: 1.6;
234 min-height: 40px;
235 display: -webkit-box;
236 -webkit-line-clamp: 2;
237 -webkit-box-orient: vertical;
238 overflow: hidden;
239}
240.plan-specs {
241 list-style: none;
242 margin: 0 0 16px;
243 padding: 0;
244 flex: 1;
245}
246.plan-specs li {
247 display: flex;
248 align-items: center;
249 gap: 8px;
250 font-size: 13px;
251 color: var(--td-text-secondary);
252 padding: 5px 0;
253}
254.plan-specs li i {
255 font-size: 17px;
256 color: var(--td-brand);
257 width: 20px;
258 text-align: center;
259}
260.plan-specs li span {
261 flex: 1;
262}
263.plan-specs li b {
264 color: var(--td-text);
265 font-weight: 600;
266}
267.plan-foot {
268 display: flex;
269 align-items: center;
270 justify-content: space-between;
271 border-top: 1px solid var(--td-border);
272 padding-top: 14px;
273}
274.plan-price {
275 display: flex;
276 align-items: baseline;
277 gap: 2px;
278}
279.price-num {
280 font-size: 22px;
281 font-weight: 700;
282 color: var(--td-error);
283}
284.price-unit {
285 font-size: 12px;
286 color: var(--td-text-placeholder);
287}
288.price-empty {
289 font-size: 13px;
290 color: var(--td-text-placeholder);
291}
292
293/* 下单对话框 */
294.order-body {
295 padding: 4px 0 0;
296}
297.order-label {
298 font-size: 13px;
299 font-weight: 600;
300 color: var(--td-text);
301 margin-bottom: 10px;
302}
303.period-grid {
304 display: grid;
305 grid-template-columns: repeat(2, 1fr);
306 gap: 8px;
307}
308.period-item {
309 display: flex;
310 flex-direction: column;
311 gap: 2px;
312 padding: 10px 12px;
313 border: 1px solid var(--td-border);
314 border-radius: 10px;
315 cursor: pointer;
316 font-size: 13px;
317 color: var(--td-text);
318 transition: border-color var(--td-dur) var(--td-ease),
319 background var(--td-dur) var(--td-ease);
320}
321.period-item input {
322 position: absolute;
323 opacity: 0;
324 pointer-events: none;
325}
326.period-item.active {
327 border-color: var(--td-brand);
328 background: var(--td-brand-light);
329}
330.period-name {
331 font-weight: 600;
332}
333.period-price {
334 font-size: 12px;
335 color: var(--td-brand);
336}
337.free-tip {
338 margin-top: 14px;
339 font-size: 13px;
340 color: var(--td-success);
341 display: flex;
342 align-items: center;
343 gap: 6px;
344}
345.no-methods {
346 margin: 12px 0 0;
347 font-size: 13px;
348 color: var(--td-text-placeholder);
349}
350
351@media (max-width: 1080px) {
352 .plan-grid {
353 grid-template-columns: repeat(2, 1fr);
354 }
355}
356@media (max-width: 680px) {
357 .plan-grid {
358 grid-template-columns: 1fr;
359 }
360}
361</style>