修复login页面调用取消自定义小时查询

This commit is contained in:
wmp
2025-09-25 10:07:29 +08:00
parent ebf50c15f1
commit 2c106e43df
2 changed files with 131 additions and 16 deletions

View File

@@ -0,0 +1,131 @@
'use client'
import { useState } from 'react'
import { useRouter } from 'next/navigation'
import { zodResolver } from '@hookform/resolvers/zod'
import { useForm } from 'react-hook-form'
import * as z from 'zod'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form'
import { Lock, User } from 'lucide-react'
import { useAuthStore } from '@/store/auth'
import { toast, Toaster } from 'sonner'
import { login } from '@/actions/auth'
const formSchema = z.object({
account: z.string().min(3, '账号至少需要3个字符'),
password: z.string().min(6, '密码至少需要6个字符'),
})
export default function LoginPage() {
const router = useRouter()
const [loading, setLoading] = useState(false)
const setAuth = useAuthStore(state => state.setAuth)
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
account: '',
password: '',
},
})
async function onSubmit(values: z.infer<typeof formSchema>) {
setLoading(true)
try {
const data = await login(values)
if (data.success) {
toast.success('登录成功', {
description: '正在跳转到仪表盘...',
})
setAuth(true)
await new Promise(resolve => setTimeout(resolve, 1000))
router.push('/dashboard')
router.refresh()
}
else {
toast.error('账号或密码错误,请重新输入')
}
}
catch (error) {
toast.error('登录失败', {
description: error instanceof Error ? error.message : '账号或密码错误,请稍后重试',
})
}
finally {
setLoading(false)
}
}
return (
<>
<div className="flex h-screen items-center justify-center bg-gray-50">
<Card className="w-[350px] shadow-lg">
<CardHeader className="space-y-1">
<CardTitle className="text-2xl font-bold text-center"></CardTitle>
</CardHeader>
<CardContent>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
<FormField
control={form.control}
name="account"
render={({ field }) => (
<FormItem>
<FormLabel></FormLabel>
<FormControl>
<div className="relative">
<User className="absolute left-2 top-2.5 h-4 w-4 text-muted-foreground" />
<Input
placeholder="请输入您的账号"
className="pl-8"
{...field}
/>
</div>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="password"
render={({ field }) => (
<FormItem>
<FormLabel></FormLabel>
<FormControl>
<div className="relative">
<Lock className="absolute left-2 top-2.5 h-4 w-4 text-muted-foreground" />
<Input type="password" placeholder="请输入密码" className="pl-8" {...field} />
</div>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<Button
type="submit"
className="w-full"
disabled={loading}
size="lg"
>
{loading
? (
<div className="flex items-center gap-2">
<div className="h-4 w-4 animate-spin rounded-full border-2 border-current border-t-transparent" />
...
</div>
)
: (
'登录'
)}
</Button>
</form>
</Form>
</CardContent>
</Card>
</div>
<Toaster richColors />
</>
)
}

View File

@@ -83,24 +83,8 @@ export default function AllocationStatus({ detailed = false }: { detailed?: bool
<option value="12">12</option>
<option value="24">24</option>
<option value="168">7</option>
<option value="custom"></option>
</select>
{timeFilter === 'custom' && (
<div className="flex items-center gap-2">
<input
type="number"
min="1"
max="720"
value={customHours}
onChange={e => setCustomHours(e.target.value)}
placeholder="输入小时数"
className="border rounded p-2 w-24"
/>
<small> (1-720)</small>
</div>
)}
<button
onClick={fetchData}
className="bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600"