import { Form, Input, Checkbox, Link, Button, Space } from '@arco-design/web-react'; import { FormInstance } from '@arco-design/web-react/es/Form'; import { IconLock, IconUser } from '@arco-design/web-react/icon'; import React, { useEffect, useRef, useState } from 'react'; import axios from 'axios'; import styles from './style/index.module.less'; import history from '../../history'; export default function LoginForm() { const formRef = useRef(); const [errorMessage, setErrorMessage] = useState(''); const [loading, setLoading] = useState(false); const [rememberPassword, setRememberPassword] = useState(false); function afterLoginSuccess(params) { // 记住密码 if (rememberPassword) { localStorage.setItem('loginParams', JSON.stringify(params)); } else { localStorage.removeItem('loginParams'); } // 记录登录状态 localStorage.setItem('userStatus', 'login'); // 跳转首页 window.location.href = history.createHref({ pathname: '/', }); } function login(params) { setErrorMessage(''); setLoading(true); axios .post('/api/user/login', params) .then((res) => { const { status, msg } = res.data; if (status === 'ok') { afterLoginSuccess(params); } else { setErrorMessage(msg || '登录出错,请刷新重试'); } }) .finally(() => { setLoading(false); }); } function onSubmitClick() { formRef.current.validate().then((values) => { login(values); }); } // 读取 localStorage,设置初始值 useEffect(() => { const params = localStorage.getItem('loginParams'); const rememberPassword = !!params; setRememberPassword(rememberPassword); if (formRef.current && rememberPassword) { const parseParams = JSON.parse(params); formRef.current.setFieldsValue(parseParams); } }, []); return (
登录 Arco Design Pro
登录 Arco Design Pro
{errorMessage}
} placeholder="用户名:admin" onPressEnter={onSubmitClick} /> } placeholder="密码:admin" onPressEnter={onSubmitClick} />
记住密码 忘记密码?
); }