修复:管理员默认商户与账户设置
This commit is contained in:
@@ -40,6 +40,8 @@ export const authApi = {
|
||||
},
|
||||
changePassword: (data: { current_password: string; new_password: string }) =>
|
||||
request.put('/auth/password', data).then((r) => r.data.data),
|
||||
updateCurrentAccount: (data: { current_password: string; username: string; nickname?: string; new_password?: string }) =>
|
||||
request.put('/auth/account', data).then((r) => r.data.data as User),
|
||||
}
|
||||
|
||||
export const dashboardApi = {
|
||||
|
||||
@@ -219,7 +219,7 @@ export default function MainLayout() {
|
||||
const [collapsed, setCollapsed] = useState<boolean>(() => {
|
||||
return localStorage.getItem(SIDEBAR_COLLAPSED_KEY) === 'true'
|
||||
})
|
||||
const { user, logout, isAdmin, merchant, merchantPermissions } = useAuth()
|
||||
const { user, logout, refreshProfile, isAdmin, merchant, merchantPermissions } = useAuth()
|
||||
const [passwordOpen, setPasswordOpen] = useState(false)
|
||||
const [passwordSaving, setPasswordSaving] = useState(false)
|
||||
const [passwordForm] = Form.useForm()
|
||||
@@ -281,20 +281,22 @@ export default function MainLayout() {
|
||||
navigate(child.path)
|
||||
}
|
||||
|
||||
const submitPasswordChange = async () => {
|
||||
const submitAccountUpdate = async () => {
|
||||
try {
|
||||
const values = await passwordForm.validateFields()
|
||||
setPasswordSaving(true)
|
||||
await authApi.changePassword({
|
||||
await authApi.updateCurrentAccount({
|
||||
current_password: values.current_password,
|
||||
new_password: values.new_password,
|
||||
username: values.username,
|
||||
nickname: values.nickname || '',
|
||||
new_password: values.new_password || undefined,
|
||||
})
|
||||
await refreshProfile()
|
||||
setPasswordOpen(false)
|
||||
passwordForm.resetFields()
|
||||
logout()
|
||||
navigate('/login')
|
||||
message.success('账户设置已保存')
|
||||
} catch (e) {
|
||||
message.error(e instanceof Error ? e.message : '密码修改失败')
|
||||
message.error(e instanceof Error ? e.message : '保存失败')
|
||||
} finally {
|
||||
setPasswordSaving(false)
|
||||
}
|
||||
@@ -313,10 +315,13 @@ export default function MainLayout() {
|
||||
},
|
||||
{ type: 'divider' },
|
||||
{
|
||||
key: 'change-password',
|
||||
key: 'account-settings',
|
||||
icon: <KeyOutlined />,
|
||||
label: '修改密码',
|
||||
onClick: () => setPasswordOpen(true),
|
||||
label: '账户设置',
|
||||
onClick: () => {
|
||||
passwordForm.setFieldsValue({ username: user?.username || '', nickname: user?.nickname || '' })
|
||||
setPasswordOpen(true)
|
||||
},
|
||||
},
|
||||
{
|
||||
key: 'logout',
|
||||
@@ -454,10 +459,10 @@ export default function MainLayout() {
|
||||
</div>
|
||||
</Content>
|
||||
<Modal
|
||||
title="修改密码"
|
||||
title="账户设置"
|
||||
open={passwordOpen}
|
||||
confirmLoading={passwordSaving}
|
||||
onOk={submitPasswordChange}
|
||||
onOk={submitAccountUpdate}
|
||||
onCancel={() => {
|
||||
setPasswordOpen(false)
|
||||
passwordForm.resetFields()
|
||||
@@ -465,14 +470,19 @@ export default function MainLayout() {
|
||||
destroyOnClose
|
||||
>
|
||||
<Form form={passwordForm} layout="vertical" style={{ marginTop: 16 }}>
|
||||
<Form.Item name="username" label="用户名" rules={[{ required: true, message: '请输入用户名' }, { min: 3, message: '用户名至少 3 位' }, { max: 64, message: '用户名不能超过 64 位' }]}>
|
||||
<Input autoComplete="username" />
|
||||
</Form.Item>
|
||||
<Form.Item name="nickname" label="昵称" rules={[{ max: 64, message: '昵称不能超过 64 位' }]}>
|
||||
<Input autoComplete="off" />
|
||||
</Form.Item>
|
||||
<Form.Item name="current_password" label="当前密码" rules={[{ required: true, message: '请输入当前密码' }]}>
|
||||
<Input.Password autoComplete="current-password" />
|
||||
</Form.Item>
|
||||
<Form.Item name="new_password" label="新密码" rules={[{ required: true, min: 8, message: '新密码至少 8 位' }]}>
|
||||
<Form.Item name="new_password" label="新密码" rules={[{ min: 8, message: '新密码至少 8 位' }]} extra="留空表示不修改密码">
|
||||
<Input.Password autoComplete="new-password" />
|
||||
</Form.Item>
|
||||
<Form.Item name="confirm_password" label="确认新密码" dependencies={['new_password']} rules={[
|
||||
{ required: true, message: '请再次输入新密码' },
|
||||
({ getFieldValue }) => ({ validator: (_, value) => !value || getFieldValue('new_password') === value ? Promise.resolve() : Promise.reject(new Error('两次输入的密码不一致')) }),
|
||||
]}>
|
||||
<Input.Password autoComplete="new-password" />
|
||||
|
||||
Reference in New Issue
Block a user