React后台管理系统-登录页面
登录页面
-
<div className="col-md-4 col-md-offset-4">
-
<div className="panel panel-default login-panel">
-
<div className="panel-heading">欢迎登录 - MMALL管理系统</div>
-
<div className="panel-body">
-
<div>
-
<div className="form-group">
-
<input type="text"
-
name="username"
-
className="form-control"
-
placeholder="请输入用户名"
-
onKeyUp={e => this.onInputKeyUp(e)}
-
onChange={e => this.onInputChange(e)}/>
-
</div>
-
<div className="form-group">
-
<input type="password"
-
name="password"
-
className="form-control"
-
placeholder="请输入密码"
-
onKeyUp={e => this.onInputKeyUp(e)}
-
onChange={e => this.onInputChange(e)}/>
-
</div>
-
<button className="btn btn-lg btn-primary btn-block"
-
onClick={e => {this.onSubmit(e)}}>登录</button>
-
</div>
-
</div>
-
</div>
-
</div>
当用户名和密码发生改变的时候,设置onChange事件,重新设置state里边username和password的值
-
this.state = {
-
username: '',
-
password: '',
-
redirect: _mm.getUrlParam('redirect') || '/'
-
}
-
// 当用户名发生改变
-
onInputChange(e){
-
let inputValue = e.target.value,
-
inputName = e.target.name;
-
this.setState({
-
[inputName] : inputValue
-
});
-
}
给输入框设置onKeyUp事件,监听输入框按下enter键的时候,提交登录数据
-
onInputKeyUp(e){
-
if(e.keyCode === 13){
-
this.onSubmit();
-
}
-
}
提交表单数据,提交之前先验证表单数据,
-
// 检查登录接口的数据是不是合法
-
checkLoginInfo(loginInfo){
-
let username = $.trim(loginInfo.username),
-
password = $.trim(loginInfo.password);
-
// 判断用户名为空
-
if(typeof username !== 'string' || username.length ===0){
-
return {
-
status: false,
-
msg: '用户名不能为空!'
-
}
-
}
-
// 判断密码为空
-
if(typeof password !== 'string' || password.length ===0){
-
return {
-
status: false,
-
msg: '密码不能为空!'
-
}
-
}
-
return {
-
status : true,
-
msg : '验证通过'
-
}
-
}
-
onSubmit(){
-
let loginInfo = {
-
username : this.state.username,
-
password : this.state.password
-
},
-
//验证表单
-
checkResult = _user.checkLoginInfo(loginInfo);
-
// 验证通过
-
if(checkResult.status){
-
_user.login(loginInfo).then((res) => {
-
_mm.setStorage('userInfo', res);
-
//console.log(this.state.redirect);
-
this.props.history.push(this.state.redirect);
-
}, (errMsg) => {
-
_mm.errorTips(errMsg);
-
});
-
}
-
// 验证不通过
-
else{
-
_mm.errorTips(checkResult.msg);
-
}
-
-
}
登录之后跳转地址this.sate.redirect= _mm.getUrlParam('redirect') || '/' , this.props.history.push(this.state.redirect);
-
// 跳转登录
-
doLogin(){
-
//window.location.pathname url路径部分,端口后边,问号前边
-
//例如 redirect="/user/index"
-
window.location.href = '/login?redirect=' + window.location.pathname;
-
// window.location.href = '/login?redirect=' + encodeURIComponent(window.location.pathname);
-
}
-
// 获取URL参数
-
getUrlParam(name){
-
//http://localhost:8086/login?redirect=/product/index
-
// param=123¶m1=456
-
-
let queryString = window.location.search.split('?')[1] || '',
-
reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"),
-
result = queryString.match(reg);
-
console.log(result);
-
return result ? decodeURIComponent(result[2]) : null;
-
}