import { useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { Button, Card, Form, Input, Tabs, message, Typography } from 'antd'; import { MOCK_SMS_FIXED_CODE } from '@dukang/shared-types'; import { saveAuth, request } from '../lib/api'; type LoginResult = { accessToken: string; refreshToken: string }; export default function LoginPage() { const navigate = useNavigate(); const [smsForm] = Form.useForm(); const [passwordForm] = Form.useForm(); const [loading, setLoading] = useState(false); const [codeCooldown, setCodeCooldown] = useState(0); const phone = Form.useWatch('phone', smsForm); async function sendCode() { if (!phone) { message.warning('请先输入手机号'); return; } await request('/admin/auth/sms/send', { method: 'POST', body: JSON.stringify({ phone, scene: 'HQ_LOGIN' }), }); message.success('验证码已发送'); setCodeCooldown(60); const timer = setInterval(() => { setCodeCooldown((c) => { if (c <= 1) { clearInterval(timer); return 0; } return c - 1; }); }, 1000); } async function finishLogin(data: LoginResult) { saveAuth(data); message.success('登录成功'); navigate('/'); } async function onSmsFinish(values: { phone: string; code: string }) { setLoading(true); try { const data = await request('/admin/auth/login/sms', { method: 'POST', body: JSON.stringify(values), }); await finishLogin(data); } catch (e) { message.error(e instanceof Error ? e.message : '登录失败'); } finally { setLoading(false); } } async function onPasswordFinish(values: { loginName: string; password: string }) { setLoading(true); try { const data = await request('/admin/auth/login/password', { method: 'POST', body: JSON.stringify(values), }); await finishLogin(data); } catch (e) { message.error(e instanceof Error ? e.message : '登录失败'); } finally { setLoading(false); } } return (
HQ 管理后台 ), }, { key: 'sms', label: '短信验证码', children: (
0} onClick={() => void sendCode()}> {codeCooldown > 0 ? `${codeCooldown}s` : '获取验证码'} } />
), }, ]} />
); }