king
2022-02-09 d59f518f466274b2caeb2e01c10c92deafe7c93b
src/components/header/loginform.jsx
@@ -1,6 +1,8 @@
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { Form, Icon, Input } from 'antd'
import { Form, Input, Checkbox } from 'antd'
import { UserOutlined, LockOutlined } from '@ant-design/icons'
import zhCN from '@/locales/zh-CN/login.js'
import enUS from '@/locales/en-US/login.js'
import './index.scss'
@@ -11,7 +13,35 @@
  }
  state = {
    dict: localStorage.getItem('lang') !== 'en-US' ? zhCN : enUS
    dict: sessionStorage.getItem('lang') !== 'en-US' ? zhCN : enUS,
    remember: false,
    username: '',
    password: ''
  }
  UNSAFE_componentWillMount () {
    let _url = window.location.href.split('#')[0] + 'cloud'
    let _user = localStorage.getItem(_url)
    if (_user) {
      try {
        _user = JSON.parse(window.decodeURIComponent(window.atob(_user)))
      } catch (e) {
        console.warn('Parse Failure')
        _user = ''
      }
    }
    if (_user && new Date().getTime() - _user.time > 1000 * 7 * 24 * 60 * 60) {
      _user = ''
      localStorage.removeItem(_url)
    }
    this.setState({
      remember: _user ? true : false,
      username: _user ? _user.username : '',
      password: _user ? _user.password : ''
    })
  }
  handleConfirm = () => {
@@ -27,47 +57,70 @@
    })
  }
  handleSubmit = e => {
    // 登录参数检验
  handleSubmit = (e, key) => {
    e.preventDefault()
    this.props.handleSubmit()
    if (e.target.value) {
      if (!this.props.form.getFieldValue(key)) {
        const input = document.getElementById(key)
        input && input.focus()
        return
      }
      this.props.handleSubmit()
    } else {
      this.handleConfirm()
    }
  }
  rememberChange = (e) => {
    let val = e.target.checked
    let _url = window.location.href.split('#')[0] + 'cloud'
    if (!val) {
      localStorage.removeItem(_url)
    }
  }
  componentDidMount () {
    const input = document.getElementById('username')
    if (input) {
      input.focus()
    }
    input && input.focus()
  }
  render() {
    const { getFieldDecorator } = this.props.form
    const { remember, username, password } = this.state
    return (
      <Form style={{margin: '0px 10px'}}>
        <Form.Item>
        <Form.Item style={{marginBottom: '0px', height: '60px'}}>
          {getFieldDecorator('username', {
            rules: [{ required: true, message: this.state.dict['login.username.empty'] }],
            initialValue: '',
            initialValue: username,
          })(
            <Input
              prefix={<Icon type="user" style={{ color: 'rgba(0,0,0,.25)' }} />}
              prefix={<UserOutlined style={{ color: 'rgba(0,0,0,.25)' }}/>}
              placeholder={this.state.dict['login.username']}
              autoComplete="off"
              onPressEnter={this.handleSubmit}
              onPressEnter={(e) => {this.handleSubmit(e, 'password')}}
            />
          )}
        </Form.Item>
        <Form.Item>
        <Form.Item style={{marginBottom: '0px', height: '55px'}}>
          {getFieldDecorator('password', {
            initialValue: '',
            initialValue: password,
            rules: [
              {
                required: true,
                message: this.state.dict['login.password.empty'],
              }
            ]
          })(<Input.Password onPressEnter={this.handleSubmit} placeholder={this.state.dict['login.password']} prefix={<Icon type="lock" style={{ color: 'rgba(0,0,0,.25)' }} />} />)}
          })(<Input.Password onPressEnter={(e) => {this.handleSubmit(e, 'username')}} placeholder={this.state.dict['login.password']} prefix={<LockOutlined style={{ color: 'rgba(0,0,0,.25)' }} />} />)}
        </Form.Item>
        <Form.Item style={{marginBottom: '10px'}}>
          {getFieldDecorator('remember', {
            valuePropName: 'checked',
            initialValue: remember,
          })(
          <Checkbox onChange={this.rememberChange}>记住密码</Checkbox>)}
        </Form.Item>
      </Form>
    )