"use client" import type React from "react" import { useState, useEffect } from "react" import { Eye, EyeOff, Phone } from "lucide-react" import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Checkbox } from "@/components/ui/checkbox" import { useRouter } from "next/navigation" import { WeChatIcon } from "@/components/icons/wechat-icon" import { AppleIcon } from "@/components/icons/apple-icon" import { useToast } from "@/components/ui/use-toast" import { useAuth } from "@/app/components/AuthProvider" import { loginApi } from "@/lib/api" // 定义登录表单类型 interface LoginForm { phone: string password: string verificationCode: string agreeToTerms: boolean } export default function LoginPage() { const [showPassword, setShowPassword] = useState(false) const [activeTab, setActiveTab] = useState<"password" | "verification">("password") const [isLoading, setIsLoading] = useState(false) const [form, setForm] = useState({ phone: "", password: "", verificationCode: "", agreeToTerms: false, }) const router = useRouter() const { toast } = useToast() const { login, isAuthenticated } = useAuth() const handleInputChange = (e: React.ChangeEvent) => { const { name, value } = e.target setForm((prev) => ({ ...prev, [name]: value })) } const handleCheckboxChange = (checked: boolean) => { setForm((prev) => ({ ...prev, agreeToTerms: checked })) } const validateForm = () => { if (!form.phone) { toast({ variant: "destructive", title: "请输入手机号", description: "手机号不能为空", }) return false } if (!form.agreeToTerms) { toast({ variant: "destructive", title: "请同意用户协议", description: "需要同意用户协议和隐私政策才能继续", }) return false } if (activeTab === "password" && !form.password) { toast({ variant: "destructive", title: "请输入密码", description: "密码不能为空", }) return false } if (activeTab === "verification" && !form.verificationCode) { toast({ variant: "destructive", title: "请输入验证码", description: "验证码不能为空", }) return false } return true } const handleLogin = async (e: React.FormEvent) => { e.preventDefault() if (!validateForm()) return setIsLoading(true) try { if (activeTab === "password") { // 发送账号密码登录请求 const response = await loginApi.login(form.phone, form.password) if (response.code === 200 && response.data) { // 获取用户信息和token const { token, token_expired, member } = response.data // 保存token和用户信息 login(token, { id: member.id, username: member.username || member.account || '', account: member.account, avatar: member.avatar }) // 显示成功提示 toast({ title: "登录成功", description: "欢迎回来!", }) // 跳转到首页 router.push("/") } else { throw new Error(response.msg || "登录失败") } } else { // 验证码登录逻辑保持原样,未来可以实现 toast({ variant: "destructive", title: "功能未实现", description: "验证码登录功能尚未实现,请使用密码登录", }) } } catch (error) { toast({ variant: "destructive", title: "登录失败", description: error instanceof Error ? error.message : "请稍后重试", }) } finally { setIsLoading(false) } } const handleSendVerificationCode = async () => { if (!form.phone) { toast({ variant: "destructive", title: "请输入手机号", description: "发送验证码需要手机号", }) return } toast({ variant: "destructive", title: "功能未实现", description: "验证码发送功能尚未实现", }) } useEffect(() => { // 检查是否已登录,如果已登录则跳转到首页 if (isAuthenticated) { router.push("/") } }, [isAuthenticated, router]) return (
setActiveTab(v as "password" | "verification")} > 验证码登录 密码登录

你所在地区仅支持 手机号 / 微信 / Apple 登录

+86

其他登录方式
) }