Skip to content

Commit df179d0

Browse files
committed
Merge remote-tracking branch 'origin/dev' into dev
2 parents e7349ef + a238400 commit df179d0

9 files changed

Lines changed: 81 additions & 23 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Alert, Button, Form, Input, Radio, RadioChangeEvent, Spin } from 'antd'
55
import i18n from '@/i18n';
66
import { IAiConfig } from '@/typings/setting';
77
import { getUser } from '@/service/user';
8-
import { ILoginUser, IRole } from '@/typings/user';
8+
import { IUserVO, IRole } from '@/typings/user';
99
import { AIFormConfig, AITypeName } from './aiTypeConfig';
1010
import styles from './index.less';
1111

@@ -21,7 +21,7 @@ function capitalizeFirstLetter(string) {
2121
// openAI 的设置项
2222
export default function SettingAI(props: IProps) {
2323
const [aiConfig, setAiConfig] = useState<IAiConfig>();
24-
const [userInfo, setUserInfo] = useState<ILoginUser>();
24+
const [userInfo, setUserInfo] = useState<IUserVO>();
2525
const [loading, setLoading] = useState(false);
2626

2727
const queryUserInfo = async () => {

chat2db-client/src/layouts/GlobalLayout/index.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,16 @@ import { GithubOutlined, SyncOutlined, WechatOutlined } from '@ant-design/icons'
1414
import { ThemeType } from '@/constants';
1515
import GlobalComponent from '../init/GlobalComponent';
1616
import styles from './index.less';
17+
import { useUserStore } from '@/store/user'
1718

1819
const GlobalLayout = () => {
1920
const [appTheme, setAppTheme] = useTheme();
2021
const [antdTheme, setAntdTheme] = useState<any>({});
22+
const { curUser } = useUserStore((state)=> {
23+
return {
24+
curUser: state.curUser
25+
}
26+
})
2127

2228
const { serviceStatus, restartPolling } = usePollRequestService({
2329
loopService: service.testService,
@@ -49,7 +55,7 @@ const GlobalLayout = () => {
4955
};
5056

5157
// 等待状态页面
52-
if (serviceStatus === ServiceStatus.PENDING) {
58+
if (serviceStatus === ServiceStatus.PENDING || curUser === null) {
5359
return <Spin className={styles.loadingBox} size="large" />;
5460
}
5561

chat2db-client/src/layouts/init/init.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { clearOlderLocalStorage } from '@/utils';
2+
import initLoginInfo from './initLoginInfo';
23
import initIndexedDB from './initIndexedDB';
34
import registerElectronApi from './registerElectronApi';
45
import registerMessage from './registerMessage';
@@ -8,6 +9,7 @@ import { LangType } from '@/constants';
89

910
const init = () => {
1011
clearOlderLocalStorage();
12+
initLoginInfo();
1113

1214
initLang();
1315
initIndexedDB();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { queryCurUser } from '@/store/user'
2+
3+
/** 初始化登陆的信息 */
4+
const initLoginInfo = () => {
5+
queryCurUser();
6+
};
7+
8+
export default initLoginInfo;

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1-
import React from 'react';
1+
import React, { useEffect } from 'react';
22

33
function Test() {
4+
const token = '1'
5+
useEffect(() => {
6+
const socket = new WebSocket(`ws://127.0.0.1:10821/api/ws/1`);
7+
socket.onopen = () => {
8+
console.log('open');
9+
socket.send('hello');
10+
};
11+
}, []);
412
return (false);
513
}
614

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import BrandLogo from '@/components/BrandLogo';
99
import i18n from '@/i18n';
1010
import { getUser, userLogout } from '@/service/user';
1111
import { INavItem } from '@/typings/main';
12-
import { ILoginUser, IRole } from '@/typings/user';
12+
import { IUserVO, IRole } from '@/typings/user';
1313

1414
// ----- hooks -----
1515
import getConnectionEnvList from './functions/getConnection';
@@ -66,7 +66,7 @@ const initNavConfig: INavItem[] = [
6666
function MainPage() {
6767
const navigate = useNavigate();
6868
const [navConfig, setNavConfig] = useState<INavItem[]>(initNavConfig);
69-
const [userInfo, setUserInfo] = useState<ILoginUser>();
69+
const [userInfo, setUserInfo] = useState<IUserVO>();
7070
const mainPageActiveTab = useMainStore((state) => state.mainPageActiveTab);
7171
const [activeNavKey, setActiveNavKey] = useState<string>(
7272
__ENV__ === 'desktop' ? mainPageActiveTab : window.location.pathname.split('/')[1] || mainPageActiveTab,
@@ -96,7 +96,6 @@ function MainPage() {
9696
}
9797
}, [activeNavKey]);
9898

99-
// 这里如果社区版没有登陆可能需要后端来个重定向?
10099
const handleInitPage = async () => {
101100
const cloneNavConfig = [...navConfig];
102101
const res = await getUser();

chat2db-client/src/service/user.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import createRequest from './base';
22
import { IPageParams, IPageResponse } from '@/typings';
3-
import { ILoginUser, IUser } from '@/typings/user';
3+
import { IUserVO, IUser } from '@/typings/user';
44

55
/** 用户登录接口 */
66
const userLogin = createRequest<{ userName: string; password: string }, boolean>('/api/oauth/login_a', {
@@ -13,7 +13,7 @@ const userLogout = createRequest<void, void>('/api/oauth/logout_a', {
1313
});
1414

1515
/** 获取用户信息 */
16-
const getUser = createRequest<void, ILoginUser>('/api/oauth/user_a', { method: 'get' });
16+
const getUser = createRequest<void, IUserVO>('/api/oauth/user_a', { method: 'get' });
1717

1818
/** 获取用户列表信息 */
1919
const getUserList = createRequest<IPageParams, IPageResponse<IUser>>('/api/user/list', {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { StoreApi } from 'zustand';
2+
import { UseBoundStoreWithEqualityFn, createWithEqualityFn } from 'zustand/traditional';
3+
import { devtools } from 'zustand/middleware';
4+
import { shallow } from 'zustand/shallow';
5+
import { IUserVO } from '@/typings/user';
6+
import { getUser } from '@/service/user';
7+
8+
export interface IUserStore {
9+
curUser: IUserVO | null;
10+
}
11+
12+
const initUserStore: IUserStore = {
13+
curUser: null,
14+
};
15+
16+
/**
17+
* 用户 store
18+
*/
19+
export const useUserStore: UseBoundStoreWithEqualityFn<StoreApi<IUserStore>> = createWithEqualityFn(
20+
devtools(() => initUserStore),
21+
shallow,
22+
);
23+
24+
/**
25+
*
26+
* @param curUser 设置当前用户
27+
*/
28+
29+
export const setCurUser = (curUser: IUserVO) => {
30+
useUserStore.setState({ curUser });
31+
};
32+
33+
/**
34+
* 获取当前用户
35+
*/
36+
37+
export const queryCurUser = async () => {
38+
const curUser = await getUser();
39+
useUserStore.setState({ curUser });
40+
// 向cookie中写入当前用户id
41+
const date = new Date('2030-12-30 12:30:00').toUTCString();
42+
document.cookie = `CHAT2DB.USER_ID=${curUser?.id};Expires=${date}`;
43+
};

chat2db-client/src/typings/user.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,10 @@ export interface IUser {
1313
role?: IRole;
1414
}
1515

16-
export interface ILoginUser {
17-
/**
18-
* Is it an administrator
19-
*/
20-
admin?: boolean;
21-
/**
22-
* 用户id
23-
*/
24-
id?: number;
25-
/**
26-
* 昵称
27-
*/
28-
nickName?: string;
29-
roleCode: IRole;
16+
export interface IUserVO {
17+
admin: boolean;
18+
id : number;
19+
nickName: string;
20+
roleCode: string;
21+
token: string;
3022
}

0 commit comments

Comments
 (0)