Commit 06887645 authored by 邓超's avatar 邓超

fix: 添加用户密码添加强度验证

parent b9e31db7
Pipeline #60886 passed with stages
...@@ -2,6 +2,7 @@ import React, { useEffect, useState } from 'react'; ...@@ -2,6 +2,7 @@ import React, { useEffect, useState } from 'react';
import { Modal, Form, Input, notification, message, TreeSelect } from 'antd'; import { Modal, Form, Input, notification, message, TreeSelect } from 'antd';
import { addUser, GetUserRelationListNew } from '@/services/userManage/api'; import { addUser, GetUserRelationListNew } from '@/services/userManage/api';
import { ok } from '../../../assets/images/icons/ok.svg'; import { ok } from '../../../assets/images/icons/ok.svg';
import styles from './AddUserModal.less';
const { SHOW_CHILD } = TreeSelect; const { SHOW_CHILD } = TreeSelect;
const AddUserModal = props => { const AddUserModal = props => {
...@@ -15,7 +16,7 @@ const AddUserModal = props => { ...@@ -15,7 +16,7 @@ const AddUserModal = props => {
); // 邮箱 ); // 邮箱
const [value, setValue] = useState(['0-1-1']); const [value, setValue] = useState(['0-1-1']);
const [treeData, setTreeData] = useState([]); const [treeData, setTreeData] = useState([]);
const [passwordLevel, setPasswordLevel] = useState('弱'); // 密码等级
const onChange = newValue => { const onChange = newValue => {
console.log('onChange ', value); console.log('onChange ', value);
setValue(newValue); setValue(newValue);
...@@ -140,6 +141,50 @@ const AddUserModal = props => { ...@@ -140,6 +141,50 @@ const AddUserModal = props => {
}); });
} }
}; };
// 密码强度验证
const changeValue = changedFields => {
if (changedFields[0].name[0] === 'password') {
// console.log(changedFields[0].value);
checkStrong(changedFields[0].value);
// console.log(checkStrong(changedFields[0].value));
}
};
const checkStrong = sValue => {
let modes = 0;
// 正则表达式验证符合要求的
if (sValue.length < 1) return modes;
if (/\d/.test(sValue)) modes++; // 数字
if (/[a-z]/.test(sValue)) modes++; // 小写
if (/[A-Z]/.test(sValue)) modes++; // 大写
if (/[_\W]/.test(sValue)) modes++; // 特殊字符
console.log(modes, 'modes');
// 逻辑处理
// eslint-disable-next-line default-case
switch (modes) {
case 1:
setPasswordLevel('弱');
// return 1;
break;
case 2:
setPasswordLevel('弱');
break;
case 3:
if (sValue.length > 8) {
setPasswordLevel('中');
} else {
setPasswordLevel('弱');
}
break;
case 4:
if (sValue.length < 10) {
setPasswordLevel('中');
} else {
setPasswordLevel('强');
}
break;
}
};
const title = ( const title = (
<span> <span>
<span style={{ fontWeight: 'bold', color: 'rgb(24, 144, 255)' }}>{orgTitle1}</span> <span style={{ fontWeight: 'bold', color: 'rgb(24, 144, 255)' }}>{orgTitle1}</span>
...@@ -168,85 +213,88 @@ const AddUserModal = props => { ...@@ -168,85 +213,88 @@ const AddUserModal = props => {
destroyOnClose destroyOnClose
afterClose={() => { afterClose={() => {
addUserForm.resetFields(); addUserForm.resetFields();
setPasswordLevel('弱');
}} }}
onOk={submitAddUser} onOk={submitAddUser}
okText="确认" okText="确认"
cancelText="取消" cancelText="取消"
> >
<Form form={addUserForm} labelCol={{ span: 4 }}> <div className={styles.modalContent}>
<Form.Item <Form form={addUserForm} labelCol={{ span: 4 }} onFieldsChange={changeValue}>
hasFeedback <Form.Item
name="loginName" hasFeedback
label="登录名称" name="loginName"
rules={[ label="登录名称"
{ rules={[
pattern: /^[a-zA-Z0-9_]{0,}$/, {
message: '长度小于16位,支持字母与数字,允许下划线', pattern: /^[a-zA-Z0-9_]{0,}$/,
}, message: '长度小于16位,支持字母与数字,允许下划线',
{ required: true }, },
]} { required: true },
> ]}
<Input placeholder="请输入登录名称(小于16位)" maxLength="16" /> >
</Form.Item> <Input placeholder="请输入登录名称(小于16位)" maxLength="16" />
<Form.Item </Form.Item>
hasFeedback <Form.Item
name="password" hasFeedback
label="账号密码" name="password"
rules={[ label="账号密码"
{ rules={[
pattern: /^[a-zA-Z0-9_]{6,16}$/, {
message: '长度6-16位,支持字母与数字,允许下划线', pattern: /^[a-zA-Z0-9_]{6,16}$/,
}, message: '长度6-16位,支持字母与数字,允许下划线',
{ required: true }, },
]} { required: true },
> ]}
<Input placeholder="请输入账号密码(6~16位)" maxlength="16" /> >
</Form.Item> <Input placeholder="请输入账号密码(6~16位)" maxlength="16" suffix={passwordLevel} />
<Form.Item </Form.Item>
hasFeedback <Form.Item
name="userName" hasFeedback
label="用户姓名" name="userName"
rules={[ label="用户姓名"
{ required: true }, rules={[
{ { required: true },
pattern: /^[A-Za-z0-9_\u4e00-\u9fa5]+$/, {
message: '长度小于16位,支持字母、中文与数字,允许下划线', pattern: /^[A-Za-z0-9_\u4e00-\u9fa5]+$/,
}, message: '长度小于16位,支持字母、中文与数字,允许下划线',
]} },
> ]}
<Input placeholder="请输入用户姓名(小于16位)" maxLength="16" /> >
</Form.Item> <Input placeholder="请输入用户姓名(小于16位)" maxLength="16" />
<Form.Item </Form.Item>
hasFeedback <Form.Item
name="phone" hasFeedback
label="手机号码" name="phone"
rules={[ label="手机号码"
{ required: true }, rules={[
{ { required: true },
pattern: new RegExp(/^1[0-9]{10}$/), {
message: '请输入正确的手机号码!', pattern: new RegExp(/^1[0-9]{10}$/),
}, message: '请输入正确的手机号码!',
]} },
> ]}
<Input placeholder="请输入手机号码" maxlength="11" autoComplete="off" /> >
</Form.Item> <Input placeholder="请输入手机号码" maxlength="11" autoComplete="off" />
<Form.Item </Form.Item>
hasFeedback <Form.Item
name="email" hasFeedback
label="电子邮箱" name="email"
rules={[ label="电子邮箱"
{ rules={[
type: 'email', {
message: '请输入正确的电子邮箱!', type: 'email',
}, message: '请输入正确的电子邮箱!',
]} },
> ]}
<Input placeholder="请输入电子邮箱" autoComplete="off" /> >
</Form.Item> <Input placeholder="请输入电子邮箱" autoComplete="off" />
{/* <Form.Item name="stationList" label="关联站点" rules={[{ required: true }]}> </Form.Item>
{/* <Form.Item name="stationList" label="关联站点" rules={[{ required: true }]}>
<TreeSelect {...tProps} showSearch treeDefaultExpandAll /> <TreeSelect {...tProps} showSearch treeDefaultExpandAll />
</Form.Item> */} </Form.Item> */}
</Form> </Form>
</div>
</Modal> </Modal>
); );
}; };
......
.modalContent {
width: 100%;
height: 100%;
.ant-form-item-feedback-icon {
margin-left: 10px;
}
}
\ No newline at end of file
/* eslint-disable import/no-unresolved */ /* eslint-disable import/no-unresolved */
import React, { useEffect } from 'react'; import React, { useEffect, useState } from 'react';
import { Modal, Form, Input, notification, message } from 'antd'; import { Modal, Form, Input, notification, message } from 'antd';
import { updateUserPassword } from '@/services/userManage/api'; import { updateUserPassword } from '@/services/userManage/api';
const ChangePasswordModal = props => { const ChangePasswordModal = props => {
const { visible, currentUser, currentSelectOrg, submitSearchUser, onSelect, onCancel } = props; const { visible, currentUser, currentSelectOrg, submitSearchUser, onSelect, onCancel } = props;
const [passwordForm] = Form.useForm(); // 修改密码 const [passwordForm] = Form.useForm(); // 修改密码
const [newPasswordLevel, setNewPasswordLevel] = useState('弱');
const [passwordConfirmLevel, setPasswordConfirmLevel] = useState('弱');
useEffect(() => { useEffect(() => {
console.log(currentUser); console.log(currentUser);
if (visible) { if (visible) {
...@@ -80,6 +81,43 @@ const ChangePasswordModal = props => { ...@@ -80,6 +81,43 @@ const ChangePasswordModal = props => {
}); });
} }
}; };
const changeValue = changedFields => {
if (changedFields[0].name[0] === 'newPassword') {
setNewPasswordLevel(checkStrong(changedFields[0].value));
}
if (changedFields[0].name[0] === 'passwordConfirm') {
setPasswordConfirmLevel(checkStrong(changedFields[0].value));
}
};
const checkStrong = sValue => {
let modes = 0;
// 正则表达式验证符合要求的
if (sValue.length < 1) return modes;
if (/\d/.test(sValue)) modes++; // 数字
if (/[a-z]/.test(sValue)) modes++; // 小写
if (/[A-Z]/.test(sValue)) modes++; // 大写
if (/[_\W]/.test(sValue)) modes++; // 特殊字符
console.log(modes, 'modes');
// 逻辑处理
// eslint-disable-next-line default-case
switch (modes) {
case 1:
return '弱';
case 2:
return '弱';
case 3:
if (sValue.length > 8) {
return '中';
}
return '弱';
case 4:
if (sValue.length < 10) {
return '中';
}
return '强';
}
};
const title = ( const title = (
<span> <span>
修改用户 修改用户
...@@ -110,7 +148,7 @@ const ChangePasswordModal = props => { ...@@ -110,7 +148,7 @@ const ChangePasswordModal = props => {
okText="确认" okText="确认"
cancelText="取消" cancelText="取消"
> >
<Form form={passwordForm} labelCol={{ span: 4 }}> <Form form={passwordForm} labelCol={{ span: 4 }} onFieldsChange={changeValue}>
<Form.Item name="oldPassword" label="原始密码"> <Form.Item name="oldPassword" label="原始密码">
<Input disabled /> <Input disabled />
</Form.Item> </Form.Item>
...@@ -125,7 +163,12 @@ const ChangePasswordModal = props => { ...@@ -125,7 +163,12 @@ const ChangePasswordModal = props => {
{ required: true }, { required: true },
]} ]}
> >
<Input.Password placeholder="请输入新密码" autoComplete="off" maxLength="16" /> <Input
placeholder="请输入新密码"
autoComplete="off"
maxLength="16"
suffix={newPasswordLevel}
/>
</Form.Item> </Form.Item>
<Form.Item <Form.Item
name="passwordConfirm" name="passwordConfirm"
...@@ -138,7 +181,12 @@ const ChangePasswordModal = props => { ...@@ -138,7 +181,12 @@ const ChangePasswordModal = props => {
{ required: true }, { required: true },
]} ]}
> >
<Input.Password placeholder="再次确认新密码" autoComplete="off" maxLength="16" /> <Input
placeholder="再次确认新密码"
autoComplete="off"
maxLength="16"
suffix={passwordConfirmLevel}
/>
</Form.Item> </Form.Item>
</Form> </Form>
</Modal> </Modal>
......
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