给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

@@ -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;
};