给token区分环境

This commit is contained in:
kron
2025-09-12 17:49:26 +08:00
parent 24b776f327
commit 0a151de3c9
6 changed files with 36 additions and 15 deletions

View File

@@ -11,7 +11,9 @@ const { updateUser } = store;
watch(
() => user.value.id,
(newVal) => {
const token = uni.getStorageSync("token");
const token = uni.getStorageSync(
`${uni.getAccountInfoSync().miniProgram.envVersion}_token`
);
if (newVal && token) {
websocket.createWebSocket(token, (content) => {
uni.$emit("socket-inbox", content);
@@ -28,7 +30,9 @@ watch(
);
onShow(() => {
const token = uni.getStorageSync("token");
const token = uni.getStorageSync(
`${uni.getAccountInfoSync().miniProgram.envVersion}_token`
);
if (user.value.id && token) {
console.log("回到前台,重新连接 websocket");
websocket.createWebSocket(token, (content) => {

View File

@@ -24,7 +24,9 @@ try {
}
function request(method, url, data = {}) {
const token = uni.getStorageSync("token");
const token = uni.getStorageSync(
`${uni.getAccountInfoSync().miniProgram.envVersion}_token`
);
const header = {};
if (token) header.Authorization = `Bearer ${token || ""}`;
return new Promise((resolve, reject) => {
@@ -40,7 +42,9 @@ function request(method, url, data = {}) {
if (code === 0) resolve(data);
else if (message) {
if (message.indexOf("登录身份已失效") !== -1) {
uni.removeStorageSync("token");
uni.removeStorageSync(
`${uni.getAccountInfoSync().miniProgram.envVersion}_token`
);
}
if (message === "ROOM_FULL") {
resolve({ full: true });
@@ -71,17 +75,17 @@ function request(method, url, data = {}) {
return;
}
if (message === "ROOM_EMPTY") {
uni.showToast({
return uni.showToast({
title: "房间已过期",
icon: "none",
});
return;
}
uni.showToast({
title: message,
icon: "none",
});
}
reject("");
}
},
fail: (err) => {
@@ -166,7 +170,10 @@ export const loginAPI = async (nickName, avatarData, code) => {
avatarData,
code,
});
uni.setStorageSync("token", result.token);
uni.setStorageSync(
`${uni.getAccountInfoSync().miniProgram.envVersion}_token`,
result.token
);
return result;
};

View File

@@ -61,7 +61,9 @@ onShow(async () => {
const rankList = await getRankListAPI();
console.log("排行数据", rankList);
updateRank(rankList);
const token = uni.getStorageSync("token");
const token = uni.getStorageSync(
`${uni.getAccountInfoSync().miniProgram.envVersion}_token`
);
if (token) {
const result = await getHomeData();
console.log("首页数据:", result);
@@ -100,16 +102,18 @@ const comingSoon = () => {
onShareAppMessage(() => {
return {
title: "快来和我一起玩射箭", // 分享卡片的标题
title: "智能真弓:实时捕捉+毫秒级同步,弓箭选手全球竞技", // 分享卡片的标题
path: "/pages/index", // 用户点击分享卡片后跳转的页面路径
imageUrl: "/static/share-bg.png", // 分享卡片的配图,可以是本地或网络图片
imageUrl:
"https://static.shelingxingqiu.com/attachment/2025-09-12/dcqoz26q0268wxmzjg.png", // 分享卡片的配图,可以是本地或网络图片
};
});
onShareTimeline(() => {
return {
title: "我发现了一个超好玩的射箭小程序,一起来玩吧", // 分享到朋友圈的标题
title: "智能真弓:实时捕捉+毫秒级同步,弓箭选手全球竞技", // 分享到朋友圈的标题
query: "from=timeline", // 用户通过朋友圈点击后,在页面 onShow 的 options 中可以获取到的参数
imageUrl: "/static/share-bg.png", // 分享到朋友圈的配图
imageUrl:
"https://static.shelingxingqiu.com/attachment/2025-09-12/dcqoz26q0268wxmzjg.png", // 分享到朋友圈的配图
};
});
</script>

View File

@@ -55,7 +55,9 @@ const toAboutUsPage = () => {
const showLogout = ref(false);
const logout = () => {
uni.removeStorageSync("token");
uni.removeStorageSync(
`${uni.getAccountInfoSync().miniProgram.envVersion}_token`
);
uni.navigateBack();
updateUser();
};

View File

@@ -15,7 +15,9 @@ export const checkConnection = () => {
data: JSON.stringify({ event: "ping", data: {} }),
fail: () => {
websocket.closeWebSocket();
const token = uni.getStorageSync("token");
const token = uni.getStorageSync(
`${uni.getAccountInfoSync().miniProgram.envVersion}_token`
);
if (!token) return;
// 如果发送失败,说明连接已断开,需要重新连接
websocket.createWebSocket(token, (content) => {

View File

@@ -102,7 +102,9 @@ function reconnect(onMessage) {
reconnectTimer && clearTimeout(reconnectTimer);
closeWebSocket(); // 确保关闭旧连接
const token = uni.getStorageSync("token");
const token = uni.getStorageSync(
`${uni.getAccountInfoSync().miniProgram.envVersion}_token`
);
if (!token) return;
reconnectTimer = setTimeout(() => {