|
@@ -1,14 +1,17 @@
|
|
|
import React from 'react'
|
|
import React from 'react'
|
|
|
import Login from 'ant-design-pro/lib/Login'
|
|
import Login from 'ant-design-pro/lib/Login'
|
|
|
import { Alert, Checkbox } from 'antd'
|
|
import { Alert, Checkbox } from 'antd'
|
|
|
-import { Link, Redirect } from 'dva/router';
|
|
|
|
|
|
|
+import { Link, Redirect } from 'dva/router'
|
|
|
|
|
+import { connect } from 'dva'
|
|
|
|
|
+import * as service from '../../services/index'
|
|
|
|
|
+import URLS from '../../constants/url'
|
|
|
import './login.less'
|
|
import './login.less'
|
|
|
|
|
|
|
|
const { UserName, Password, Submit } = Login;
|
|
const { UserName, Password, Submit } = Login;
|
|
|
|
|
|
|
|
-function authenticate(cb) {
|
|
|
|
|
- window.localStorage.setItem("isAuthenticated", "true");
|
|
|
|
|
- window.localStorage.setItem("expireTime", (new Date().getTime()+ 24 * 60 * 60 * 1000 ) + '');
|
|
|
|
|
|
|
+function authenticate(token, expireTime, cb) {
|
|
|
|
|
+ window.localStorage.setItem("token", token);
|
|
|
|
|
+ window.localStorage.setItem("expireTime", expireTime);
|
|
|
setTimeout(cb, 100); // fake async
|
|
setTimeout(cb, 100); // fake async
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -24,14 +27,8 @@ class LoginComponent extends React.Component {
|
|
|
this.setState({
|
|
this.setState({
|
|
|
notice: '',
|
|
notice: '',
|
|
|
}, () => {
|
|
}, () => {
|
|
|
- if (err && (values.username !== 'admin' || values.password !== '888888')) {
|
|
|
|
|
- setTimeout(() => {
|
|
|
|
|
- this.setState({
|
|
|
|
|
- notice: '账号名或密码错误!',
|
|
|
|
|
- });
|
|
|
|
|
- }, 500);
|
|
|
|
|
- }else {
|
|
|
|
|
- this.login();
|
|
|
|
|
|
|
+ if (!err) {
|
|
|
|
|
+ this.login(values.username, values.password);
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
@@ -41,9 +38,35 @@ class LoginComponent extends React.Component {
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- login = () => {
|
|
|
|
|
- authenticate(() => {
|
|
|
|
|
- this.setState({ redirectToReferrer: true });
|
|
|
|
|
|
|
+ login = (username, password) => {
|
|
|
|
|
+ const self = this;
|
|
|
|
|
+ let body = {
|
|
|
|
|
+ userName: username,
|
|
|
|
|
+ passWord: password
|
|
|
|
|
+ };
|
|
|
|
|
+ service.fetch({
|
|
|
|
|
+ url: URLS.LOGIN,
|
|
|
|
|
+ body: body
|
|
|
|
|
+ }).then(r => {
|
|
|
|
|
+ if(!r.err && r.data.code > 0) {
|
|
|
|
|
+ return r.data.data;
|
|
|
|
|
+ }else {
|
|
|
|
|
+ self.setState({
|
|
|
|
|
+ notice: r.err || r.data.msg,
|
|
|
|
|
+ });
|
|
|
|
|
+ let obj = {};
|
|
|
|
|
+ throw obj;
|
|
|
|
|
+ }
|
|
|
|
|
+ }).then(resData => {
|
|
|
|
|
+ console.log('登录', body, resData);
|
|
|
|
|
+ const token = resData.token;
|
|
|
|
|
+ // const expireTime = (new Date().getTime()+ 24 * 60 * 60 * 1000 ) + '';
|
|
|
|
|
+ const expireTime = resData.times;
|
|
|
|
|
+ authenticate(token, expireTime, () => {
|
|
|
|
|
+ this.setState({ redirectToReferrer: true });
|
|
|
|
|
+ });
|
|
|
|
|
+ }).catch(ex => {
|
|
|
|
|
+ console.error('fetch error', ex);
|
|
|
});
|
|
});
|
|
|
};
|
|
};
|
|
|
|
|
|
|
@@ -68,8 +91,8 @@ class LoginComponent extends React.Component {
|
|
|
this.state.notice &&
|
|
this.state.notice &&
|
|
|
<Alert style={{ marginBottom: 24 }} message={this.state.notice} type="error" showIcon closable />
|
|
<Alert style={{ marginBottom: 24 }} message={this.state.notice} type="error" showIcon closable />
|
|
|
}
|
|
}
|
|
|
- <UserName name="username" />
|
|
|
|
|
- <Password name="password" />
|
|
|
|
|
|
|
+ <UserName name="username" placeholder='输入用户名' />
|
|
|
|
|
+ <Password name="password" placeholder='输入密码'/>
|
|
|
<div>
|
|
<div>
|
|
|
<Checkbox checked={this.state.autoLogin} onChange={this.changeAutoLogin}>记住密码</Checkbox>
|
|
<Checkbox checked={this.state.autoLogin} onChange={this.changeAutoLogin}>记住密码</Checkbox>
|
|
|
<a style={{ float: 'right' }} href="">忘记密码</a>
|
|
<a style={{ float: 'right' }} href="">忘记密码</a>
|
|
@@ -88,4 +111,4 @@ class LoginComponent extends React.Component {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-export default LoginComponent;
|
|
|
|
|
|
|
+export default connect(({ present: { main } }) => ({ main }))(LoginComponent);
|