vue 记住密码功能

<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)

}

});

},

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值