feat: 添加支付方式管理功能,创建支付方式列表和添加支付方式页面,集成表单验证和状态管理
This commit is contained in:
351
src/views/payment/index.vue
Normal file
351
src/views/payment/index.vue
Normal file
@@ -0,0 +1,351 @@
|
||||
<script lang='ts' setup>
|
||||
import { alertController, toastController } from "@ionic/vue";
|
||||
import { addOutline, cardOutline, checkmarkCircleOutline, createOutline, logoAlipay, trashOutline } from "ionicons/icons";
|
||||
|
||||
const router = useRouter();
|
||||
|
||||
interface PaymentMethod {
|
||||
id: number;
|
||||
type: "bank" | "alipay";
|
||||
name: string;
|
||||
account: string;
|
||||
bankName?: string;
|
||||
bankBranch?: string;
|
||||
isDefault: boolean;
|
||||
}
|
||||
|
||||
const paymentMethods = ref<PaymentMethod[]>([
|
||||
{
|
||||
id: 1,
|
||||
type: "bank",
|
||||
name: "张三",
|
||||
account: "6222 **** **** 1234",
|
||||
bankName: "中国工商银行",
|
||||
bankBranch: "北京朝阳支行",
|
||||
isDefault: true,
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
type: "alipay",
|
||||
name: "李四",
|
||||
account: "138****8000",
|
||||
isDefault: false,
|
||||
},
|
||||
]);
|
||||
|
||||
async function showToast(message: string, color: "success" | "danger" | "warning" = "success") {
|
||||
const toast = await toastController.create({
|
||||
message,
|
||||
duration: 2000,
|
||||
position: "top",
|
||||
color,
|
||||
});
|
||||
await toast.present();
|
||||
}
|
||||
|
||||
function handleAdd() {
|
||||
router.push("/payment/add");
|
||||
}
|
||||
|
||||
function handleEdit(payment: PaymentMethod) {
|
||||
router.push(`/payment/add?id=${payment.id}`);
|
||||
}
|
||||
|
||||
async function handleSetDefault(payment: PaymentMethod) {
|
||||
if (payment.isDefault) {
|
||||
return;
|
||||
}
|
||||
|
||||
paymentMethods.value.forEach((item) => {
|
||||
item.isDefault = false;
|
||||
});
|
||||
|
||||
payment.isDefault = true;
|
||||
|
||||
// TODO: 调用 API 更新默认收款方式
|
||||
await showToast("已设为默认收款方式");
|
||||
}
|
||||
|
||||
async function handleDelete(payment: PaymentMethod) {
|
||||
const alert = await alertController.create({
|
||||
header: "确认删除",
|
||||
message: `确定要删除这个收款方式吗?`,
|
||||
buttons: [
|
||||
{
|
||||
text: "取消",
|
||||
role: "cancel",
|
||||
},
|
||||
{
|
||||
text: "删除",
|
||||
role: "destructive",
|
||||
handler: async () => {
|
||||
if (payment.isDefault && paymentMethods.value.length > 1) {
|
||||
const otherPayment = paymentMethods.value.find(item => item.id !== payment.id);
|
||||
if (otherPayment) {
|
||||
otherPayment.isDefault = true;
|
||||
}
|
||||
}
|
||||
|
||||
const index = paymentMethods.value.findIndex(item => item.id === payment.id);
|
||||
if (index > -1) {
|
||||
paymentMethods.value.splice(index, 1);
|
||||
}
|
||||
|
||||
// TODO: 调用 API 删除收款方式
|
||||
await showToast("删除成功");
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
await alert.present();
|
||||
}
|
||||
|
||||
function getPaymentIcon(type: string) {
|
||||
return type === "bank" ? cardOutline : logoAlipay;
|
||||
}
|
||||
|
||||
function getPaymentTypeName(type: string) {
|
||||
return type === "bank" ? "银行卡" : "支付宝";
|
||||
}
|
||||
|
||||
function maskAccount(account: string) {
|
||||
// 如果已经是脱敏格式,直接返回
|
||||
if (account.includes("*")) {
|
||||
return account;
|
||||
}
|
||||
// 否则进行脱敏处理
|
||||
if (account.length > 8) {
|
||||
return `${account.slice(0, 4)} **** **** ${account.slice(-4)}`;
|
||||
}
|
||||
return account;
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<ion-page>
|
||||
<ion-header class="ion-no-border">
|
||||
<ion-toolbar class="ion-toolbar">
|
||||
<ion-buttons slot="start">
|
||||
<back-button />
|
||||
</ion-buttons>
|
||||
<ion-title>收款方式</ion-title>
|
||||
<ion-buttons slot="end">
|
||||
<ion-button @click="handleAdd">
|
||||
<ion-icon slot="icon-only" :icon="addOutline" />
|
||||
</ion-button>
|
||||
</ion-buttons>
|
||||
</ion-toolbar>
|
||||
</ion-header>
|
||||
|
||||
<ion-content>
|
||||
<div v-if="paymentMethods.length > 0" class="ion-padding">
|
||||
<div class="space-y-3">
|
||||
<div
|
||||
v-for="payment in paymentMethods"
|
||||
:key="payment.id"
|
||||
class="payment-card"
|
||||
>
|
||||
<!-- 收款方式信息 -->
|
||||
<div class="payment-info">
|
||||
<div class="flex items-center justify-between mb-3">
|
||||
<div class="flex items-center gap-2">
|
||||
<ion-icon
|
||||
:icon="getPaymentIcon(payment.type)"
|
||||
class="text-2xl"
|
||||
:class="payment.type === 'alipay' ? 'text-[#1677ff]' : 'text-primary'"
|
||||
/>
|
||||
<span class="payment-type">{{ getPaymentTypeName(payment.type) }}</span>
|
||||
</div>
|
||||
<ion-badge v-if="payment.isDefault" color="danger" class="default-badge">
|
||||
默认
|
||||
</ion-badge>
|
||||
</div>
|
||||
|
||||
<div class="payment-details">
|
||||
<div class="detail-row">
|
||||
<span class="label">姓名</span>
|
||||
<span class="value">{{ payment.name }}</span>
|
||||
</div>
|
||||
<div class="detail-row">
|
||||
<span class="label">账号</span>
|
||||
<span class="value">{{ maskAccount(payment.account) }}</span>
|
||||
</div>
|
||||
<div v-if="payment.type === 'bank' && payment.bankName" class="detail-row">
|
||||
<span class="label">银行</span>
|
||||
<span class="value">{{ payment.bankName }}</span>
|
||||
</div>
|
||||
<div v-if="payment.type === 'bank' && payment.bankBranch" class="detail-row">
|
||||
<span class="label">开户行</span>
|
||||
<span class="value">{{ payment.bankBranch }}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 操作按钮 -->
|
||||
<div class="payment-actions">
|
||||
<ion-button
|
||||
fill="clear"
|
||||
size="small"
|
||||
@click="handleSetDefault(payment)"
|
||||
>
|
||||
<ion-icon slot="start" :icon="checkmarkCircleOutline" />
|
||||
{{ payment.isDefault ? '默认方式' : '设为默认' }}
|
||||
</ion-button>
|
||||
|
||||
<div class="flex gap-2">
|
||||
<ion-button
|
||||
fill="clear"
|
||||
size="small"
|
||||
color="medium"
|
||||
@click="handleEdit(payment)"
|
||||
>
|
||||
<ion-icon slot="start" :icon="createOutline" />
|
||||
编辑
|
||||
</ion-button>
|
||||
|
||||
<ion-button
|
||||
fill="clear"
|
||||
size="small"
|
||||
color="danger"
|
||||
@click="handleDelete(payment)"
|
||||
>
|
||||
<ion-icon slot="start" :icon="trashOutline" />
|
||||
删除
|
||||
</ion-button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 空状态 -->
|
||||
<div v-else class="empty-state">
|
||||
<empty title="暂无收款方式">
|
||||
<template #icon>
|
||||
<ion-icon :icon="cardOutline" class="empty-icon" />
|
||||
</template>
|
||||
<template #extra>
|
||||
<ion-button class="add-button" @click="handleAdd">
|
||||
<ion-icon slot="start" :icon="addOutline" />
|
||||
添加收款方式
|
||||
</ion-button>
|
||||
</template>
|
||||
</empty>
|
||||
</div>
|
||||
|
||||
<!-- 底部添加按钮 -->
|
||||
<div v-if="paymentMethods.length > 0" class="fixed-bottom">
|
||||
<ion-button expand="block" class="add-button" @click="handleAdd">
|
||||
<ion-icon slot="start" :icon="addOutline" />
|
||||
添加新收款方式
|
||||
</ion-button>
|
||||
</div>
|
||||
</ion-content>
|
||||
</ion-page>
|
||||
</template>
|
||||
|
||||
<style lang='css' scoped>
|
||||
.payment-card {
|
||||
background: white;
|
||||
border-radius: 16px;
|
||||
padding: 16px;
|
||||
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.06);
|
||||
}
|
||||
|
||||
.payment-info {
|
||||
padding-bottom: 12px;
|
||||
border-bottom: 1px solid #f0f0f0;
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
|
||||
.payment-type {
|
||||
font-size: 16px;
|
||||
font-weight: 600;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.default-badge {
|
||||
font-size: 11px;
|
||||
padding: 2px 8px;
|
||||
}
|
||||
|
||||
.payment-details {
|
||||
margin-top: 12px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.detail-row {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.detail-row .label {
|
||||
font-size: 13px;
|
||||
color: #999;
|
||||
min-width: 60px;
|
||||
}
|
||||
|
||||
.detail-row .value {
|
||||
font-size: 14px;
|
||||
color: #333;
|
||||
font-weight: 500;
|
||||
text-align: right;
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.payment-actions {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.payment-actions ion-button {
|
||||
--padding-start: 8px;
|
||||
--padding-end: 8px;
|
||||
font-size: 13px;
|
||||
text-transform: none;
|
||||
}
|
||||
|
||||
.empty-state {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
min-height: 60vh;
|
||||
padding: 40px 20px;
|
||||
}
|
||||
|
||||
.empty-icon {
|
||||
font-size: 80px;
|
||||
color: #ddd;
|
||||
}
|
||||
|
||||
.add-button {
|
||||
--background: linear-gradient(135deg, #c41e3a 0%, #8b1a2e 100%);
|
||||
--background-activated: linear-gradient(135deg, #8b1a2e 0%, #c41e3a 100%);
|
||||
--border-radius: 12px;
|
||||
--padding-top: 14px;
|
||||
--padding-bottom: 14px;
|
||||
font-weight: 600;
|
||||
font-size: 15px;
|
||||
text-transform: none;
|
||||
letter-spacing: 0.5px;
|
||||
margin-top: 12px;
|
||||
}
|
||||
|
||||
.fixed-bottom {
|
||||
position: fixed;
|
||||
bottom: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
padding: 12px 16px;
|
||||
background: white;
|
||||
box-shadow: 0 -2px 12px rgba(0, 0, 0, 0.08);
|
||||
z-index: 10;
|
||||
padding-bottom: calc(12px + var(--ion-safe-area-bottom));
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user