266 lines
6.2 KiB
Vue
266 lines
6.2 KiB
Vue
<script setup lang="ts">
|
||
import { computed, ref, watch } from 'vue'
|
||
import qrcode from 'qrcode-generator'
|
||
import { useCountDown } from '@/composables/useCountDown'
|
||
import { formatMoney } from '@/utils/format'
|
||
|
||
/**
|
||
* 微信 Native 支付二维码弹窗。
|
||
*
|
||
* <p>后端 `/pay/web/wechat/native` 返回 `codeUrl`(形如 `weixin://wxpay/bizpayurl?pr=xxx`),
|
||
* 本组件用 `qrcode-generator` 把它渲染成二维码(`createSvgTag` → `v-html`),
|
||
* 用户用微信「扫一扫」完成支付。</p>
|
||
*
|
||
* <p>⚠️ 后端**没有「查询支付结果」接口**,所以没有轮询——付完由用户点「我已完成支付」
|
||
* 进入支付结果页;二维码倒计时只是展示(Native 侧 `code_url` 真实有效期由微信控制)。</p>
|
||
*/
|
||
interface Props {
|
||
visible: boolean
|
||
/** 二维码内容(微信 codeUrl) */
|
||
content: string
|
||
/** 订单金额(元) */
|
||
amount: number
|
||
/** 二维码倒计时(秒),默认 5 分钟 */
|
||
expiresIn?: number
|
||
}
|
||
|
||
const props = withDefaults(defineProps<Props>(), { expiresIn: 300 })
|
||
|
||
const emit = defineEmits<{
|
||
'update:visible': [visible: boolean]
|
||
/** 重新下单换一张二维码 */
|
||
refresh: []
|
||
/** 用户声明已付款 */
|
||
paid: []
|
||
}>()
|
||
|
||
const { seconds, start, reset: resetCountDown } = useCountDown(props.expiresIn)
|
||
const started = ref(false)
|
||
|
||
const expired = computed(() => started.value && seconds.value <= 0)
|
||
|
||
const countdownText = computed(() => {
|
||
const m = Math.floor(seconds.value / 60)
|
||
const s = seconds.value % 60
|
||
return `${String(m).padStart(2, '0')}:${String(s).padStart(2, '0')}`
|
||
})
|
||
|
||
/** 内容仅来自后端下发的 codeUrl,本地生成 SVG,非用户输入 */
|
||
const qrSvg = computed(() => {
|
||
if (!props.content) return ''
|
||
try {
|
||
const qr = qrcode(0, 'M')
|
||
qr.addData(props.content)
|
||
qr.make()
|
||
return qr.createSvgTag({ cellSize: 4, margin: 2, scalable: true })
|
||
} catch {
|
||
return ''
|
||
}
|
||
})
|
||
|
||
watch(
|
||
() => [props.visible, props.content] as const,
|
||
([visible, content]) => {
|
||
if (visible && content) {
|
||
start(props.expiresIn)
|
||
started.value = true
|
||
return
|
||
}
|
||
if (!visible) {
|
||
resetCountDown()
|
||
started.value = false
|
||
}
|
||
},
|
||
{ immediate: true },
|
||
)
|
||
|
||
function close(): void {
|
||
emit('update:visible', false)
|
||
}
|
||
|
||
function refresh(): void {
|
||
start(props.expiresIn)
|
||
started.value = true
|
||
emit('refresh')
|
||
}
|
||
|
||
function onPaid(): void {
|
||
emit('paid')
|
||
}
|
||
|
||
function onBoxClick(): void {
|
||
if (expired.value) refresh()
|
||
}
|
||
</script>
|
||
|
||
<template>
|
||
<Teleport to="body">
|
||
<div v-if="visible" class="wx-qr" @click.self="close">
|
||
<div class="wx-qr__panel">
|
||
<h3 class="wx-qr__title">微信扫码支付</h3>
|
||
<p class="wx-qr__amount">¥{{ formatMoney(amount) }}</p>
|
||
|
||
<div
|
||
class="wx-qr__box"
|
||
:class="{ 'is-expired': expired }"
|
||
@click="onBoxClick"
|
||
>
|
||
<!-- eslint-disable-next-line vue/no-v-html -- 内容由本地 QR 库生成,非用户输入 -->
|
||
<div v-if="!expired && qrSvg" class="wx-qr__code" v-html="qrSvg" />
|
||
<div v-else class="wx-qr__mask">
|
||
<span class="wx-qr__mask-icon">⟳</span>
|
||
<span class="wx-qr__mask-text">
|
||
{{ expired ? '二维码已过期' : '二维码生成中' }}<br />点击刷新
|
||
</span>
|
||
</div>
|
||
</div>
|
||
|
||
<p class="wx-qr__status">
|
||
<template v-if="!expired">
|
||
请使用手机微信 <strong>[扫一扫]</strong> 扫描上方二维码, 二维码
|
||
<strong class="wx-qr__count">{{ countdownText }}</strong> 后失效
|
||
</template>
|
||
<template v-else> 二维码已过期,请点击二维码或下方按钮刷新 </template>
|
||
</p>
|
||
|
||
<div class="wx-qr__actions">
|
||
<button class="wx-qr__btn" @click="refresh">刷新二维码</button>
|
||
<button class="wx-qr__btn wx-qr__btn--cta" @click="onPaid">
|
||
我已完成支付
|
||
</button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</Teleport>
|
||
</template>
|
||
|
||
<style lang="less" scoped>
|
||
.wx-qr {
|
||
position: fixed;
|
||
inset: 0;
|
||
z-index: 1000;
|
||
.flex(row, center);
|
||
background: rgba(0, 0, 0, 0.45);
|
||
|
||
&__panel {
|
||
width: 360px;
|
||
max-width: calc(100vw - 32px);
|
||
padding: @spacing-lg;
|
||
border-radius: @radius-lg;
|
||
background: @checkout-card;
|
||
box-shadow: @shadow-lg;
|
||
.flex(column, flex-start, center);
|
||
}
|
||
|
||
&__title {
|
||
font-size: @font-size-lg;
|
||
font-weight: 600;
|
||
color: @heading-color;
|
||
}
|
||
|
||
&__amount {
|
||
margin-top: @spacing-xs;
|
||
font-size: @font-size-xl;
|
||
font-weight: 600;
|
||
color: @checkout-amount;
|
||
}
|
||
|
||
&__box {
|
||
.flex(row, center, center);
|
||
width: 200px;
|
||
height: 200px;
|
||
margin-top: @spacing-base;
|
||
border: 1px solid @checkout-line;
|
||
border-radius: @radius-base;
|
||
background: @checkout-card;
|
||
|
||
&.is-expired {
|
||
background: @bg-color;
|
||
cursor: pointer;
|
||
}
|
||
}
|
||
|
||
&__code {
|
||
width: 100%;
|
||
height: 100%;
|
||
padding: @spacing-sm;
|
||
|
||
// v-html 注入的 SVG 拿不到 scoped 属性,必须 :deep
|
||
:deep(svg) {
|
||
display: block;
|
||
width: 100%;
|
||
height: 100%;
|
||
}
|
||
}
|
||
|
||
&__mask {
|
||
.flex(column, center);
|
||
gap: @spacing-xs;
|
||
color: @text-color-secondary;
|
||
}
|
||
|
||
&__mask-icon {
|
||
font-size: 36px;
|
||
line-height: 1;
|
||
}
|
||
|
||
&__mask-text {
|
||
font-size: @font-size-sm;
|
||
text-align: center;
|
||
line-height: 1.6;
|
||
}
|
||
|
||
&__status {
|
||
margin-top: @spacing-base;
|
||
font-size: @font-size-sm;
|
||
color: @text-color-secondary;
|
||
text-align: center;
|
||
line-height: 1.6;
|
||
|
||
strong {
|
||
color: @heading-color;
|
||
}
|
||
}
|
||
|
||
&__count {
|
||
color: @checkout-amount;
|
||
font-weight: 600;
|
||
}
|
||
|
||
&__actions {
|
||
.flex(row, center);
|
||
gap: @spacing-sm;
|
||
width: 100%;
|
||
margin-top: @spacing-lg;
|
||
}
|
||
|
||
&__btn {
|
||
flex: 1;
|
||
height: 40px;
|
||
border: 1px solid @border-color-dark;
|
||
border-radius: @radius-round;
|
||
background: @checkout-card;
|
||
color: @text-color;
|
||
font-size: @font-size-base;
|
||
transition: all 0.2s;
|
||
|
||
&:hover {
|
||
border-color: @checkout-cta;
|
||
color: @checkout-cta;
|
||
}
|
||
|
||
&--cta {
|
||
border-color: @checkout-cta;
|
||
background: @checkout-cta;
|
||
color: @white;
|
||
font-weight: 600;
|
||
|
||
&:hover {
|
||
background: @checkout-cta-hover;
|
||
color: @white;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
</style>
|