index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import React, { useRef, useState } from 'react';
  2. import { connect } from 'umi';
  3. import { Form, Select, Input, Button, message } from 'antd';
  4. import { queryDepList } from '@/Project/services/user';
  5. import styles from './index.less';
  6. import { UserOutlined, LockOutlined } from '@ant-design/icons';
  7. const { Option } = Select;
  8. function Login(props) {
  9. const {
  10. submitting,
  11. params,
  12. dispatch,
  13. initState: { refresh },
  14. } = props;
  15. const [depList, setDepList] = useState([]);
  16. const [hasName, setHasName] = useState(false);
  17. const formRef = useRef();
  18. const onHandleChange = async (name) => {
  19. if (!name) {
  20. setDepList([]);
  21. setHasName(false);
  22. return;
  23. }
  24. try {
  25. var res = await queryDepList(name);
  26. setDepList(res.data);
  27. setHasName(true);
  28. formRef.current.setFieldsValue({
  29. DepId: res.data[0]?.ID + '',
  30. });
  31. } catch (error) {
  32. setDepList([]);
  33. setHasName(true);
  34. message.error('用户名不存在');
  35. }
  36. };
  37. const handleSubmit = (values) => {
  38. dispatch({
  39. type: 'login/login',
  40. payload: {
  41. ...values,
  42. // 防止浏览器自动填充
  43. password: values.password2,
  44. },
  45. callback: (token) => {
  46. params.login(token);
  47. refresh();
  48. },
  49. });
  50. };
  51. return (
  52. <div className={styles.main}>
  53. <div className={styles.content}>
  54. <Form
  55. name="basic"
  56. className={styles.inp_box}
  57. labelCol={{ span: 0 }}
  58. wrapperCol={{ span: 24 }}
  59. onFinish={handleSubmit}
  60. autoComplete="new-password"
  61. ref={formRef}
  62. >
  63. <Form.Item
  64. label=""
  65. name="username"
  66. autoComplete="off"
  67. rules={[{ required: true, message: '请输入用户名' }]}
  68. >
  69. <Input
  70. prefix={<UserOutlined className={styles.prefixIcon} />}
  71. className={styles.inp}
  72. size="large"
  73. autoComplete="off"
  74. placeholder="请输入用户名"
  75. onBlur={(e) => onHandleChange(e.target.value)}
  76. />
  77. </Form.Item>
  78. {hasName && (
  79. <Form.Item
  80. label=""
  81. name="DepId"
  82. rules={[{ required: true, message: '请选择部门' }]}
  83. >
  84. <Select placeholder="请选择部门" size="large">
  85. {depList.map((item) => (
  86. <Option value={item.ID + ''} key={item.ID}>
  87. {item.Name}
  88. </Option>
  89. ))}
  90. </Select>
  91. </Form.Item>
  92. )}
  93. <Form.Item
  94. label=""
  95. // 不使用password,防止浏览器自动填充表单
  96. name="password2"
  97. className={styles.inp}
  98. rules={[{ required: true, message: '请输入密码' }]}
  99. >
  100. <Input.Password
  101. prefix={<LockOutlined className={styles.prefixIcon} />}
  102. className={styles.inp}
  103. size="large"
  104. placeholder="请输入密码"
  105. autoComplete="new-password"
  106. />
  107. </Form.Item>
  108. {/* <Form.Item
  109. name="remember"
  110. valuePropName="checked"
  111. wrapperCol={{ offset: 8, span: 16 }}
  112. >
  113. <Checkbox>自动登录</Checkbox>
  114. </Form.Item> */}
  115. <Form.Item>
  116. <Button
  117. size="large"
  118. style={{ width: '100%', marginTop: 24 }}
  119. loading={submitting}
  120. type="primary"
  121. htmlType="submit"
  122. >
  123. 登录
  124. </Button>
  125. </Form.Item>
  126. </Form>
  127. </div>
  128. </div>
  129. );
  130. }
  131. export default connect(({ login, loading }) => ({
  132. login,
  133. submitting: loading.effects['login/login'],
  134. }))(Login);