"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import * as z from "zod"; import { motion } from "motion/react"; import { Eye, EyeOff, Lock, Mail, AlertCircle, UserCircle } from "lucide-react"; import { useAuthStore } from "@/lib/stores/auth-store"; import { api } from "@/lib/api/client"; import ENDPOINTS from "@/lib/api/endpoints"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Card, CardContent } from "@/components/ui/card"; import { cn } from "@/lib/utils"; const loginSchema = z.object({ email: z.string().email("请输入有效的邮箱地址"), password: z.string().min(6, "密码至少6个字符"), }); type LoginFormData = z.infer; interface LoginResponse { access_token: string; user: { id: number; email: string; full_name: string; role: string; permissions: string[]; company_id: number; }; } // 测试用户数据 const TEST_USERS = [ { name: "管理员", email: "admin@xingchen.com", password: "admin123", role: "管理员" }, { name: "财务主管", email: "finance@xingchen.com", password: "finance123", role: "财务主管" }, { name: "会计", email: "accountant@xingchen.com", password: "account123", role: "会计" }, ]; export default function LoginPage() { const router = useRouter(); const { login } = useAuthStore(); const [error, setError] = useState(""); const [loading, setLoading] = useState(false); const [showPassword, setShowPassword] = useState(false); // onSubmit 必须在 handleSubmit 之前定义 const onSubmit = async (data: LoginFormData) => { setLoading(true); setError(""); try { const response = await api.post(ENDPOINTS.AUTH.LOGIN, data); const { access_token, user } = response.data; // 保存到 localStorage login(user, access_token); // 设置 cookie document.cookie = `auth_token=${access_token}; path=/; max-age=86400; SameSite=Lax`; document.cookie = `company_id=${user.company_id}; path=/; max-age=86400; SameSite=Lax`; // 直接跳转 router.push("/dashboard"); } catch (err: unknown) { const error = err as { message?: string }; setError(error.message || "登录失败,请检查邮箱和密码"); } finally { setLoading(false); } }; const { register, handleSubmit, setValue, formState: { errors }, } = useForm({ resolver: zodResolver(loginSchema), }); // 快速填充测试用户 const fillTestUser = (email: string, password: string) => { setValue("email", email); setValue("password", password); setError(""); }; return (
{/* Background decoration */}
{/* Logo & Title */}

财务AI助手

财务AI助手

登录账户

{/* 测试用户快捷登录 */}
快速登录测试账号
{TEST_USERS.map((user) => ( ))}
{/* Error Alert */} {error && (

{error}

)} {/* Email Field */}
{errors.email && (

{errors.email.message}

)}
{/* Password Field */}
{errors.password && (

{errors.password.message}

)}
{/* Submit Button */}
{/* Footer */}

登录即表示您同意我们的服务条款

{/* Help Text */} 遇到问题?请联系管理员
); }