fix:修复冷启动丢失用户信息导致重复登录问题
This commit is contained in:
@@ -9,7 +9,7 @@ import Signin from "@/components/Signin.vue";
|
||||
import CreateRoom from "@/components/CreateRoom.vue";
|
||||
import Avatar from "@/components/Avatar.vue";
|
||||
|
||||
import { getRoomAPI, joinRoomAPI, getBattleDataAPI } from "@/apis";
|
||||
import { getRoomAPI, joinRoomAPI, getBattleDataAPI, getHomeData } from "@/apis";
|
||||
import { debounce, canEenter } from "@/util";
|
||||
|
||||
import useStore from "@/store";
|
||||
@@ -88,8 +88,26 @@ onShow(async () => {
|
||||
onLoad(async (options) => {
|
||||
if (options.roomID) {
|
||||
roomID.value = options.roomID;
|
||||
if (user.value.id) enterRoom(options.roomID);
|
||||
else showSignin.value = true;
|
||||
if (user.value.id) {
|
||||
// 内存中有用户状态,直接进房
|
||||
enterRoom(options.roomID);
|
||||
} else {
|
||||
// 冷启动(如分享链接进入)时 Pinia 状态已重置,尝试用已存储的 token 静默恢复用户信息
|
||||
const env = uni.getAccountInfoSync().miniProgram.envVersion;
|
||||
const token = uni.getStorageSync(`${env}_token`);
|
||||
if (token) {
|
||||
try {
|
||||
const homeData = await getHomeData();
|
||||
if (homeData?.user) {
|
||||
store.updateUser(homeData.user);
|
||||
enterRoom(options.roomID);
|
||||
return;
|
||||
}
|
||||
} catch {}
|
||||
}
|
||||
// token 不存在或已失效,弹出授权登录弹窗
|
||||
showSignin.value = true;
|
||||
}
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user