king
2021-11-22 2eb57414b648430420cf56d432aeddb5a72ad30c
src/components/header/loginform.jsx
@@ -1,6 +1,6 @@
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { Form, Icon, Input } from 'antd'
import { Form, Icon, Input, Checkbox } from 'antd'
import zhCN from '@/locales/zh-CN/login.js'
import enUS from '@/locales/en-US/login.js'
import './index.scss'
@@ -11,7 +11,35 @@
  }
  state = {
    dict: sessionStorage.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 = () => {
@@ -41,6 +69,15 @@
    }
  }
  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')
    input && input.focus()
@@ -48,13 +85,14 @@
  render() {
    const { getFieldDecorator } = this.props.form
    const { remember, username, password } = this.state
    return (
      <Form style={{margin: '0px 10px'}}>
        <Form.Item>
          {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)' }} />}
@@ -64,9 +102,9 @@
            />
          )}
        </Form.Item>
        <Form.Item>
        <Form.Item style={{marginBottom: '15px'}}>
          {getFieldDecorator('password', {
            initialValue: '',
            initialValue: password,
            rules: [
              {
                required: true,
@@ -75,6 +113,13 @@
            ]
          })(<Input.Password onPressEnter={(e) => {this.handleSubmit(e, 'username')}} placeholder={this.state.dict['login.password']} prefix={<Icon type="lock" 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>
    )
  }