fix:房间号更新改为pinia

This commit is contained in:
2026-05-07 17:15:28 +08:00
parent 29a6f46a0d
commit 8c66ef78c6
3 changed files with 28 additions and 21 deletions

View File

@@ -47,6 +47,10 @@ const allReady = ref(false);
const timer = ref(null);
const goBattle = ref(false);
/**
* 从服务端刷新当前房间数据,更新成员列表、准备状态等信息
* 仅在 roomNumber 有效且房间未开始时执行
*/
async function refreshRoomData() {
if (!roomNumber.value) return;
const result = await getRoomAPI(roomNumber.value);
@@ -109,9 +113,6 @@ async function refreshRoomData() {
}
if (timer.value) clearInterval(timer.value);
// timer.value = setTimeout(refreshRoomData, 2000);
// 通知 Header 组件更新房号展示
uni.$emit("battle-room-info", { roomNumber: roomNumber.value });
}
const startGame = async () => {
@@ -248,19 +249,32 @@ onShareAppMessage(() => {
};
});
/**
* onShow 生命周期:页面显示时刷新房间数据
* 注uni-app 中 onShow 可能早于 onLoad 执行,此时 roomNumber 尚未赋值,
* refreshRoomData 会提前 return。
*/
onShow(() => {
goBattle.value = false;
refreshRoomData();
});
/**
* 页面加载时解析路由参数,初始化房间号
* - 存入本地 ref供页面内部逻辑使用
* - 同步到 Pinia Store供 Header 组件展示房号胶囊)
*/
onLoad(async (options) => {
if (options.roomNumber) {
roomNumber.value = options.roomNumber;
// 立即通知 Header 显示房号,无需等待异步 API 返回
uni.$emit("battle-room-info", { roomNumber: options.roomNumber });
store.updateRoomNumber(options.roomNumber);
}
});
/**
* 组件挂载后:保持屏幕常亮、注册 WebSocket 消息监听
* 房间号已通过 onLoad 同步到 StoreHeader 从 Store 直接读取,无需额外通知
*/
onMounted(() => {
uni.setKeepScreenOn({
keepScreenOn: true,