勾选记住账号
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()
},