Files
shoot-miniprograms/src/components/Container.vue

336 lines
8.2 KiB
Vue
Raw Normal View History

2025-05-23 21:20:38 +08:00
<script setup>
2025-11-26 17:12:55 +08:00
import { ref, computed, onMounted, onBeforeUnmount } from "vue";
import { onShow } from "@dcloudio/uni-app";
2025-05-23 21:20:38 +08:00
import AppBackground from "@/components/AppBackground.vue";
import Header from "@/components/Header.vue";
2025-07-02 17:21:44 +08:00
import ScreenHint from "@/components/ScreenHint.vue";
2025-07-13 14:57:16 +08:00
import BackToGame from "@/components/BackToGame.vue";
2026-02-07 10:52:56 +08:00
import { laserAimAPI, getBattleAPI } from "@/apis";
2026-01-12 15:03:20 +08:00
import { capsuleHeight, debounce } from "@/util";
2025-11-26 17:12:55 +08:00
import AudioManager from "@/audioManager";
2025-08-07 10:48:05 +08:00
const props = defineProps({
2025-05-23 21:20:38 +08:00
title: {
type: String,
default: "",
},
bgType: {
2025-05-27 12:38:39 +08:00
type: Number,
2025-05-23 21:20:38 +08:00
default: 0,
},
2025-06-17 16:42:53 +08:00
onBack: {
type: Function,
default: null,
},
2026-01-09 18:12:27 +08:00
scroll: {
type: Boolean,
default: true,
2025-06-18 12:32:08 +08:00
},
2025-07-02 17:21:44 +08:00
isHome: {
type: Boolean,
default: false,
},
2025-07-13 14:57:16 +08:00
showBackToGame: {
type: Boolean,
default: false,
2025-07-13 14:57:16 +08:00
},
2025-07-29 10:46:37 +08:00
bgColor: {
type: String,
default: "#050b19",
},
whiteBackArrow: {
type: Boolean,
default: true,
},
2026-01-12 10:09:36 +08:00
showBottom: {
type: Boolean,
default: true,
},
2025-05-23 21:20:38 +08:00
});
2026-01-09 18:12:27 +08:00
const isIOS = uni.getDeviceInfo().osName === "ios";
2025-07-02 17:21:44 +08:00
const showHint = ref(false);
2025-07-03 21:12:59 +08:00
const hintType = ref(0);
2025-09-18 09:28:14 +08:00
const isLoading = ref(false);
2025-11-26 17:12:55 +08:00
const audioInitProgress = ref(1);
const audioProgress = ref(0);
const audioTimer = ref(null);
2025-09-18 09:28:14 +08:00
2025-07-03 21:12:59 +08:00
const showGlobalHint = (type) => {
hintType.value = type;
2025-07-02 17:21:44 +08:00
showHint.value = true;
};
2025-09-18 09:28:14 +08:00
2025-07-14 13:39:10 +08:00
const hideGlobalHint = () => {
showHint.value = false;
};
2025-09-18 09:28:14 +08:00
2025-12-30 18:10:31 +08:00
const restart = () => {
uni.restartMiniProgram({
path: "/pages/index",
});
};
2025-11-26 17:12:55 +08:00
const checkAudioProgress = async () => {
return new Promise((resolve, reject) => {
try {
audioInitProgress.value = AudioManager.getLoadProgress();
if (audioInitProgress.value === 1) return resolve();
audioTimer.value = setInterval(() => {
2025-12-30 18:10:31 +08:00
audioProgress.value = AudioManager.getLoadProgress();
2025-11-26 17:12:55 +08:00
if (audioProgress.value === 1) {
setTimeout(() => {
audioInitProgress.value = 1;
}, 200);
clearInterval(audioTimer.value);
resolve();
}
}, 200);
} catch (err) {
reject(err);
}
});
};
const audioFinalProgress = computed(() => {
const left = 1 - audioInitProgress.value;
2025-11-27 18:16:32 +08:00
return Math.max(0, (audioProgress.value - audioInitProgress.value) / left);
2025-11-26 17:12:55 +08:00
});
onBeforeUnmount(() => {
if (audioTimer.value) clearInterval(audioTimer.value);
});
onShow(() => {
uni.$showHint = showGlobalHint;
2025-07-14 13:39:10 +08:00
uni.$hideHint = hideGlobalHint;
2025-11-26 17:12:55 +08:00
uni.$checkAudio = checkAudioProgress;
showHint.value = false;
});
2025-09-18 09:28:14 +08:00
const backToGame = debounce(async () => {
2025-09-18 09:28:14 +08:00
if (isLoading.value) return; // 防止重复点击
2025-10-30 09:19:34 +08:00
2025-09-18 09:28:14 +08:00
try {
isLoading.value = true;
2026-02-07 10:52:56 +08:00
const result = await getBattleAPI();
if (result && result.matchId) {
await checkAudioProgress();
if (result.mode <= 3) {
uni.navigateTo({
url: `/pages/team-battle?battleId=${result.matchId}`,
});
} else {
uni.navigateTo({
url: `/pages/melee-battle?battleId=${result.matchId}`,
});
}
2025-09-18 09:28:14 +08:00
}
} catch (error) {
2025-10-30 09:19:34 +08:00
console.error("获取当前游戏失败:", error);
2025-09-18 09:28:14 +08:00
} finally {
isLoading.value = false;
}
});
2025-09-18 09:28:14 +08:00
2025-07-11 00:47:34 +08:00
const goBack = () => {
uni.navigateBack();
};
2025-10-30 09:19:34 +08:00
2025-10-31 10:22:02 +08:00
const goCalibration = async () => {
await laserAimAPI();
2025-10-30 09:19:34 +08:00
uni.navigateTo({
url: "/pages/calibration",
});
};
2025-05-23 21:20:38 +08:00
</script>
<template>
2025-08-07 10:48:05 +08:00
<view :style="{ paddingTop: capsuleHeight + 'px' }">
2025-07-29 10:46:37 +08:00
<AppBackground :type="bgType" :bgColor="bgColor" />
<Header
v-if="!isHome"
:title="title"
:onBack="onBack"
:whiteBackArrow="whiteBackArrow"
/>
2025-07-13 14:57:16 +08:00
<BackToGame v-if="showBackToGame" />
2026-01-09 18:12:27 +08:00
<scroll-view
:scroll-y="scroll"
:enhanced="true"
:bounces="false"
:show-scrollbar="false"
2025-07-03 21:12:59 +08:00
:style="{
2026-01-09 18:12:27 +08:00
height: `calc(100vh - ${capsuleHeight + (isHome ? 0 : 50)}px - ${
2026-01-12 15:03:20 +08:00
$slots.bottom && showBottom ? (isIOS ? '75px' : '65px') : '0px'
2026-01-09 18:12:27 +08:00
})`,
2025-07-03 21:12:59 +08:00
}"
2025-06-15 22:01:06 +08:00
>
2025-05-23 21:20:38 +08:00
<slot></slot>
2026-01-09 18:12:27 +08:00
</scroll-view>
<view
class="bottom-part"
2026-01-12 10:09:36 +08:00
v-if="$slots.bottom && showBottom"
2026-01-12 15:03:20 +08:00
:style="{ height: isIOS ? '65px' : '55px', paddingTop: '10px' }"
2026-01-09 18:12:27 +08:00
>
<slot name="bottom"></slot>
2025-05-23 21:20:38 +08:00
</view>
2025-07-03 21:12:59 +08:00
<ScreenHint :show="showHint">
2025-07-11 00:47:34 +08:00
<view v-if="hintType === 1" class="tip-content">
2025-07-24 11:54:38 +08:00
<text>完成进行中的对局才能开启新的</text>
<text>您有正在进行中的对局是否进入?</text>
2025-07-03 21:12:59 +08:00
<view>
<button hover-class="none" @click="() => (showHint = false)">
不进入
</button>
2025-10-30 09:19:34 +08:00
<button hover-class="none" @click="backToGame" :disabled="isLoading">
{{ isLoading ? "加载中..." : "进入" }}
2025-09-18 09:28:14 +08:00
</button>
2025-07-03 21:12:59 +08:00
</view>
</view>
2025-07-11 00:47:34 +08:00
<view v-if="hintType === 2" class="tip-content">
<text>离开比赛可能会导致比赛失败</text>
<text>确认离开吗</text>
<view>
2025-07-21 16:15:14 +08:00
<button hover-class="none" @click="goBack">离开比赛</button>
2025-07-11 00:47:34 +08:00
<button hover-class="none" @click="() => (showHint = false)">
继续比赛
</button>
</view>
</view>
2025-07-11 01:00:54 +08:00
<view v-if="hintType === 3" class="tip-content">
<text>今天不玩了吗</text>
<view>
<button hover-class="none" @click="() => (showHint = false)">
取消
</button>
2025-07-21 16:15:14 +08:00
<button hover-class="none" @click="goBack">确认</button>
2025-07-11 01:00:54 +08:00
</view>
</view>
2025-10-30 09:19:34 +08:00
<view v-if="hintType === 4" class="tip-content">
<text>完成智能弓校准即可解锁全部功能</text>
<view>
<button hover-class="none" @click="() => (showHint = false)">
取消
</button>
<button hover-class="none" @click="goCalibration">去校准</button>
</view>
</view>
2025-07-02 17:21:44 +08:00
</ScreenHint>
2025-11-26 17:12:55 +08:00
<view v-if="audioInitProgress < 1" class="audio-progress">
<image
src="https://static.shelingxingqiu.com/attachment/2025-11-26/deihtj15xjwcz3c1tx.png"
mode="widthFix"
/>
<view>
<view :style="{ width: `${audioFinalProgress * 100}%` }">
2025-12-30 18:10:31 +08:00
<!-- <image
2025-11-26 17:12:55 +08:00
src="https://static.shelingxingqiu.com/attachment/2025-11-24/degu91a7si77sg9jqv.png"
mode="widthFix"
2025-12-30 18:10:31 +08:00
/> -->
2025-11-26 17:12:55 +08:00
</view>
2025-12-29 11:53:19 +08:00
</view>
<view>
<text>若加载时间过长</text>
2025-12-30 18:10:31 +08:00
<button hover-class="none" @click="restart">点击这里重启</button>
2025-11-26 17:12:55 +08:00
</view>
</view>
2025-05-23 21:20:38 +08:00
</view>
</template>
<style scoped>
2025-07-11 00:47:34 +08:00
.tip-content {
2025-07-03 21:12:59 +08:00
flex-direction: column;
display: flex;
align-items: center;
justify-content: center;
color: #fff;
width: 100%;
font-size: 14px;
}
2025-07-11 00:47:34 +08:00
.tip-content > text {
text-align: center;
}
.tip-content > view {
2025-07-03 21:12:59 +08:00
display: flex;
align-items: center;
justify-content: space-around;
margin-top: 50rpx;
width: 100%;
}
2025-07-11 00:47:34 +08:00
.tip-content > view > button {
2025-07-03 21:12:59 +08:00
padding: 12px;
border-radius: 20px;
background-color: #fff6;
color: #fff;
width: 45%;
font-size: 16px;
}
2025-07-21 16:15:14 +08:00
.tip-content > view > button:last-child {
2025-07-03 21:12:59 +08:00
background-color: #fed847;
color: #000;
}
2025-09-18 09:28:14 +08:00
.tip-content > view > button:disabled {
background-color: #ccc;
color: #666;
opacity: 0.6;
}
2025-11-26 17:12:55 +08:00
.audio-progress {
z-index: 999;
width: 100vw;
height: 100vh;
position: fixed;
top: 0;
left: 0;
2025-12-30 18:10:31 +08:00
background: rgb(0 0 0 / 0.8);
2025-11-26 17:12:55 +08:00
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.audio-progress > image:nth-child(1) {
width: 140rpx;
height: 150rpx;
margin-bottom: 20rpx;
}
.audio-progress > view:nth-child(2) {
width: 380rpx;
height: 6rpx;
2025-12-30 18:10:31 +08:00
background: #595959;
2025-11-26 17:12:55 +08:00
border-radius: 4rpx;
display: flex;
flex-direction: column;
align-items: flex-start;
justify-content: flex-start;
}
.audio-progress > view:nth-child(2) > view {
background: #ffe431;
min-height: 6rpx;
border-radius: 4rpx;
display: flex;
align-items: center;
justify-content: flex-end;
transition: width 0.5s ease;
}
.audio-progress > view:nth-child(2) > view > image {
width: 46rpx;
height: 26rpx;
}
2025-12-29 11:53:19 +08:00
.audio-progress > view:nth-child(3) {
display: flex;
align-items: center;
justify-content: center;
}
.audio-progress > view:nth-child(3) > text {
2025-11-26 17:12:55 +08:00
font-size: 22rpx;
color: #a2a2a2;
text-align: center;
2025-12-29 11:53:19 +08:00
line-height: 32rpx;
}
.audio-progress > view:nth-child(3) > button {
font-size: 22rpx;
color: #ffe431;
line-height: 32rpx;
2025-12-30 18:10:31 +08:00
padding: 20rpx 0;
2025-11-26 17:12:55 +08:00
}
2025-05-23 21:20:38 +08:00
</style>