ChangePasswordModal.tsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. 'use client';
  2. import { useState, useEffect } from 'react';
  3. import { X, Lock, Loader2, Save, Mail, KeyRound, Eye, EyeOff } from 'lucide-react';
  4. import { useLanguage } from '@/lib/i18n/LanguageContext';
  5. import MessageModal from '@/components/common/MessageModal';
  6. import { authService } from '@/lib/services';
  7. interface ChangePasswordModalProps {
  8. isOpen: boolean;
  9. onClose: () => void;
  10. }
  11. export default function ChangePasswordModal({ isOpen, onClose }: ChangePasswordModalProps) {
  12. const { t } = useLanguage();
  13. const [loading, setLoading] = useState(false);
  14. const [countdown, setCountdown] = useState(0);
  15. const [email, setEmail] = useState('');
  16. const [code, setCode] = useState('');
  17. const [newPassword, setNewPassword] = useState('');
  18. const [confirmPassword, setConfirmPassword] = useState('');
  19. const [showPassword, setShowPassword] = useState(false);
  20. const [showConfirmPassword, setShowConfirmPassword] = useState(false);
  21. const [msgModal, setMsgModal] = useState({
  22. isOpen: false,
  23. title: '',
  24. message: '',
  25. type: 'info' as 'info' | 'error' | 'success',
  26. onOk: null as (() => void) | null,
  27. });
  28. const showMessage = (message: string, type: 'info' | 'error' | 'success' = 'info', onOk?: () => void) => {
  29. setMsgModal({
  30. isOpen: true,
  31. title: type === 'error' ? t('common.error') : t('common.notice'),
  32. message,
  33. type,
  34. onOk: onOk || null,
  35. });
  36. };
  37. const handleCloseMsg = () => {
  38. const callback = msgModal.onOk;
  39. setMsgModal((prev) => ({ ...prev, isOpen: false }));
  40. if (callback) callback();
  41. };
  42. useEffect(() => {
  43. if (isOpen) {
  44. const userStr = localStorage.getItem('user_info');
  45. if (userStr) {
  46. try {
  47. const user = JSON.parse(userStr);
  48. if (user.email) setEmail(user.email);
  49. } catch (e) {
  50. console.error(e);
  51. }
  52. }
  53. setCode('');
  54. setNewPassword('');
  55. setConfirmPassword('');
  56. }
  57. }, [isOpen]);
  58. useEffect(() => {
  59. let timer: any;
  60. if (countdown > 0) {
  61. timer = setTimeout(() => setCountdown(countdown - 1), 1000);
  62. }
  63. return () => clearTimeout(timer);
  64. }, [countdown]);
  65. const handleSendCode = async () => {
  66. if (!email) return showMessage(t('settings.email_not_found') || '未检测到绑定邮箱', 'error');
  67. try {
  68. await authService.sendResetCode(email);
  69. showMessage(`${t('settings.code_sent') || '验证码已发送至'} ${email}`, 'success');
  70. setCountdown(60);
  71. } catch (error: any) {
  72. console.error(error);
  73. const msg = error.response?.data?.message || t('common.unknown_error');
  74. showMessage(`${t('settings.send_failed') || '发送失败'}: ${msg}`, 'error');
  75. }
  76. };
  77. const handleSubmit = async (e: React.FormEvent) => {
  78. e.preventDefault();
  79. if (!code) return showMessage(t('settings.enter_code') || '请输入验证码', 'error');
  80. if (!newPassword) return showMessage(t('settings.enter_password') || '请输入新密码', 'error');
  81. if (newPassword !== confirmPassword) return showMessage(t('settings.password_mismatch') || '两次输入的密码不一致', 'error');
  82. setLoading(true);
  83. try {
  84. await authService.resetPassword({
  85. email,
  86. code,
  87. new_password: newPassword
  88. });
  89. showMessage(t('settings.password_changed_success') || '密码重置成功!请重新登录。', 'success', () => {
  90. localStorage.removeItem('rsid');
  91. localStorage.removeItem('user_info');
  92. window.location.href = '/login';
  93. onClose();
  94. });
  95. } catch (error: any) {
  96. console.error(error);
  97. const msg = error.response?.data?.message || error.message || '重置失败';
  98. showMessage(`${t('settings.change_failed') || '密码修改失败'}: ${msg}`, 'error');
  99. } finally {
  100. setLoading(false);
  101. }
  102. };
  103. if (!isOpen) return null;
  104. return (
  105. <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-grayscale p-4">
  106. <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">
  107. {/* Header */}
  108. <div className="px-5 py-3.5 border-b-2 border-black flex justify-between items-center bg-neutral-200 text-black select-none">
  109. <div className="flex items-center gap-2 text-xs font-bold uppercase tracking-wider">
  110. <span className="w-2.5 h-2.5 bg-amber-300 border border-black inline-block" />
  111. <span>[ CHANGE PASSWORD // 重置凭证密码 ]</span>
  112. </div>
  113. <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">
  114. ✕
  115. </button>
  116. </div>
  117. <form onSubmit={handleSubmit} className="p-6 bg-white space-y-4">
  118. {/* 邮箱 & 验证码 */}
  119. <div className="space-y-2">
  120. <label className="block text-xs font-bold uppercase text-black">[ 01 // 验证码接收邮箱 ]</label>
  121. <input
  122. type="text"
  123. disabled
  124. value={email}
  125. className="w-full border-2 border-black bg-neutral-100 p-2.5 text-xs font-mono text-neutral-600 cursor-not-allowed"
  126. />
  127. <div className="flex gap-2 pt-1">
  128. <input
  129. type="text"
  130. required
  131. 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]"
  132. placeholder={t('settings.code_placeholder') || '------'}
  133. maxLength={6}
  134. value={code}
  135. onChange={(e) => setCode(e.target.value)}
  136. />
  137. <button
  138. type="button"
  139. disabled={countdown > 0}
  140. onClick={handleSendCode}
  141. 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"
  142. >
  143. {countdown > 0 ? `${countdown}s` : (t('settings.get_code') || '获取验证码')}
  144. </button>
  145. </div>
  146. </div>
  147. <div className="border-t-2 border-dashed border-neutral-300 my-2"></div>
  148. {/* 新密码 */}
  149. <div className="space-y-1">
  150. <label className="block text-xs font-bold uppercase text-black">[ 02 // 输入新密码 ]</label>
  151. <div className="relative">
  152. <input
  153. type={showPassword ? "text" : "password"}
  154. required
  155. 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"
  156. placeholder={t('settings.new_password_placeholder') || '设置新安全密码...'}
  157. value={newPassword}
  158. onChange={(e) => setNewPassword(e.target.value)}
  159. />
  160. <button
  161. type="button"
  162. onClick={() => setShowPassword(!showPassword)}
  163. className="absolute right-3 top-3 text-black"
  164. >
  165. {showPassword ? <EyeOff size={14}/> : <Eye size={14}/>}
  166. </button>
  167. </div>
  168. </div>
  169. {/* 确认密码 */}
  170. <div className="space-y-1">
  171. <label className="block text-xs font-bold uppercase text-black">[ 03 // 再次确认新密码 ]</label>
  172. <input
  173. type={showPassword ? "text" : "password"}
  174. required
  175. 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"
  176. placeholder={t('settings.confirm_password_placeholder') || '再次输入新密码...'}
  177. value={confirmPassword}
  178. onChange={(e) => setConfirmPassword(e.target.value)}
  179. />
  180. {confirmPassword && newPassword !== confirmPassword && (
  181. <p className="text-[10px] text-rose-600 font-bold mt-1">[ ! 两次输入的密码不一致 ]</p>
  182. )}
  183. </div>
  184. <div className="pt-3 flex justify-end gap-2.5 border-t-2 border-dashed border-neutral-300">
  185. <button
  186. type="button"
  187. onClick={onClose}
  188. 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"
  189. >
  190. [ {t('common.cancel') || '取消'} ]
  191. </button>
  192. <button
  193. type="submit"
  194. disabled={loading}
  195. 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"
  196. >
  197. {loading ? <Loader2 size={14} className="animate-spin" /> : <><Save size={13} /> <span>[ {t('common.confirm') || '确认重置'} ]</span></>}
  198. </button>
  199. </div>
  200. </form>
  201. </div>
  202. {/* 消息弹窗 */}
  203. <MessageModal
  204. isOpen={msgModal.isOpen}
  205. title={msgModal.title}
  206. message={msgModal.message}
  207. type={msgModal.type}
  208. onClose={handleCloseMsg}
  209. />
  210. </div>
  211. );
  212. }