2
0

fix:充值界面接口对接

This commit is contained in:
zhongzm
2024-12-24 19:12:07 +08:00
parent b214cc0552
commit 7faf830a77

View File

@@ -2,12 +2,11 @@
import { ref, computed, onMounted, onUnmounted } from 'vue'; import { ref, computed, onMounted, onUnmounted } from 'vue';
import { useBillingStore } from '@/store/modules/billing/billing'; import { useBillingStore } from '@/store/modules/billing/billing';
import { useI18n } from 'vue-i18n'; import { useI18n } from 'vue-i18n';
import { useFormRules } from '@/hooks/common/form'; import { submitOrder } from '@/service/api/auth';
import { message } from 'ant-design-vue';
import type { Ref } from 'vue'; import type { Ref } from 'vue';
import type { Rule } from 'ant-design-vue/es/form';
const { t } = useI18n(); const { t } = useI18n();
const { patternRules } = useFormRules();
interface RechargeOption { interface RechargeOption {
amount: number; amount: number;
@@ -15,8 +14,6 @@ interface RechargeOption {
price: number; price: number;
} }
const emailAddress = ref('');
const emailError = ref('');
const customAmount = ref<string | number | undefined>(undefined); const customAmount = ref<string | number | undefined>(undefined);
const selectedAmount = ref<number | null>(null); const selectedAmount = ref<number | null>(null);
const isCustomMode = ref<boolean>(false); const isCustomMode = ref<boolean>(false);
@@ -81,57 +78,32 @@ onUnmounted(() => {
const balance = computed(() => billingStore.balance); const balance = computed(() => billingStore.balance);
// 创建邮箱验证规则,将 trigger 改为 'blur' // 修改充值处理方法,移除邮箱验证
const emailRules = computed<Rule[]>(() => [ const handleRecharge = async () => {
{ required: true, message: t('page.login.register.emailRequired'), trigger: 'blur' }, // 验证金额
patternRules.email // 预定义规则已经设置为 blur 触发 if (!paymentAmount.value || paymentAmount.value <= 0) {
]); message.error('请选择或输入正确的充值金额');
return;
// 移除 change 事件处理,改用 blur 事件处理 }
const handleEmailBlur = async (e: FocusEvent) => {
const target = e.target as HTMLInputElement;
emailAddress.value = target.value;
try { try {
// 使用验证规则进行验证 await submitOrder({
await Promise.all(emailRules.value.map(rule => { type: 1,
if (rule.validator) { orderAmount: paymentAmount.value
return rule.validator(rule, target.value, () => {}); });
} message.success('充值订单提交成功!');
if (rule.pattern) {
if (!rule.pattern.test(target.value)) { // 更新余额,传入充值金额
throw rule.message; await billingStore.updateBalance(paymentAmount.value);
}
}
return Promise.resolve();
}));
emailError.value = '';
} catch (error) { } catch (error) {
emailError.value = error as string; message.error('充值失败,请重试!');
console.error('Failed to submit recharge order:', error);
} }
}; };
</script> </script>
<template> <template>
<div class="recharge-container p-4"> <div class="recharge-container p-4">
<!-- 邮箱输入部分 -->
<div class="email-section mb-6">
<div class="text-lg font-bold mb-2">{{ t('page.carddata.email') }}</div>
<div class="email-input-wrapper">
<AInput
v-model:value="emailAddress"
:placeholder="t('page.login.common.emailPlaceholder')"
class="email-input"
size="large"
:status="emailError ? 'error' : ''"
@blur="handleEmailBlur"
/>
<div v-if="emailError" class="error-message">
<span class="text-red-500">{{ emailError }}</span>
</div>
</div>
</div>
<!-- 充值金额选择 --> <!-- 充值金额选择 -->
<div class="amount-section mb-6"> <div class="amount-section mb-6">
<div class="text-lg font-bold mb-2">{{ t('page.carddata.Rechargeamount') }}</div> <div class="text-lg font-bold mb-2">{{ t('page.carddata.Rechargeamount') }}</div>
@@ -179,7 +151,8 @@ const handleEmailBlur = async (e: FocusEvent) => {
type="primary" type="primary"
size="large" size="large"
block block
:disabled="!paymentAmount" :disabled="!paymentAmount || paymentAmount <= 0"
@click="handleRecharge"
> >
¥{{ paymentAmount.toFixed(2) }} {{ t('page.carddata.pay') }} ¥{{ paymentAmount.toFixed(2) }} {{ t('page.carddata.pay') }}
</AButton> </AButton>
@@ -257,24 +230,7 @@ const handleEmailBlur = async (e: FocusEvent) => {
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05); box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
} }
.email-input-wrapper { /* 除之前的后缀样式 */
display: flex;
flex-direction: column;
gap: 4px;
}
.email-input {
border-radius: 8px;
}
.error-message {
font-size: 12px;
line-height: 1.5;
padding-left: 4px;
min-height: 18px;
}
/* 移除之前的后缀样式 */
:deep(.ant-input-suffix) { :deep(.ant-input-suffix) {
display: none; display: none;
} }