Skip to content

Commit

Permalink
unify: getuser
Browse files Browse the repository at this point in the history
  • Loading branch information
shanhexi committed Jan 3, 2024
1 parent bf1e2b2 commit 48553d2
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 49 deletions.
36 changes: 9 additions & 27 deletions chat2db-client/src/blocks/Setting/AiSetting/index.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,34 +21,16 @@ function capitalizeFirstLetter(string) {
// openAI 的设置项
export default function SettingAI(props: IProps) {
const [aiConfig, setAiConfig] = useState<IAiConfig>();
const [userInfo, setUserInfo] = useState<IUserVO>();
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 <Spin spinning={loading} />;
}

if (!aiConfig) {
return <Alert description={i18n('setting.ai.tips')} type="warning" showIcon />;
}
Expand Down Expand Up @@ -84,7 +66,7 @@ export default function SettingAI(props: IProps) {
};

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

{/* {aiConfig?.aiSqlSource === AIType.CHAT2DBAI && !aiConfig.apiKey && <Popularize source="setting" />} */}
</Spin>
</>
);
}
16 changes: 6 additions & 10 deletions chat2db-client/src/pages/login/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
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';
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;
Expand All @@ -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 (
Expand Down
21 changes: 12 additions & 9 deletions chat2db-client/src/pages/main/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ 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';

// ----- 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';
Expand Down Expand Up @@ -65,8 +66,12 @@ const initNavConfig: INavItem[] = [

function MainPage() {
const navigate = useNavigate();
const { userInfo } = useUserStore(state => {
return {
userInfo: state.curUser
}
});
const [navConfig, setNavConfig] = useState<INavItem[]>(initNavConfig);
const [userInfo, setUserInfo] = useState<IUserVO>();
const mainPageActiveTab = useMainStore((state) => state.mainPageActiveTab);
const [activeNavKey, setActiveNavKey] = useState<string>(
__ENV__ === 'desktop' ? mainPageActiveTab : window.location.pathname.split('/')[1] || mainPageActiveTab,
Expand Down Expand Up @@ -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',
Expand All @@ -112,7 +115,7 @@ function MainPage() {
name: i18n('team.title'),
});
}
if (!res.admin && hasTeamIcon) {
if (!userInfo.admin && hasTeamIcon) {
cloneNavConfig.splice(3, 1);
}
}
Expand All @@ -130,7 +133,7 @@ function MainPage() {

const handleLogout = () => {
userLogout().then(() => {
setUserInfo(undefined);
setCurUser(undefined);
navigate('/login');
});
};
Expand Down
3 changes: 2 additions & 1 deletion chat2db-client/src/store/user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const useUserStore: UseBoundStoreWithEqualityFn<StoreApi<IUserStore>> = c
* @param curUser 设置当前用户
*/

export const setCurUser = (curUser: IUserVO) => {
export const setCurUser = (curUser?: IUserVO) => {
useUserStore.setState({ curUser });
};

Expand All @@ -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
};
4 changes: 2 additions & 2 deletions chat2db-client/src/typings/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 48553d2

Please sign in to comment.