Login.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import React, { Component, Fragment } from 'react';
  2. import { connect } from 'dva';
  3. import { formatMessage, FormattedMessage } from 'umi/locale';
  4. import { CopyrightOutlined } from '@ant-design/icons';
  5. import { Form } from '@ant-design/compatible';
  6. import '@ant-design/compatible/assets/index.css';
  7. import { Checkbox, Alert, Select } from 'antd';
  8. import Login from '@/components/Login';
  9. import { clearToken } from '@/utils/utils';
  10. import Link from 'umi/link';
  11. import GlobalFooter from '@/components/GlobalFooter';
  12. import DocumentTitle from 'react-document-title';
  13. import logo from '@/assets/logo.png';
  14. import { queryDepV2 } from '@/services/api';
  15. import styles from './Login.less';
  16. const { Option } = Select;
  17. const { Tab, UserName, Password, Submit } = Login;
  18. const links = [
  19. {
  20. key: 'help',
  21. title: formatMessage({ id: 'layout.user.link.help' }),
  22. href: '',
  23. },
  24. {
  25. key: 'privacy',
  26. title: formatMessage({ id: 'layout.user.link.privacy' }),
  27. href: '',
  28. },
  29. {
  30. key: 'terms',
  31. title: formatMessage({ id: 'layout.user.link.terms' }),
  32. href: '',
  33. },
  34. ];
  35. const copyright = (
  36. <Fragment>
  37. Copyright <CopyrightOutlined /> 金科环境股份有限公司
  38. </Fragment>
  39. );
  40. @connect(({ login, loading }) => ({
  41. login,
  42. submitting: loading.effects['purchaseLogin/login'],
  43. }))
  44. @Form.create()
  45. class LoginPage extends Component {
  46. state = {
  47. type: 'account',
  48. loginType: localStorage.type,
  49. autoLogin: true,
  50. depList: [],
  51. userName: '',
  52. depId: undefined,
  53. };
  54. componentDidMount = () => {
  55. clearToken();
  56. };
  57. onTabChange = type => {
  58. this.setState({ type });
  59. };
  60. onHandleBLur = async name => {
  61. const { dispatch } = this.props;
  62. const { userName } = this.state;
  63. if (!name || name == userName) return;
  64. var res = await queryDepV2({
  65. userName: name,
  66. });
  67. if (res) {
  68. this.setState({
  69. userName: name,
  70. depList: res.data,
  71. depId: undefined,
  72. });
  73. }
  74. };
  75. handleSubmit = (err, values) => {
  76. const { type, depId } = this.state;
  77. const {
  78. login,
  79. submitting,
  80. location: {
  81. query: { referrer },
  82. },
  83. } = this.props;
  84. if (!depId) {
  85. message.error('请选择部门');
  86. return;
  87. }
  88. if (!err) {
  89. const { dispatch } = this.props;
  90. dispatch({
  91. type: 'purchaseLogin/login',
  92. payload: {
  93. ...values,
  94. type,
  95. DepId: depId + '',
  96. referrer: referrer && decodeURIComponent(decodeURIComponent(referrer)),
  97. },
  98. });
  99. }
  100. };
  101. changeAutoLogin = e => {
  102. this.setState({
  103. autoLogin: e.target.checked,
  104. });
  105. };
  106. changeDep = value => {
  107. this.setState({
  108. depId: value,
  109. });
  110. };
  111. renderMessage = content => (
  112. <Alert style={{ marginBottom: 24 }} message={content} type="error" showIcon />
  113. );
  114. render() {
  115. const {
  116. login,
  117. submitting,
  118. location: {
  119. query: { referrer },
  120. },
  121. } = this.props;
  122. const { type, autoLogin, depList, depId, loginType } = this.state;
  123. console.log(referrer);
  124. return (
  125. <DocumentTitle title={`登录-人日`}>
  126. <div className={styles.container}>
  127. <div className={styles.content}>
  128. <div className={styles.main}>
  129. <Login
  130. defaultActiveKey={type}
  131. onTabChange={this.onTabChange}
  132. onSubmit={this.handleSubmit}
  133. ref={form => {
  134. this.loginForm = form;
  135. }}
  136. >
  137. {login.status === 'error' &&
  138. login.type === 'account' &&
  139. !submitting &&
  140. this.renderMessage(
  141. formatMessage({ id: 'app.login.message-invalid-credentials' })
  142. )}
  143. <div className={styles.inp_box}>
  144. <UserName
  145. name="UserName"
  146. className={styles.inp}
  147. placeholder="请输入用户名"
  148. rules={[
  149. {
  150. required: true,
  151. message: formatMessage({ id: 'validation.userName.required' }),
  152. },
  153. ]}
  154. onBlur={e => {
  155. this.onHandleBLur(e.target.value);
  156. }}
  157. />
  158. <Select
  159. value={depId}
  160. onChange={this.changeDep}
  161. style={{ marginBottom: 20 }}
  162. placeholder="请选择部门"
  163. >
  164. {depList.map(item => (
  165. <Option value={item.ID} key={item.ID}>
  166. {item.Name}
  167. </Option>
  168. ))}
  169. </Select>
  170. <Password
  171. name="Password"
  172. className={styles.inp}
  173. placeholder="请输入密码"
  174. rules={[
  175. {
  176. required: true,
  177. message: formatMessage({ id: 'validation.password.required' }),
  178. },
  179. ]}
  180. onPressEnter={e => {
  181. e.preventDefault();
  182. this.loginForm.validateFields(this.handleSubmit);
  183. }}
  184. />
  185. {/* <div>
  186. <Checkbox checked={autoLogin} onChange={this.changeAutoLogin}>
  187. <FormattedMessage id="app.login.remember-me" />
  188. </Checkbox>
  189. <a style={{ float: 'right' }} href="">
  190. <FormattedMessage id="app.login.forgot-password" />
  191. </a>
  192. </div> */}
  193. <Submit loading={submitting}>
  194. <FormattedMessage id="app.login.login" />
  195. </Submit>
  196. </div>
  197. </Login>
  198. </div>
  199. </div>
  200. <GlobalFooter links={links} copyright={copyright} />
  201. </div>
  202. </DocumentTitle>
  203. );
  204. }
  205. }
  206. export default LoginPage;