From 653533e80cb36c8912657fac511b861ef898ae5b Mon Sep 17 00:00:00 2001 From: Jimmy Liow Date: Tue, 2 Jul 2024 10:16:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=9B=86=E6=88=90=20PageSpy=EF=BC=9B?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E7=94=A8=E6=88=B7=E4=BF=A1=E6=81=AF=E8=AF=BB?= =?UTF-8?q?=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pageSpy/index.jsx | 4 ++++ src/stores/Auth.js | 33 +++++++++++++++++++++++++++------ src/views/App.jsx | 26 ++++++-------------------- src/views/account/Profile.jsx | 28 ++++++---------------------- 4 files changed, 43 insertions(+), 48 deletions(-) diff --git a/src/pageSpy/index.jsx b/src/pageSpy/index.jsx index 5cf6ac9..0bfcd65 100644 --- a/src/pageSpy/index.jsx +++ b/src/pageSpy/index.jsx @@ -2,11 +2,15 @@ import { loadScript } from '@/utils/commons'; import { PROJECT_NAME } from '@/config'; export const loadPageSpy = (title) => { + + if (window.$pageSpy) return + const PageSpySrc = [ 'https://page-spy.mycht.cn/page-spy/index.min.js', 'https://page-spy.mycht.cn/plugin/data-harbor/index.min.js', 'https://page-spy.mycht.cn/plugin/rrweb/index.min.js', ]; + Promise.all(PageSpySrc.map((src) => loadScript(src))).then(() => { // 注册插件 PageSpy.registerPlugin(new DataHarborPlugin({ maximum: 2 * 1024 * 1024 })); diff --git a/src/stores/Auth.js b/src/stores/Auth.js index cc47621..acfe18f 100644 --- a/src/stores/Auth.js +++ b/src/stores/Auth.js @@ -48,6 +48,13 @@ const initialState = { tokenTimeout: true, loginStatus: 0, defaltRoute: '', + currentUser: { + username: '', + realname: '', + rolesName: '', + emailAddress: '', + travelAgencyName: '', + }, permissionList: [] } @@ -57,12 +64,29 @@ const useAuthStore = create(lifecycleware((set, get) => ({ onAuth: async () => { const { startTokenInterval, loadUserPermission } = get() - const { userId, loginToken } = usingStorage() + const { setStorage, loginToken } = usingStorage() appendRequestParams('token', loginToken) - appendRequestParams('lmi_sn', userId) - await loadUserPermission(userId) + const userJson = await fetchUserDetail(loginToken) + + appendRequestParams('lmi_sn', userJson.LMI_SN) + + setStorage(KEY_USER_ID, userJson.LMI_SN) + setStorage(KEY_TRAVEL_AGENCY_ID, userJson.LMI_VEI_SN) + + set(() => ({ + currentUser: { + username: userJson.LoginName, + realname: userJson.real_name, + rolesName: userJson.roles_name, + emailAddress: userJson.LMI_listmail, + travelAgencyName: userJson.VName, + } + })) + + await loadUserPermission(userJson.LMI_SN) startTokenInterval() + loadPageSpy(userJson.real_name) }, authenticate: async (usr, pwd) => { @@ -70,11 +94,8 @@ const useAuthStore = create(lifecycleware((set, get) => ({ const { setStorage } = usingStorage() const { token: loginToken } = await fetchLoginToken(usr, pwd) - const userDetail = await fetchUserDetail(loginToken) setStorage(KEY_LOGIN_TOKEN, loginToken) - setStorage(KEY_USER_ID, userDetail.LMI_SN) - setStorage(KEY_TRAVEL_AGENCY_ID, userDetail.LMI_VEI_SN) await onAuth() diff --git a/src/views/App.jsx b/src/views/App.jsx index 564e93e..2acfb41 100644 --- a/src/views/App.jsx +++ b/src/views/App.jsx @@ -27,10 +27,9 @@ function App() { const { t, i18n } = useTranslation(); const [password, setPassword] = useState('') - const [userDetail, setUserDetail] = useState({}) - const [authenticate, tokenTimeout, isPermitted] = useAuthStore( - (state) => [state.authenticate, state.tokenTimeout, state.isPermitted]) + const [authenticate, tokenTimeout, isPermitted, currentUser] = useAuthStore( + (state) => [state.authenticate, state.tokenTimeout, state.isPermitted, state.currentUser]) const { loginToken } = usingStorage() @@ -42,19 +41,6 @@ function App() { // 除了路由 /p...以外都需要登陆系统 const needToLogin = href !== '/login' && isEmpty(loginToken) - useEffect(() => { - if (isNotEmpty(loginToken)) { - fetchUserDetail(loginToken) - .then(u => { - setUserDetail({ - username: u.LoginName, - realname: u.real_name, - travelAgencyName: u.VName, - }) - }) - } - }, [loginToken]) - useEffect(() => { if (needToLogin) { navigate('/login') @@ -62,7 +48,7 @@ function App() { }, [href]) const onSubmit = () => { - authenticate(userDetail?.username, password) + authenticate(currentUser?.username, password) .catch(ex => { console.error(ex) alert(t('Validation.LoginFailed')) @@ -109,7 +95,7 @@ function App() { setPassword(e.target.value)} onPressEnter={onSubmit} - addonBefore={userDetail?.username} /> + addonBefore={currentUser?.username} /> @@ -149,7 +135,7 @@ function App() { - {userDetail?.travelAgencyName} + {currentUser?.travelAgencyName} @@ -169,7 +155,7 @@ function App() { > e.preventDefault()}> - {userDetail?.realname} + {currentUser?.realname} diff --git a/src/views/account/Profile.jsx b/src/views/account/Profile.jsx index ce8b3e1..d40ca83 100644 --- a/src/views/account/Profile.jsx +++ b/src/views/account/Profile.jsx @@ -1,37 +1,21 @@ import { useEffect, useState } from 'react' import { Descriptions, Col, Row } from 'antd'; import { useTranslation } from 'react-i18next' -import { fetchUserDetail } from '@/stores/Auth' -import { usingStorage } from '@/hooks/usingStorage' +import useAuthStore from '@/stores/Auth' function Profile() { const { t } = useTranslation() - const [userDetail, setUserDetail] = useState({}) - const { loginToken } = usingStorage() - - useEffect (() => { - fetchUserDetail(loginToken) - .then(json => { - setUserDetail({ - username: json.LoginName, - realname: json.real_name, - rolesName: json.roles_name, - emailAddress: json.LMI_listmail, - travelAgencyName: json.VName, - }) - }) - }, []) - + const currentUser = useAuthStore(state => state.currentUser) return ( - {userDetail?.username} - {userDetail?.realname}({userDetail?.rolesName}) - {userDetail?.emailAddress} - {userDetail?.travelAgencyName} + {currentUser?.username} + {currentUser?.realname}({currentUser?.rolesName}) + {currentUser?.emailAddress} + {currentUser?.travelAgencyName}