优化支付功能
This commit is contained in:
@@ -0,0 +1,472 @@
|
||||
<template>
|
||||
<div class="package-list-page">
|
||||
<!-- 顶部栏 -->
|
||||
<div class="page-top">
|
||||
<div class="toolbar">
|
||||
<div class="toolbar-left">
|
||||
<el-button link class="back-btn" @click="goHome">
|
||||
<el-icon><ArrowLeft /></el-icon>
|
||||
返回
|
||||
</el-button>
|
||||
<span v-if="tenantName" class="tenant-name">
|
||||
<el-icon><OfficeBuilding /></el-icon>
|
||||
{{ tenantName }}
|
||||
</span>
|
||||
</div>
|
||||
<div class="toolbar-right">
|
||||
<el-button link @click="ordersVisible = true">我的订单</el-button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="page-body">
|
||||
<el-skeleton v-if="loading" :rows="6" animated />
|
||||
|
||||
<div v-else class="package-grid">
|
||||
<el-card
|
||||
v-for="pkg in packages"
|
||||
:key="pkg.id"
|
||||
class="package-card"
|
||||
shadow="hover"
|
||||
>
|
||||
<div class="card-header">
|
||||
<div class="header-main">
|
||||
<h3 class="package-name">{{ pkg.name }}</h3>
|
||||
<el-tag size="small" type="info" effect="plain">{{ pkg.code }}</el-tag>
|
||||
</div>
|
||||
<div class="package-price">
|
||||
<span class="price-symbol">¥</span>
|
||||
<span class="price-value">{{ Number(pkg.price || 0).toFixed(2) }}</span>
|
||||
<span class="price-unit">/ {{ durationText(pkg.duration_days) }}</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card-divider"></div>
|
||||
|
||||
<!-- 功能模块 -->
|
||||
<div v-if="pkg.modules && pkg.modules.length > 0" class="package-features">
|
||||
<span class="feature-label">包含功能:</span>
|
||||
<el-tag
|
||||
v-for="m in pkg.modules"
|
||||
:key="m.module_code"
|
||||
size="small"
|
||||
effect="plain"
|
||||
class="feature-tag"
|
||||
>
|
||||
{{ m.module_name || m.module_code }}
|
||||
</el-tag>
|
||||
</div>
|
||||
|
||||
<!-- 用户数配额 -->
|
||||
<div class="package-quota">
|
||||
<el-icon :size="14"><User /></el-icon>
|
||||
<span>用户数上限:{{ pkg.user_quota || 20 }} 人</span>
|
||||
<el-tooltip
|
||||
v-if="pkg.extra_user_price"
|
||||
content="超出上限后,按单价增购"
|
||||
placement="top"
|
||||
>
|
||||
<el-icon :size="14" class="extra-price-icon"><InfoFilled /></el-icon>
|
||||
</el-tooltip>
|
||||
</div>
|
||||
|
||||
<!-- 备注 -->
|
||||
<p v-if="pkg.remark" class="package-remark">{{ pkg.remark }}</p>
|
||||
|
||||
<!-- 购买按钮 -->
|
||||
<div class="card-footer">
|
||||
<el-button
|
||||
type="primary"
|
||||
size="large"
|
||||
:loading="buyingIds[pkg.id]"
|
||||
@click="handleBuy(pkg)"
|
||||
class="buy-btn"
|
||||
>
|
||||
立即购买
|
||||
</el-button>
|
||||
</div>
|
||||
</el-card>
|
||||
</div>
|
||||
|
||||
<div v-if="!loading && packages.length === 0" class="empty-state">
|
||||
<el-icon :size="64"><Box /></el-icon>
|
||||
<p class="empty-text">暂无可购买套餐</p>
|
||||
<p class="empty-tip">请联系平台管理员配置套餐后购买</p>
|
||||
<el-button type="primary" @click="goHome">返回首页</el-button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 我的订单 -->
|
||||
<el-dialog v-model="ordersVisible" title="我的订单" width="720px">
|
||||
<el-table :data="myOrders" size="small" v-loading="ordersLoading">
|
||||
<el-table-column prop="order_no" label="订单号" min-width="160" show-overflow-tooltip />
|
||||
<el-table-column prop="product_name" label="套餐" min-width="160" show-overflow-tooltip />
|
||||
<el-table-column label="金额" width="100" align="center">
|
||||
<template #default="{ row }">¥{{ Number(row.amount || 0).toFixed(2) }}</template>
|
||||
</el-table-column>
|
||||
<el-table-column label="状态" width="100" align="center">
|
||||
<template #default="{ row }">
|
||||
<el-tag :type="orderStatusType(row.status)" size="small">
|
||||
{{ orderStatusText(row.status) }}
|
||||
</el-tag>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column prop="create_time" label="下单时间" width="170" align="center" />
|
||||
</el-table>
|
||||
</el-dialog>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, ref } from "vue";
|
||||
import { useRouter } from "vue-router";
|
||||
import { ElMessage } from "element-plus";
|
||||
import { ArrowLeft, OfficeBuilding, Box, User, InfoFilled } from "@element-plus/icons-vue";
|
||||
import { useAuthStore } from "@/stores/auth";
|
||||
import {
|
||||
getPackageList,
|
||||
createPackageOrder,
|
||||
getProductOrderStatus,
|
||||
getMyProductOrders,
|
||||
getPaymentChannels,
|
||||
} from "@/api/product";
|
||||
|
||||
const router = useRouter();
|
||||
const authStore = useAuthStore();
|
||||
|
||||
const loading = ref(false);
|
||||
const packages = ref<any[]>([]);
|
||||
const buyingIds = ref<Record<string, boolean>>({});
|
||||
|
||||
const ordersVisible = ref(false);
|
||||
const myOrders = ref<any[]>([]);
|
||||
const ordersLoading = ref(false);
|
||||
|
||||
const tenantName = computed(() => authStore.user?.tenant_name || "");
|
||||
|
||||
const durationText = (days: number) => {
|
||||
if (!days) return "年";
|
||||
if (days === 365) return "年";
|
||||
if (days === 30) return "月";
|
||||
if (days === 90) return "季";
|
||||
return `${days}天`;
|
||||
};
|
||||
|
||||
const orderStatusText = (s: string) =>
|
||||
({ pending: "待支付", paid: "已支付", activated: "已开通", closed: "已关闭" }[s] || s);
|
||||
const orderStatusType = (s: string) =>
|
||||
({ pending: "warning", paid: "primary", activated: "success", closed: "info" }[s] || "info");
|
||||
|
||||
const goHome = () => router.push("/home");
|
||||
|
||||
const loadPackages = async () => {
|
||||
loading.value = true;
|
||||
try {
|
||||
const res = await getPackageList();
|
||||
packages.value = res.code === 200 && res.data ? res.data.list || [] : [];
|
||||
} catch {
|
||||
packages.value = [];
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
const loadMyOrders = async () => {
|
||||
ordersLoading.value = true;
|
||||
try {
|
||||
const res = await getMyProductOrders();
|
||||
if (res.code === 200) myOrders.value = res.data?.list || [];
|
||||
} catch {
|
||||
myOrders.value = [];
|
||||
} finally {
|
||||
ordersLoading.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
const handleBuy = async (pkg: any) => {
|
||||
buyingIds.value[pkg.id] = true;
|
||||
try {
|
||||
// 先获取可用支付渠道
|
||||
const channelRes = await getPaymentChannels();
|
||||
const channels = channelRes.code === 200 ? channelRes.data?.list || [] : [];
|
||||
const channel = channels.length > 0 ? channels[0].channel : "";
|
||||
|
||||
const res = await createPackageOrder({
|
||||
package_code: pkg.code,
|
||||
channel,
|
||||
pay_type: "",
|
||||
});
|
||||
if (res.code !== 200) {
|
||||
ElMessage.error(res.msg || "下单失败");
|
||||
return;
|
||||
}
|
||||
const orderNo = res.data?.order_no || "";
|
||||
const payParams = res.data?.pay_params || null;
|
||||
const offline = res.data?.offline === true;
|
||||
|
||||
if (offline) {
|
||||
ElMessage.success("购买申请已提交,请等待平台确认开通");
|
||||
loadMyOrders();
|
||||
return;
|
||||
}
|
||||
|
||||
// 在线支付:复用产品购买的支付弹窗逻辑
|
||||
// 这里简单跳转到产品购买页的支付流程,或直接展示支付二维码
|
||||
// 为简化,直接用 orderNo 轮询状态
|
||||
ElMessage.success("下单成功,请完成支付");
|
||||
startPolling(orderNo);
|
||||
} catch (e: any) {
|
||||
ElMessage.error(e?.message || "下单失败");
|
||||
} finally {
|
||||
buyingIds.value[pkg.id] = false;
|
||||
}
|
||||
};
|
||||
|
||||
let pollTimer: any = null;
|
||||
const startPolling = (orderNo: string) => {
|
||||
if (pollTimer) clearInterval(pollTimer);
|
||||
pollTimer = setInterval(() => {
|
||||
checkOrder(orderNo, true);
|
||||
}, 5000);
|
||||
// 立即检查一次
|
||||
checkOrder(orderNo, true);
|
||||
};
|
||||
|
||||
const stopPolling = () => {
|
||||
if (pollTimer) {
|
||||
clearInterval(pollTimer);
|
||||
pollTimer = null;
|
||||
}
|
||||
};
|
||||
|
||||
const checkOrder = async (orderNo: string, silent = false) => {
|
||||
try {
|
||||
const res = await getProductOrderStatus(orderNo);
|
||||
if (res.code === 200 && res.data) {
|
||||
const status = res.data.status;
|
||||
if (status === "activated") {
|
||||
stopPolling();
|
||||
ElMessage.success("购买成功,套餐已开通");
|
||||
loadMyOrders();
|
||||
return;
|
||||
}
|
||||
if (status === "paid") {
|
||||
if (!silent) ElMessage.info("已支付,等待平台开通…");
|
||||
return;
|
||||
}
|
||||
if (!silent) ElMessage.info("尚未检测到支付成功,请确认已完成支付");
|
||||
}
|
||||
} catch {
|
||||
if (!silent) ElMessage.error("查询订单失败");
|
||||
}
|
||||
};
|
||||
|
||||
watch(ordersVisible, (v) => {
|
||||
if (v) loadMyOrders();
|
||||
});
|
||||
|
||||
onMounted(() => {
|
||||
loadPackages();
|
||||
});
|
||||
|
||||
import { watch } from "vue";
|
||||
</script>
|
||||
|
||||
<style scoped lang="less">
|
||||
.package-list-page {
|
||||
min-height: 100vh;
|
||||
background: #f5f7fa;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.page-top {
|
||||
background: #fff;
|
||||
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
|
||||
position: sticky;
|
||||
top: 0;
|
||||
z-index: 100;
|
||||
|
||||
.toolbar {
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
padding: 10px 24px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
|
||||
.toolbar-left {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
|
||||
.tenant-name {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
font-size: 14px;
|
||||
color: #606266;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.page-body {
|
||||
flex: 1;
|
||||
width: 100%;
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
padding: 24px;
|
||||
}
|
||||
|
||||
.package-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
|
||||
gap: 20px;
|
||||
}
|
||||
|
||||
.package-card {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
height: 100%;
|
||||
transition: transform 0.2s, box-shadow 0.2s;
|
||||
|
||||
&:hover {
|
||||
transform: translateY(-4px);
|
||||
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.card-header {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
justify-content: space-between;
|
||||
gap: 16px;
|
||||
padding-bottom: 12px;
|
||||
border-bottom: 1px solid #ebeef5;
|
||||
|
||||
.header-main {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
|
||||
.package-name {
|
||||
margin: 0 0 8px;
|
||||
font-size: 18px;
|
||||
font-weight: 600;
|
||||
color: #1a1a2e;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
}
|
||||
|
||||
.package-price {
|
||||
display: flex;
|
||||
align-items: baseline;
|
||||
gap: 2px;
|
||||
flex-shrink: 0;
|
||||
padding-top: 4px;
|
||||
|
||||
.price-symbol {
|
||||
font-size: 16px;
|
||||
color: #f56c6c;
|
||||
}
|
||||
|
||||
.price-value {
|
||||
font-size: 28px;
|
||||
font-weight: 700;
|
||||
color: #f56c6c;
|
||||
}
|
||||
|
||||
.price-unit {
|
||||
font-size: 13px;
|
||||
color: #909399;
|
||||
margin-left: -4px;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.card-divider {
|
||||
margin: 0 -20px 12px;
|
||||
border-bottom: 1px solid #f0f0f0;
|
||||
}
|
||||
|
||||
.package-features {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
margin-bottom: 10px;
|
||||
|
||||
.feature-label {
|
||||
font-size: 12px;
|
||||
color: #909399;
|
||||
}
|
||||
|
||||
.feature-tag {
|
||||
white-space: nowrap;
|
||||
}
|
||||
}
|
||||
|
||||
.package-quota {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
font-size: 13px;
|
||||
color: #606266;
|
||||
margin-bottom: 10px;
|
||||
|
||||
.extra-price-icon {
|
||||
color: #909399;
|
||||
cursor: help;
|
||||
}
|
||||
}
|
||||
|
||||
.package-remark {
|
||||
font-size: 12px;
|
||||
color: #909399;
|
||||
line-height: 1.6;
|
||||
margin: 0 0 16px;
|
||||
display: -webkit-box;
|
||||
-webkit-line-clamp: 2;
|
||||
-webkit-box-orient: vertical;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.card-footer {
|
||||
margin-top: auto;
|
||||
padding-top: 12px;
|
||||
border-top: 1px solid #ebeef5;
|
||||
|
||||
.buy-btn {
|
||||
width: 100%;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.empty-state {
|
||||
text-align: center;
|
||||
padding: 80px 20px;
|
||||
color: #c0c4cc;
|
||||
|
||||
.empty-text {
|
||||
font-size: 18px;
|
||||
color: #606266;
|
||||
}
|
||||
|
||||
.empty-tip {
|
||||
font-size: 14px;
|
||||
color: #909399;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.page-body {
|
||||
padding: 16px 12px;
|
||||
}
|
||||
|
||||
.package-grid {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user