chore(deps): add unstorage and migrate ChatStorage to use SpAsyncUtil
This commit is contained in:
@@ -7,8 +7,9 @@
|
||||
import type { FetchHook } from "ofetch";
|
||||
import { ApiPath } from "../api_path";
|
||||
import { ApiError, ErrorCode } from "../api_result";
|
||||
import { authStorage } from "../../storage/auth_storage";
|
||||
import { AuthStorage } from "@/data/storage/auth/auth_storage";
|
||||
import { deviceIdentifier } from "@/utils/device_identifier";
|
||||
import { Result } from "@/utils/result";
|
||||
|
||||
/**
|
||||
* 不需要 auth token 刷新的路径
|
||||
@@ -32,6 +33,22 @@ let isRefreshing = false;
|
||||
*/
|
||||
let refreshPromise: Promise<void> | null = null;
|
||||
|
||||
/**
|
||||
* 获取当前可用的 auth token(登录 token 优先,其次游客 token)
|
||||
*/
|
||||
async function getAuthTokenAsync(): Promise<string | null> {
|
||||
const storage = AuthStorage.getInstance();
|
||||
const loginResult = await storage.getLoginToken();
|
||||
if (Result.isOk(loginResult) && loginResult.data && loginResult.data.length > 0) {
|
||||
return loginResult.data;
|
||||
}
|
||||
const guestResult = await storage.getGuestToken();
|
||||
if (Result.isOk(guestResult) && guestResult.data && guestResult.data.length > 0) {
|
||||
return guestResult.data;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 实际刷新 token(被拦截器调用)
|
||||
*/
|
||||
@@ -50,15 +67,16 @@ async function refreshToken(
|
||||
* 刷新登录 token
|
||||
*/
|
||||
async function refreshLoginToken(baseUrl: string): Promise<void> {
|
||||
const refreshToken = authStorage.getRefreshToken();
|
||||
if (!refreshToken) {
|
||||
const storage = AuthStorage.getInstance();
|
||||
const refreshResult = await storage.getRefreshToken();
|
||||
if (!Result.isOk(refreshResult) || !refreshResult.data) {
|
||||
throw new Error("No refresh token available");
|
||||
}
|
||||
|
||||
const response = await fetch(`${baseUrl}${ApiPath.refresh}`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ refreshToken }),
|
||||
body: JSON.stringify({ refreshToken: refreshResult.data }),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
@@ -71,9 +89,8 @@ async function refreshLoginToken(baseUrl: string): Promise<void> {
|
||||
};
|
||||
|
||||
if (json.success && json.data) {
|
||||
if (json.data.token) authStorage.setLoginToken(json.data.token);
|
||||
if (json.data.refreshToken)
|
||||
authStorage.setRefreshToken(json.data.refreshToken);
|
||||
if (json.data.token) await storage.setLoginToken(json.data.token);
|
||||
if (json.data.refreshToken) await storage.setRefreshToken(json.data.refreshToken);
|
||||
} else {
|
||||
throw new Error("Refresh response invalid");
|
||||
}
|
||||
@@ -83,6 +100,7 @@ async function refreshLoginToken(baseUrl: string): Promise<void> {
|
||||
* 刷新游客 token
|
||||
*/
|
||||
async function refreshGuestToken(baseUrl: string): Promise<void> {
|
||||
const storage = AuthStorage.getInstance();
|
||||
const deviceId = await deviceIdentifier.getDeviceId();
|
||||
|
||||
const response = await fetch(`${baseUrl}${ApiPath.guestLogin}`, {
|
||||
@@ -101,8 +119,8 @@ async function refreshGuestToken(baseUrl: string): Promise<void> {
|
||||
};
|
||||
|
||||
if (json.success && json.data) {
|
||||
if (json.data.token) authStorage.setGuestToken(json.data.token);
|
||||
if (json.data.deviceId) authStorage.setDeviceId(json.data.deviceId);
|
||||
if (json.data.token) await storage.setGuestToken(json.data.token);
|
||||
if (json.data.deviceId) await storage.setDeviceId(json.data.deviceId);
|
||||
} else {
|
||||
throw new Error("Guest refresh response invalid");
|
||||
}
|
||||
@@ -124,7 +142,7 @@ export const onResponseErrorAuthRefresh: FetchHook = async (ctx) => {
|
||||
if (NO_AUTH_REFRESH_PATHS.some((p) => path.includes(p))) return;
|
||||
|
||||
// 已登录则刷新登录 token,否则刷新游客 token
|
||||
const isGuestMode = !authStorage.hasLoginToken();
|
||||
const isGuestMode = !AuthStorage.getInstance().hasLoginToken();
|
||||
|
||||
// 并发刷新:等待或启动
|
||||
if (isRefreshing && refreshPromise) {
|
||||
@@ -144,9 +162,9 @@ export const onResponseErrorAuthRefresh: FetchHook = async (ctx) => {
|
||||
}
|
||||
|
||||
// 刷新失败 → 清除数据并抛出
|
||||
const newToken = authStorage.getAuthToken();
|
||||
const newToken = await getAuthTokenAsync();
|
||||
if (!newToken) {
|
||||
authStorage.clearAuthData();
|
||||
AuthStorage.getInstance().clearAuthData();
|
||||
throw new ApiError(
|
||||
ErrorCode.unknownError,
|
||||
"Token refresh failed",
|
||||
|
||||
Reference in New Issue
Block a user