Unverified Commit c2795d67 authored by czy868's avatar czy868 Committed by GitHub

[Feature-787][dlink-web] user can change password (#1414)

* -- 1209新增用户修改密码功能(前端) * -- 删除无用代码(前端) Co-authored-by: 's avatarcaizhengyu <caizhengyu@qudian.com>
parent 33a0c248
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
*/ */
import React, {useCallback, useRef} from 'react'; import React, { useCallback, useRef, useState } from 'react';
import { import {
LogoutOutlined, LogoutOutlined,
SafetyOutlined, SafetyOutlined,
...@@ -26,14 +26,16 @@ import { ...@@ -26,14 +26,16 @@ import {
SettingOutlined, SettingOutlined,
UserSwitchOutlined UserSwitchOutlined
} from '@ant-design/icons'; } from '@ant-design/icons';
import {Avatar, Menu, Modal, Spin} from 'antd'; import { Avatar, Menu, Modal, Spin } from 'antd';
import {history, useModel} from 'umi'; import { handleOption } from "@/components/Common/crud";
import {stringify} from 'querystring'; import { history, useModel } from 'umi';
import { stringify } from 'querystring';
import HeaderDropdown from '../HeaderDropdown'; import HeaderDropdown from '../HeaderDropdown';
import styles from './index.less'; import styles from './index.less';
import {outLogin} from '@/services/ant-design-pro/api'; import { outLogin } from '@/services/ant-design-pro/api';
import {ActionType} from "@ant-design/pro-table"; import { ActionType } from "@ant-design/pro-table";
import {l} from "@/utils/intl"; import { l } from "@/utils/intl";
import PasswordForm from "@/pages/AuthenticationCenter/UserManager/components/PasswordForm";
export type GlobalHeaderRightProps = { export type GlobalHeaderRightProps = {
menu?: boolean; menu?: boolean;
...@@ -44,8 +46,8 @@ export type GlobalHeaderRightProps = { ...@@ -44,8 +46,8 @@ export type GlobalHeaderRightProps = {
*/ */
const loginOut = async () => { const loginOut = async () => {
await outLogin(); await outLogin();
const {query = {}, pathname} = history.location; const { query = {}, pathname } = history.location;
const {redirect} = query; const { redirect } = query;
// Note: There may be security issues, please note // Note: There may be security issues, please note
if (window.location.pathname !== '/user/login' && !redirect) { if (window.location.pathname !== '/user/login' && !redirect) {
history.replace({ history.replace({
...@@ -60,11 +62,14 @@ const loginOut = async () => { ...@@ -60,11 +62,14 @@ const loginOut = async () => {
const requestUrl = '/api/tenant/switchTenant'; const requestUrl = '/api/tenant/switchTenant';
const url = '/api/user';
const AvatarDropdown: React.FC<GlobalHeaderRightProps> = ({menu}) => { const AvatarDropdown: React.FC<GlobalHeaderRightProps> = ({ menu }) => {
const {initialState, setInitialState} = useModel('@@initialState'); const { initialState, setInitialState } = useModel('@@initialState');
const actionRef = useRef<ActionType>(); const actionRef = useRef<ActionType>();
const [formValues, setFormValues] = useState({});
const [passwordModalVisible, handlePasswordModalVisible] = useState<boolean>(false);
const onMenuClick = useCallback( const onMenuClick = useCallback(
(event: { (event: {
key: React.Key; key: React.Key;
...@@ -72,11 +77,16 @@ const AvatarDropdown: React.FC<GlobalHeaderRightProps> = ({menu}) => { ...@@ -72,11 +77,16 @@ const AvatarDropdown: React.FC<GlobalHeaderRightProps> = ({menu}) => {
item: React.ReactInstance; item: React.ReactInstance;
domEvent: React.MouseEvent<HTMLElement>; domEvent: React.MouseEvent<HTMLElement>;
}) => { }) => {
const {key} = event; const { key } = event;
if (key === 'logout' && initialState) { if (key === 'logout' && initialState) {
setInitialState({...initialState, currentUser: undefined}); setInitialState({ ...initialState, currentUser: undefined });
loginOut(); loginOut();
return; return;
} else if (key === 'changePassWord') {
handlePasswordModalVisible(true);
setFormValues({ username: initialState?.currentUser?.username })
} else {
} }
// history.push(`/account/${key}`); // history.push(`/account/${key}`);
}, },
...@@ -99,7 +109,7 @@ const AvatarDropdown: React.FC<GlobalHeaderRightProps> = ({menu}) => { ...@@ -99,7 +109,7 @@ const AvatarDropdown: React.FC<GlobalHeaderRightProps> = ({menu}) => {
return loading; return loading;
} }
const {currentUser} = initialState; const { currentUser } = initialState;
if (!currentUser || !currentUser.username) { if (!currentUser || !currentUser.username) {
return loading; return loading;
...@@ -115,7 +125,7 @@ const AvatarDropdown: React.FC<GlobalHeaderRightProps> = ({menu}) => { ...@@ -115,7 +125,7 @@ const AvatarDropdown: React.FC<GlobalHeaderRightProps> = ({menu}) => {
disabled={item.id === currentUser.currentTenant?.id} disabled={item.id === currentUser.currentTenant?.id}
key={item.id} key={item.id}
title={item.tenantCode} title={item.tenantCode}
icon={<SecurityScanOutlined/>} icon={<SecurityScanOutlined />}
onClick={(e) => { onClick={(e) => {
// get choose tenant title // get choose tenant title
let title: string = e.domEvent.target.textContent; let title: string = e.domEvent.target.textContent;
...@@ -123,7 +133,7 @@ const AvatarDropdown: React.FC<GlobalHeaderRightProps> = ({menu}) => { ...@@ -123,7 +133,7 @@ const AvatarDropdown: React.FC<GlobalHeaderRightProps> = ({menu}) => {
let tenantInfoId = e.key; let tenantInfoId = e.key;
Modal.confirm({ Modal.confirm({
title: l('menu.account.checkTenant'), title: l('menu.account.checkTenant'),
content: l('menu.account.checkTenantConfirm', '', {tenantCode: title}), content: l('menu.account.checkTenantConfirm', '', { tenantCode: title }),
okText: l('button.confirm'), okText: l('button.confirm'),
cancelText: l('button.cancel'), cancelText: l('button.cancel'),
onOk: async () => { onOk: async () => {
...@@ -152,7 +162,7 @@ const AvatarDropdown: React.FC<GlobalHeaderRightProps> = ({menu}) => { ...@@ -152,7 +162,7 @@ const AvatarDropdown: React.FC<GlobalHeaderRightProps> = ({menu}) => {
<Menu.SubMenu <Menu.SubMenu
key="chooseTenantList" key="chooseTenantList"
title={l('menu.account.checkTenant')} title={l('menu.account.checkTenant')}
icon={<UserSwitchOutlined/>} icon={<UserSwitchOutlined />}
> >
{chooseTenantList} {chooseTenantList}
</Menu.SubMenu> </Menu.SubMenu>
...@@ -167,30 +177,48 @@ const AvatarDropdown: React.FC<GlobalHeaderRightProps> = ({menu}) => { ...@@ -167,30 +177,48 @@ const AvatarDropdown: React.FC<GlobalHeaderRightProps> = ({menu}) => {
)} )}
{menu && ( {menu && (
<Menu.Item key="personSettings" disabled> <Menu.Item key="personSettings" disabled>
<SettingOutlined/> <SettingOutlined />
{l('menu.account.settings')} {l('menu.account.settings')}
</Menu.Item> </Menu.Item>
)} )}
{menu && ( {menu && (
<Menu.Item key="changePassWord" disabled> <Menu.Item key="changePassWord">
<SafetyOutlined/> <SafetyOutlined />
{l('menu.account.changePassword')} {l('menu.account.changePassword')}
</Menu.Item> </Menu.Item>
)} )}
{menu && <Menu.Divider/>} {menu && <Menu.Divider />}
<Menu.Item key="logout"> <Menu.Item key="logout">
<LogoutOutlined/> <LogoutOutlined />
{l('menu.account.logout')} {l('menu.account.logout')}
</Menu.Item> </Menu.Item>
</Menu> </Menu>
); );
return ( return (
<HeaderDropdown overlay={menuHeaderDropdown}> <div>
<span className={`${styles.action} ${styles.account}`}> <HeaderDropdown overlay={menuHeaderDropdown}>
<Avatar size="small" className={styles.avatar} src={currentUser.avatar} alt="avatar"/> <span className={`${styles.action} ${styles.account}`}>
<span className={`${styles.name} anticon`}>{currentUser.username}</span> <Avatar size="small" className={styles.avatar} src={currentUser.avatar} alt="avatar" />
</span> <span className={`${styles.name} anticon`}>{currentUser.username}</span>
</HeaderDropdown> </span>
</HeaderDropdown>
{formValues && Object.keys(formValues).length ? (
<PasswordForm
onSubmit={async (value) => {
const success = await handleOption(url + "/modifyPassword", l('button.changePassword'), value);
if (success) {
handlePasswordModalVisible(false);
setFormValues({});
}
}}
onCancel={() => {
handlePasswordModalVisible(false);
}}
modalVisible={passwordModalVisible}
values={formValues}
/>
) : null}
</div>
); );
}; };
......
...@@ -18,10 +18,10 @@ ...@@ -18,10 +18,10 @@
*/ */
import React, {useState} from 'react'; import React, { useState } from 'react';
import {Button, Form, Input, Modal} from 'antd'; import { Button, Form, Input, Modal } from 'antd';
import {PasswordItem} from "@/pages/AuthenticationCenter/data.d"; import { PasswordItem } from "@/pages/AuthenticationCenter/data.d";
import {l} from "@/utils/intl"; import { l } from "@/utils/intl";
export type PasswordFormProps = { export type PasswordFormProps = {
onCancel: (flag?: boolean) => void; onCancel: (flag?: boolean) => void;
...@@ -31,8 +31,8 @@ export type PasswordFormProps = { ...@@ -31,8 +31,8 @@ export type PasswordFormProps = {
}; };
const formLayout = { const formLayout = {
labelCol: {span: 7}, labelCol: { span: 7 },
wrapperCol: {span: 13}, wrapperCol: { span: 13 },
}; };
const PasswordForm: React.FC<PasswordFormProps> = (props) => { const PasswordForm: React.FC<PasswordFormProps> = (props) => {
...@@ -51,8 +51,8 @@ const PasswordForm: React.FC<PasswordFormProps> = (props) => { ...@@ -51,8 +51,8 @@ const PasswordForm: React.FC<PasswordFormProps> = (props) => {
const submitForm = async () => { const submitForm = async () => {
const fieldsValue = await form.validateFields(); const fieldsValue = await form.validateFields();
setFormVals({...formVals, ...fieldsValue}); setFormVals({ ...formVals, ...fieldsValue });
handleSubmit({...formVals, ...fieldsValue}); handleSubmit({ ...formVals, ...fieldsValue });
}; };
const renderContent = () => { const renderContent = () => {
...@@ -62,15 +62,15 @@ const PasswordForm: React.FC<PasswordFormProps> = (props) => { ...@@ -62,15 +62,15 @@ const PasswordForm: React.FC<PasswordFormProps> = (props) => {
name="password" name="password"
label={l('pages.user.UserOldPassword')} label={l('pages.user.UserOldPassword')}
hasFeedback hasFeedback
rules={[{required: true, message: l('pages.user.UserEnterOldPassword')}]}> rules={[{ required: true, message: l('pages.user.UserEnterOldPassword') }]}>
<Input.Password placeholder={l('pages.user.UserEnterOldPassword')}/> <Input.Password placeholder={l('pages.user.UserEnterOldPassword')} />
</Form.Item> </Form.Item>
<Form.Item <Form.Item
name="newPassword" name="newPassword"
label={l('pages.user.UserNewPassword')} label={l('pages.user.UserNewPassword')}
hasFeedback hasFeedback
rules={[{required: true, message: l('pages.user.UserEnterNewPassword')}]}> rules={[{ required: true, message: l('pages.user.UserEnterNewPassword') }]}>
<Input.Password placeholder={l('pages.user.UserEnterNewPassword')}/> <Input.Password placeholder={l('pages.user.UserEnterNewPassword')} />
</Form.Item> </Form.Item>
<Form.Item <Form.Item
name="newPasswordCheck" name="newPasswordCheck"
...@@ -82,7 +82,7 @@ const PasswordForm: React.FC<PasswordFormProps> = (props) => { ...@@ -82,7 +82,7 @@ const PasswordForm: React.FC<PasswordFormProps> = (props) => {
required: true, required: true,
message: l('pages.user.UserNewPasswordNotMatch'), message: l('pages.user.UserNewPasswordNotMatch'),
}, },
({getFieldValue}) => ({ ({ getFieldValue }) => ({
validator(_, value) { validator(_, value) {
if (!value || getFieldValue('newPassword') === value) { if (!value || getFieldValue('newPassword') === value) {
return Promise.resolve(); return Promise.resolve();
...@@ -91,7 +91,7 @@ const PasswordForm: React.FC<PasswordFormProps> = (props) => { ...@@ -91,7 +91,7 @@ const PasswordForm: React.FC<PasswordFormProps> = (props) => {
}, },
}), }),
]}> ]}>
<Input.Password placeholder={l('pages.user.UserEnterRepeatNewPassword')}/> <Input.Password placeholder={l('pages.user.UserEnterRepeatNewPassword')} />
</Form.Item> </Form.Item>
</> </>
); );
...@@ -111,7 +111,7 @@ const PasswordForm: React.FC<PasswordFormProps> = (props) => { ...@@ -111,7 +111,7 @@ const PasswordForm: React.FC<PasswordFormProps> = (props) => {
return ( return (
<Modal <Modal
width={"40%"} width={"40%"}
bodyStyle={{padding: '32px 40px 48px'}} bodyStyle={{ padding: '32px 40px 48px' }}
destroyOnClose destroyOnClose
title={l('button.changePassword')} title={l('button.changePassword')}
visible={modalVisible} visible={modalVisible}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment