feat: 区别不同的小程序的缓存问题
This commit is contained in:
19
src/constant/token.js
Normal file
19
src/constant/token.js
Normal file
@@ -0,0 +1,19 @@
|
||||
import { appId, clientId } from "@/constant/base";
|
||||
|
||||
/// 存储在本地的认证 token 键名
|
||||
export const clientAuthToken = "AUTH_TOKEN_" + clientId + "_" + appId;
|
||||
|
||||
/// 设置本地存储的认证 token
|
||||
export const setStorageSyncToken = (token) => {
|
||||
uni.setStorageSync(clientAuthToken, token);
|
||||
};
|
||||
|
||||
/// 获取本地存储的认证 token
|
||||
export const getStorageSyncToken = () => {
|
||||
return uni.getStorageSync(clientAuthToken);
|
||||
};
|
||||
|
||||
/// 移除本地存储的认证 token
|
||||
export const removeStorageSyncToken = () => {
|
||||
uni.setStorageSync(clientAuthToken, "");
|
||||
};
|
||||
@@ -2,6 +2,7 @@ import { wxLogin } from "../request/api/LoginApi";
|
||||
import { loginAuth, bindPhone, checkPhone } from "@/manager/LoginManager";
|
||||
import { clientId } from "@/constant/base";
|
||||
import { NOTICE_EVENT_LOGIN_SUCCESS } from "@/constant/constant";
|
||||
import { getStorageSyncToken, setStorageSyncToken } from "../constant/token";
|
||||
|
||||
// 跳转登录
|
||||
export const goLogin = () => uni.navigateTo({ url: "/pages/login/index" });
|
||||
@@ -47,7 +48,7 @@ export const onLogin = async (e) => {
|
||||
|
||||
// 检测token
|
||||
export const checkToken = () => {
|
||||
const token = uni.getStorageSync("token");
|
||||
const token = getStorageSyncToken();
|
||||
|
||||
return new Promise((resolve) => {
|
||||
if (!token) {
|
||||
@@ -61,7 +62,7 @@ export const checkToken = () => {
|
||||
|
||||
// 刷新token
|
||||
export const refreshToken = () => {
|
||||
const token = uni.getStorageSync("token");
|
||||
const token = getStorageSyncToken();
|
||||
|
||||
if (!token) {
|
||||
return;
|
||||
@@ -81,7 +82,7 @@ export const refreshToken = () => {
|
||||
const response = await wxLogin(params);
|
||||
|
||||
if (response.access_token) {
|
||||
uni.setStorageSync("token", response.access_token);
|
||||
setStorageSyncToken(response.access_token);
|
||||
// 登录成功后,触发登录成功事件
|
||||
uni.$emit(NOTICE_EVENT_LOGIN_SUCCESS);
|
||||
}
|
||||
|
||||
@@ -7,9 +7,10 @@ import { getWeChatAuthCode } from "./AuthManager";
|
||||
import { useAppStore } from "@/store";
|
||||
import { clientId } from "@/constant/base";
|
||||
import { NOTICE_EVENT_LOGIN_SUCCESS } from "@/constant/constant";
|
||||
import { removeStorageSyncToken, setStorageSyncToken } from "../constant/token";
|
||||
|
||||
const loginAuth = (e) => {
|
||||
uni.setStorageSync("token", "");
|
||||
removeStorageSyncToken();
|
||||
const appStore = useAppStore();
|
||||
|
||||
return new Promise(async (resolve, reject) => {
|
||||
@@ -26,7 +27,7 @@ const loginAuth = (e) => {
|
||||
console.log("获取到的微信授权response:", response);
|
||||
|
||||
if (response.access_token) {
|
||||
uni.setStorageSync("token", response.access_token);
|
||||
setStorageSyncToken(response.access_token);
|
||||
// 登录成功后,触发登录成功事件
|
||||
uni.$emit(NOTICE_EVENT_LOGIN_SUCCESS);
|
||||
resolve();
|
||||
|
||||
@@ -159,6 +159,7 @@ import WebSocketManager from "@/utils/WebSocketManager";
|
||||
import { ThrottleUtils, IdUtils } from "@/utils";
|
||||
import { checkToken } from "@/hooks/useGoLogin";
|
||||
import { useAppStore } from "@/store";
|
||||
import { getStorageSyncToken } from "@/constant/token";
|
||||
|
||||
const appStore = useAppStore();
|
||||
/// 导航栏相关
|
||||
@@ -346,7 +347,7 @@ onLoad(() => {
|
||||
// token存在,初始化数据
|
||||
const initHandler = () => {
|
||||
console.log("initHandler");
|
||||
const token = uni.getStorageSync("token");
|
||||
const token = getStorageSyncToken();
|
||||
|
||||
if (!token) return;
|
||||
loadRecentConversation();
|
||||
@@ -414,7 +415,7 @@ const initWebSocket = async () => {
|
||||
}
|
||||
|
||||
// 使用配置的WebSocket服务器地址
|
||||
const token = uni.getStorageSync("token");
|
||||
const token = getStorageSyncToken();
|
||||
const wsUrl = `${appStore.serverConfig.wssUrl}?access_token=${token}`;
|
||||
|
||||
// 初始化WebSocket管理器
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import { goLogin } from "@/hooks/useGoLogin";
|
||||
import { useAppStore } from "@/store";
|
||||
import { getStorageSyncToken } from "@/constant/token";
|
||||
import { removeStorageSyncToken } from "@/constant/token";
|
||||
|
||||
/// 请求流式数据的API
|
||||
const API = "/agent/assistant/chat";
|
||||
@@ -72,7 +74,7 @@ const stopAbortTask = () => {
|
||||
|
||||
const agentChatStream = (params, onChunk) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const token = uni.getStorageSync("token");
|
||||
const token = getStorageSyncToken();
|
||||
const requestId = Date.now().toString(); // 生成唯一请求ID
|
||||
|
||||
// 重置状态
|
||||
@@ -130,7 +132,7 @@ const agentChatStream = (params, onChunk) => {
|
||||
res.statusCode
|
||||
);
|
||||
if (res.statusCode === 424) {
|
||||
uni.setStorageSync("token", "");
|
||||
removeStorageSyncToken();
|
||||
goLogin();
|
||||
}
|
||||
if (onChunk) {
|
||||
@@ -144,8 +146,7 @@ const agentChatStream = (params, onChunk) => {
|
||||
}
|
||||
} else {
|
||||
console.log(
|
||||
`❌ 请求 [${requestId}] ${
|
||||
isAborted ? "已终止" : "已过期"
|
||||
`❌ 请求 [${requestId}] ${isAborted ? "已终止" : "已过期"
|
||||
},忽略complete回调`
|
||||
);
|
||||
}
|
||||
@@ -240,7 +241,7 @@ const weAtob = (string) => {
|
||||
r2,
|
||||
i = 0;
|
||||
|
||||
for (; i < string.length; ) {
|
||||
for (; i < string.length;) {
|
||||
bitmap =
|
||||
(b64.indexOf(string.charAt(i++)) << 18) |
|
||||
(b64.indexOf(string.charAt(i++)) << 12) |
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { removeStorageSyncToken } from "@/constant/token";
|
||||
import request from "../base/request";
|
||||
|
||||
const wxLogin = (args) => {
|
||||
@@ -8,7 +9,7 @@ const wxLogin = (args) => {
|
||||
},
|
||||
};
|
||||
|
||||
uni.setStorageSync("token", "");
|
||||
removeStorageSyncToken();
|
||||
|
||||
return request.post("/auth/oauth2/token", args, config);
|
||||
};
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import { getCurrentConfig } from "@/constant/base";
|
||||
import { useAppStore } from "@/store";
|
||||
import { getStorageSyncToken } from "@/constant/token";
|
||||
|
||||
export const updateImageFile = (file) => {
|
||||
const { serverConfig } = useAppStore();
|
||||
const url = serverConfig.baseUrl + "/hotelBiz/hotBizCommon/upload";
|
||||
const token = uni.getStorageSync("token");
|
||||
const token = getStorageSyncToken();
|
||||
const clientId = getCurrentConfig().clientId;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
@@ -2,6 +2,8 @@ import { goLogin } from "../../hooks/useGoLogin";
|
||||
import { getCurrentConfig } from "@/constant/base";
|
||||
import { useAppStore } from "@/store";
|
||||
import { NOTICE_EVENT_LOGOUT } from "@/constant/constant";
|
||||
import { getStorageSyncToken } from "@/constant/token";
|
||||
import { removeStorageSyncToken } from "@/constant/token";
|
||||
|
||||
const clientId = getCurrentConfig().clientId;
|
||||
const defaultConfig = {
|
||||
@@ -19,7 +21,7 @@ function request(url, args = {}, method = "POST", customConfig = {}) {
|
||||
url = appStore.serverConfig?.baseUrl + url;
|
||||
}
|
||||
// 动态获取 token
|
||||
const token = uni.getStorageSync("token");
|
||||
const token = getStorageSyncToken();
|
||||
|
||||
let header = {
|
||||
...defaultConfig.header,
|
||||
@@ -58,7 +60,7 @@ function request(url, args = {}, method = "POST", customConfig = {}) {
|
||||
resolve(res.data);
|
||||
if (res.statusCode && res.statusCode === 424) {
|
||||
console.log("424错误,重新登录");
|
||||
uni.setStorageSync("token", "");
|
||||
removeStorageSyncToken();
|
||||
uni.$emit(NOTICE_EVENT_LOGOUT);
|
||||
goLogin();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user