| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- 'use client';
- import { useState, useEffect } from 'react';
- import { X, Lock, Loader2, Save, Mail, KeyRound, Eye, EyeOff } from 'lucide-react';
- import { useLanguage } from '@/lib/i18n/LanguageContext';
- import MessageModal from '@/components/common/MessageModal';
- import { authService } from '@/lib/services';
- interface ChangePasswordModalProps {
- isOpen: boolean;
- onClose: () => void;
- }
- export default function ChangePasswordModal({ isOpen, onClose }: ChangePasswordModalProps) {
- const { t } = useLanguage();
- const [loading, setLoading] = useState(false);
- const [countdown, setCountdown] = useState(0);
-
- const [email, setEmail] = useState('');
- const [code, setCode] = useState('');
- const [newPassword, setNewPassword] = useState('');
- const [confirmPassword, setConfirmPassword] = useState('');
-
- const [showPassword, setShowPassword] = useState(false);
- const [showConfirmPassword, setShowConfirmPassword] = useState(false);
- const [msgModal, setMsgModal] = useState({
- isOpen: false,
- title: '',
- message: '',
- type: 'info' as 'info' | 'error' | 'success',
- onOk: null as (() => void) | null,
- });
- const showMessage = (message: string, type: 'info' | 'error' | 'success' = 'info', onOk?: () => void) => {
- setMsgModal({
- isOpen: true,
- title: type === 'error' ? t('common.error') : t('common.notice'),
- message,
- type,
- onOk: onOk || null,
- });
- };
- const handleCloseMsg = () => {
- const callback = msgModal.onOk;
- setMsgModal((prev) => ({ ...prev, isOpen: false }));
- if (callback) callback();
- };
- useEffect(() => {
- if (isOpen) {
- const userStr = localStorage.getItem('user_info');
- if (userStr) {
- try {
- const user = JSON.parse(userStr);
- if (user.email) setEmail(user.email);
- } catch (e) {
- console.error(e);
- }
- }
- setCode('');
- setNewPassword('');
- setConfirmPassword('');
- }
- }, [isOpen]);
- useEffect(() => {
- let timer: any;
- if (countdown > 0) {
- timer = setTimeout(() => setCountdown(countdown - 1), 1000);
- }
- return () => clearTimeout(timer);
- }, [countdown]);
- const handleSendCode = async () => {
- if (!email) return showMessage(t('settings.email_not_found') || '未检测到绑定邮箱', 'error');
-
- try {
- await authService.sendResetCode(email);
-
- showMessage(`${t('settings.code_sent') || '验证码已发送至'} ${email}`, 'success');
- setCountdown(60);
- } catch (error: any) {
- console.error(error);
- const msg = error.response?.data?.message || t('common.unknown_error');
- showMessage(`${t('settings.send_failed') || '发送失败'}: ${msg}`, 'error');
- }
- };
- const handleSubmit = async (e: React.FormEvent) => {
- e.preventDefault();
- if (!code) return showMessage(t('settings.enter_code') || '请输入验证码', 'error');
- if (!newPassword) return showMessage(t('settings.enter_password') || '请输入新密码', 'error');
- if (newPassword !== confirmPassword) return showMessage(t('settings.password_mismatch') || '两次输入的密码不一致', 'error');
- setLoading(true);
- try {
- await authService.resetPassword({
- email,
- code,
- new_password: newPassword
- });
- showMessage(t('settings.password_changed_success') || '密码重置成功!请重新登录。', 'success', () => {
- localStorage.removeItem('rsid');
- localStorage.removeItem('user_info');
- window.location.href = '/login';
- onClose();
- });
-
- } catch (error: any) {
- console.error(error);
- const msg = error.response?.data?.message || error.message || '重置失败';
- showMessage(`${t('settings.change_failed') || '密码修改失败'}: ${msg}`, 'error');
- } finally {
- setLoading(false);
- }
- };
- if (!isOpen) return null;
- return (
- <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-grayscale p-4">
- <div className="bg-neutral-50 border-2 border-black shadow-[8px_8px_0px_#000] w-full max-w-md overflow-hidden font-mono animate-in zoom-in-95 duration-200">
-
- {/* Header */}
- <div className="px-5 py-3.5 border-b-2 border-black flex justify-between items-center bg-neutral-200 text-black select-none">
- <div className="flex items-center gap-2 text-xs font-bold uppercase tracking-wider">
- <span className="w-2.5 h-2.5 bg-amber-300 border border-black inline-block" />
- <span>[ CHANGE PASSWORD // 重置凭证密码 ]</span>
- </div>
- <button onClick={onClose} className="w-6 h-6 bg-white hover:bg-amber-300 border border-black flex items-center justify-center font-bold text-xs transition">
- ✕
- </button>
- </div>
- <form onSubmit={handleSubmit} className="p-6 bg-white space-y-4">
-
- {/* 邮箱 & 验证码 */}
- <div className="space-y-2">
- <label className="block text-xs font-bold uppercase text-black">[ 01 // 验证码接收邮箱 ]</label>
- <input
- type="text"
- disabled
- value={email}
- className="w-full border-2 border-black bg-neutral-100 p-2.5 text-xs font-mono text-neutral-600 cursor-not-allowed"
- />
-
- <div className="flex gap-2 pt-1">
- <input
- type="text"
- required
- className="flex-1 border-2 border-black bg-neutral-50 p-2.5 text-xs font-mono text-center tracking-widest font-bold outline-none shadow-[2px_2px_0px_#000]"
- placeholder={t('settings.code_placeholder') || '------'}
- maxLength={6}
- value={code}
- onChange={(e) => setCode(e.target.value)}
- />
- <button
- type="button"
- disabled={countdown > 0}
- onClick={handleSendCode}
- className="w-32 border-2 border-black bg-neutral-100 hover:bg-neutral-200 text-black text-xs font-mono font-bold transition shadow-[2px_2px_0px_#000] active:translate-x-0.5 active:translate-y-0.5 active:shadow-none disabled:opacity-50"
- >
- {countdown > 0 ? `${countdown}s` : (t('settings.get_code') || '获取验证码')}
- </button>
- </div>
- </div>
- <div className="border-t-2 border-dashed border-neutral-300 my-2"></div>
- {/* 新密码 */}
- <div className="space-y-1">
- <label className="block text-xs font-bold uppercase text-black">[ 02 // 输入新密码 ]</label>
- <div className="relative">
- <input
- type={showPassword ? "text" : "password"}
- required
- className="w-full border-2 border-black bg-neutral-50 p-2.5 pr-10 text-xs font-mono outline-none shadow-[2px_2px_0px_#000] text-black"
- placeholder={t('settings.new_password_placeholder') || '设置新安全密码...'}
- value={newPassword}
- onChange={(e) => setNewPassword(e.target.value)}
- />
- <button
- type="button"
- onClick={() => setShowPassword(!showPassword)}
- className="absolute right-3 top-3 text-black"
- >
- {showPassword ? <EyeOff size={14}/> : <Eye size={14}/>}
- </button>
- </div>
- </div>
- {/* 确认密码 */}
- <div className="space-y-1">
- <label className="block text-xs font-bold uppercase text-black">[ 03 // 再次确认新密码 ]</label>
- <input
- type={showPassword ? "text" : "password"}
- required
- className="w-full border-2 border-black bg-neutral-50 p-2.5 text-xs font-mono outline-none shadow-[2px_2px_0px_#000] text-black"
- placeholder={t('settings.confirm_password_placeholder') || '再次输入新密码...'}
- value={confirmPassword}
- onChange={(e) => setConfirmPassword(e.target.value)}
- />
- {confirmPassword && newPassword !== confirmPassword && (
- <p className="text-[10px] text-rose-600 font-bold mt-1">[ ! 两次输入的密码不一致 ]</p>
- )}
- </div>
- <div className="pt-3 flex justify-end gap-2.5 border-t-2 border-dashed border-neutral-300">
- <button
- type="button"
- onClick={onClose}
- className="px-4 py-2 bg-neutral-100 border-2 border-black text-black font-bold text-xs shadow-[2px_2px_0px_#000] hover:bg-neutral-200 transition active:translate-x-0.5 active:translate-y-0.5 uppercase"
- >
- [ {t('common.cancel') || '取消'} ]
- </button>
- <button
- type="submit"
- disabled={loading}
- className="flex-1 bg-amber-300 hover:bg-amber-400 text-black border-2 border-black py-2 font-black text-xs shadow-[3px_3px_0px_#000] transition active:translate-x-0.5 active:translate-y-0.5 active:shadow-none flex justify-center items-center gap-1.5 uppercase disabled:opacity-50"
- >
- {loading ? <Loader2 size={14} className="animate-spin" /> : <><Save size={13} /> <span>[ {t('common.confirm') || '确认重置'} ]</span></>}
- </button>
- </div>
- </form>
- </div>
- {/* 消息弹窗 */}
- <MessageModal
- isOpen={msgModal.isOpen}
- title={msgModal.title}
- message={msgModal.message}
- type={msgModal.type}
- onClose={handleCloseMsg}
- />
- </div>
- );
- }
|