vue记住账号密码实现过程

勾选记住账号
login(){

获取接口登录成功后
const data = {
username: this.loginForm.username,
password: this.loginForm.password,
};
service
.postF(“login”, data)
.then((res) => {
console.log(“res”, res.data);
if(res.data.code == 201) {
this.show=true;
return
}
//提示登录成功信息
//获取token
const userToken = res.headers[getConfig().authtokenname];
//保存token在localStorage 中
window.localStorage.setItem(“token”, userToken);

        //记住账号
        const username = this.loginForm.username;
        const password = this.loginForm.password;
        // console.log(typeof this.checked, this.loginForm);
        //判断是否被勾选
        if (this.checked == true) {
          this.setCookie(username, password, 7);
        } else if (this.checked == false) { //没有勾选状态,传空值(清楚cookie)
          // console.log(11);
          this.setCookie("", "", 2);
        }
        //跳转登录
        this.$router.replace("/homepage");
      })
      .catch(() => {});

}
//设置cookie
setCookie(c_username, c_password, saveDays) {
const exDate = new Date();
exDate.setTime(exDate.getTime() + 24 * 60 * 60 * 1000 * saveDays);
//拼接cookie
window.document.cookie=“userName”+"="+c_username+";path=/;expires="+exDate.toGMTString();
window.document.cookie=“userpassword”+"="+c_password+";path=/;expires="+exDate.toGMTString();
// console.log(“cookie”, window.document.cookie);
},

//读取cookie,将用户账号显示在输入框中
getCookie() {
  console.log("cookie",document.cookie);
  if (document.cookie.length > 0) {
    this.checked=true;
    const arr = document.cookie.split("; ");  //记得把空格一起切割了
    for (var i = 0; i < arr.length; i++) {
      var arr2 = arr[i].split("=");
      // console.log("arr2",arr2[1])
      if (arr2[0] == "userName" ) {
        this.loginForm.username = arr2[1];
      } 
      if (arr2[0] == "userpassword") {
        this.loginForm.password = arr2[1];
      }
    }
  }
  // this.checked=false
},
//页面加载获取cookie值

mounted() {
this.getCookie()
},

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值