import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { Form, Icon, Input, Button, Checkbox, Select, Modal } from 'antd'
|
import './index.scss'
|
|
const { warning } = Modal
|
|
class MainSearch extends Component {
|
static propTpyes = {
|
isDisabled: PropTypes.bool,
|
changelang: PropTypes.func,
|
handleSubmit: PropTypes.func,
|
dict: PropTypes.object,
|
auth: PropTypes.bool,
|
lang: PropTypes.string,
|
platName: PropTypes.string
|
}
|
|
state = {
|
username: '',
|
password: ''
|
}
|
|
handleConfirm = () => {
|
// 表单提交时检查输入值是否正确
|
return new Promise((resolve, reject) => {
|
this.props.form.validateFieldsAndScroll((err, values) => {
|
if (!err) {
|
resolve(values)
|
} else {
|
reject(err)
|
}
|
})
|
})
|
}
|
|
changelang = (item) => {
|
this.props.changelang(item)
|
}
|
|
handleSubmit = e => {
|
// 登录参数检验
|
e.preventDefault()
|
if (!this.props.auth) {
|
warning({
|
title: this.props.dict['login.auth.tip'],
|
okText: this.props.dict['login.auth.ok'],
|
cancelText: this.props.dict['login.auth.cancel'],
|
onOk() {},
|
onCancel() {}
|
})
|
return
|
}
|
this.props.handleSubmit()
|
}
|
|
componentDidMount () {
|
let _url = window.location.href.split('#')[0]
|
let _user = localStorage.getItem(_url)
|
|
if (_user) {
|
try {
|
_user = JSON.parse(window.decodeURIComponent(window.atob(_user)))
|
} catch {
|
console.warn('Parse Failure')
|
_user = ''
|
}
|
}
|
|
if (_user) {
|
this.setState({
|
username: _user.username,
|
password: _user.password
|
}, () => {
|
const input = document.getElementById('username')
|
if (input) {
|
input.focus()
|
}
|
})
|
} else {
|
const input = document.getElementById('username')
|
if (input) {
|
input.focus()
|
}
|
}
|
}
|
|
render() {
|
const { getFieldDecorator } = this.props.form
|
|
return (
|
<Form onSubmit={this.handleSubmit} className="login-form" id="login-form">
|
<h4>{this.props.platName}</h4>
|
<Form.Item>
|
{getFieldDecorator('username', {
|
rules: [{ required: true, message: this.props.dict['login.username.empty'] }],
|
initialValue: this.state.username || '',
|
})(
|
<Input
|
prefix={<Icon type="user" style={{ color: 'rgba(0,0,0,.25)' }} />}
|
placeholder={this.props.dict['login.username']}
|
autoComplete="off"
|
/>,
|
)}
|
</Form.Item>
|
<Form.Item>
|
{getFieldDecorator('password', {
|
initialValue: this.state.password || '',
|
rules: [
|
{
|
required: true,
|
message: this.props.dict['login.password.empty'],
|
}
|
]
|
})(<Input.Password placeholder={this.props.dict['login.password']} prefix={<Icon type="lock" style={{ color: 'rgba(0,0,0,.25)' }} />} />)}
|
</Form.Item>
|
<Form.Item className="minline">
|
{getFieldDecorator('remember', {
|
valuePropName: 'checked',
|
initialValue: true,
|
})(<Checkbox>{this.props.dict['login.remember']}</Checkbox>)}
|
</Form.Item>
|
<Form.Item className="minline right">
|
{getFieldDecorator('lang', {
|
initialValue: this.props.lang,
|
})(
|
<Select
|
onChange={(value) => {this.changelang(value)}}
|
getPopupContainer={() => document.getElementById('login-form')}
|
>
|
<Select.Option value="zh-CN">中文简体</Select.Option>
|
<Select.Option value="en-US">English</Select.Option>
|
</Select>
|
)}
|
</Form.Item>
|
<Form.Item className="btn-login">
|
<Button type="primary" htmlType="submit" className="login-form-button" disabled={this.props.isDisabled} loading={this.props.isDisabled}>
|
{this.props.dict['login.submit']}
|
</Button>
|
</Form.Item>
|
</Form>
|
)
|
}
|
}
|
|
export default Form.create()(MainSearch)
|