Vue 记住密码

Vue 记住密码,第1张

导入:加密

// 安装
npm install --save js-base64

// 引入
const Base64 = require('js-base64').Base64

创建函数:

 created()
  {
       this.getCookie()
  },

methods:{
  //储存
 setCookie(name ,pws ,exdays)
    {
      var exdate = new Date(); //获取时间
      exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays);
      window.document.cookie = "userName" + "=" + name + ";path=/;expires=" + exdate.toGMTString();

      let passWord = Base64.encode(pws)
      window.document.cookie = "userPwd" + "=" + passWord + ";path=/;expires=" + exdate.toGMTString();

    },
//获取
    getCookie()
    {
       if (document.cookie.length > 0) {
                    var arr = document.cookie.split('; '); 
                    for (var i = 0; i < arr.length; i++) {
                        var arr2 = arr[i].split('='); 
                        if (arr2[0] == 'userName') {
                            this.loginForm.username = arr2[1];
                        } else if (arr2[0] == 'userPwd') {
                            this.loginForm.password=Base64.decode(arr2[1]);
                            this.checked = true
                        }
                    }
                }
    },
//删除
    clearCookie() 
    {
        this.setCookie("", "", -1); 
    }

}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1323019.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-12
下一篇 2022-06-12

发表评论

登录后才能评论

评论列表(0条)

保存