//记住密码
if(this.state.rememberPassword){ //是否保存密码
let accountInfo = this.state.username+ '&' +this.state.password + '&' + this.state.typeLogin;
let Days = 3; //cookie保存时间
let exp = new Date();
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = 'accountInfo' + "="+ escape (accountInfo) + ";expires=" + exp.toGMTString();
}else {
let exp = new Date();
exp.setTime(exp.getTime() - 1);
let accountInfo = document.cookie
var cookie_pos = accountInfo.indexOf('accountInfo');
if(cookie_pos!=-1){
document.cookie= 'accountInfo' + "="+' '+";expires="+exp.toGMTString();
}
this.state.name = '';
this.state.password = '';
}
//判断cookie中是否有账号信息,有就可以进行预填写,没有则直接返回
loadAccountInfo = () => {
//读取cookie长度,看是否存在(测试)
/* let accountInfo123 = document.cookie
var cookie_pos = accountInfo123.indexOf('accountInfo');
console.log(cookie_pos + '@@@@@@@')*/
//读取cookie
let arr,reg=new RegExp("(^| )"+'accountInfo'+"=([^;]*)(;|$)");
let accountInfo =''
if(arr=document.cookie.match(reg)){
accountInfo = unescape(arr[2]);
}
else{
accountInfo = null;
}
if(Boolean(accountInfo) == false){
return false;
}else{
let userName = "";
let passWord = "";
let typeLogin = "";
let i=new Array()
i = accountInfo.split("&");
userName = i[0],
passWord = i[1],
typeLogin = i[2]
this.setState({
username : userName,
password : passWord,
rememberPassword : true,
typeLogin : typeLogin
})
/* this.state.username = userName;
this.state.password = passWord;
this.state.rememberPassword = true;*/
/*console.log(this.state.username + this.state.password)*/
}
/* let accountInfo = getCookie('accountInfo');
if(Boolean(accountInfo) == false){
return false;
}else{
let userName = "";
let passWord = "";
let index = accountInfo.indexOf("&");
userName = accountInfo.substring(0,index);
passWord = accountInfo.substring(index+1);
this.state.username = userName;
this.state.password = passWord;
this.state.rememberPassword = true;
}*/
};
//在componentDidMount中执行读取cookie
componentDidMount() {
this.loadAccountInfo();
/*window.addEventListener('keydown', this.handleKeyDown)*/
}
<input type="text" placeholder="用户名/手机号" className="login_input" id="usernameLogin"
maxLength={11} onChange={this.handleChecked1} value={this.state.username}/>
<input type="password" placeholder="密码" className="login_input" id="pwdLogin" maxLength={20} onChange={this.handleChecked2} value={this.state.password}/>
//记住密码选择框
<input type="checkbox" checked={this.state.rememberPassword} onChange={this.handleChecked} value={this.state.rememberPassword}/>
<span className='color-white'>记住密码(三天)</span>