diff --git a/src/main.jsx b/src/main.jsx index 2d3c53f..1bf374b 100644 --- a/src/main.jsx +++ b/src/main.jsx @@ -31,8 +31,8 @@ import Airticket from '@/views/airticket/Index' import AirticketPlan from '@/views/airticket/Plan' import { ThemeContext } from '@/stores/ThemeContext' import { usingStorage } from '@/hooks/usingStorage' +import useAuthStore from './stores/Auth' import { isNotEmpty } from '@/utils/commons' -import { notifyAuth } from './utils/lifecycle' import { PERM_ACCOUNT_MANAGEMENT, PERM_ROLE_NEW, PERM_OVERSEA, PERM_AIR_TICKET } from '@/config' @@ -81,7 +81,7 @@ const initAppliction = async () => { const { loginToken, userId } = usingStorage() if (isNotEmpty(userId) && isNotEmpty(loginToken)) { - await notifyAuth() + await useAuthStore.getState().initAuth() } const router = await initRouter() diff --git a/src/stores/Auth.js b/src/stores/Auth.js index acfe18f..5f86bab 100644 --- a/src/stores/Auth.js +++ b/src/stores/Auth.js @@ -58,22 +58,24 @@ const initialState = { permissionList: [] } -const useAuthStore = create(lifecycleware((set, get) => ({ +const useAuthStore = create((set, get) => ({ ...initialState, - onAuth: async () => { + initAuth: async () => { const { startTokenInterval, loadUserPermission } = get() const { setStorage, loginToken } = usingStorage() - appendRequestParams('token', loginToken) const userJson = await fetchUserDetail(loginToken) + appendRequestParams('token', loginToken) appendRequestParams('lmi_sn', userJson.LMI_SN) setStorage(KEY_USER_ID, userJson.LMI_SN) setStorage(KEY_TRAVEL_AGENCY_ID, userJson.LMI_VEI_SN) + await loadUserPermission(userJson.LMI_SN) + set(() => ({ currentUser: { username: userJson.LoginName, @@ -84,20 +86,19 @@ const useAuthStore = create(lifecycleware((set, get) => ({ } })) - await loadUserPermission(userJson.LMI_SN) startTokenInterval() loadPageSpy(userJson.real_name) }, authenticate: async (usr, pwd) => { - const { onAuth } = get() + const { onRefresh } = get() const { setStorage } = usingStorage() const { token: loginToken } = await fetchLoginToken(usr, pwd) setStorage(KEY_LOGIN_TOKEN, loginToken) - await onAuth() + await initAuth() set(() => ({ tokenTimeout: false, @@ -190,6 +191,6 @@ const useAuthStore = create(lifecycleware((set, get) => ({ }) }, -}))) +})) export default useAuthStore \ No newline at end of file diff --git a/src/utils/lifecycle.js b/src/utils/lifecycle.js index 0d3deb1..3b896db 100644 --- a/src/utils/lifecycle.js +++ b/src/utils/lifecycle.js @@ -10,16 +10,19 @@ export const addAuthLinstener = (fn) => { } export const notifyInit = async () => { - initListener.forEach(async (fn) => { - await fn() - }) + for (const listener of initListener) { + await listener() + } } export const notifyAuth = async (obj) => { - authListener.forEach(async (fn) => await fn(obj)) + for (const listener of authListener) { + await listener(obj) + } } -// Zustand 中间件,用于订阅前端应用的生命周期,实验阶段 +// Zustand 中间件,用于订阅前端应用的生命周期,实验阶段。 +// 失败,无法同步调用异步方法! export const lifecycleware = (fn) => (set, get, store) => { addInitLinstener(() => {