优化握力环训练页:内联视频舞台并修复小程序兼容与完成浮层布局。

将示范视频从自定义组件移至页面主体,避免 mp 端 vOn/ref 崩溃;奖励特效改为 prop 信号驱动,完成弹层移出 page 容器修复右侧裁切,并修正后台音频配置与 CLI 热重载监听。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-05-29 10:59:17 +08:00
co-authored by Cursor
parent 9f3db8f281
commit 9b5cd5b2b9
6 changed files with 661 additions and 685 deletions
+299 -265
View File
@@ -12,15 +12,56 @@
</view>
</view>
<!-- ===== 握力环画布区域 ===== -->
<!-- ===== 握力环画布区域(直接内联,避免原生 video 嵌自定义组件的 mp 兼容问题) ===== -->
<view class="stage-canvas-wrapper">
<GripRingCanvas
ref="gripCanvasRef"
:bpm="currentBpm"
:is-playing="isPlaying"
:hint-text="centerHintText"
@toggle-play="onTogglePlay"
/>
<!-- 环境氛围:中心放射光晕 -->
<view class="ambient-glow" />
<!-- 捏碎特效画布(prop 信号驱动) -->
<CrushCanvas :crush-signal="crushSignal" />
<!-- 握力环容器 -->
<view class="ring-container" :class="{ playing: isPlaying }">
<!-- 节拍光环 - 跟随 BPM 精确缩放 -->
<view class="beat-halo" />
<!-- 视频示范主体(圆形) -->
<view class="video-ring">
<video
id="gripDemoVideo"
class="demo-video"
:src="TUTORIAL_VIDEO_URL"
:poster="TUTORIAL_POSTER_URL"
muted
loop
:controls="false"
:show-center-play-btn="false"
:show-play-btn="false"
:show-fullscreen-btn="false"
:show-progress="false"
:enable-progress-gesture="false"
object-fit="cover"
/>
<view class="video-ring-border" />
</view>
<!-- 中心提示(仅未开始时显示) -->
<view v-if="!isPlaying" class="center-hint">
<view class="hint-icon">
<view class="play-triangle" />
</view>
<text class="hint-text">{{ centerHintText }}</text>
</view>
<!-- BPM 角标(播放中) -->
<view v-else class="bpm-chip">
<text class="bpm-value">{{ currentBpm }}</text>
<text class="bpm-label">BPM</text>
</view>
<!-- 点击层(捕获点击切换训练状态) -->
<view class="tap-overlay" @click="onTogglePlay" />
</view>
<view class="bgm-toggle" @click="toggleBgm">
<text class="bgm-icon">{{ bgmEnabled ? '🎵' : '🔇' }}</text>
@@ -46,7 +87,7 @@
</view>
<view class="stat-chip">
<text class="stat-chip-label">糖分</text>
<text class="stat-chip-value" :class="{ muted: !hasStats }">{{ hasStats ? `${totalSugar.toFixed(1)}g` : '—' }}</text>
<text class="stat-chip-value" :class="{ muted: !hasStats }">{{ hasStats ? `${totalSugar.toFixed(1)}g` : '—' }}</text>
</view>
</view>
@@ -99,9 +140,10 @@
<text class="end-link" @click="onEndTraining">结束训练 </text>
</view>
</view>
</view>
<!-- ===== 奖励弹窗 ===== -->
<view v-if="showRewardPopup" class="reward-popup-mask" @click="closeRewardPopup">
<!-- ===== 奖励弹窗 ===== -->
<view v-if="showRewardPopup" class="reward-popup-mask" @click="closeRewardPopup">
<view class="reward-popup" @click.stop>
<text class="reward-emoji">{{ currentReward.emoji }}</text>
<text class="reward-title">{{ currentReward.title }}</text>
@@ -111,46 +153,6 @@
</view>
</view>
<!-- ===== 视频教学入口 ===== -->
<view class="tutorial-card" @click="openTutorial">
<view class="tutorial-icon">
<view class="tutorial-play-triangle" />
</view>
<view class="tutorial-info">
<text class="tutorial-title">观看动作示范</text>
<text class="tutorial-sub">视频教学 · 学会正确握法</text>
</view>
<text class="tutorial-arrow"></text>
</view>
<!-- ===== 视频教学弹层 ===== -->
<view v-if="showTutorial" class="tutorial-mask" @click="closeTutorial">
<view class="tutorial-modal" @click.stop>
<view class="tutorial-header">
<text class="tutorial-modal-title">动作示范</text>
<view class="tutorial-close" @click="closeTutorial">
<text class="tutorial-close-icon">×</text>
</view>
</view>
<video
class="tutorial-video"
:src="TUTORIAL_VIDEO_URL"
controls
autoplay
loop
object-fit="contain"
show-center-play-btn
/>
<view class="tutorial-tips">
<text class="tutorial-tips-title">关键要领</text>
<view v-for="(tip, i) in TIPS" :key="i" class="tutorial-tip-item">
<view class="tip-dot" />
<text class="tip-content">{{ tip }}</text>
</view>
</view>
</view>
</view>
<!-- ===== 训练完成浮层 ===== -->
<view v-if="showSummary" class="summary-mask" @click="dismissSummary">
<view class="completion-card" @click.stop>
@@ -175,15 +177,15 @@
</view>
<view class="stat-item">
<text class="stat-icon">🔥</text>
<text class="stat-value">{{ totalSugar.toFixed(1) }}g</text>
<text class="stat-label">消耗糖分</text>
<text class="stat-value">{{ totalSugar.toFixed(1) }}g</text>
<text class="stat-label">消耗糖分 (估算)</text>
</view>
</view>
<!-- 食物对照 -->
<view v-if="sugarComparisons.length > 0" class="food-comparison-card">
<view class="food-comparison-header">
<text class="food-comparison-title">相当于消耗了</text>
<text class="food-comparison-title">大约相当于</text>
<text class="food-comparison-hint">这些食物的糖分 🍭</text>
</view>
@@ -212,14 +214,13 @@
</view>
</view>
</view>
</view>
</template>
<script setup lang="ts">
import { computed, ref, watch } from 'vue'
import { onShow, onHide, onShareAppMessage, onShareTimeline } from '@dcloudio/uni-app'
import { useMetronome } from '../hooks/useMetronome'
import GripRingCanvas from './components/grip-ring-canvas.vue'
import CrushCanvas from './components/crush-canvas.vue'
type PresetId = 'light' | 'medium' | 'heavy'
interface GripPreset {
@@ -241,10 +242,10 @@ const PRESET_ICONS: Record<PresetId, string> = {
heavy: '🔥',
}
const TIPS = ['握紧停 1 秒', '完全放松', '左右手交替'] as const
// 视频教学
// 画布指导视频(跟随训练状态播放)
const TUTORIAL_VIDEO_URL = 'https://gz-1349751149.cos.ap-guangzhou.myqcloud.com/uploads/video/20260528/20260528173911bf6f93386.mp4'
// 视频封面(未开始/暂停时显示的静态首帧)
const TUTORIAL_POSTER_URL = 'https://gz-1349751149.cos.ap-guangzhou.myqcloud.com/uploads/images/20260529/202605291030231aed12802.jpg'
interface TargetOption {
minutes: 0 | 5 | 10 | 15
@@ -324,10 +325,10 @@ const FOOD_SUGAR_TABLE = [
const currentPreset = ref<PresetId>('medium')
const targetMinutes = ref<0 | 5 | 10 | 15>(0) // 训练目标(0=自由)
const gripCanvasRef = ref<any>(null)
// 捏碎特效信号(prop 驱动, 替代跨组件 ref 调用; nonce 保证连续同类型也能触发 watch)
const crushSignal = ref<{ type: 'egg' | 'walnut' | 'can' | 'balloon'; nonce: number } | null>(null)
const showRewardPopup = ref(false)
const showSummary = ref(false)
const showTutorial = ref(false) // 视频教学弹层
const currentReward = ref<RewardConfig>(REWARDS[0])
const crushAudioCtx = ref<UniApp.InnerAudioContext | null>(null)
const bgmAudioCtx = ref<UniApp.InnerAudioContext | null>(null)
@@ -355,6 +356,18 @@ const metronome = useMetronome({
})
const { isPlaying } = metronome
// 示范视频跟随训练状态: 开始训练才播放, 暂停/未开始则暂停
// 视频已内联在页面主体(非自定义组件内), page 级 createVideoContext 即可定位
watch(isPlaying, (playing) => {
const videoCtx = uni.createVideoContext('gripDemoVideo')
if (!videoCtx) return
if (playing) {
videoCtx.play()
} else {
videoCtx.pause()
}
})
const intervalMs = computed(() => 60000 / currentBpm.value)
const hasStats = computed(() => isPlaying.value || totalReps.value > 0)
const isPaused = computed(() => !isPlaying.value && totalReps.value > 0 && !showSummary.value)
@@ -379,7 +392,7 @@ const statusText = computed(() => {
// 分享文案
const shareTitle = computed(() => {
if (totalReps.value === 0) return '握力环训练 · 一起练起来'
return `刚刚完成 ${totalReps.value} 次握力训练,消耗 ${totalSugar.value.toFixed(1)}g 糖分 💪`
return `刚刚完成 ${totalReps.value} 次握力训练,消耗 ${totalSugar.value.toFixed(1)}g 糖分 💪`
})
@@ -451,9 +464,10 @@ function triggerReward() {
const reward = REWARDS[rewardIndex.value]
currentReward.value = reward
// 立即触发粒子特效和音效
gripCanvasRef.value?.triggerCrushEffect(reward.itemType)
// 立即触发粒子特效 + 音效 + 震动
crushSignal.value = { type: reward.itemType, nonce: Date.now() }
playCrushSound(reward.sound)
triggerHaptic()
// 递进到下一个奖励等级
rewardIndex.value++
@@ -475,6 +489,21 @@ function closeRewardPopup() {
showRewardPopup.value = false
}
// 奖励震动反馈: "砰" + 余震两段,配合爆裂特效
function triggerHaptic() {
const buzz = (type: 'heavy' | 'medium' | 'light') => {
// type 仅微信高版本支持,fail 时降级为无参短震
uni.vibrateShort({
type,
fail: () => {
try { uni.vibrateShort({}) } catch (_) {}
},
} as any)
}
buzz('heavy')
setTimeout(() => buzz('medium'), 120)
}
function playCrushSound(sound: string) {
// 如果没有音效URL,跳过播放
if (!sound) return
@@ -587,20 +616,6 @@ function dismissSummary() {
showSummary.value = false
}
// 打开视频教学(避免与 BGM 音轨冲突)
function openTutorial() {
stopBgm()
showTutorial.value = true
}
function closeTutorial() {
showTutorial.value = false
// 训练中且 BGM 开关开着 → 恢复 BGM
if (isPlaying.value && bgmEnabled.value) {
startBgm()
}
}
function onContinue() {
showSummary.value = false
startTrainingInternal()
@@ -837,6 +852,212 @@ $shadow-sm: 0 4rpx 12rpx rgba(15, 23, 42, 0.04);
border-radius: 0 0 40rpx 40rpx;
overflow: hidden;
box-sizing: border-box;
background: linear-gradient(180deg, #f0fdfa 0%, #f8fafc 55%, #f1f5f9 100%);
}
/* ============================================================
* 握力环舞台(由 grip-ring-canvas 组件内联而来)
* ============================================================ */
.ambient-glow {
position: absolute;
width: 1200rpx;
height: 1200rpx;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
border-radius: 50%;
background:
radial-gradient(circle,
rgba(204, 251, 241, 0.9) 0%,
rgba(240, 253, 250, 0.5) 30%,
rgba(241, 245, 249, 0.2) 55%,
transparent 72%);
pointer-events: none;
}
.ring-container {
position: relative;
width: 560rpx;
height: 560rpx;
display: flex;
align-items: center;
justify-content: center;
z-index: 1;
}
.beat-halo {
position: absolute;
width: 560rpx;
height: 560rpx;
border-radius: 50%;
background: radial-gradient(circle,
rgba(20, 184, 166, 0.45) 0%,
rgba(45, 212, 191, 0.18) 38%,
rgba(20, 184, 166, 0) 70%);
opacity: 0;
transition: opacity 0.3s ease;
will-change: transform, opacity;
}
.ring-container.playing .beat-halo {
opacity: 1;
animation: beat-pulse calc(var(--beat-duration, 750ms) * 2) ease-in-out infinite;
}
@keyframes beat-pulse {
0%, 100% {
transform: scale(1);
opacity: 0.4;
}
50% {
transform: scale(0.86);
opacity: 0.95;
}
}
.video-ring {
position: relative;
width: 460rpx;
height: 460rpx;
border-radius: 50%;
overflow: hidden;
box-shadow:
0 20rpx 56rpx rgba(20, 184, 166, 0.35),
0 10rpx 28rpx rgba(45, 212, 191, 0.22);
will-change: transform;
z-index: 1;
}
.ring-container.playing .video-ring {
animation: video-squeeze calc(var(--beat-duration, 750ms) * 2) ease-in-out infinite;
}
@keyframes video-squeeze {
0%, 100% {
transform: scale(1);
}
50% {
transform: scale(0.95);
}
}
.demo-video {
width: 100%;
height: 100%;
border-radius: 50%;
background: #e2f5f1;
}
.video-ring-border {
position: absolute;
inset: 0;
border-radius: 50%;
pointer-events: none;
box-shadow:
inset 0 0 0 4rpx rgba(255, 255, 255, 0.6),
inset 0 8rpx 24rpx rgba(15, 118, 110, 0.25);
}
.center-hint {
position: absolute;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 16rpx;
pointer-events: none;
z-index: 3;
animation: hint-fade-in 0.3s ease;
}
@keyframes hint-fade-in {
from {
opacity: 0;
transform: scale(0.9);
}
to {
opacity: 1;
transform: scale(1);
}
}
.hint-icon {
width: 116rpx;
height: 116rpx;
display: flex;
align-items: center;
justify-content: center;
background: rgba(15, 23, 42, 0.45);
backdrop-filter: blur(6rpx);
border: 3rpx solid rgba(255, 255, 255, 0.85);
border-radius: 50%;
box-shadow: 0 12rpx 32rpx rgba(15, 23, 42, 0.3);
animation: hint-bounce 2s ease-in-out infinite;
will-change: transform;
}
.play-triangle {
width: 0;
height: 0;
border-left: 34rpx solid #ffffff;
border-top: 23rpx solid transparent;
border-bottom: 23rpx solid transparent;
margin-left: 8rpx;
}
@keyframes hint-bounce {
0%, 100% {
transform: scale(1);
}
50% {
transform: scale(1.06);
}
}
.hint-text {
font-size: 26rpx;
color: #ffffff;
font-weight: 700;
letter-spacing: 3rpx;
text-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.55);
}
.bpm-chip {
position: absolute;
bottom: 24rpx;
left: 50%;
transform: translateX(-50%);
display: flex;
align-items: baseline;
gap: 6rpx;
padding: 8rpx 22rpx;
border-radius: 999rpx;
background: rgba(15, 118, 110, 0.78);
box-shadow: 0 6rpx 18rpx rgba(15, 118, 110, 0.35);
pointer-events: none;
z-index: 3;
}
.bpm-value {
font-size: 36rpx;
color: #ffffff;
font-weight: 900;
line-height: 1;
letter-spacing: -1rpx;
font-variant-numeric: tabular-nums;
}
.bpm-label {
font-size: 20rpx;
color: rgba(255, 255, 255, 0.85);
font-weight: 700;
letter-spacing: 2rpx;
}
.tap-overlay {
position: absolute;
inset: 0;
z-index: 5;
}
/* ============================================================
@@ -1189,193 +1410,6 @@ $shadow-sm: 0 4rpx 12rpx rgba(15, 23, 42, 0.04);
}
}
/* ============================================================
* 视频教学入口卡片
* ============================================================ */
.tutorial-card {
display: flex;
align-items: center;
gap: 16rpx;
padding: 18rpx 24rpx 18rpx 18rpx;
background: linear-gradient(135deg, #ffffff 0%, #f0fdfa 100%);
border-radius: 24rpx;
border: 2rpx solid rgba(20, 184, 166, 0.18);
box-shadow:
0 4rpx 16rpx rgba(15, 23, 42, 0.04),
0 1rpx 0 rgba(255, 255, 255, 0.9) inset;
transition: transform 0.15s;
&:active {
transform: scale(0.98);
background: linear-gradient(135deg, #f0fdfa 0%, #ecfeff 100%);
}
}
.tutorial-icon {
width: 72rpx;
height: 72rpx;
border-radius: 50%;
background: linear-gradient(140deg, $brand 0%, $brand-deep 100%);
display: flex;
align-items: center;
justify-content: center;
flex-shrink: 0;
box-shadow: 0 4rpx 12rpx rgba(20, 184, 166, 0.35);
}
.tutorial-play-triangle {
width: 0;
height: 0;
border-left: 22rpx solid #fff;
border-top: 14rpx solid transparent;
border-bottom: 14rpx solid transparent;
margin-left: 6rpx;
}
.tutorial-info {
flex: 1;
display: flex;
flex-direction: column;
gap: 4rpx;
min-width: 0;
}
.tutorial-title {
font-size: 28rpx;
font-weight: 700;
color: $text-1;
letter-spacing: 1rpx;
}
.tutorial-sub {
font-size: 22rpx;
color: $text-3;
letter-spacing: 0.5rpx;
}
.tutorial-arrow {
font-size: 36rpx;
color: #cbd5e1;
font-weight: 300;
line-height: 1;
flex-shrink: 0;
}
/* ============================================================
* 视频教学弹层
* ============================================================ */
.tutorial-mask {
position: fixed;
inset: 0;
background: rgba(15, 23, 42, 0.65);
backdrop-filter: blur(8rpx);
display: flex;
align-items: center;
justify-content: center;
z-index: 950;
padding: 0 24rpx;
animation: fade-in 0.2s ease;
}
.tutorial-modal {
width: 100%;
max-width: 700rpx;
background: #fff;
border-radius: 28rpx;
overflow: hidden;
display: flex;
flex-direction: column;
animation: tutorial-pop 0.25s cubic-bezier(0.34, 1.56, 0.64, 1);
}
@keyframes tutorial-pop {
from {
transform: scale(0.92);
opacity: 0;
}
to {
transform: scale(1);
opacity: 1;
}
}
.tutorial-header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 24rpx 28rpx 16rpx;
}
.tutorial-modal-title {
font-size: 32rpx;
font-weight: 700;
color: $text-1;
letter-spacing: 1rpx;
}
.tutorial-close {
width: 56rpx;
height: 56rpx;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
background: #f1f5f9;
&:active {
background: #e2e8f0;
}
}
.tutorial-close-icon {
font-size: 40rpx;
color: $text-2;
line-height: 1;
font-weight: 300;
}
.tutorial-video {
width: 100%;
// 视频源 720×720 方形, 容器同比贴合避免左右黑边
// modal max-width = 700rpx, 这里也用 700rpx 完美 1:1
height: 700rpx;
background: #000;
}
.tutorial-tips {
padding: 24rpx 28rpx 32rpx;
display: flex;
flex-direction: column;
gap: 12rpx;
}
.tutorial-tips-title {
font-size: 24rpx;
color: $text-3;
letter-spacing: 1rpx;
margin-bottom: 4rpx;
}
.tutorial-tip-item {
display: flex;
align-items: center;
gap: 12rpx;
}
.tip-dot {
width: 8rpx;
height: 8rpx;
border-radius: 50%;
background: $brand;
flex-shrink: 0;
}
.tip-content {
font-size: 26rpx;
color: $text-2;
line-height: 1.5;
}
/* ============================================================
* 完成浮层
* ============================================================ */