手机号从地址栏里获取

This commit is contained in:
2026-07-01 22:13:25 +08:00
parent 8a67d72de4
commit b78ef8798c
4 changed files with 55 additions and 4 deletions
+36 -2
View File
@@ -56,6 +56,15 @@ export function isLoggedIn() {
return !!localStorage.getItem(ACCESS_TOKEN);
}
const AUTH_RECOVERY_EXEMPT_PATHS = ['/auth/session/bootstrap', '/auth/token/refresh'];
async function recoverSession(): Promise<SessionPayload> {
const refreshed = await refreshSession();
if (refreshed) return refreshed;
clearAuth();
return bootstrapSession();
}
async function rawRequest<T>(
path: string,
options: RequestInit = {},
@@ -79,12 +88,34 @@ async function rawRequest<T>(
return json.data as T;
}
async function requestWithAuthRetry<T>(
path: string,
options: RequestInit = {},
retried = false,
): Promise<T> {
try {
return await rawRequest<T>(path, options);
} catch (e) {
const err = e as Error & { status?: number };
const canRecover =
err.status === 401 &&
!retried &&
!AUTH_RECOVERY_EXEMPT_PATHS.some((p) => path.startsWith(p));
if (!canRecover) throw e;
await recoverSession();
return requestWithAuthRetry<T>(path, options, true);
}
}
export async function request<T>(
_clientApp: string,
path: string,
options: RequestInit = {},
): Promise<T> {
return rawRequest<T>(path, options);
if (!isLoggedIn() && !AUTH_RECOVERY_EXEMPT_PATHS.some((p) => path.startsWith(p))) {
await bootstrapSession();
}
return requestWithAuthRetry<T>(path, options);
}
export async function bootstrapSession(): Promise<SessionPayload> {
@@ -145,7 +176,10 @@ export async function ensureSession(): Promise<SessionPayload> {
}
export async function bindPhone(phone: string, code: string): Promise<SessionPayload> {
const data = await rawRequest<SessionPayload>('/auth/phone/bind', {
if (!isLoggedIn()) {
await bootstrapSession();
}
const data = await requestWithAuthRetry<SessionPayload>('/auth/phone/bind', {
method: 'POST',
body: JSON.stringify({ phone, code }),
});