<el-checkbox class="dl-jzmm" v-model="isChecked">记住密码</el-checkbox>
data () {
return {
isChecked:false,
}
},
mounted() { this.getCookie(); },
methods:{
loginPwd () {
loginPwds(this.user1).then((res)=>{
if(res.data.code==500&&res.data.status==false){
this.$notify({
title : '错误信息',
message : res.data.message,
type : 'warning'
});
return;
}
if(res.data.status == true){
this.exitflag = true;
// 当后台返回代码里面显示登录成功之后我们将token进行保存
localStorage.setItem('userInfo', JSON.stringify(res.data.data));
localStorage.setItem('userToken',res.data.data.token);
//记住密码
if (this.isChecked) { // 记住密码
this.setCookie(this.user1.phone, this.user1.password, 30); // 保存期限为30天
} else {
this.clearCookie(); // 清空 Cookie
}
this.$notify({
showClose: true,
message : '登陆成功!',
type : 'success'
});
window.setTimeout(()=>{
if(this.exitflag){
this.$router.push('/index');//退出之后跳转登录页面
this.$router.go(0);
}
},2000)
}
});
},
// 设置Cookie
setCookie(phone, password, exdays) { // 用户名, 密码, 保存天数
let exdate = new Date(); // 获取时间
exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays);
// 字符串拼接cookie
window.document.cookie = 'phone=' + phone + ';path=/;expires=' + exdate.toGMTString();
window.document.cookie = 'userPwd=' + password + ';path=/;expires=' + exdate.toGMTString();
},
// 读取Cookie
getCookie() {
// console.log(document.cookie);
if (document.cookie.length > 0) {
let arr = document.cookie.split('; '); // 这里显示的格式需要切割一下自己可输出看下
for (let i = 0; i < arr.length; i++) {
let arr2 = arr[i].split('='); // 再次切割
// 判断查找相对应的值
if (arr2[0] == 'phone') {
this.user1.phone = arr2[1]; // 保存到保存数据的地方
} else if (arr2[0] == 'userPwd') {
this.user1.password = arr2[1];
}
}
}
},
// 清除Cookie
clearCookie() {
this.setCookie('', '', -1); // 修改2值都为空,天数为负1天就好了
},
loginCode () {
loginCodes(this.user2).then((res)=>{
// console.log(res);
if(res.data.code==500&&res.data.status==false){
this.$notify({
title : '错误信息',
message : res.data.message,
type : 'warning'
});
return;
}
if(res.data.status == true){
this.exitflag = true;
// 当后台返回代码里面显示登录成功之后我们将token进行保存
localStorage.setItem('userInfo', JSON.stringify(res.data.data));
localStorage.setItem('userToken',res.data.data.token);
this.$notify({
showClose: true,
message : '登陆成功!',
type : 'success'
});
window.setTimeout(()=>{
if(this.exitflag){
this.$router.push('/index');//退出之后跳转登录页面
this.$router.go(0);
}
},2000)
}
});
},
}