react关于cookie记住密码

//记住密码
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)*/
}



                                
//记住密码选择框

记住密码(三天)

 

你可能感兴趣的:(react关于cookie记住密码)