security.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * @Author: your name
  3. * @Date: 2022-03-03 17:42:10
  4. * @LastEditTime: 2023-06-12 16:14:59
  5. * @LastEditors: code4eat awesomedema@gmail.com
  6. * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
  7. * @FilePath: /KC-MiddlePlatform/src/pages/personalCenter/components/security.tsx
  8. */
  9. import React, { useState } from 'react';
  10. import { List } from 'antd';
  11. import KCModal from '@/components/KCModal';
  12. import { ProFormText } from '@ant-design/pro-form';
  13. import { Form, Alert, Progress, notification, Modal } from 'antd';
  14. import PasswordQualityCalculator from 'password-quality-calculator';
  15. import { editUsers, editUsersPsd } from '@/service/user';
  16. import { useModel } from 'umi';
  17. type Unpacked<T> = T extends (infer U)[] ? U : T;
  18. const passwordStrength = {
  19. strong: <span className="strong">强</span>,
  20. medium: <span className="medium">中</span>,
  21. weak: <span className="weak">弱 Weak</span>,
  22. };
  23. const SecurityView: React.FC = () => {
  24. const getData = () => [
  25. {
  26. title: '账户密码',
  27. description: (
  28. <>
  29. 当前密码强度:
  30. {passwordStrength}
  31. </>
  32. ),
  33. actions: [
  34. <a key="Modify" onClick={() => resetPasswordHandle()}>
  35. 修改
  36. </a>,
  37. ],
  38. },
  39. // {
  40. // title: '密保手机',
  41. // description: `已绑定手机:138****8293`,
  42. // actions: [<a key="Modify">修改</a>],
  43. // },
  44. // {
  45. // title: '密保问题',
  46. // description: '未设置密保问题,密保问题可有效保护账户安全',
  47. // actions: [<a key="Set">设置</a>],
  48. // },
  49. // {
  50. // title: '备用邮箱',
  51. // description: `已绑定邮箱:ant***sign.com`,
  52. // actions: [<a key="Modify">修改</a>],
  53. // },
  54. // {
  55. // title: 'MFA 设备',
  56. // description: '未绑定 MFA 设备,绑定后,可以进行二次确认',
  57. // actions: [<a key="bind">绑定</a>],
  58. // },
  59. ];
  60. const { initialState, setInitialState } = useModel('@@initialState');
  61. const [modalVisible, setmodalVisible] = useState(false);
  62. const [passwordStrength, setpasswordStrength] = useState(0);
  63. const [showPasswordCompareTip, setshowPasswordCompareTip] = useState(false);
  64. const data = getData();
  65. const resetPasswordHandle = () => {
  66. setmodalVisible(true);
  67. };
  68. const onVisibleChangeHandle = (bool: boolean) => {
  69. setmodalVisible(bool);
  70. };
  71. const passwordChangeHandle = (e: React.ChangeEvent<HTMLInputElement>) => {
  72. // console.log(PasswordQualityCalculator(e.target.value));
  73. setpasswordStrength(PasswordQualityCalculator(e.target.value));
  74. };
  75. const onFinishhandle = async (values: any) => {
  76. const { newPassword, reNewPassword } = values;
  77. if (reNewPassword != newPassword) {
  78. setshowPasswordCompareTip(true);
  79. setTimeout(() => {
  80. setshowPasswordCompareTip(false);
  81. }, 2000);
  82. return false;
  83. }
  84. const userData = localStorage.getItem('userData');
  85. if (userData) {
  86. const resp = await editUsersPsd({
  87. id: JSON.parse(userData).userId,
  88. password: newPassword,
  89. });
  90. if (resp) {
  91. Modal.confirm({
  92. title: '密码修改成功,前往重新登录?',
  93. closable: false,
  94. okText:'确定',
  95. onOk: () => {
  96. if (initialState?.logout) {
  97. initialState.logout();
  98. }
  99. },
  100. cancelText: '',
  101. });
  102. return true;
  103. }
  104. } else {
  105. notification['error']({
  106. message: '用户信息获取错误!',
  107. });
  108. }
  109. };
  110. return (
  111. <div>
  112. <KCModal
  113. title="设置密码"
  114. width={600}
  115. visible={modalVisible}
  116. onVisibleChange={onVisibleChangeHandle}
  117. layout="horizontal"
  118. labelCol={{ span: 6 }}
  119. wrapperCol={{ span: 16 }}
  120. onFinish={(val) => onFinishhandle(val)}
  121. >
  122. <div style={{ position: 'relative', paddingTop: '24px' }}>
  123. <Alert
  124. message="两次输入密码不一致!"
  125. type="error"
  126. showIcon
  127. style={{
  128. position: 'absolute',
  129. marginBottom: '16px',
  130. top: showPasswordCompareTip ? -27 : -70,
  131. transition: 'all 0.3s ease-in',
  132. width: '100%',
  133. }}
  134. />
  135. <ProFormText.Password
  136. label="原密码"
  137. name="oldPassword"
  138. rules={[{ required: true, message: '这是必填项' }]}
  139. />
  140. <ProFormText.Password
  141. label="新密码"
  142. name="newPassword"
  143. rules={[{ required: true, message: '这是必填项' }]}
  144. fieldProps={{
  145. onChange: (e) => passwordChangeHandle(e),
  146. }}
  147. />
  148. <ProFormText.Password
  149. label="再次输入新密码"
  150. name="reNewPassword"
  151. rules={[{ required: true, message: '这是必填项' }]}
  152. />
  153. <Form.Item label="密码强度">
  154. <Progress
  155. percent={passwordStrength}
  156. steps={10}
  157. strokeColor={{ '0%': '#108ee9', '100%': '#87d068' }}
  158. />
  159. </Form.Item>
  160. </div>
  161. </KCModal>
  162. <List<Unpacked<typeof data>>
  163. itemLayout="horizontal"
  164. dataSource={data}
  165. renderItem={(item) => (
  166. <List.Item actions={item.actions} className="rightListItem">
  167. <List.Item.Meta title={item.title} description={item.description} />
  168. </List.Item>
  169. )}
  170. />
  171. </div>
  172. );
  173. };
  174. export default SecurityView;