ChangePasswordModal.tsx 9.6 KB

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