auto_trade_sys/frontend/src/components/Login.jsx
2026-01-20 15:55:34 +08:00

73 lines
1.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import React, { useState } from 'react'
import { api } from '../services/api'
import './Login.css'
const Login = ({ onLoggedIn }) => {
const [username, setUsername] = useState('')
const [password, setPassword] = useState('')
const [busy, setBusy] = useState(false)
const [error, setError] = useState('')
const doLogin = async () => {
setBusy(true)
setError('')
try {
await api.login(username, password)
if (typeof onLoggedIn === 'function') await onLoggedIn()
} catch (e) {
setError(e?.message || '登录失败')
} finally {
setBusy(false)
}
}
return (
<div className="login-page">
<div className="login-card">
<div className="login-title">自动交易系统</div>
<div className="login-subtitle">请先登录</div>
<label className="login-field">
<div className="login-label">用户名</div>
<input
className="login-input"
value={username}
onChange={(e) => setUsername(e.target.value)}
placeholder="例如admin"
autoComplete="username"
/>
</label>
<label className="login-field">
<div className="login-label">密码</div>
<input
className="login-input"
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
placeholder="请输入密码"
autoComplete="current-password"
onKeyDown={(e) => {
if (e.key === 'Enter') doLogin()
}}
/>
</label>
{error ? <div className="login-error">{error}</div> : null}
<button
type="button"
className="login-btn"
disabled={busy || !username.trim() || !password}
onClick={doLogin}
>
{busy ? '登录中...' : '登录'}
</button>
</div>
</div>
)
}
export default Login