From 48553d2c6e51b2d138e8d6b330dd5b94e4f9fc31 Mon Sep 17 00:00:00 2001 From: shanhexi Date: Wed, 3 Jan 2024 15:41:41 +0800 Subject: [PATCH] unify: getuser --- .../src/blocks/Setting/AiSetting/index.tsx | 36 +++++-------------- chat2db-client/src/pages/login/index.tsx | 16 ++++----- chat2db-client/src/pages/main/index.tsx | 21 ++++++----- chat2db-client/src/store/user/index.ts | 3 +- chat2db-client/src/typings/user.ts | 4 +-- 5 files changed, 31 insertions(+), 49 deletions(-) diff --git a/chat2db-client/src/blocks/Setting/AiSetting/index.tsx b/chat2db-client/src/blocks/Setting/AiSetting/index.tsx index b0bb910ed..fd84059df 100644 --- a/chat2db-client/src/blocks/Setting/AiSetting/index.tsx +++ b/chat2db-client/src/blocks/Setting/AiSetting/index.tsx @@ -1,13 +1,13 @@ import React, { useEffect, useState } from 'react'; import configService from '@/service/config'; import { AIType } from '@/typings/ai'; -import { Alert, Button, Form, Input, Radio, RadioChangeEvent, Spin } from 'antd'; +import { Alert, Button, Form, Input, Radio, RadioChangeEvent } from 'antd'; import i18n from '@/i18n'; import { IAiConfig } from '@/typings/setting'; -import { getUser } from '@/service/user'; -import { IUserVO, IRole } from '@/typings/user'; +import { IRole } from '@/typings/user'; import { AIFormConfig, AITypeName } from './aiTypeConfig'; import styles from './index.less'; +import { useUserStore } from '@/store/user' interface IProps { handleApplyAiConfig: (aiConfig: IAiConfig) => void; @@ -21,34 +21,16 @@ function capitalizeFirstLetter(string) { // openAI 的设置项 export default function SettingAI(props: IProps) { const [aiConfig, setAiConfig] = useState(); - const [userInfo, setUserInfo] = useState(); - const [loading, setLoading] = useState(false); - - const queryUserInfo = async () => { - setLoading(true); - try { - const res = await getUser(); - // 向cookie中写入当前用户id - const date = new Date('2030-12-30 12:30:00').toUTCString(); - document.cookie = `CHAT2DB.USER_ID=${res?.id};Expires=${date}`; - setUserInfo(res); - } finally { - setLoading(false); + const { userInfo } = useUserStore(state => { + return { + userInfo: state.curUser } - }; - - useEffect(() => { - queryUserInfo(); - }, []); + }) useEffect(() => { setAiConfig(props.aiConfig); }, [props.aiConfig]); - if (loading) { - return ; - } - if (!aiConfig) { return ; } @@ -84,7 +66,7 @@ export default function SettingAI(props: IProps) { }; return ( - + <>
{i18n('setting.title.aiSource')}:
@@ -128,6 +110,6 @@ export default function SettingAI(props: IProps) {
{/* {aiConfig?.aiSqlSource === AIType.CHAT2DBAI && !aiConfig.apiKey && } */} -
+ ); } diff --git a/chat2db-client/src/pages/login/index.tsx b/chat2db-client/src/pages/login/index.tsx index 0a9793df0..8a4045262 100644 --- a/chat2db-client/src/pages/login/index.tsx +++ b/chat2db-client/src/pages/login/index.tsx @@ -1,6 +1,6 @@ import React, { useEffect } from 'react'; import { Button, Form, Input, Tooltip } from 'antd'; -import { userLogin, getUser } from '@/service/user'; +import { userLogin } from '@/service/user'; import LogoImg from '@/assets/logo/logo.png'; import styles from './index.less'; import Setting from '@/blocks/Setting'; @@ -8,6 +8,7 @@ import Iconfont from '@/components/Iconfont'; import i18n from '@/i18n'; // import { useNavigate } from 'react-router-dom'; import { logoutClearSomeLocalStorage, navigate } from '@/utils'; +import { queryCurUser } from '@/store/user' interface IFormData { userName: string; @@ -19,17 +20,12 @@ const Login: React.FC = () => { logoutClearSomeLocalStorage(); }, []); - // const navigate = useNavigate(); const handleLogin = async (formData: IFormData) => { const token = await userLogin(formData); - getUser().then((res) => { - // 向cookie中写入当前用户id - const date = new Date('2030-12-30 12:30:00').toUTCString(); - document.cookie = `CHAT2DB.USER_ID=${res?.id};Expires=${date}`; - if (token && res) { - navigate('/'); - } - }); + const res = await queryCurUser(); + if (token && res) { + navigate('/'); + } }; return ( diff --git a/chat2db-client/src/pages/main/index.tsx b/chat2db-client/src/pages/main/index.tsx index 2fd842398..98be93503 100644 --- a/chat2db-client/src/pages/main/index.tsx +++ b/chat2db-client/src/pages/main/index.tsx @@ -7,9 +7,9 @@ import Iconfont from '@/components/Iconfont'; import BrandLogo from '@/components/BrandLogo'; import i18n from '@/i18n'; -import { getUser, userLogout } from '@/service/user'; +import { userLogout } from '@/service/user'; import { INavItem } from '@/typings/main'; -import { IUserVO, IRole } from '@/typings/user'; +import { IRole } from '@/typings/user'; // ----- hooks ----- import getConnectionEnvList from './functions/getConnection'; @@ -17,6 +17,7 @@ import getConnectionEnvList from './functions/getConnection'; // ----- store ----- import { useMainStore, setMainPageActiveTab } from '@/pages/main/store/main'; import { getConnectionList } from '@/pages/main/store/connection'; +import { useUserStore, setCurUser } from '@/store/user'; // ----- block ----- import Workspace from './workspace'; @@ -65,8 +66,12 @@ const initNavConfig: INavItem[] = [ function MainPage() { const navigate = useNavigate(); + const { userInfo } = useUserStore(state => { + return { + userInfo: state.curUser + } + }); const [navConfig, setNavConfig] = useState(initNavConfig); - const [userInfo, setUserInfo] = useState(); const mainPageActiveTab = useMainStore((state) => state.mainPageActiveTab); const [activeNavKey, setActiveNavKey] = useState( __ENV__ === 'desktop' ? mainPageActiveTab : window.location.pathname.split('/')[1] || mainPageActiveTab, @@ -98,11 +103,9 @@ function MainPage() { const handleInitPage = async () => { const cloneNavConfig = [...navConfig]; - const res = await getUser(); - if (res) { - setUserInfo(res); + if (userInfo) { const hasTeamIcon = cloneNavConfig.find((i) => i.key === 'team'); - if (res.admin && !hasTeamIcon) { + if (userInfo.admin && !hasTeamIcon) { cloneNavConfig.splice(3, 0, { key: 'team', icon: '\ue64b', @@ -112,7 +115,7 @@ function MainPage() { name: i18n('team.title'), }); } - if (!res.admin && hasTeamIcon) { + if (!userInfo.admin && hasTeamIcon) { cloneNavConfig.splice(3, 1); } } @@ -130,7 +133,7 @@ function MainPage() { const handleLogout = () => { userLogout().then(() => { - setUserInfo(undefined); + setCurUser(undefined); navigate('/login'); }); }; diff --git a/chat2db-client/src/store/user/index.ts b/chat2db-client/src/store/user/index.ts index 1e3fb172a..b44b35e5f 100644 --- a/chat2db-client/src/store/user/index.ts +++ b/chat2db-client/src/store/user/index.ts @@ -26,7 +26,7 @@ export const useUserStore: UseBoundStoreWithEqualityFn> = c * @param curUser 设置当前用户 */ -export const setCurUser = (curUser: IUserVO) => { +export const setCurUser = (curUser?: IUserVO) => { useUserStore.setState({ curUser }); }; @@ -41,4 +41,5 @@ export const queryCurUser = async () => { // 向cookie中写入当前用户id const date = new Date('2030-12-30 12:30:00').toUTCString(); document.cookie = `CHAT2DB.USER_ID=${curUser?.id};Expires=${date}`; + return curUser }; diff --git a/chat2db-client/src/typings/user.ts b/chat2db-client/src/typings/user.ts index 5d2f5c72f..bed4a740e 100644 --- a/chat2db-client/src/typings/user.ts +++ b/chat2db-client/src/typings/user.ts @@ -13,10 +13,10 @@ export interface IUser { role?: IRole; } -export interface IUserVO { +export type IUserVO = { admin: boolean; id : number; nickName: string; roleCode: string; token: string; -} +} | null