import { AlipayOutlined, LockOutlined, MobileOutlined, TaobaoOutlined, UserOutlined, WeiboOutlined, } from '@ant-design/icons'; import { LoginFormPage, ProConfigProvider, ProFormCaptcha, ProFormCheckbox, ProFormText, } from '@ant-design/pro-components'; import { history, useModel, useNavigate } from '@umijs/max'; import { Button, Divider, message, Space, Tabs, theme } from 'antd'; import type { CSSProperties } from 'react'; import { useState } from 'react'; import { flushSync } from 'react-dom'; import { getTenantIdByName, login } from '@/services/login'; import * as authUtil from '@/utils/auth'; type LoginType = 'phone' | 'account'; const iconStyles: CSSProperties = { color: 'rgba(0, 0, 0, 0.2)', fontSize: '18px', verticalAlign: 'middle', cursor: 'pointer', }; const Page = () => { const [loginType, setLoginType] = useState('account'); const { token } = theme.useToken(); const [messageApi, contextHolder] = message.useMessage(); const { initialState, setInitialState } = useModel('@@initialState'); const navigate = useNavigate(); // 获取租户 ID const getTenantId = async (name: string) => { const data = await getTenantIdByName({ name }); authUtil.setTenantId(data); }; const fetchUserInfo = async () => { const userInfo = await initialState?.fetchUserInfo?.(); if (userInfo) { flushSync(() => { setInitialState((s) => ({ ...s, currentUser: userInfo, })); }); } }; // 添加登录处理函数 const handleSubmit = async (values: any) => { try { // 根据登录类型处理不同的参数 const params = { tenantName: '芋道源码', ...values, }; await getTenantId(params.tenantName); if (params.rememberMe) { authUtil.setLoginForm(params); } else { authUtil.removeLoginForm(); } // 调用登录接口 const data = await login(params); // 登录成功 messageApi.success('登录成功!'); // 跳转到首页 authUtil.setToken(data); await fetchUserInfo(); const urlParams = new URL(window.location.href).searchParams; // navigate(urlParams.get("redirect") || "/"); window.location.href = urlParams.get('redirect') || '/'; } catch (error) { messageApi.error('登录失败,请检查网络或稍后重试!'); } }; return (
{contextHolder} // 去看看 // // ), // }} actions={
其他登录方式
} > setLoginType(activeKey as LoginType)} > {loginType === 'account' && ( <> ), style: { backgroundColor: 'rgb(0 0 0 / 77%)', color: 'white', }, }} placeholder={'用户名: admin or user'} rules={[ { required: true, message: '请输入用户名!', }, ]} /> ), style: { backgroundColor: 'rgb(0 0 0 / 77%)', color: 'white', }, }} placeholder={'密码: ant.design'} rules={[ { required: true, message: '请输入密码!', }, ]} /> )} {loginType === 'phone' && ( <> ), style: { backgroundColor: 'rgb(0 0 0 / 77%)', color: 'white', }, }} name="mobile" placeholder={'手机号'} rules={[ { required: true, message: '请输入手机号!', }, { pattern: /^1\d{10}$/, message: '手机号格式错误!', }, ]} /> ), }} captchaProps={{ size: 'large', }} placeholder={'请输入验证码'} captchaTextRender={(timing, count) => { if (timing) { return `${count} ${'获取验证码'}`; } return '获取验证码'; }} name="captcha" rules={[ { required: true, message: '请输入验证码!', }, ]} onGetCaptcha={async () => { message.success('获取验证码成功!验证码为:1234'); }} /> )}
记住我 忘记密码
); }; export default () => { return ( ); };