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

View File

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

View File

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

View File

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

View File

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

View File

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