仰望星辰工作室

better-staridc-MNBT

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