Skip to content

Commit

Permalink
Merge pull request #1056 from chat2db/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
shanhexi authored Jan 3, 2024
2 parents 18eced8 + bf1e2b2 commit 57edb00
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 25 deletions.
4 changes: 2 additions & 2 deletions chat2db-client/src/blocks/Setting/AiSetting/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Alert, Button, Form, Input, Radio, RadioChangeEvent, Spin } from 'antd'
import i18n from '@/i18n';
import { IAiConfig } from '@/typings/setting';
import { getUser } from '@/service/user';
import { ILoginUser, IRole } from '@/typings/user';
import { IUserVO, IRole } from '@/typings/user';
import { AIFormConfig, AITypeName } from './aiTypeConfig';
import styles from './index.less';

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

const queryUserInfo = async () => {
Expand Down
8 changes: 7 additions & 1 deletion chat2db-client/src/layouts/GlobalLayout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ import { GithubOutlined, SyncOutlined, WechatOutlined } from '@ant-design/icons'
import { ThemeType } from '@/constants';
import GlobalComponent from '../init/GlobalComponent';
import styles from './index.less';
import { useUserStore } from '@/store/user'

const GlobalLayout = () => {
const [appTheme, setAppTheme] = useTheme();
const [antdTheme, setAntdTheme] = useState<any>({});
const { curUser } = useUserStore((state)=> {
return {
curUser: state.curUser
}
})

const { serviceStatus, restartPolling } = usePollRequestService({
loopService: service.testService,
Expand Down Expand Up @@ -49,7 +55,7 @@ const GlobalLayout = () => {
};

// 等待状态页面
if (serviceStatus === ServiceStatus.PENDING) {
if (serviceStatus === ServiceStatus.PENDING || curUser === null) {
return <Spin className={styles.loadingBox} size="large" />;
}

Expand Down
2 changes: 2 additions & 0 deletions chat2db-client/src/layouts/init/init.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { clearOlderLocalStorage } from '@/utils';
import initLoginInfo from './initLoginInfo';
import initIndexedDB from './initIndexedDB';
import registerElectronApi from './registerElectronApi';
import registerMessage from './registerMessage';
Expand All @@ -8,6 +9,7 @@ import { LangType } from '@/constants';

const init = () => {
clearOlderLocalStorage();
initLoginInfo();

initLang();
initIndexedDB();
Expand Down
8 changes: 8 additions & 0 deletions chat2db-client/src/layouts/init/initLoginInfo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { queryCurUser } from '@/store/user'

/** 初始化登陆的信息 */
const initLoginInfo = () => {
queryCurUser();
};

export default initLoginInfo;
10 changes: 9 additions & 1 deletion chat2db-client/src/pages/demo/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import React from 'react';
import React, { useEffect } from 'react';

function Test() {
const token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOjIsImRldmljZSI6ImRlZmF1bHQtZGV2aWNlIiwiZWZmIjoxNzA2ODU0NTMwMDI3LCJyblN0ciI6Ik1RcHRPOUVBVlJlbGRQa1RFN01MZUpLeG5KTGVwRFpaIn0.knOw08E6mwWF_GpkeQ8KflQlfQuNu4jd-_Bgh7EnCj4'
useEffect(() => {
const socket = new WebSocket(`ws://127.0.0.1:10821/api/ws/${token}`);
socket.onopen = () => {
console.log('open');
socket.send('hello');
};
}, []);
return (false);
}

Expand Down
5 changes: 2 additions & 3 deletions chat2db-client/src/pages/main/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import BrandLogo from '@/components/BrandLogo';
import i18n from '@/i18n';
import { getUser, userLogout } from '@/service/user';
import { INavItem } from '@/typings/main';
import { ILoginUser, IRole } from '@/typings/user';
import { IUserVO, IRole } from '@/typings/user';

// ----- hooks -----
import getConnectionEnvList from './functions/getConnection';
Expand Down Expand Up @@ -66,7 +66,7 @@ const initNavConfig: INavItem[] = [
function MainPage() {
const navigate = useNavigate();
const [navConfig, setNavConfig] = useState<INavItem[]>(initNavConfig);
const [userInfo, setUserInfo] = useState<ILoginUser>();
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 @@ -96,7 +96,6 @@ function MainPage() {
}
}, [activeNavKey]);

// 这里如果社区版没有登陆可能需要后端来个重定向?
const handleInitPage = async () => {
const cloneNavConfig = [...navConfig];
const res = await getUser();
Expand Down
4 changes: 2 additions & 2 deletions chat2db-client/src/service/user.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import createRequest from './base';
import { IPageParams, IPageResponse } from '@/typings';
import { ILoginUser, IUser } from '@/typings/user';
import { IUserVO, IUser } from '@/typings/user';

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

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

/** 获取用户列表信息 */
const getUserList = createRequest<IPageParams, IPageResponse<IUser>>('/api/user/list', {
Expand Down
44 changes: 44 additions & 0 deletions chat2db-client/src/store/user/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { StoreApi } from 'zustand';
import { UseBoundStoreWithEqualityFn, createWithEqualityFn } from 'zustand/traditional';
import { devtools } from 'zustand/middleware';
import { shallow } from 'zustand/shallow';
import { IUserVO } from '@/typings/user';
import { getUser } from '@/service/user';

export interface IUserStore {
curUser?: IUserVO | null;
}

const initUserStore: IUserStore = {
curUser: null,
};

/**
* 用户 store
*/
export const useUserStore: UseBoundStoreWithEqualityFn<StoreApi<IUserStore>> = createWithEqualityFn(
devtools(() => initUserStore),
shallow,
);

/**
*
* @param curUser 设置当前用户
*/

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

/**
* 获取当前用户
*/

export const queryCurUser = async () => {
// null 表示在padding,返回 void 0(undefined)表示未登录
const curUser = await getUser() || void 0;
useUserStore.setState({ curUser });
// 向cookie中写入当前用户id
const date = new Date('2030-12-30 12:30:00').toUTCString();
document.cookie = `CHAT2DB.USER_ID=${curUser?.id};Expires=${date}`;
};
20 changes: 6 additions & 14 deletions chat2db-client/src/typings/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,10 @@ export interface IUser {
role?: IRole;
}

export interface ILoginUser {
/**
* Is it an administrator
*/
admin?: boolean;
/**
* 用户id
*/
id?: number;
/**
* 昵称
*/
nickName?: string;
roleCode: IRole;
export interface IUserVO {
admin: boolean;
id : number;
nickName: string;
roleCode: string;
token: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public class Application {

public static void main(String[] args) {
ConfigUtils.initProcess();
CompletableFuture.runAsync(() -> {
new Thread(() -> {
Dbutils.init();
});
}).start();
SpringApplication.run(Application.class, args);
}
}

0 comments on commit 57edb00

Please sign in to comment.