|
@@ -1,14 +1,41 @@
|
|
|
-import { Button, Form, Input } from 'antd';
|
|
|
+import { Button, Form, Input, message } from 'antd';
|
|
|
import { login } from '../../api/user';
|
|
|
import './index.scss';
|
|
|
+import { UserOutlined, KeyOutlined } from '@ant-design/icons';
|
|
|
+import { useDispatch } from 'react-redux';
|
|
|
+import { setUser } from '../../store/slice/user';
|
|
|
+import { useNavigate, useSearchParams } from 'react-router-dom';
|
|
|
|
|
|
-const onFinish = async (values) => {
|
|
|
- let { data } = await login(values);
|
|
|
+const onLogin = async (user, { dispatch, navigate }, to = '/') => {
|
|
|
+ let {
|
|
|
+ data: { code, msg, data },
|
|
|
+ } = await login(user);
|
|
|
|
|
|
- console.log(data);
|
|
|
+ if (code == 0) {
|
|
|
+ message.success(msg);
|
|
|
+
|
|
|
+ //! 存token
|
|
|
+ localStorage.setItem('token', data.token);
|
|
|
+ //! redux存储用户信息
|
|
|
+ dispatch(setUser(data.user));
|
|
|
+ //! 跳转页面:如果有查询参数r 就 到r的地址,否则 去主页
|
|
|
+ navigate(to);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ message.error(msg);
|
|
|
};
|
|
|
|
|
|
function Login() {
|
|
|
+ const dispatch = useDispatch();
|
|
|
+ const navigate = useNavigate();
|
|
|
+ const [searchParams] = useSearchParams();
|
|
|
+ const to = searchParams.get('r') ? searchParams.get('r') : undefined;
|
|
|
+
|
|
|
+ function onFinish(values) {
|
|
|
+ onLogin(values, { dispatch, navigate }, to);
|
|
|
+ }
|
|
|
+
|
|
|
return (
|
|
|
<div className="login_container">
|
|
|
<div className="login">
|
|
@@ -37,7 +64,7 @@ function Login() {
|
|
|
},
|
|
|
]}
|
|
|
>
|
|
|
- <Input />
|
|
|
+ <Input prefix={<UserOutlined />} placeholder="账号..." allowClear />
|
|
|
</Form.Item>
|
|
|
|
|
|
<Form.Item
|
|
@@ -50,7 +77,11 @@ function Login() {
|
|
|
},
|
|
|
]}
|
|
|
>
|
|
|
- <Input.Password />
|
|
|
+ <Input.Password
|
|
|
+ prefix={<KeyOutlined />}
|
|
|
+ placeholder="密码..."
|
|
|
+ allowClear
|
|
|
+ />
|
|
|
</Form.Item>
|
|
|
|
|
|
<Form.Item
|