king
2024-06-15 b488c2f9630583a72e2bcae3df68f4227622ec78
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import React, {Component} from 'react'
import PropTypes from 'prop-types'
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'
 
class HeaderLoginForm extends Component {
  static propTpyes = {
    handleSubmit: PropTypes.func
  }
 
  state = {
    dict: sessionStorage.getItem('lang') !== 'en-US' ? zhCN : enUS,
    remember: false,
    username: '',
    password: '',
    oripassword: '',
    delay: +sessionStorage.getItem('mkDelay')
  }
 
  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)
    }
 
    if (_user) {
      this.setState({
        remember: true,
        username: _user.username,
        password: _user.password ? '*********' : '',
        oripassword: _user.password
      })
    }
  }
 
  handleConfirm = () => {
    const { oripassword } = this.state
    // 表单提交时检查输入值是否正确
    return new Promise((resolve, reject) => {
      this.props.form.validateFieldsAndScroll((err, values) => {
        if (!err) {
          if (oripassword && values.password === '*********') {
            values.password = oripassword
          }
          
          values.username = values.username.replace(/\t+|\v+|\s+/g, '')
          values.password = values.password.replace(/\t+|\v+|\s+/g, '')
 
          resolve(values)
        } else {
          reject(err)
        }
      })
    })
  }
 
  handleSubmit = (e, key) => {
    e.preventDefault()
    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 { username, password } = this.state
 
    if (username && !password) {
      const input = document.getElementById('password')
      input && input.focus()
    } else {
      const input = document.getElementById('username')
      input && input.focus()
    }
  }
 
  render() {
    const { getFieldDecorator } = this.props.form
    const { remember, username, password, delay } = this.state
 
    return (
      <Form style={{margin: '0px 10px'}}>
        {delay > 1000 ? <Form.Item style={{marginBottom: '0px', marginTop: '-10px'}}>
          升级到<a target="_blank" rel="noopener noreferrer" href="https://cloud.mk9h.cn/admin/index.html">企业版</a>,获得更高效的开发体验。
        </Form.Item> : null}
        <Form.Item style={{marginBottom: '0px', height: '60px'}}>
          {getFieldDecorator('username', {
            rules: [{ required: true, message: this.state.dict['login.username.empty'] }],
            initialValue: username,
          })(
            <Input
              prefix={<UserOutlined style={{ color: 'rgba(0,0,0,.25)' }}/>}
              placeholder={this.state.dict['login.username']}
              autoComplete="off"
              onPressEnter={(e) => {this.handleSubmit(e, 'password')}}
            />
          )}
        </Form.Item>
        <Form.Item style={{marginBottom: '0px', height: '55px'}}>
          {getFieldDecorator('password', {
            initialValue: password,
            rules: [
              {
                required: true,
                message: this.state.dict['login.password.empty'],
              }
            ]
          })(<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>
        {window.GLOB.keepKey ? <Form.Item style={{marginBottom: '10px'}}>
          {getFieldDecorator('remember', {
            valuePropName: 'checked',
            initialValue: remember,
          })(
          <Checkbox onChange={this.rememberChange}>记住密码</Checkbox>)}
        </Form.Item> : <div style={{height: '20px'}}></div>}
      </Form>
    )
  }
}
 
export default Form.create()(HeaderLoginForm)