feat: enhance WechatModal component functionality

This commit is contained in:
Cat Tom 2025-03-26 02:06:38 +08:00
parent 922973af93
commit e903c54a10

View File

@ -1,17 +1,25 @@
<script setup> <script setup>
import { ref, watch, onUnmounted } from 'vue' import { ref, watch, onUnmounted, computed } from 'vue'
import { useEventListener } from '@vueuse/core' import { useEventListener } from '@vueuse/core'
import { useTheme } from 'vuetify' import { useTheme } from 'vuetify'
const props = defineProps({ const props = defineProps({
show: { modelValue: {
type: Boolean, type: Boolean,
required: true required: true
}, },
qrCode: { qrCode: {
type: String, type: String,
required: true, required: true,
validator: value => value.startsWith('/') || value.startsWith('http') validator: value => {
if (!value) return false
return (
value.startsWith('/') ||
value.startsWith('http') ||
value.startsWith('data:image') ||
value.startsWith('@/')
)
}
}, },
title: { title: {
type: String, type: String,
@ -23,7 +31,8 @@ const props = defineProps({
}, },
duration: { duration: {
type: Number, type: Number,
default: 0 // 0 default: 0,
validator: value => value >= 0
}, },
closeOnClickOutside: { closeOnClickOutside: {
type: Boolean, type: Boolean,
@ -32,27 +41,48 @@ const props = defineProps({
closeOnEsc: { closeOnEsc: {
type: Boolean, type: Boolean,
default: true default: true
},
showCloseButton: {
type: Boolean,
default: true
},
zIndex: {
type: Number,
default: 9999
} }
}) })
const emit = defineEmits(['update:show', 'closed', 'opened']) const emit = defineEmits([
'update:modelValue',
'closed',
'opened',
'qr-loaded',
'qr-error'
])
const theme = useTheme() const theme = useTheme()
const modalRef = ref(null) const modalRef = ref(null)
const qrLoaded = ref(false)
let autoCloseTimer = null let autoCloseTimer = null
const computedStyles = computed(() => ({
zIndex: props.zIndex
}))
// //
const closeModal = () => { const closeModal = () => {
clearTimeout(autoCloseTimer) clearTimeout(autoCloseTimer)
emit('update:show', false) emit('update:modelValue', false)
emit('closed') emit('closed')
} }
// //
const handleClickOutside = (event) => { const handleClickOutside = (event) => {
if (props.closeOnClickOutside && if (
modalRef.value && props.closeOnClickOutside &&
!modalRef.value.contains(event.target)) { modalRef.value &&
!modalRef.value.contains(event.target)
) {
closeModal() closeModal()
} }
} }
@ -73,19 +103,31 @@ const startAutoCloseTimer = () => {
} }
} }
// //
watch(() => props.show, (val) => { const handleImageLoad = () => {
if (val) { qrLoaded.value = true
startAutoCloseTimer() emit('qr-loaded')
emit('opened') startAutoCloseTimer()
}
// const handleImageError = () => {
console.error('微信二维码加载失败')
emit('qr-error')
}
//
watch(() => props.modelValue, (val) => {
if (val) {
qrLoaded.value = false
emit('opened')
document.body.style.overflow = 'hidden' document.body.style.overflow = 'hidden'
document.documentElement.style.paddingRight =
window.innerWidth - document.documentElement.clientWidth + 'px'
} else { } else {
//
document.body.style.overflow = '' document.body.style.overflow = ''
document.documentElement.style.paddingRight = ''
} }
}) }, { immediate: true })
// //
useEventListener(document, 'mousedown', handleClickOutside) useEventListener(document, 'mousedown', handleClickOutside)
@ -95,15 +137,17 @@ useEventListener(document, 'keydown', handleKeydown)
onUnmounted(() => { onUnmounted(() => {
clearTimeout(autoCloseTimer) clearTimeout(autoCloseTimer)
document.body.style.overflow = '' document.body.style.overflow = ''
document.documentElement.style.paddingRight = ''
}) })
</script> </script>
<template> <template>
<Transition name="wechat-modal"> <Transition name="wechat-modal">
<div <div
v-if="show" v-if="modelValue"
class="wechat-modal-mask" class="wechat-modal-mask"
:class="{ 'dark-mode': theme.global.name.value === 'dark' }" :class="{ 'dark-mode': theme.global.name.value === 'dark' }"
:style="computedStyles"
> >
<div <div
ref="modalRef" ref="modalRef"
@ -115,6 +159,7 @@ onUnmounted(() => {
<div class="wechat-modal-header"> <div class="wechat-modal-header">
<h3 class="modal-title">{{ title }}</h3> <h3 class="modal-title">{{ title }}</h3>
<button <button
v-if="showCloseButton"
class="modal-close-btn" class="modal-close-btn"
@click="closeModal" @click="closeModal"
aria-label="关闭微信二维码弹窗" aria-label="关闭微信二维码弹窗"
@ -129,15 +174,20 @@ onUnmounted(() => {
:src="qrCode" :src="qrCode"
alt="微信二维码" alt="微信二维码"
class="qr-code-image" class="qr-code-image"
:class="{ 'loaded': qrLoaded }"
loading="lazy" loading="lazy"
@load="startAutoCloseTimer" @load="handleImageLoad"
@error="handleImageError"
> >
<div v-if="!qrLoaded" class="qr-code-loader">
<div class="loader-spinner"></div>
</div>
</div> </div>
<p class="qr-hint">{{ hint }}</p> <p class="qr-hint">{{ hint }}</p>
</div> </div>
<div class="wechat-modal-footer"> <div class="wechat-modal-footer">
<div class="scan-animation"></div> <div class="scan-animation" v-if="qrLoaded"></div>
<p class="footer-text">长按识别二维码</p> <p class="footer-text">长按识别二维码</p>
</div> </div>
</div> </div>
@ -148,7 +198,7 @@ onUnmounted(() => {
<style scoped> <style scoped>
.wechat-modal-mask { .wechat-modal-mask {
position: fixed; position: fixed;
z-index: 9999; z-index: v-bind('zIndex');
top: 0; top: 0;
left: 0; left: 0;
right: 0; right: 0;
@ -166,7 +216,7 @@ onUnmounted(() => {
} }
.wechat-modal-container { .wechat-modal-container {
width: 320px; width: min(320px, 90vw);
background-color: var(--v-theme-surface); background-color: var(--v-theme-surface);
border-radius: 12px; border-radius: 12px;
box-shadow: 0 4px 30px rgba(0, 0, 0, 0.1); box-shadow: 0 4px 30px rgba(0, 0, 0, 0.1);
@ -212,12 +262,15 @@ onUnmounted(() => {
} }
.qr-code-wrapper { .qr-code-wrapper {
position: relative;
padding: 12px; padding: 12px;
background: white; background: white;
border-radius: 8px; border-radius: 8px;
display: inline-block; display: inline-block;
margin-bottom: 16px; margin-bottom: 16px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05); box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
min-width: 200px;
min-height: 200px;
} }
.qr-code-image { .qr-code-image {
@ -225,10 +278,40 @@ onUnmounted(() => {
height: 200px; height: 200px;
object-fit: contain; object-fit: contain;
display: block; display: block;
transition: opacity 0.3s ease;
opacity: 0;
&.loaded {
opacity: 1;
}
}
.qr-code-loader {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
display: flex;
align-items: center;
justify-content: center;
}
.loader-spinner {
width: 40px;
height: 40px;
border: 4px solid rgba(var(--v-theme-primary), 0.2);
border-top-color: var(--v-theme-primary);
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
} }
.qr-hint { .qr-hint {
margin: 0; margin: 12px 0 0;
color: var(--text-secondary); color: var(--text-secondary);
font-size: 0.95rem; font-size: 0.95rem;
line-height: 1.5; line-height: 1.5;
@ -246,7 +329,7 @@ onUnmounted(() => {
top: -30px; top: -30px;
left: 50%; left: 50%;
transform: translateX(-50%); transform: translateX(-50%);
width: 220px; width: calc(100% - 40px);
height: 4px; height: 4px;
background: linear-gradient( background: linear-gradient(
to bottom, to bottom,
@ -258,7 +341,7 @@ onUnmounted(() => {
} }
.footer-text { .footer-text {
margin: 0; margin: 8px 0 0;
color: var(--v-theme-primary); color: var(--v-theme-primary);
font-size: 0.9rem; font-size: 0.9rem;
font-weight: 500; font-weight: 500;
@ -270,11 +353,11 @@ onUnmounted(() => {
opacity: 1; opacity: 1;
} }
80% { 80% {
top: 190px; top: calc(100% - 30px);
opacity: 0.8; opacity: 0.8;
} }
100% { 100% {
top: 190px; top: calc(100% - 30px);
opacity: 0; opacity: 0;
} }
} }
@ -295,20 +378,15 @@ onUnmounted(() => {
transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.1); transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.1);
} }
/* 响应式调整 */ /* 减少运动偏好设置 */
@media (max-width: 480px) { @media (prefers-reduced-motion: reduce) {
.wechat-modal-container { .wechat-modal-container,
width: 90%; .wechat-modal-mask,
max-width: 300px; .qr-code-image,
} .scan-animation,
.loader-spinner {
.qr-code-image { transition: none !important;
width: 180px; animation: none !important;
height: 180px;
}
.scan-animation {
width: 200px;
} }
} }
</style> </style>