fix:登录跳转失败问题
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { createContext, useCallback, useContext, useEffect, useState, type ReactNode } from 'react';
|
||||
import { toAppPath } from '@dukang/weixin-sdk';
|
||||
import { clearAuth, isLoggedIn, request } from '../lib/api';
|
||||
import { clearAuth, isLoggedIn, request, saveAuth, type PartnerAuthPayload } from '../lib/api';
|
||||
|
||||
import type { PartnerMe, PartnerStaffRole } from '@dukang/shared-types';
|
||||
|
||||
@@ -12,6 +12,7 @@ type PartnerSessionValue = {
|
||||
account: PartnerAccount | null;
|
||||
loading: boolean;
|
||||
loggedIn: boolean;
|
||||
applySession: (session: PartnerAuthPayload) => void;
|
||||
refresh: () => Promise<PartnerAccount | null>;
|
||||
logout: () => void;
|
||||
};
|
||||
@@ -23,6 +24,15 @@ export function PartnerSessionProvider({ children }: { children: ReactNode }) {
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [loggedIn, setLoggedIn] = useState(() => isLoggedIn());
|
||||
|
||||
const applySession = useCallback((session: PartnerAuthPayload) => {
|
||||
saveAuth(session);
|
||||
setLoggedIn(true);
|
||||
setLoading(false);
|
||||
if (session.partner) {
|
||||
setAccount(session.partner as PartnerAccount);
|
||||
}
|
||||
}, []);
|
||||
|
||||
const refresh = useCallback(async (): Promise<PartnerAccount | null> => {
|
||||
if (!isLoggedIn()) {
|
||||
setAccount(null);
|
||||
@@ -38,6 +48,9 @@ export function PartnerSessionProvider({ children }: { children: ReactNode }) {
|
||||
return data;
|
||||
} catch {
|
||||
setAccount(null);
|
||||
if (!isLoggedIn()) {
|
||||
setLoggedIn(false);
|
||||
}
|
||||
return null;
|
||||
} finally {
|
||||
setLoading(false);
|
||||
@@ -57,7 +70,7 @@ export function PartnerSessionProvider({ children }: { children: ReactNode }) {
|
||||
|
||||
return (
|
||||
<PartnerSessionContext.Provider
|
||||
value={{ account, loading, loggedIn, refresh, logout }}
|
||||
value={{ account, loading, loggedIn, applySession, refresh, logout }}
|
||||
>
|
||||
{children}
|
||||
</PartnerSessionContext.Provider>
|
||||
|
||||
Reference in New Issue
Block a user