生日调整
This commit is contained in:
parent
33a4cac829
commit
c79e037aab
@ -62,20 +62,11 @@
|
|||||||
<text class="deco-icon">🎁</text>
|
<text class="deco-icon">🎁</text>
|
||||||
<text class="deco-icon">🎈</text>
|
<text class="deco-icon">🎈</text>
|
||||||
</view>
|
</view>
|
||||||
|
|
||||||
<!-- 右上角音乐图标 -->
|
|
||||||
<view
|
|
||||||
class="music-icon-wrapper"
|
|
||||||
:class="{ 'playing': isMusicPlaying, 'fade-out': isOpening }"
|
|
||||||
@click="toggleMusic"
|
|
||||||
>
|
|
||||||
<text class="music-icon-emoji">🎵</text>
|
|
||||||
</view>
|
|
||||||
</view>
|
</view>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { ref, computed, onUnmounted } from "vue";
|
import { ref, computed } from "vue";
|
||||||
import { onLoad, onShow } from "@dcloudio/uni-app";
|
import { onLoad, onShow } from "@dcloudio/uni-app";
|
||||||
import { useUserStore } from "@/store/modules/user";
|
import { useUserStore } from "@/store/modules/user";
|
||||||
|
|
||||||
@ -85,8 +76,6 @@ const openId = ref<string>("");
|
|||||||
const receiverName = ref<string>("");
|
const receiverName = ref<string>("");
|
||||||
const isOpening = ref(false);
|
const isOpening = ref(false);
|
||||||
const isOpened = ref(false);
|
const isOpened = ref(false);
|
||||||
const audioContext = ref<any>(null);
|
|
||||||
const isMusicPlaying = ref(false);
|
|
||||||
|
|
||||||
// 页面背景样式
|
// 页面背景样式
|
||||||
const pageStyle = computed(() => {
|
const pageStyle = computed(() => {
|
||||||
@ -113,124 +102,6 @@ const openEnvelope = () => {
|
|||||||
}, 1500);
|
}, 1500);
|
||||||
};
|
};
|
||||||
|
|
||||||
// 初始化音频(等待用户交互后自动播放)
|
|
||||||
const initAudio = () => {
|
|
||||||
try {
|
|
||||||
// #ifdef H5
|
|
||||||
audioContext.value = new Audio('/static/base/music/srkl.mp3');
|
|
||||||
audioContext.value.loop = true;
|
|
||||||
// 监听播放状态
|
|
||||||
audioContext.value.addEventListener('play', () => {
|
|
||||||
isMusicPlaying.value = true;
|
|
||||||
});
|
|
||||||
audioContext.value.addEventListener('pause', () => {
|
|
||||||
isMusicPlaying.value = false;
|
|
||||||
});
|
|
||||||
audioContext.value.addEventListener('ended', () => {
|
|
||||||
isMusicPlaying.value = false;
|
|
||||||
});
|
|
||||||
|
|
||||||
// 标记是否已尝试播放
|
|
||||||
let hasTriedPlay = false;
|
|
||||||
|
|
||||||
// 播放音乐的辅助函数
|
|
||||||
const attemptPlay = () => {
|
|
||||||
if (!audioContext.value || hasTriedPlay || isMusicPlaying.value) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
hasTriedPlay = true;
|
|
||||||
audioContext.value.play().then(() => {
|
|
||||||
isMusicPlaying.value = true;
|
|
||||||
}).catch(() => {
|
|
||||||
// 播放失败,重置标记以便下次尝试
|
|
||||||
hasTriedPlay = false;
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
// 监听用户交互事件,在第一次交互时自动播放
|
|
||||||
let hasInteracted = false;
|
|
||||||
const playOnInteraction = () => {
|
|
||||||
if (!hasInteracted && !isMusicPlaying.value) {
|
|
||||||
hasInteracted = true;
|
|
||||||
attemptPlay();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// 监听多种用户交互事件(只绑定一次)
|
|
||||||
const events = ['click', 'touchstart'];
|
|
||||||
events.forEach(eventType => {
|
|
||||||
document.addEventListener(eventType, playOnInteraction, { once: true, passive: true });
|
|
||||||
});
|
|
||||||
|
|
||||||
// 尝试自动播放(可能被阻止,但不记录错误)
|
|
||||||
attemptPlay();
|
|
||||||
// #endif
|
|
||||||
|
|
||||||
// #ifdef MP-WEIXIN
|
|
||||||
audioContext.value = uni.createInnerAudioContext();
|
|
||||||
audioContext.value.src = '/static/base/music/srkl.mp3';
|
|
||||||
audioContext.value.loop = true;
|
|
||||||
// 监听播放状态
|
|
||||||
audioContext.value.onPlay(() => {
|
|
||||||
isMusicPlaying.value = true;
|
|
||||||
});
|
|
||||||
audioContext.value.onPause(() => {
|
|
||||||
isMusicPlaying.value = false;
|
|
||||||
});
|
|
||||||
audioContext.value.onStop(() => {
|
|
||||||
isMusicPlaying.value = false;
|
|
||||||
});
|
|
||||||
// 微信小程序可以自动播放
|
|
||||||
audioContext.value.play();
|
|
||||||
isMusicPlaying.value = true;
|
|
||||||
// #endif
|
|
||||||
} catch (e) {
|
|
||||||
console.error("初始化音频失败:", e);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// 播放音乐
|
|
||||||
const playMusic = () => {
|
|
||||||
if (!audioContext.value) return;
|
|
||||||
try {
|
|
||||||
audioContext.value.play();
|
|
||||||
isMusicPlaying.value = true;
|
|
||||||
} catch (e) {
|
|
||||||
console.error("播放音乐失败:", e);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// 切换音乐播放
|
|
||||||
const toggleMusic = () => {
|
|
||||||
if (!audioContext.value) return;
|
|
||||||
|
|
||||||
try {
|
|
||||||
if (isMusicPlaying.value) {
|
|
||||||
audioContext.value.pause();
|
|
||||||
isMusicPlaying.value = false;
|
|
||||||
} else {
|
|
||||||
audioContext.value.play();
|
|
||||||
isMusicPlaying.value = true;
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.error("音乐播放控制失败:", e);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// 页面卸载时清理音频
|
|
||||||
onUnmounted(() => {
|
|
||||||
if (audioContext.value) {
|
|
||||||
try {
|
|
||||||
audioContext.value.pause();
|
|
||||||
// #ifdef MP-WEIXIN
|
|
||||||
audioContext.value.destroy?.();
|
|
||||||
// #endif
|
|
||||||
} catch (e) {
|
|
||||||
console.error("清理音频失败:", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// 重置信封状态(返回时恢复初始状态)
|
// 重置信封状态(返回时恢复初始状态)
|
||||||
const resetEnvelopeState = () => {
|
const resetEnvelopeState = () => {
|
||||||
isOpening.value = false;
|
isOpening.value = false;
|
||||||
@ -255,9 +126,6 @@ onLoad(async (options) => {
|
|||||||
if (!xxtsId.value) {
|
if (!xxtsId.value) {
|
||||||
uni.showToast({ title: "缺少贺卡ID", icon: "none" });
|
uni.showToast({ title: "缺少贺卡ID", icon: "none" });
|
||||||
}
|
}
|
||||||
|
|
||||||
// 初始化并播放背景音乐
|
|
||||||
initAudio();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// 页面显示时重置状态(从其他页面返回时)
|
// 页面显示时重置状态(从其他页面返回时)
|
||||||
@ -608,46 +476,4 @@ onShow(() => {
|
|||||||
opacity: 0;
|
opacity: 0;
|
||||||
transform: translateY(30px);
|
transform: translateY(30px);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 右上角音乐图标 */
|
|
||||||
.music-icon-wrapper {
|
|
||||||
position: fixed;
|
|
||||||
top: calc(20px + env(safe-area-inset-top));
|
|
||||||
right: 20px;
|
|
||||||
width: 50px;
|
|
||||||
height: 50px;
|
|
||||||
border-radius: 50%;
|
|
||||||
background: rgba(255, 255, 255, 0.9);
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
justify-content: center;
|
|
||||||
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.15);
|
|
||||||
z-index: 100;
|
|
||||||
cursor: pointer;
|
|
||||||
transition: all 0.3s ease;
|
|
||||||
}
|
|
||||||
|
|
||||||
.music-icon-wrapper:active {
|
|
||||||
transform: scale(0.95);
|
|
||||||
}
|
|
||||||
|
|
||||||
.music-icon-wrapper.playing .music-icon-emoji {
|
|
||||||
animation: music-rotate 2s linear infinite;
|
|
||||||
}
|
|
||||||
|
|
||||||
.music-icon-wrapper.fade-out {
|
|
||||||
opacity: 0;
|
|
||||||
transform: scale(0.8);
|
|
||||||
}
|
|
||||||
|
|
||||||
.music-icon-emoji {
|
|
||||||
font-size: 28px;
|
|
||||||
display: block;
|
|
||||||
transition: transform 0.3s ease;
|
|
||||||
}
|
|
||||||
|
|
||||||
@keyframes music-rotate {
|
|
||||||
0% { transform: rotate(0deg); }
|
|
||||||
100% { transform: rotate(360deg); }
|
|
||||||
}
|
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user