Skip to content

Commit 48553d2

Browse files
committed
unify: getuser
1 parent bf1e2b2 commit 48553d2

File tree

5 files changed

+31
-49
lines changed

5 files changed

+31
-49
lines changed

chat2db-client/src/blocks/Setting/AiSetting/index.tsx

+9-27
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import React, { useEffect, useState } from 'react';
22
import configService from '@/service/config';
33
import { AIType } from '@/typings/ai';
4-
import { Alert, Button, Form, Input, Radio, RadioChangeEvent, Spin } from 'antd';
4+
import { Alert, Button, Form, Input, Radio, RadioChangeEvent } from 'antd';
55
import i18n from '@/i18n';
66
import { IAiConfig } from '@/typings/setting';
7-
import { getUser } from '@/service/user';
8-
import { IUserVO, IRole } from '@/typings/user';
7+
import { IRole } from '@/typings/user';
98
import { AIFormConfig, AITypeName } from './aiTypeConfig';
109
import styles from './index.less';
10+
import { useUserStore } from '@/store/user'
1111

1212
interface IProps {
1313
handleApplyAiConfig: (aiConfig: IAiConfig) => void;
@@ -21,34 +21,16 @@ function capitalizeFirstLetter(string) {
2121
// openAI 的设置项
2222
export default function SettingAI(props: IProps) {
2323
const [aiConfig, setAiConfig] = useState<IAiConfig>();
24-
const [userInfo, setUserInfo] = useState<IUserVO>();
25-
const [loading, setLoading] = useState(false);
26-
27-
const queryUserInfo = async () => {
28-
setLoading(true);
29-
try {
30-
const res = await getUser();
31-
// 向cookie中写入当前用户id
32-
const date = new Date('2030-12-30 12:30:00').toUTCString();
33-
document.cookie = `CHAT2DB.USER_ID=${res?.id};Expires=${date}`;
34-
setUserInfo(res);
35-
} finally {
36-
setLoading(false);
24+
const { userInfo } = useUserStore(state => {
25+
return {
26+
userInfo: state.curUser
3727
}
38-
};
39-
40-
useEffect(() => {
41-
queryUserInfo();
42-
}, []);
28+
})
4329

4430
useEffect(() => {
4531
setAiConfig(props.aiConfig);
4632
}, [props.aiConfig]);
4733

48-
if (loading) {
49-
return <Spin spinning={loading} />;
50-
}
51-
5234
if (!aiConfig) {
5335
return <Alert description={i18n('setting.ai.tips')} type="warning" showIcon />;
5436
}
@@ -84,7 +66,7 @@ export default function SettingAI(props: IProps) {
8466
};
8567

8668
return (
87-
<Spin spinning={loading}>
69+
<>
8870
<div className={styles.aiSqlSource}>
8971
<div className={styles.aiSqlSourceTitle}>{i18n('setting.title.aiSource')}:</div>
9072
<Radio.Group onChange={handleAiTypeChange} value={aiConfig?.aiSqlSource}>
@@ -128,6 +110,6 @@ export default function SettingAI(props: IProps) {
128110
</div>
129111

130112
{/* {aiConfig?.aiSqlSource === AIType.CHAT2DBAI && !aiConfig.apiKey && <Popularize source="setting" />} */}
131-
</Spin>
113+
</>
132114
);
133115
}

chat2db-client/src/pages/login/index.tsx

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import React, { useEffect } from 'react';
22
import { Button, Form, Input, Tooltip } from 'antd';
3-
import { userLogin, getUser } from '@/service/user';
3+
import { userLogin } from '@/service/user';
44
import LogoImg from '@/assets/logo/logo.png';
55
import styles from './index.less';
66
import Setting from '@/blocks/Setting';
77
import Iconfont from '@/components/Iconfont';
88
import i18n from '@/i18n';
99
// import { useNavigate } from 'react-router-dom';
1010
import { logoutClearSomeLocalStorage, navigate } from '@/utils';
11+
import { queryCurUser } from '@/store/user'
1112

1213
interface IFormData {
1314
userName: string;
@@ -19,17 +20,12 @@ const Login: React.FC = () => {
1920
logoutClearSomeLocalStorage();
2021
}, []);
2122

22-
// const navigate = useNavigate();
2323
const handleLogin = async (formData: IFormData) => {
2424
const token = await userLogin(formData);
25-
getUser().then((res) => {
26-
// 向cookie中写入当前用户id
27-
const date = new Date('2030-12-30 12:30:00').toUTCString();
28-
document.cookie = `CHAT2DB.USER_ID=${res?.id};Expires=${date}`;
29-
if (token && res) {
30-
navigate('/');
31-
}
32-
});
25+
const res = await queryCurUser();
26+
if (token && res) {
27+
navigate('/');
28+
}
3329
};
3430

3531
return (

chat2db-client/src/pages/main/index.tsx

+12-9
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ import Iconfont from '@/components/Iconfont';
77
import BrandLogo from '@/components/BrandLogo';
88

99
import i18n from '@/i18n';
10-
import { getUser, userLogout } from '@/service/user';
10+
import { userLogout } from '@/service/user';
1111
import { INavItem } from '@/typings/main';
12-
import { IUserVO, IRole } from '@/typings/user';
12+
import { IRole } from '@/typings/user';
1313

1414
// ----- hooks -----
1515
import getConnectionEnvList from './functions/getConnection';
1616

1717
// ----- store -----
1818
import { useMainStore, setMainPageActiveTab } from '@/pages/main/store/main';
1919
import { getConnectionList } from '@/pages/main/store/connection';
20+
import { useUserStore, setCurUser } from '@/store/user';
2021

2122
// ----- block -----
2223
import Workspace from './workspace';
@@ -65,8 +66,12 @@ const initNavConfig: INavItem[] = [
6566

6667
function MainPage() {
6768
const navigate = useNavigate();
69+
const { userInfo } = useUserStore(state => {
70+
return {
71+
userInfo: state.curUser
72+
}
73+
});
6874
const [navConfig, setNavConfig] = useState<INavItem[]>(initNavConfig);
69-
const [userInfo, setUserInfo] = useState<IUserVO>();
7075
const mainPageActiveTab = useMainStore((state) => state.mainPageActiveTab);
7176
const [activeNavKey, setActiveNavKey] = useState<string>(
7277
__ENV__ === 'desktop' ? mainPageActiveTab : window.location.pathname.split('/')[1] || mainPageActiveTab,
@@ -98,11 +103,9 @@ function MainPage() {
98103

99104
const handleInitPage = async () => {
100105
const cloneNavConfig = [...navConfig];
101-
const res = await getUser();
102-
if (res) {
103-
setUserInfo(res);
106+
if (userInfo) {
104107
const hasTeamIcon = cloneNavConfig.find((i) => i.key === 'team');
105-
if (res.admin && !hasTeamIcon) {
108+
if (userInfo.admin && !hasTeamIcon) {
106109
cloneNavConfig.splice(3, 0, {
107110
key: 'team',
108111
icon: '\ue64b',
@@ -112,7 +115,7 @@ function MainPage() {
112115
name: i18n('team.title'),
113116
});
114117
}
115-
if (!res.admin && hasTeamIcon) {
118+
if (!userInfo.admin && hasTeamIcon) {
116119
cloneNavConfig.splice(3, 1);
117120
}
118121
}
@@ -130,7 +133,7 @@ function MainPage() {
130133

131134
const handleLogout = () => {
132135
userLogout().then(() => {
133-
setUserInfo(undefined);
136+
setCurUser(undefined);
134137
navigate('/login');
135138
});
136139
};

chat2db-client/src/store/user/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const useUserStore: UseBoundStoreWithEqualityFn<StoreApi<IUserStore>> = c
2626
* @param curUser 设置当前用户
2727
*/
2828

29-
export const setCurUser = (curUser: IUserVO) => {
29+
export const setCurUser = (curUser?: IUserVO) => {
3030
useUserStore.setState({ curUser });
3131
};
3232

@@ -41,4 +41,5 @@ export const queryCurUser = async () => {
4141
// 向cookie中写入当前用户id
4242
const date = new Date('2030-12-30 12:30:00').toUTCString();
4343
document.cookie = `CHAT2DB.USER_ID=${curUser?.id};Expires=${date}`;
44+
return curUser
4445
};

chat2db-client/src/typings/user.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ export interface IUser {
1313
role?: IRole;
1414
}
1515

16-
export interface IUserVO {
16+
export type IUserVO = {
1717
admin: boolean;
1818
id : number;
1919
nickName: string;
2020
roleCode: string;
2121
token: string;
22-
}
22+
} | null

0 commit comments

Comments
 (0)