Files
it-tools-pro/src/pages/Buy.page.vue
T
jinghui f420785887 feat(buy): 站内支付页 /buy — 支付成功当场显示卡密;三处购买入口统一改指站内
- 新增 /buy 支付页:读本机机器码、列本项目可售套餐、支付宝/微信/QQ钱包、优惠码选填;
  拉起收银台后本页留守轮询,支付成功显示卡密并自动核验授权
- 购买入口改造:顶栏「永久解锁 13 个功能」、锁定弹窗「购买授权」、授权应用页
  「前往购买卡密」三处统一跳转站内 /buy(原跳外部 auth.88531.cn/#/shop)
- CardAuth 后端:订单支付完成时补发卡密写回订单(原带机器码下单只建授权不发卡)
- CHANGELOG 新增 Version 2026.11.11-it-tools-pro
2026-11-11 18:21:26 +08:00

703 lines
16 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.
<script setup lang="ts">
import { useHead } from '@vueuse/head';
import { AUTH_BASE, getMachineId, licenseState, checkAuth } from '@/tools/license/license';
useHead({ title: '购买卡密 - IT-TOOLS-PRO' });
const PROJECT_ID = 6;
const ORDER_KEY = 'it-tools-buy-order';
const POLL_MS = 5000;
interface CardType {
id: number;
name: string;
description: string;
duration_days: number;
price: string;
original_price: string;
}
const machineId = ref('');
const loading = ref(true);
const loadErr = ref('');
const plans = ref<CardType[]>([]);
const selPlan = ref<CardType | null>(null);
const payType = ref('alipay');
const couponCode = ref('');
const ordering = ref(false);
const errMsg = ref('');
const orderNo = ref('');
const payUrl = ref('');
const orderStatus = ref<'pending' | 'paid' | 'expired' | ''>('');
const waitingPay = ref(false);
const cardKey = ref('');
const paidAt = ref('');
const authMsg = ref('');
const copied = ref<'machine' | 'card' | ''>('');
const PAY_METHODS = [
{ key: 'alipay', label: '支付宝', icon: '💳' },
{ key: 'wxpay', label: '微信支付', icon: '💚' },
{ key: 'qqpay', label: 'QQ 钱包', icon: '🐧' },
];
let timer: ReturnType<typeof setInterval> | null = null;
function fmtDays(d: number): string {
if (d >= 999999) return '永久有效';
if (d >= 365) return `${Math.floor(d / 365)} 年`;
if (d >= 30) return `${Math.floor(d / 30)} 个月`;
return `${d} 天`;
}
async function loadPlans(): Promise<void> {
loading.value = true;
loadErr.value = '';
try {
const res = await fetch(`${AUTH_BASE}/public/projects/${PROJECT_ID}/card-types`);
const j = await res.json();
if (j.code !== 200) throw new Error(j.message || '获取套餐失败');
plans.value = (j.data || []) as CardType[];
if (plans.value.length && !selPlan.value) selPlan.value = plans.value[0];
} catch (e: any) {
loadErr.value = e?.message || '加载失败,请刷新重试';
} finally {
loading.value = false;
}
}
function copyText(text: string, tag: 'machine' | 'card'): void {
navigator.clipboard?.writeText(text).then(() => {
copied.value = tag;
setTimeout(() => {
if (copied.value === tag) copied.value = '';
}, 1600);
});
}
function stopPoll(): void {
if (timer) {
clearInterval(timer);
timer = null;
}
waitingPay.value = false;
}
async function pollOnce(): Promise<void> {
if (!orderNo.value) return;
try {
const res = await fetch(
`${AUTH_BASE}/public/orders/query?order_no=${encodeURIComponent(orderNo.value)}`
);
const j = await res.json();
if (j.code !== 200) return;
const d = j.data || {};
orderStatus.value = d.status || '';
if (d.status === 'paid') {
stopPoll();
cardKey.value = d.card_key || '';
paidAt.value = d.paid_at || '';
await refreshAuth();
} else if (d.status === 'expired') {
stopPoll();
errMsg.value = '订单已过期,请重新下单';
}
} catch {
/* 网络抖动忽略,继续轮询 */
}
}
function startPoll(): void {
stopPoll();
waitingPay.value = true;
pollOnce();
timer = setInterval(pollOnce, POLL_MS);
}
async function refreshAuth(): Promise<void> {
authMsg.value = '正在核验授权…';
try {
const ok = await checkAuth();
authMsg.value = ok ? '授权已生效,13 个高级功能已解锁' : '卡密已发放,授权暂未刷新,请点击下方重新核验';
} catch {
authMsg.value = '授权核验失败,请稍后重试';
}
}
async function buy(): Promise<void> {
errMsg.value = '';
if (!selPlan.value) {
errMsg.value = '请先选择套餐';
return;
}
ordering.value = true;
try {
const body: Record<string, any> = {
project_id: PROJECT_ID,
card_type_id: selPlan.value.id,
pay_type: payType.value,
bot_qq: machineId.value,
contact_qq: machineId.value,
};
if (couponCode.value.trim()) body.coupon_code = couponCode.value.trim();
const res = await fetch(`${AUTH_BASE}/public/orders`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
});
const j = await res.json();
if (j.code !== 200) throw new Error(j.message || '下单失败');
orderNo.value = j.data.order_no;
payUrl.value = j.data.pay_url;
orderStatus.value = 'pending';
cardKey.value = '';
authMsg.value = '';
localStorage.setItem(ORDER_KEY, JSON.stringify({ order_no: orderNo.value }));
// 支付页新窗口打开,本页留守轮询
window.open(payUrl.value, '_blank', 'noopener');
startPoll();
} catch (e: any) {
errMsg.value = e?.message || '下单失败,请稍后重试';
} finally {
ordering.value = false;
}
}
function reopenPay(): void {
if (payUrl.value) window.open(payUrl.value, '_blank', 'noopener');
}
function clearOrder(): void {
stopPoll();
orderNo.value = '';
payUrl.value = '';
orderStatus.value = '';
cardKey.value = '';
authMsg.value = '';
errMsg.value = '';
localStorage.removeItem(ORDER_KEY);
}
onMounted(async () => {
machineId.value = getMachineId();
await loadPlans();
// 恢复未完成订单,继续轮询
try {
const saved = JSON.parse(localStorage.getItem(ORDER_KEY) || 'null');
if (saved?.order_no) {
orderNo.value = saved.order_no;
orderStatus.value = 'pending';
startPoll();
}
} catch {
/* 忽略脏数据 */
}
});
onUnmounted(stopPoll);
</script>
<template>
<div class="px-page">
<div class="hero">
<div class="hero-title">🔐 购买卡密</div>
<div class="hero-sub">
支付成功后本页直接显示卡密,同时本机立即解锁 13 个高级功能
</div>
</div>
<!-- 本机机器码 -->
<div class="box machine-box">
<div class="box-label">本机机器码</div>
<div class="machine-row">
<code class="machine-id">{{ machineId || '读取中…' }}</code>
<c-button size="small" @click="copyText(machineId, 'machine')">
{{ copied === 'machine' ? '已复制 ✓' : '复制' }}
</c-button>
</div>
<div class="box-hint">
下单会自动绑定到这台电脑;换电脑时用支付后得到的卡密重新激活即可。
</div>
</div>
<div v-if="licenseState.authorized" class="box ok-box">
✅ 本机已拥有授权,无需重复购买。
</div>
<!-- 套餐 -->
<div class="section">
<div class="section-title">① 选择套餐</div>
<div v-if="loading" class="tip">套餐加载中…</div>
<div v-else-if="loadErr" class="tip err">{{ loadErr }}</div>
<div v-else class="plan-grid">
<div
v-for="p in plans"
:key="p.id"
class="plan-card"
:class="{ on: selPlan?.id === p.id }"
@click="selPlan = p"
>
<div class="plan-name">{{ p.name }}</div>
<div class="plan-price">
<span class="cur">¥</span>{{ p.price }}
<span v-if="Number(p.original_price) > 0" class="origin">¥{{ p.original_price }}</span>
</div>
<div class="plan-meta">{{ fmtDays(p.duration_days) }} · 13 个高级功能</div>
<div v-if="p.description" class="plan-desc">{{ p.description }}</div>
</div>
<div v-if="!plans.length" class="tip">暂无可售套餐</div>
</div>
</div>
<!-- 支付方式 -->
<div class="section">
<div class="section-title">② 选择支付方式</div>
<div class="pay-grid">
<div
v-for="m in PAY_METHODS"
:key="m.key"
class="pay-card"
:class="{ on: payType === m.key }"
@click="payType = m.key"
>
<span class="pay-icon">{{ m.icon }}</span>{{ m.label }}
</div>
</div>
</div>
<!-- 优惠码 -->
<div class="section">
<div class="section-title">③ 优惠码<span class="opt">(选填)</span></div>
<input
v-model="couponCode"
class="coupon-input"
placeholder="没有可留空"
maxlength="32"
/>
</div>
<div class="buy-bar">
<div class="amount">
应付 <b>¥{{ selPlan ? selPlan.price : '0.00' }}</b>
</div>
<c-button type="primary" size="large" :loading="ordering" @click="buy">
{{ ordering ? '正在创建订单…' : '立即支付' }}
</c-button>
</div>
<div v-if="errMsg" class="tip err">{{ errMsg }}</div>
<!-- 订单状态 -->
<div v-if="orderNo" class="box order-box">
<div class="order-head">
<span>订单号 <code>{{ orderNo }}</code></span>
<a v-if="payUrl && orderStatus === 'pending'" href="javascript:;" class="lnk" @click="reopenPay">
打开支付页 ↗
</a>
</div>
<div v-if="orderStatus === 'pending'" class="order-wait">
<span class="dots" />
等待支付完成…(支付页已在新标签打开,付款后本页会自动刷新)
<a href="javascript:;" class="lnk" @click="pollOnce">刷新状态</a>
<a href="javascript:;" class="lnk dim" @click="clearOrder">取消此单</a>
</div>
<template v-if="orderStatus === 'paid'">
<div class="paid-head">✅ 支付成功</div>
<div class="card-box">
<div class="card-label">你的卡密(请妥善保存)</div>
<div class="card-row">
<code class="card-key">{{ cardKey || '—' }}</code>
<c-button
v-if="cardKey"
size="small"
type="primary"
@click="copyText(cardKey, 'card')"
>
{{ copied === 'card' ? '已复制 ✓' : '复制卡密' }}
</c-button>
</div>
<div v-if="paidAt" class="box-hint">支付时间:{{ paidAt }}</div>
</div>
<div class="auth-line" :class="{ pending: licenseState.authorized === false }">
{{ authMsg || '授权核验中…' }}
</div>
<div class="auth-actions">
<c-button size="small" :loading="licenseState.checking" @click="refreshAuth">
重新核验授权
</c-button>
<c-button size="small" @click="clearOrder">再买一单</c-button>
</div>
</template>
<div v-if="orderStatus === 'expired'" class="tip err">订单已过期</div>
</div>
<div class="notes">
<div class="note-item">🔒 买断制:不按次、不按月,一次付费永久使用。</div>
<div class="note-item">🧭 授权跟随机器码绑定,换电脑用卡密重新激活即可恢复。</div>
<div class="note-item">🎁 支付成功即解锁 13 个高级功能,无需等待人工发货。</div>
<div class="note-item">❓ 卡密丢失可凭订单号联系管理员找回。</div>
</div>
</div>
</template>
<style lang="less" scoped>
.px-page {
max-width: 860px;
margin: 0 auto;
padding: 40px 20px 80px;
}
.hero {
text-align: center;
margin-bottom: 30px;
.hero-title {
font-size: 30px;
font-weight: 800;
background: linear-gradient(48deg, rgba(37, 99, 108, 1) 0%, rgba(59, 149, 111, 1) 55%, rgba(20, 160, 88, 1) 100%);
-webkit-background-clip: text;
background-clip: text;
color: transparent;
}
.hero-sub {
margin-top: 10px;
font-size: 14.5px;
opacity: 0.65;
}
}
.box {
border: 1px solid rgba(128, 128, 128, 0.25);
border-radius: 12px;
padding: 14px 16px;
margin-bottom: 22px;
}
.machine-box {
.box-label,
.card-label {
font-size: 12.5px;
opacity: 0.6;
margin-bottom: 8px;
}
.machine-row {
display: flex;
align-items: center;
gap: 10px;
flex-wrap: wrap;
}
.machine-id {
font-size: 17px;
font-weight: 700;
letter-spacing: 1px;
user-select: all;
}
}
.box-hint {
margin-top: 8px;
font-size: 12.5px;
opacity: 0.6;
}
.ok-box {
color: #18a058;
font-weight: 600;
}
.section {
margin-bottom: 24px;
.section-title {
font-size: 16px;
font-weight: 700;
margin-bottom: 12px;
}
.opt {
font-size: 12.5px;
font-weight: 400;
opacity: 0.55;
}
}
.tip {
font-size: 13.5px;
opacity: 0.7;
&.err {
color: #d03050;
opacity: 1;
margin-top: 8px;
}
}
.plan-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
gap: 12px;
}
.plan-card {
border: 1px solid rgba(128, 128, 128, 0.25);
border-radius: 12px;
padding: 16px;
cursor: pointer;
transition: border-color 0.15s, box-shadow 0.15s;
&:hover {
border-color: rgba(24, 160, 88, 0.5);
}
&.on {
border-color: #18a058;
box-shadow: 0 0 0 1px rgba(24, 160, 88, 0.35), 0 6px 18px rgba(24, 160, 88, 0.12);
}
.plan-name {
font-size: 15.5px;
font-weight: 700;
}
.plan-price {
margin-top: 8px;
font-size: 30px;
font-weight: 800;
color: #18a058;
.cur {
font-size: 16px;
margin-right: 2px;
}
.origin {
margin-left: 8px;
font-size: 13px;
font-weight: 400;
opacity: 0.45;
text-decoration: line-through;
}
}
.plan-meta {
margin-top: 6px;
font-size: 13px;
opacity: 0.7;
}
.plan-desc {
margin-top: 6px;
font-size: 12.5px;
opacity: 0.55;
}
}
.pay-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
gap: 10px;
}
.pay-card {
border: 1px solid rgba(128, 128, 128, 0.25);
border-radius: 10px;
padding: 12px 14px;
font-size: 14px;
cursor: pointer;
display: flex;
align-items: center;
gap: 8px;
transition: border-color 0.15s, box-shadow 0.15s;
&:hover {
border-color: rgba(24, 160, 88, 0.5);
}
&.on {
border-color: #18a058;
box-shadow: 0 0 0 1px rgba(24, 160, 88, 0.35);
font-weight: 600;
}
.pay-icon {
font-size: 17px;
}
}
.coupon-input {
width: 100%;
max-width: 320px;
padding: 9px 12px;
border: 1px solid rgba(128, 128, 128, 0.3);
border-radius: 8px;
background: transparent;
color: inherit;
font-size: 14px;
outline: none;
&:focus {
border-color: #18a058;
}
}
.buy-bar {
display: flex;
align-items: center;
justify-content: space-between;
gap: 16px;
padding: 16px;
border: 1px solid rgba(128, 128, 128, 0.25);
border-radius: 12px;
margin-bottom: 12px;
flex-wrap: wrap;
.amount {
font-size: 14.5px;
opacity: 0.75;
b {
font-size: 26px;
color: #18a058;
margin-left: 4px;
}
}
}
.order-box {
border-color: rgba(24, 160, 88, 0.45);
}
.order-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
font-size: 13.5px;
opacity: 0.8;
flex-wrap: wrap;
code {
user-select: all;
}
}
.lnk {
color: #18a058;
text-decoration: none;
font-size: 13.5px;
&.dim {
opacity: 0.6;
margin-left: 12px;
}
&:hover {
text-decoration: underline;
}
}
.order-wait {
margin-top: 12px;
font-size: 14px;
display: flex;
align-items: center;
gap: 8px;
flex-wrap: wrap;
}
.dots {
width: 10px;
height: 10px;
border-radius: 50%;
background: #f0a020;
animation: blink 1s infinite alternate;
flex-shrink: 0;
}
@keyframes blink {
from { opacity: 0.25; }
to { opacity: 1; }
}
.paid-head {
margin-top: 14px;
font-size: 18px;
font-weight: 800;
color: #18a058;
}
.card-box {
margin-top: 12px;
padding: 14px;
border: 1px dashed rgba(24, 160, 88, 0.55);
border-radius: 10px;
background: rgba(24, 160, 88, 0.06);
.card-label {
font-size: 12.5px;
opacity: 0.7;
margin-bottom: 8px;
}
.card-row {
display: flex;
align-items: center;
gap: 12px;
flex-wrap: wrap;
}
.card-key {
font-size: 19px;
font-weight: 800;
letter-spacing: 1.2px;
user-select: all;
word-break: break-all;
}
}
.auth-line {
margin-top: 12px;
font-size: 14px;
color: #18a058;
font-weight: 600;
&.pending {
color: #f0a020;
}
}
.auth-actions {
margin-top: 12px;
display: flex;
gap: 10px;
flex-wrap: wrap;
}
.notes {
margin-top: 28px;
border-top: 1px solid rgba(128, 128, 128, 0.2);
padding-top: 18px;
.note-item {
font-size: 13.5px;
opacity: 0.65;
line-height: 1.9;
}
}
</style>