From c6e98578147d65822f84b1b91b68552fa3dda8cf Mon Sep 17 00:00:00 2001 From: Jimmy Liow Date: Wed, 3 Jul 2024 10:39:47 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=90=8C=E6=AD=A5=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E6=9D=83=E9=99=90=EF=BC=8C=E9=81=BF=E5=85=8D=E5=88=B7=E6=96=B0?= =?UTF-8?q?=E5=90=8E=E8=B7=AF=E7=94=B1=E6=B2=A1=E6=9C=89=E6=9D=83=E9=99=90?= =?UTF-8?q?=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.jsx | 4 ++-- src/stores/Auth.js | 15 ++++++++------- src/utils/lifecycle.js | 13 ++++++++----- 3 files changed, 18 insertions(+), 14 deletions(-) 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(() => {