来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
162 lines
5.2 KiB
TypeScript
162 lines
5.2 KiB
TypeScript
import React, { useState } from 'react';
|
|
import { useNavigate } from 'react-router-dom';
|
|
import { Loader2 } from 'lucide-react';
|
|
import { buildApiUrl } from '../lib/apiConfig';
|
|
import { readAuthSessionRaw } from '../lib/authSession';
|
|
import { useAuth } from '../hooks/useAuth';
|
|
import { notify } from '../utils/notify';
|
|
import { CHANGE_PASSWORD_HINT, validateChangePassword } from '../utils/passwordPolicy';
|
|
|
|
interface ChangePasswordFormProps {
|
|
onSuccess?: () => void;
|
|
onCancel?: () => void;
|
|
showCancel?: boolean;
|
|
showLogout?: boolean;
|
|
onLogout?: () => void;
|
|
subtitle?: string;
|
|
}
|
|
|
|
export const ChangePasswordForm: React.FC<ChangePasswordFormProps> = ({
|
|
onSuccess,
|
|
onCancel,
|
|
showCancel = false,
|
|
showLogout = false,
|
|
onLogout,
|
|
subtitle,
|
|
}) => {
|
|
const navigate = useNavigate();
|
|
const { logout } = useAuth();
|
|
const [currentPassword, setCurrentPassword] = useState('');
|
|
const [newPassword, setNewPassword] = useState('');
|
|
const [confirmPassword, setConfirmPassword] = useState('');
|
|
const [loading, setLoading] = useState(false);
|
|
const [error, setError] = useState('');
|
|
|
|
const handleSubmit = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
setError('');
|
|
|
|
const policyError = validateChangePassword(newPassword);
|
|
if (policyError) {
|
|
setError(policyError);
|
|
return;
|
|
}
|
|
if (newPassword === currentPassword) {
|
|
setError('新密码不能与当前密码相同');
|
|
return;
|
|
}
|
|
if (newPassword !== confirmPassword) {
|
|
setError('两次输入的新密码不一致');
|
|
return;
|
|
}
|
|
|
|
setLoading(true);
|
|
try {
|
|
const raw = readAuthSessionRaw();
|
|
const session = raw ? JSON.parse(raw) : null;
|
|
const response = await fetch(buildApiUrl('/auth/change-password'), {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
Authorization: session?.access_token ? `Bearer ${session.access_token}` : '',
|
|
},
|
|
body: JSON.stringify({
|
|
current_password: currentPassword,
|
|
new_password: newPassword,
|
|
}),
|
|
});
|
|
const payload = await response.json().catch(() => ({}));
|
|
if (!response.ok) {
|
|
throw new Error(payload?.error_description || '修改密码失败');
|
|
}
|
|
|
|
await logout();
|
|
notify('密码已修改,请重新登录');
|
|
onSuccess?.();
|
|
navigate('/login?reason=password_changed');
|
|
} catch (err: any) {
|
|
setError(err?.message || '修改密码失败');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<form onSubmit={handleSubmit} className="space-y-4">
|
|
{subtitle && <p className="app-foreground-muted text-sm">{subtitle}</p>}
|
|
|
|
{error && (
|
|
<div className="p-3 bg-red-50 dark:bg-red-950/30 border border-red-200 dark:border-red-800 rounded-lg text-red-600 dark:text-red-300 text-sm">
|
|
{error}
|
|
</div>
|
|
)}
|
|
|
|
<div>
|
|
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">当前密码</label>
|
|
<input
|
|
type="password"
|
|
value={currentPassword}
|
|
onChange={(e) => setCurrentPassword(e.target.value)}
|
|
className="app-input w-full px-4 py-2.5 border rounded-lg focus:ring-2 focus:ring-emerald-500 outline-none"
|
|
required
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">新密码</label>
|
|
<input
|
|
type="password"
|
|
value={newPassword}
|
|
onChange={(e) => setNewPassword(e.target.value)}
|
|
minLength={8}
|
|
className="app-input w-full px-4 py-2.5 border rounded-lg focus:ring-2 focus:ring-emerald-500 outline-none"
|
|
required
|
|
/>
|
|
<p className="app-foreground-muted text-xs mt-1">{CHANGE_PASSWORD_HINT}</p>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">确认新密码</label>
|
|
<input
|
|
type="password"
|
|
value={confirmPassword}
|
|
onChange={(e) => setConfirmPassword(e.target.value)}
|
|
minLength={8}
|
|
className="app-input w-full px-4 py-2.5 border rounded-lg focus:ring-2 focus:ring-emerald-500 outline-none"
|
|
required
|
|
/>
|
|
</div>
|
|
|
|
<div className="flex gap-3">
|
|
{showCancel && onCancel && (
|
|
<button
|
|
type="button"
|
|
onClick={onCancel}
|
|
className="app-border app-row-hover flex-1 py-2.5 border text-gray-700 dark:text-gray-300 rounded-lg"
|
|
>
|
|
取消
|
|
</button>
|
|
)}
|
|
<button
|
|
type="submit"
|
|
disabled={loading}
|
|
className="flex-1 py-2.5 bg-emerald-600 hover:bg-emerald-700 disabled:bg-gray-300 text-white rounded-lg font-medium flex items-center justify-center gap-2"
|
|
>
|
|
{loading ? <Loader2 className="w-5 h-5 animate-spin" /> : null}
|
|
确认修改
|
|
</button>
|
|
</div>
|
|
|
|
{showLogout && onLogout && (
|
|
<button
|
|
type="button"
|
|
onClick={onLogout}
|
|
className="app-foreground-muted w-full py-2 text-sm hover:text-gray-700 dark:hover:text-gray-200"
|
|
>
|
|
退出登录
|
|
</button>
|
|
)}
|
|
</form>
|
|
);
|
|
};
|