Files
huihuiSquare/digital-avatar-app/src/views/TokenCharge.vue
T

393 lines
7.9 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<div class="token-charge-page">
<!-- 顶部导航 -->
<header class="page-header">
<button class="back-btn" @click="goBack">‹</button>
<h1 class="page-title">Token 充值</h1>
<div class="header-right"></div>
</header>
<!-- 当前余额 -->
<section class="balance-section">
<div class="balance-card">
<span class="balance-label">当前余额</span>
<span class="balance-amount">{{ currentBalance.toLocaleString() }}</span>
<span class="balance-unit">Token</span>
<span class="balance-used">累计使用 {{ totalConsumed.toLocaleString() }} Token</span>
</div>
</section>
<!-- 充值套餐 -->
<section class="plans-section">
<h3 class="section-title">选择充值套餐</h3>
<div class="plan-list">
<div
class="plan-card"
v-for="plan in plans"
:key="plan.id"
:class="{ selected: selectedPlan?.id === plan.id }"
@click="selectedPlan = plan"
>
<div class="plan-badge" v-if="plan.badge">{{ plan.badge }}</div>
<div class="plan-amount">{{ formatTokenAmount(plan.amount) }}</div>
<div class="plan-unit">Token</div>
<div class="plan-price">¥{{ plan.price }}</div>
<div class="plan-desc" v-if="plan.desc">{{ plan.desc }}</div>
</div>
</div>
</section>
<!-- 支付方式 -->
<section class="payment-section">
<h3 class="section-title">支付方式</h3>
<div class="payment-list">
<div
class="payment-card"
:class="{ selected: paymentMethod === 'wechat' }"
@click="paymentMethod = 'wechat'"
>
<span class="payment-icon">💚</span>
<span class="payment-name">微信支付</span>
<span class="payment-check" v-if="paymentMethod === 'wechat'">✓</span>
</div>
<div
class="payment-card"
:class="{ selected: paymentMethod === 'alipay' }"
@click="paymentMethod = 'alipay'"
>
<span class="payment-icon">💙</span>
<span class="payment-name">支付宝</span>
<span class="payment-check" v-if="paymentMethod === 'alipay'">✓</span>
</div>
</div>
</section>
<!-- 充值按钮 -->
<section class="checkout-section">
<button
class="checkout-btn"
:class="{ disabled: !selectedPlan }"
:disabled="!selectedPlan"
@click="doCharge"
>
{{ selectedPlan ? `立即支付 ¥${selectedPlan.price}` : '请选择充值套餐' }}
</button>
</section>
</div>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { getTokenBalance, getRechargePlans, chargeToken } from '@/api'
const router = useRouter()
// 当前余额
const currentBalance = ref<number>(0)
const totalConsumed = ref<number>(0)
// 充值套餐
const plans = ref<Array<{
id: string
amount: number
price: number
badge?: string
desc?: string
}>>([])
// 选中的套餐
const selectedPlan = ref<any>(null)
// 支付方式
const paymentMethod = ref<'wechat' | 'alipay'>('wechat')
// 从后端加载余额与套餐
const loadData = async () => {
try {
const b: any = await getTokenBalance()
currentBalance.value = b?.balance ?? 0
totalConsumed.value = b?.totalConsumed ?? 0
} catch (e) {
console.error('加载余额失败', e)
}
try {
const p: any = await getRechargePlans()
plans.value = p || []
} catch (e) {
console.error('加载套餐失败', e)
}
}
// 执行充值(写入后端)
const charging = ref(false)
const formatTokenAmount = (amount: number) => {
if (amount >= 1_000_000 && amount % 1_000_000 === 0) return `${amount / 1_000_000}M`
return amount.toLocaleString()
}
const doCharge = async () => {
if (!selectedPlan.value || charging.value) return
charging.value = true
try {
const methodText = paymentMethod.value === 'wechat' ? '微信支付' : '支付宝'
const res: any = await chargeToken(selectedPlan.value.id)
currentBalance.value = res?.balance ?? currentBalance.value
alert(`已通过${methodText}成功充值,当前余额:${currentBalance.value} Token`)
} catch (e) {
alert('充值失败,请重试')
} finally {
charging.value = false
}
}
// 返回
const goBack = () => {
router.back()
}
onMounted(() => {
loadData()
})
</script>
<style scoped>
.token-charge-page {
min-height: 100vh;
background: #F8F9FA;
padding-bottom: 80px;
}
/* 顶部导航 */
.page-header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 16px 20px;
background: white;
border-bottom: 1px solid #EDEEF1;
}
.back-btn {
background: none;
border: none;
font-size: 24px;
cursor: pointer;
padding: 4px 8px;
color: #18191C;
}
.page-title {
font-size: 17px;
font-weight: 600;
margin: 0;
color: #18191C;
}
.header-right {
width: 32px;
}
/* 当前余额 */
.balance-section {
padding: 20px;
}
.balance-card {
display: flex;
flex-wrap: wrap;
align-items: baseline;
gap: 8px;
padding: 20px;
background: linear-gradient(135deg, #F97316 0%, #FB923C 100%);
border-radius: 16px;
color: white;
box-shadow: 0 4px 12px rgba(249, 115, 22, 0.3);
}
.balance-label {
font-size: 14px;
opacity: 0.9;
}
.balance-amount {
font-size: 32px;
font-weight: 700;
}
.balance-unit {
font-size: 14px;
opacity: 0.9;
}
.balance-used {
flex-basis: 100%;
font-size: 12px;
opacity: 0.82;
}
/* 充值套餐 */
.plans-section {
padding: 0 20px 20px;
}
.section-title {
font-size: 16px;
font-weight: 600;
margin: 0 0 16px;
color: #18191C;
}
.plan-list {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 12px;
}
.plan-card {
position: relative;
padding: 20px 16px;
background: white;
border-radius: 12px;
text-align: center;
border: 2px solid #EDEEF1;
cursor: pointer;
transition: all 0.2s;
}
.plan-card:hover {
border-color: #F97316;
}
.plan-card.selected {
border-color: #F97316;
background: #FFF0E6;
}
.plan-badge {
position: absolute;
top: -1px;
right: -1px;
background: #F97316;
color: white;
padding: 4px 12px;
border-radius: 0 12px 0 12px;
font-size: 11px;
font-weight: 600;
}
.plan-amount {
font-size: 28px;
font-weight: 700;
color: #18191C;
margin-bottom: 4px;
}
.plan-unit {
font-size: 12px;
color: #9398AE;
margin-bottom: 12px;
}
.plan-price {
font-size: 20px;
font-weight: 600;
color: #F97316;
margin-bottom: 8px;
}
.plan-desc {
font-size: 11px;
color: #9398AE;
}
/* 支付方式 */
.payment-section {
padding: 0 20px 20px;
}
.payment-list {
display: flex;
flex-direction: column;
gap: 12px;
}
.payment-card {
display: flex;
align-items: center;
gap: 12px;
padding: 16px;
background: white;
border-radius: 12px;
border: 2px solid #EDEEF1;
cursor: pointer;
transition: all 0.2s;
}
.payment-card:hover {
border-color: #F97316;
}
.payment-card.selected {
border-color: #F97316;
background: #FFF0E6;
}
.payment-icon {
font-size: 24px;
}
.payment-name {
flex: 1;
font-size: 15px;
font-weight: 500;
color: #18191C;
}
.payment-check {
width: 24px;
height: 24px;
border-radius: 50%;
background: #F97316;
color: white;
display: flex;
align-items: center;
justify-content: center;
font-size: 14px;
font-weight: 600;
}
/* 充值按钮 */
.checkout-section {
padding: 0 20px;
}
.checkout-btn {
width: 100%;
padding: 16px;
background: linear-gradient(135deg, #F97316 0%, #FB923C 100%);
color: white;
border: none;
border-radius: 16px;
font-size: 17px;
font-weight: 600;
cursor: pointer;
box-shadow: 0 4px 12px rgba(249, 115, 22, 0.3);
transition: all 0.2s;
}
.checkout-btn:hover {
transform: translateY(-2px);
box-shadow: 0 8px 20px rgba(249, 115, 22, 0.4);
}
.checkout-btn.disabled {
background: #D1D5DB;
box-shadow: none;
cursor: not-allowed;
}
.checkout-btn.disabled:hover {
transform: none;
}
</style>