index.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import React, { ChangeEvent, FormEvent } from 'react'
  21. import Helmet from 'react-helmet'
  22. import { connect } from 'react-redux'
  23. import { createStructuredSelector } from 'reselect'
  24. import { RouteComponentProps } from 'react-router-dom'
  25. import LoginForm from './LoginForm'
  26. import { compose } from 'redux'
  27. import { login, logged } from '../App/actions'
  28. import {
  29. makeSelectLoginLoading,
  30. makeSelectOauth2Enabled
  31. } from '../App/selectors'
  32. import checkLogin from 'utils/checkLogin'
  33. import { setToken } from 'utils/request'
  34. import { statistic } from 'utils/statistic/statistic.dv'
  35. import ExternalLogin from '../ExternalLogin'
  36. const styles = require('./Login.less')
  37. type MappedStates = ReturnType<typeof mapStateToProps>
  38. type MappedDispatches = ReturnType<typeof mapDispatchToProps>
  39. type ILoginProps = MappedStates & MappedDispatches
  40. interface ILoginStates {
  41. username: string
  42. password: string
  43. }
  44. export class Login extends React.PureComponent<
  45. ILoginProps & RouteComponentProps,
  46. ILoginStates
  47. > {
  48. constructor(props) {
  49. super(props)
  50. this.state = {
  51. username: '',
  52. password: ''
  53. }
  54. }
  55. public componentWillMount() {
  56. this.checkNormalLogin()
  57. }
  58. private checkNormalLogin = () => {
  59. if (checkLogin()) {
  60. const token = localStorage.getItem('TOKEN')
  61. const loginUser = localStorage.getItem('loginUser')
  62. setToken(token)
  63. this.props.onLogged(JSON.parse(loginUser))
  64. this.props.history.replace('/')
  65. }
  66. }
  67. private findPassword = () => {
  68. const { history } = this.props
  69. history.push('/findPassword')
  70. }
  71. private changeUsername = (e: ChangeEvent<HTMLInputElement>) => {
  72. this.setState({
  73. username: e.target.value.trim()
  74. })
  75. }
  76. private changePassword = (e: ChangeEvent<HTMLInputElement>) => {
  77. this.setState({
  78. password: e.target.value
  79. })
  80. }
  81. private toSignUp = () => {
  82. const { history } = this.props
  83. history.replace('/register')
  84. }
  85. private doLogin = (e: FormEvent<HTMLFormElement>) => {
  86. e.preventDefault()
  87. const { onLogin, history } = this.props
  88. const { username, password } = this.state
  89. if (username && password) {
  90. onLogin(username, password, () => {
  91. history.replace('/')
  92. statistic.whenSendTerminal()
  93. statistic.setOperations(
  94. {
  95. create_time: statistic.getCurrentDateTime()
  96. },
  97. (data) => {
  98. const loginRecord = {
  99. ...data,
  100. action: 'login'
  101. }
  102. statistic.sendOperation(loginRecord)
  103. }
  104. )
  105. })
  106. }
  107. }
  108. public render() {
  109. const { loginLoading, oauth2Enabled } = this.props
  110. const { username, password } = this.state
  111. return (
  112. <div className={styles.window}>
  113. <Helmet title="Login" />
  114. <LoginForm
  115. username={username}
  116. password={password}
  117. loading={loginLoading}
  118. onChangeUsername={this.changeUsername}
  119. onChangePassword={this.changePassword}
  120. onLogin={this.doLogin}
  121. />
  122. <p className={styles.tips}>
  123. <a
  124. href="javascript:;"
  125. className={styles.register}
  126. onClick={this.toSignUp}
  127. >
  128. 注册新账户
  129. </a>
  130. <a
  131. href="javascript:;"
  132. className={styles.forgetPassword}
  133. onClick={this.findPassword}
  134. >
  135. 忘记密码?
  136. </a>
  137. </p>
  138. {oauth2Enabled && <ExternalLogin />}
  139. </div>
  140. )
  141. }
  142. }
  143. const mapStateToProps = createStructuredSelector({
  144. loginLoading: makeSelectLoginLoading(),
  145. oauth2Enabled: makeSelectOauth2Enabled()
  146. })
  147. export function mapDispatchToProps(dispatch) {
  148. return {
  149. onLogin: (username: string, password: string, resolve: () => void) =>
  150. dispatch(login(username, password, resolve)),
  151. onLogged: (user) => dispatch(logged(user))
  152. }
  153. }
  154. const withConnect = connect<{}, {}, ILoginProps>(
  155. mapStateToProps,
  156. mapDispatchToProps
  157. )
  158. export default compose(withConnect)(Login)