index.tsx
7.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// 个人资料:展示当前账号信息 + 修改密码。
// 账号信息取自 /system/auth/info(UserInfo);改密走 /system/auth/change-password,
// 该接口改后当前令牌失效,故成功后清除本地登录态并回到登录页。
import { useEffect, useState, type ReactNode } from 'react';
import { useNavigate } from 'react-router-dom';
import { App, Avatar, Tag } from 'antd';
import { UserOutlined } from '@ant-design/icons';
import { UCard, UForm, UInput, UButton, UAlert, UText, Skeleton } from '@uj/ui';
import { auth, type UserInfo } from '@/api';
import { useAppStore } from '@/store/useAppStore';
import { useAccess } from '@/access';
import { formatDateTime } from '@/utils';
interface PasswordForm {
oldPassword: string;
newPassword: string;
confirmPassword: string;
}
export default function Profile() {
const navigate = useNavigate();
const { message } = App.useApp();
const { logout } = useAppStore();
const { roleCodes } = useAccess();
const [info, setInfo] = useState<UserInfo | null>(null);
const [loading, setLoading] = useState(true);
const [submitting, setSubmitting] = useState(false);
const [form] = UForm.useForm<PasswordForm>();
useEffect(() => {
let alive = true;
auth
.info()
.then((data) => {
if (alive) setInfo(data);
})
.catch((e: unknown) => {
if (alive) {
message.error(e instanceof Error && e.message ? e.message : '获取账号信息失败');
}
})
.finally(() => {
if (alive) setLoading(false);
});
return () => {
alive = false;
};
}, [message]);
const onSubmit = async (values: PasswordForm) => {
setSubmitting(true);
try {
await auth.changePassword({
oldPassword: values.oldPassword,
newPassword: values.newPassword,
});
message.success('密码修改成功,请使用新密码重新登录');
// 令牌已失效,清本地登录态并回登录页
logout();
navigate('/login');
} catch (e: unknown) {
message.error(e instanceof Error && e.message ? e.message : '修改密码失败');
} finally {
setSubmitting(false);
}
};
if (loading) return <Skeleton variant="detail" />;
if (!info) return <UCard>{<UText type="secondary">暂无账号信息</UText>}</UCard>;
const codes = info.roleCodes?.length ? info.roleCodes : roleCodes;
const detailRows: { label: string; value: ReactNode }[] = [
{ label: '姓名', value: info.name },
{ label: '账号', value: info.account },
{ label: '手机号', value: info.mobile || '—' },
{
label: '角色',
value: codes.length ? (
<span className="uj-profile-roles">
{codes.map((c) => (
<Tag key={c} color="blue">
{c}
</Tag>
))}
</span>
) : (
'—'
),
},
{ label: '数据范围', value: info.dataScopeLabel || '—' },
];
const metaChips: { label: string; value: string }[] = [
{ label: '本次登录', value: formatDateTime(info.loginAt) },
{ label: '会话过期', value: formatDateTime(info.expireAt) },
];
return (
<div className="uj-profile">
{/* 顶部身份横幅(整宽) */}
<UCard
className="uj-profile-hero"
styles={{
body: { padding: 0, background: 'linear-gradient(135deg,#f3f7ff 0%,#eaf0ff 100%)' },
}}
>
<div className="uj-profile-hero-inner">
<div className="uj-profile-hero-id">
<Avatar size={64} className="uj-profile-avatar" icon={<UserOutlined />} />
<div>
<div className="uj-profile-name">{info.name}</div>
<UText type="secondary" className="uj-profile-account">
{info.account}
</UText>
</div>
</div>
<div className="uj-profile-hero-meta">
{metaChips.map((m) => (
<div key={m.label} className="uj-profile-chip">
<span className="uj-profile-chip-label">{m.label}</span>
<span className="uj-profile-chip-value">{m.value}</span>
</div>
))}
</div>
</div>
</UCard>
{/* 下方双栏(等高) */}
<div className="uj-profile-grid">
{/* 左:账户详情 */}
<UCard
className="uj-profile-info"
title={<span className="uj-profile-card-title">账户详情</span>}
styles={{ body: { padding: 20 } }}
>
<div className="uj-profile-rows">
{detailRows.map((r) => (
<div key={r.label} className="uj-profile-row">
<span className="uj-profile-label">{r.label}</span>
<span className="uj-profile-value">{r.value}</span>
</div>
))}
</div>
<div className="uj-profile-note">
账号信息由系统管理员维护,如需变更请联系管理员处理。
</div>
</UCard>
{/* 右:修改密码 */}
<UCard
className="uj-profile-security"
title={<span className="uj-profile-card-title">修改密码</span>}
styles={{ body: { padding: 20 } }}
>
<UAlert
type="info"
showIcon
message="修改成功后当前登录令牌失效,需使用新密码重新登录。"
style={{ marginBottom: 18 }}
/>
<UForm form={form} layout="vertical" onFinish={onSubmit} style={{ maxWidth: 380 }}>
<UForm.Item
name="oldPassword"
label="当前密码"
rules={[{ required: true, message: '请输入当前密码' }]}
>
<UInput.Password placeholder="请输入当前密码" autoComplete="current-password" />
</UForm.Item>
<UForm.Item
name="newPassword"
label="新密码"
rules={[
{ required: true, message: '请输入新密码' },
{ min: 8, message: '新密码至少 8 位' },
({ getFieldValue }) => ({
validator(_, v: string) {
if (!v || v !== getFieldValue('oldPassword')) return Promise.resolve();
return Promise.reject(new Error('新密码不能与当前密码相同'));
},
}),
]}
>
<UInput.Password placeholder="至少 8 位" autoComplete="new-password" />
</UForm.Item>
<UForm.Item
name="confirmPassword"
label="确认新密码"
dependencies={['newPassword']}
rules={[
{ required: true, message: '请再次输入新密码' },
({ getFieldValue }) => ({
validator(_, v: string) {
if (!v || v === getFieldValue('newPassword')) return Promise.resolve();
return Promise.reject(new Error('两次输入的密码不一致'));
},
}),
]}
>
<UInput.Password placeholder="请再次输入新密码" autoComplete="new-password" />
</UForm.Item>
<UButton htmlType="submit" loading={submitting}>
确认修改
</UButton>
</UForm>
</UCard>
</div>
</div>
);
}