添加分享房间链接和对战结束返回房间
This commit is contained in:
@@ -2,12 +2,14 @@
|
||||
import { ref, watch, onMounted, onBeforeUnmount } from "vue";
|
||||
import { onShow } from "@dcloudio/uni-app";
|
||||
|
||||
import { isGamingAPI, getCurrentGameAPI } from "@/apis";
|
||||
import { getCurrentGameAPI, getUserGameState } from "@/apis";
|
||||
import { debounce } from "@/util";
|
||||
|
||||
import useStore from "@/store";
|
||||
import { storeToRefs } from "pinia";
|
||||
const { user } = storeToRefs(useStore());
|
||||
const store = useStore();
|
||||
const { user, game } = storeToRefs(store);
|
||||
const { updateGame } = store;
|
||||
|
||||
const props = defineProps({
|
||||
signin: {
|
||||
@@ -15,13 +17,14 @@ const props = defineProps({
|
||||
default: () => {},
|
||||
},
|
||||
});
|
||||
const show = ref(false);
|
||||
const loading = ref(false);
|
||||
|
||||
onShow(async () => {
|
||||
if (user.value.id) {
|
||||
const isGaming = await isGamingAPI();
|
||||
show.value = isGaming;
|
||||
setTimeout(async () => {
|
||||
const state = await getUserGameState();
|
||||
updateGame(state.gaming, state.roomId);
|
||||
}, 1000);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -29,10 +32,10 @@ watch(
|
||||
() => user.value,
|
||||
async (value) => {
|
||||
if (!value.id) {
|
||||
show.value = false;
|
||||
updateGame(false, "");
|
||||
} else {
|
||||
const isGaming = await isGamingAPI();
|
||||
show.value = isGaming;
|
||||
const state = await getUserGameState();
|
||||
updateGame(state.gaming, state.roomId);
|
||||
}
|
||||
}
|
||||
);
|
||||
@@ -40,26 +43,23 @@ watch(
|
||||
const onClick = debounce(async () => {
|
||||
if (loading.value) return;
|
||||
try {
|
||||
const isGaming = await isGamingAPI();
|
||||
show.value = isGaming;
|
||||
|
||||
if (isGaming) {
|
||||
loading.value = true;
|
||||
loading.value = true;
|
||||
if (game.value.inBattle) {
|
||||
await uni.$checkAudio();
|
||||
const result = await getCurrentGameAPI();
|
||||
loading.value = false;
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: "比赛已结束",
|
||||
icon: "none",
|
||||
} else if (game.value.roomID) {
|
||||
uni.navigateTo({
|
||||
url: "/pages/battle-room?roomNumber=" + game.value.roomID,
|
||||
});
|
||||
} else {
|
||||
updateGame(false, "");
|
||||
}
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
});
|
||||
const gameOver = () => {
|
||||
show.value = false;
|
||||
updateGame(false, "");
|
||||
};
|
||||
onMounted(() => {
|
||||
uni.$on("game-over", gameOver);
|
||||
@@ -70,10 +70,19 @@ onBeforeUnmount(() => {
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<view v-if="show" class="back-to-game" @click="onClick">
|
||||
<view
|
||||
v-if="game.inBattle || game.roomID"
|
||||
class="back-to-game"
|
||||
@click="onClick"
|
||||
>
|
||||
<image src="../static/back-to-game-bg.png" mode="widthFix" />
|
||||
<image src="../static/pk-icon.png" mode="widthFix" />
|
||||
<text>返回进行中的对局</text>
|
||||
<block v-if="game.inBattle">
|
||||
<image src="../static/pk-icon.png" mode="widthFix" />
|
||||
<text>返回进行中的对局</text>
|
||||
</block>
|
||||
<block v-else-if="game.roomID">
|
||||
<text>返回房间</text>
|
||||
</block>
|
||||
<image src="../static/back.png" mode="widthFix" />
|
||||
</view>
|
||||
</template>
|
||||
@@ -93,17 +102,18 @@ onBeforeUnmount(() => {
|
||||
.back-to-game > image:first-child {
|
||||
position: absolute;
|
||||
width: 100%;
|
||||
height: 100rpx;
|
||||
}
|
||||
.back-to-game > image:nth-child(2) {
|
||||
position: relative;
|
||||
width: 60px;
|
||||
height: 60px;
|
||||
}
|
||||
.back-to-game > text:nth-child(3) {
|
||||
.back-to-game > text {
|
||||
position: relative;
|
||||
font-size: 14px;
|
||||
}
|
||||
.back-to-game > image:nth-child(4) {
|
||||
.back-to-game > image:last-child {
|
||||
position: relative;
|
||||
width: 15px;
|
||||
margin-left: 5px;
|
||||
|
||||
Reference in New Issue
Block a user