import { useState } from 'react' import { Card, Form, Input, Button, message, Typography, Alert, Progress } from 'antd' import { LockOutlined, KeyOutlined, UserOutlined } from '@ant-design/icons' import { useTranslation } from 'react-i18next' import { apiService } from '../services/api' import { useMediaQuery } from 'react-responsive' const { Title } = Typography /** * 计算密码强度 * @param password 密码 * @returns 强度等级 0-4 (0: 弱, 1: 较弱, 2: 中等, 3: 强, 4: 很强) */ const getPasswordStrength = (password: string): number => { if (!password) return 0 if (password.length < 6) return 0 let strength = 0 // 长度加分 if (password.length >= 6) strength += 1 if (password.length >= 8) strength += 1 if (password.length >= 12) strength += 1 // 字符类型加分 if (/[a-z]/.test(password)) strength += 0.5 if (/[A-Z]/.test(password)) strength += 0.5 if (/\d/.test(password)) strength += 0.5 if (/[^a-zA-Z0-9]/.test(password)) strength += 0.5 return Math.min(4, Math.floor(strength)) } const ResetPassword: React.FC = () => { const { t } = useTranslation() const isMobile = useMediaQuery({ maxWidth: 768 }) const [loading, setLoading] = useState(false) const [passwordStrength, setPasswordStrength] = useState(0) const [form] = Form.useForm() /** * 获取密码强度文本和颜色 */ const getPasswordStrengthInfo = (strength: number): { text: string; color: string; percent: number } => { switch (strength) { case 0: return { text: t('resetPassword.weak') || '弱', color: '#ff4d4f', percent: 25 } case 1: return { text: t('resetPassword.fair') || '较弱', color: '#ff7a45', percent: 50 } case 2: return { text: t('resetPassword.medium') || '中等', color: '#faad14', percent: 75 } case 3: return { text: t('resetPassword.strong') || '强', color: '#52c41a', percent: 100 } case 4: return { text: t('resetPassword.veryStrong') || '很强', color: '#52c41a', percent: 100 } default: return { text: t('resetPassword.weak') || '弱', color: '#ff4d4f', percent: 0 } } } const handleReset = async (values: { resetKey: string username: string newPassword: string confirmPassword: string }) => { if (values.newPassword !== values.confirmPassword) { message.error(t('resetPassword.passwordMismatch') || '两次输入的密码不一致') return } setLoading(true) try { const response = await apiService.auth.resetPassword({ resetKey: values.resetKey, username: values.username, newPassword: values.newPassword }) if (response.data.code === 0) { message.success(t('resetPassword.success') || '密码重置成功', 1) // 使用 window.location.href 强制跳转到登录页,确保跳转成功 setTimeout(() => { window.location.href = '/login' }, 500) } else { message.error(response.data.msg || t('resetPassword.failed') || '密码重置失败') } } catch (error: any) { console.error('密码重置失败:', error) const errorMsg = error.response?.data?.msg || error.message || t('resetPassword.failed') || '密码重置失败' message.error(errorMsg) } finally { setLoading(false) } } return (