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

232 lines
5.4 KiB
Vue
Raw Normal View History

2025-05-23 21:20:38 +08:00
<script setup>
2025-09-24 21:05:06 +08:00
import { ref, onMounted } 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";
2025-10-31 10:22:02 +08:00
import { getCurrentGameAPI, laserAimAPI } from "@/apis";
import { debounce } from "@/util";
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,
},
2025-06-18 12:32:08 +08:00
overflow: {
type: String,
default: "auto",
},
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,
},
2025-05-23 21:20:38 +08:00
});
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-08-07 10:48:05 +08:00
const capsuleHeight = ref(0);
2025-09-18 09:28:14 +08:00
const isLoading = ref(false);
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
onMounted(() => {
2025-08-07 10:48:05 +08:00
const menuBtnInfo = uni.getMenuButtonBoundingClientRect();
capsuleHeight.value = menuBtnInfo.top - 9;
});
2025-09-18 09:28:14 +08:00
onShow(() => {
uni.$showHint = showGlobalHint;
2025-07-14 13:39:10 +08:00
uni.$hideHint = hideGlobalHint;
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;
2025-10-30 09:19:34 +08:00
2025-09-18 09:28:14 +08:00
// 设置请求超时
const timeoutPromise = new Promise((_, reject) => {
2025-10-30 09:19:34 +08:00
setTimeout(() => reject(new Error("请求超时")), 10000); // 10秒超时
2025-09-18 09:28:14 +08:00
});
2025-10-30 09:19:34 +08:00
const result = await Promise.race([getCurrentGameAPI(), timeoutPromise]);
2025-09-18 09:28:14 +08:00
// 处理返回结果
if (result && result.gameId) {
// 跳转到游戏页面
uni.navigateTo({
2025-10-30 09:19:34 +08:00
url: `/pages/battle-room?gameId=${result.gameId}`,
2025-09-18 09:28:14 +08:00
});
} else {
uni.showToast({
2025-10-30 09:19:34 +08:00
title: "没有进行中的对局",
icon: "none",
2025-09-18 09:28:14 +08:00
});
}
2025-10-30 09:19:34 +08:00
2025-09-18 09:28:14 +08:00
showHint.value = false;
} catch (error) {
2025-10-30 09:19:34 +08:00
console.error("获取当前游戏失败:", error);
2025-09-18 09:28:14 +08:00
uni.showToast({
2025-10-30 09:19:34 +08:00
title: error.message || "网络请求失败,请重试",
icon: "none",
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" />
2025-06-15 22:01:06 +08:00
<view
class="content"
2025-07-03 21:12:59 +08:00
:style="{
2025-08-07 10:48:05 +08:00
height: `calc(100vh - ${capsuleHeight + (isHome ? 0 : 50)}px)`,
2025-07-03 21:12:59 +08:00
overflow,
}"
2025-06-15 22:01:06 +08:00
>
2025-05-23 21:20:38 +08:00
<slot></slot>
</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-05-23 21:20:38 +08:00
</view>
</template>
<style scoped>
.content {
2025-06-06 00:34:54 +08:00
width: 100vw;
2025-07-02 17:21:44 +08:00
height: 100vh;
2025-06-06 00:34:54 +08:00
overflow-x: hidden;
2025-06-02 14:42:07 +08:00
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-between;
2025-06-21 12:59:59 +08:00
overflow-x: hidden;
2025-05-23 21:20:38 +08:00
}
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-05-23 21:20:38 +08:00
</style>