better-staridc-MNBT
1<template>
2 <div class="td-page td-form-narrow">
3 <div class="td-card">
4 <div class="td-card-head">余额充值</div>
5 <div class="td-card-body">
6 <p class="recharge-tip">选择支付方式并输入充值金额,金额到账后可用于购买主机等服务。</p>
7
8 <div v-if="methods.length">
9 <div class="form-label">支付方式</div>
10 <div class="pay-grid">
11 <label
12 v-for="m in methods"
13 :key="m.plugin + '__' + m.method"
14 class="pay-item"
15 :class="{ active: type === m.plugin + '__' + m.method }"
16 >
17 <input
18 type="radio"
19 name="paytype"
20 :value="m.plugin + '__' + m.method"
21 v-model="type"
22 />
23 <i class="mdi mdi-credit-card-outline"></i>
24 <span>{{ m.display_name || m.plugin + ' / ' + m.method }}</span>
25 <i class="mdi mdi-check pay-check" v-if="type === m.plugin + '__' + m.method"></i>
26 </label>
27 </div>
28
29 <div class="form-label">充值金额</div>
30 <t-input-number
31 v-model="amount"
32 :min="1"
33 :max="50000"
34 :step="10"
35 :precision="2"
36 theme="column"
37 size="large"
38 placeholder="最低 1 元"
39 style="width: 100%"
40 >
41 <template #prefixIcon><i class="mdi mdi-currency-cny"></i></template>
42 </t-input-number>
43
44 <div class="quick-btns">
45 <t-button v-for="v in quickAmounts" :key="v" variant="outline" size="small" @click="amount = v">
46 {{ v }} 元
47 </t-button>
48 </div>
49
50 <t-button theme="primary" size="large" block style="margin-top: 18px" :loading="loading" @click="onSubmit">
51 立即充值
52 </t-button>
53 </div>
54
55 <t-empty v-else description="暂无可用的支付方式,请联系管理员启用支付插件" style="padding: 40px 0" />
56 </div>
57 </div>
58 </div>
59</template>
60
61<script setup>
62import { ref, onMounted } from 'vue'
63import { MessagePlugin } from 'tdesign-vue-next'
64import { getBalanceMethods, createRecharge, goPay } from '@/account/api/plugins'
65
66const methods = ref([])
67const type = ref('')
68const amount = ref(50)
69const loading = ref(false)
70const quickAmounts = [10, 50, 100, 500]
71
72async function loadMethods() {
73 const res = await getBalanceMethods()
74 if (!res.ok) return
75 methods.value = res.data.methods || []
76 if (methods.value.length) {
77 type.value = methods.value[0].plugin + '__' + methods.value[0].method
78 }
79}
80
81async function onSubmit() {
82 if (!type.value) {
83 MessagePlugin.warning('请选择支付方式')
84 return
85 }
86 if (!amount.value || amount.value < 1) {
87 MessagePlugin.warning('充值金额至少 1 元')
88 return
89 }
90 loading.value = true
91 const res = await createRecharge(amount.value, type.value)
92 loading.value = false
93 if (!res.ok) {
94 MessagePlugin.error(res.message || '创建订单失败')
95 return
96 }
97 if (!goPay(res)) {
98 MessagePlugin.success('充值订单已创建')
99 }
100}
101
102onMounted(loadMethods)
103</script>
104
105<style scoped>
106.recharge-tip {
107 margin: 0 0 18px;
108 font-size: 13px;
109 color: var(--td-text-secondary);
110}
111.form-label {
112 font-size: 13px;
113 font-weight: 600;
114 color: var(--td-text);
115 margin: 0 0 10px;
116}
117.pay-grid {
118 display: grid;
119 grid-template-columns: repeat(2, 1fr);
120 gap: 10px;
121 margin-bottom: 18px;
122}
123.pay-item {
124 position: relative;
125 display: flex;
126 align-items: center;
127 gap: 10px;
128 padding: 12px 14px;
129 border: 1px solid var(--td-border);
130 border-radius: 10px;
131 cursor: pointer;
132 font-size: 13px;
133 color: var(--td-text);
134 transition: border-color var(--td-dur) var(--td-ease),
135 background var(--td-dur) var(--td-ease),
136 box-shadow var(--td-dur) var(--td-ease);
137}
138.pay-item input {
139 position: absolute;
140 opacity: 0;
141 pointer-events: none;
142}
143.pay-item i:first-child {
144 font-size: 20px;
145 color: var(--td-text-placeholder);
146}
147.pay-item:hover {
148 border-color: var(--td-brand);
149}
150.pay-item.active {
151 border-color: var(--td-brand);
152 background: var(--td-brand-light);
153 color: var(--td-brand);
154 box-shadow: 0 0 0 1px var(--td-brand) inset;
155}
156.pay-item.active i:first-child {
157 color: var(--td-brand);
158}
159.pay-check {
160 margin-left: auto;
161 font-size: 16px;
162}
163.quick-btns {
164 display: flex;
165 gap: 8px;
166 margin-top: 10px;
167 flex-wrap: wrap;
168}
169
170@media (max-width: 480px) {
171 .pay-grid {
172 grid-template-columns: 1fr;
173 }
174}
175</style>