Commit 232e6205 authored by littleforest's avatar littleforest
parents 4cbcf488 68dbacb0
/**
* @see https://umijs.org/zh-CN/plugins/plugin-access
* */
export default function access(initialState: { userInfo: { token: '' }; }) {
console.log('initialState', initialState)
const { token } = initialState.userInfo ?? {};
return {
token
};
export default function access(initialState: {
userInfo: { token: ''; roles: []; name: ''; userName: '' };
}) {
console.log('initialState', initialState);
return initialState.userInfo ?? {};
}
......@@ -2,8 +2,9 @@ import styles from './index.less';
import { Button, Checkbox, Form, Input, Modal } from 'antd';
import { LockOutlined, UserOutlined } from '@ant-design/icons';
import logoSvg from '../../assets/logo.svg';
import { useState } from 'react';
import { useEffect, useRef, useState } from 'react';
import { loginChatGLM } from '@/services/chatGLM/api';
import { useModel } from 'umi';
interface ILoginModalProps {
open: boolean;
setOpen: (value: boolean) => void;
......@@ -11,14 +12,28 @@ interface ILoginModalProps {
const LoginModal = (props: ILoginModalProps) => {
const { open, setOpen } = props;
const [loading, setLoading] = useState(false);
const { initialState, setInitialState } = useModel('@@initialState');
const [form] = Form.useForm();
const submitRef = useRef<HTMLButtonElement>(null);
const reset = () => {
setOpen(false);
form.resetFields();
};
};
useEffect(() => {
if (open) {
const account = localStorage.getItem('account');
localStorage.removeItem('account');
if (account) {
setTimeout(() => {
submitRef.current?.click();
}, 300);
}
}
}, [open]);
const handleSubmit = async (values: any) => {
setLoading(true);
......@@ -28,14 +43,23 @@ const LoginModal = (props: ILoginModalProps) => {
name: username,
password,
});
if (checked) {
localStorage.setItem('account', JSON.stringify({ username, password, checked }));
}
setInitialState({ ...initialState, userInfo: result });
localStorage.setItem('user', JSON.stringify(result));
setLoading(false);
reset();
} catch(error) {
} catch (error) {
setLoading(false);
}
};
function getAccount() {
const account = localStorage.getItem('account');
return JSON.parse(account || JSON.stringify({ username: '', password: '', checked: false }));
}
return (
<Modal
open={open}
......@@ -57,31 +81,28 @@ const LoginModal = (props: ILoginModalProps) => {
</div>
<p className={styles.modal_tip}>账户密码登录</p>
<Form
initialValues={getAccount()}
name="normal_login"
initialValues={{ remember: true }}
onFinish={handleSubmit}
onValuesChange={(value) => {
console.log(value);
}}
className={styles.modal_form}
>
<Form.Item
name="username"
rules={[{ required: true, message: '请输入你的账户!' }]}
>
<Input prefix={<UserOutlined />} placeholder="账户" />
<Form.Item name="username" rules={[{ required: true, message: '请输入你的账户!' }]}>
<Input prefix={<UserOutlined />} placeholder="账户" value={''} />
</Form.Item>
<Form.Item
name="password"
rules={[{ required: true, message: '请输入你的密码!' }]}
>
<Input prefix={<LockOutlined />} type="password" placeholder="密码" />
<Form.Item name="password" rules={[{ required: true, message: '请输入你的密码!' }]}>
<Input prefix={<LockOutlined />} type="password" placeholder="密码" value={''} />
</Form.Item>
<Form.Item name="remember" valuePropName="checked" className={styles.modal_checked}>
<Form.Item name="checked" valuePropName="checked" className={styles.modal_checked}>
<Checkbox>自动登录</Checkbox>
</Form.Item>
<Form.Item>
<Button type="primary" htmlType="submit" className={styles.modal_button} loading={loading}>
<Button
ref={submitRef}
type="primary"
htmlType="submit"
className={styles.modal_button}
loading={loading}
>
登录
</Button>
</Form.Item>
......
......@@ -19,6 +19,7 @@ import NewSession from '@/components/newSession';
import { useWebSocket, useRequest } from 'ahooks';
import { GetChatGLMToken, GetPromptList } from '@/services/chatGLM/api';
import { useAccess } from 'umi';
import { PageLoading } from '@ant-design/pro-layout';
const { useBreakpoint } = Grid;
const { Title } = Typography;
......@@ -36,15 +37,16 @@ const Chat: React.FC = () => {
const [showSider, setShowSider] = useState(true);
const [firstChat, setFirstChat] = useState(false);
const [form] = Form.useForm();
const [selectSiderTabs, setSelectSiderTabs] = useState('');
const { data: glmToken } = useRequest(GetChatGLMToken);
const { data: promptList, loading: promptLoading } = useRequest(GetPromptList);
const { data: glmToken, run: getChatGLMToken } = useRequest(GetChatGLMToken, { manual: true });
const {
data: promptList,
loading: promptLoading,
run: getPromptList,
} = useRequest(GetPromptList, { manual: true });
const [answer, setAnswer] = useState('');
const [loading, setLoading] = useState(true);
const [open, setOpen] = useState(false);
const tabShow = showSider && !isMobile;
const [loginModalOpen, setLoginModalOpen] = useState(false);
const access = useAccess();
const messageHistory = useRef<IHistory[]>([]);
......@@ -91,13 +93,13 @@ const Chat: React.FC = () => {
);
}
console.log('access', access);
useEffect(() => {
if (access?.token) {
setOpen(false);
getChatGLMToken();
getPromptList();
setLoginModalOpen(false);
} else {
setOpen(true);
setLoginModalOpen(true);
}
}, [access]);
......@@ -136,7 +138,7 @@ const Chat: React.FC = () => {
return (
<>
<LoginModal open={open} setOpen={setOpen} />
<LoginModal open={loginModalOpen} setOpen={setLoginModalOpen} />
{isMobile && renderMobileHeader()}
<Row>
{(showSider || !isMobile) && (
......@@ -154,7 +156,11 @@ const Chat: React.FC = () => {
</div>
<div className={styles.chatFooter}>
<Row style={{ width: '240px' }} justify={'center'} align={'middle'}>
<Col span={24} className={styles.chatFooterItem}>
<Col
span={24}
className={styles.chatFooterItem}
hidden={!access.roles?.find((x) => ['管理员', 'admin'].includes(x))}
>
<Space
wrap
onClick={() => {
......@@ -168,7 +174,7 @@ const Chat: React.FC = () => {
<Col span={24} className={styles.chatFooterItem} style={{ color: '#009B95' }}>
<Space wrap>
<UserOutlined />
游客登录
{access?.name || '请先登录'}
</Space>
</Col>
</Row>
......@@ -209,7 +215,11 @@ const Chat: React.FC = () => {
</div>
)}
</Col>
{promptLoading && (
<Col span={24}>
<PageLoading></PageLoading>
</Col>
)}
<Col span={'24'} style={{ padding: isMobile ? '' : '0 100px' }}>
{firstChat ? (
<div style={{ marginBottom: 150 }}>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment