112 lines
4.0 KiB
TypeScript
112 lines
4.0 KiB
TypeScript
import { useState } from 'react'
|
|
import { Link } from 'react-router-dom'
|
|
import { Building2, Eye, EyeOff } from 'lucide-react'
|
|
import { useForm } from 'react-hook-form'
|
|
import { zodResolver } from '@hookform/resolvers/zod'
|
|
import { z } from 'zod'
|
|
import api from '../../lib/api'
|
|
import { Input, Label } from '../../components/ui/Input'
|
|
import Button from '../../components/ui/Button'
|
|
|
|
const schema = z.object({
|
|
phone: z.string().regex(/^1[3-9]\d{9}$/, '手机号格式不正确'),
|
|
newPassword: z.string().min(8, '密码至少8位').max(32, '密码最多32位'),
|
|
}).refine((data) => data.newPassword.length >= 8, {
|
|
message: '密码至少8位',
|
|
path: ['newPassword'],
|
|
})
|
|
|
|
type FormData = z.infer<typeof schema>
|
|
|
|
export default function ForgotPassword() {
|
|
const [showPassword, setShowPassword] = useState(false)
|
|
const [error, setError] = useState('')
|
|
const [success, setSuccess] = useState(false)
|
|
const [loading, setLoading] = useState(false)
|
|
|
|
const { register, handleSubmit, formState: { errors } } = useForm<FormData>({
|
|
resolver: zodResolver(schema),
|
|
})
|
|
|
|
const onSubmit = async (data: FormData) => {
|
|
setError('')
|
|
setLoading(true)
|
|
try {
|
|
await api.post('/auth/reset-password', data)
|
|
setSuccess(true)
|
|
} catch (err: any) {
|
|
setError(err.response?.data?.error?.message || '重置失败,请稍后重试')
|
|
} finally {
|
|
setLoading(false)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="min-h-screen flex items-center justify-center bg-surface px-4">
|
|
<div className="w-full max-w-sm">
|
|
<div className="flex items-center justify-center gap-2 mb-8">
|
|
<Building2 className="w-8 h-8 text-primary" />
|
|
<span className="text-xl font-bold">用工合规助手</span>
|
|
</div>
|
|
|
|
<div className="card">
|
|
<h1 className="text-lg font-semibold mb-4">重置密码</h1>
|
|
|
|
{success ? (
|
|
<div className="text-center py-4">
|
|
<div className="text-green-600 mb-3">密码已重置成功</div>
|
|
<Link to="/login" className="text-primary hover:underline text-sm">返回登录</Link>
|
|
</div>
|
|
) : (
|
|
<>
|
|
{error && (
|
|
<div className="mb-4 px-3 py-2 rounded-md bg-red-50 text-red-700 text-sm">{error}</div>
|
|
)}
|
|
|
|
<form onSubmit={handleSubmit(onSubmit)} className="space-y-4">
|
|
<div>
|
|
<Label>手机号</Label>
|
|
<Input
|
|
type="tel"
|
|
placeholder="请输入注册手机号"
|
|
{...register('phone')}
|
|
maxLength={11}
|
|
/>
|
|
{errors.phone && <p className="text-xs text-red-500 mt-1">{errors.phone.message}</p>}
|
|
</div>
|
|
|
|
<div>
|
|
<Label>新密码</Label>
|
|
<div className="relative">
|
|
<Input
|
|
type={showPassword ? 'text' : 'password'}
|
|
placeholder="至少8位"
|
|
{...register('newPassword')}
|
|
/>
|
|
<button
|
|
type="button"
|
|
onClick={() => setShowPassword(!showPassword)}
|
|
className="absolute right-2 top-1/2 -translate-y-1/2 text-gray-400"
|
|
>
|
|
{showPassword ? <EyeOff className="w-4 h-4" /> : <Eye className="w-4 h-4" />}
|
|
</button>
|
|
</div>
|
|
{errors.newPassword && <p className="text-xs text-red-500 mt-1">{errors.newPassword.message}</p>}
|
|
</div>
|
|
|
|
<Button type="submit" className="w-full" disabled={loading}>
|
|
{loading ? '重置中...' : '重置密码'}
|
|
</Button>
|
|
</form>
|
|
|
|
<div className="mt-4 text-center text-sm">
|
|
<Link to="/login" className="text-primary hover:underline">返回登录</Link>
|
|
</div>
|
|
</>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|