添加分享房间链接和对战结束返回房间

This commit is contained in:
kron
2026-01-09 11:50:21 +08:00
parent 4aa14c6a4c
commit 71b25144a4
14 changed files with 204 additions and 209 deletions

View File

@@ -1,52 +1,54 @@
<script setup>
import { ref } from "vue";
import { onShow } from "@dcloudio/uni-app";
import { onLoad, onShow } from "@dcloudio/uni-app";
import Container from "@/components/Container.vue";
import Guide from "@/components/Guide.vue";
import SButton from "@/components/SButton.vue";
import SModal from "@/components/SModal.vue";
import Signin from "@/components/Signin.vue";
import CreateRoom from "@/components/CreateRoom.vue";
import Avatar from "@/components/Avatar.vue";
import { getRoomAPI, joinRoomAPI, isGamingAPI, getBattleDataAPI } from "@/apis";
import { getRoomAPI, joinRoomAPI, getBattleDataAPI } from "@/apis";
import { debounce, canEenter } from "@/util";
import useStore from "@/store";
import { storeToRefs } from "pinia";
const store = useStore();
const { user, device, online } = storeToRefs(store);
import { debounce, canEenter } from "@/util";
const { user, device, online, game } = storeToRefs(store);
const showModal = ref(false);
const showSignin = ref(false);
const warnning = ref("");
const roomNumber = ref("");
const data = ref({});
const roomID = ref("");
const enterRoom = debounce(async () => {
const enterRoom = debounce(async (number) => {
if (!canEenter(user.value, device.value, online.value)) return;
const isGaming = await isGamingAPI();
if (isGaming) {
if (game.value.inBattle) {
uni.$showHint(1);
return;
}
if (!roomNumber.value) {
if (!number) {
warnning.value = "请输入房间号";
showModal.value = true;
} else {
const room = await getRoomAPI(roomNumber.value);
const room = await getRoomAPI(number);
if (room.number) {
const alreadyIn = room.members.find(
(item) => item.userInfo.id === user.value.id
);
if (!alreadyIn) {
const result = await joinRoomAPI(roomNumber.value);
const result = await joinRoomAPI(number);
if (result.full) {
warnning.value = "房间已满员";
showModal.value = true;
return;
}
}
roomNumber.value = "";
showModal.value = false;
uni.navigateTo({
url: `/pages/battle-room?roomNumber=${room.number}`,
url: "/pages/battle-room?roomNumber=" + number,
});
} else {
warnning.value = room.started ? "该房间对战已开始,无法加入" : "查无此房";
@@ -56,18 +58,24 @@ const enterRoom = debounce(async () => {
});
const onCreateRoom = async () => {
if (!canEenter(user.value, device.value, online.value)) return;
const isGaming = await isGamingAPI();
if (isGaming) {
uni.$showHint(1);
return;
}
warnning.value = "";
showModal.value = true;
};
const onSignin = () => {
showSignin.value = false;
if (roomID.value) enterRoom(roomID.value);
};
onShow(async () => {
const result = await getBattleDataAPI();
data.value = result;
});
onLoad(async (options) => {
roomID.value = options.roomID || "";
if (options.roomID) {
if (user.value.id) enterRoom(options.roomID);
else showSignin.value = true;
}
});
</script>
<template>
@@ -127,7 +135,7 @@ onShow(async () => {
v-model="roomNumber"
placeholder-style="color: #ccc"
/>
<view @click="enterRoom">进入房间</view>
<view @click="enterRoom(roomNumber)">进入房间</view>
</view>
</view>
<view class="create-room">
@@ -159,6 +167,9 @@ onShow(async () => {
</view>
<CreateRoom v-if="!warnning" :onConfirm="() => (showModal = false)" />
</SModal>
<SModal :show="showSignin" :onClose="() => (showSignin = false)">
<Signin :onClose="onSignin" />
</SModal>
</view>
</Container>
</template>