vue-cookies


    安装vue-cookies    
    npm install vue-cookies --save
    
    引入vue-cookies
    import Vue from 'vue'
    import VueCookies from 'vue-cookies'
    Vue.use(VueCookies)
    
    -设置全局配置,设置cookie过期时间和url
    this.$cookies.config(expireTimes[,path])

    -设置一个cookie
    this.$cookies.set(keyName, value[, expireTimes[, path[, domain[, secure]]]])
    
    -获取一个cookie
    this.$cookies.get(keyName)
    
    -删除一个cookie
    this.$cookies.remove(keyName [, path [, domain]])
    
    -检查某个 cookie name是否存在
    this.$cookies.isKey(keyName)
    
    -获取所有 cookie name,以数组形式返回
    this.$cookies.keys()
    
    
    设置过期时间
    y	year    年
    m	month   月
    d	day     日
    h	hour    时
    min	minute  分
    s	second  秒
    
    全局30天后过期
    this.$cookies.config('30d');
    
    1天后过期
    this.$cookies.set("key", "value", "1d");
    
    浏览器会话结束时过期
    this.$cookies.set("key", "value", "0");
    
    永不过期
    this.$cookies.set("key", "value", -1);

// 使用

 data() {
     return {
          cookiesState: false,//是否存储cookies
          savePwd: true,//是否保存密码
          username: "",
          password: "",
      };
  },

// 初始化生命周期获取存储数据
 created() {
   // 获取记住密码状态
 let savePwd = this.$cookies.get('savePwd') + '';
 // 是记住密码,获取密码
 if (savePwd == 'true') {
     this.cookiesState = true;
     let isTf = this.$cookies.isKey('userObj');
     if (isTf) {
         let userObj = this.$cookies.get('userObj');
         this.username = userObj.username;
         let pwd = '';
         for (let i = 0; i < userObj.len; i++) {
             pwd += '*';
         }
         this.password = pwd;
     } else {
         this.$message({
             message: "用户信息已过期",
             type: "warning",
         });
     }
 }
 // 是否保存密码
 this.savePwd = savePwd == 'true' ? true : false;
 }




// 调用登录接口之前
  let userObj = {
       username: this.username,
       password: '',
       len: 0
   };

   let isTf = this.$cookies.isKey('userObj');
   let cookiePwd = null;
   let cookieLen = 0;
   let pwd = '';
   if (isTf) {
       cookiePwd = this.$cookies.get('userObj').password;
       cookieLen = this.$cookies.get('userObj').len;
       for (let i = 0; i < cookieLen; i++) {
           pwd += '*';
       }
   }

   if (this.cookiesState && pwd != '' && pwd == this.password) {
       userObj.password = cookiePwd;
       userObj.len = cookieLen;
   } else {
       userObj.password = md5(this.password);
       userObj.len = this.password.length;
   }

// 掉接口之后,进行cookies存储
	let savePwd = this.savePwd + '';
	this.$cookies.set('savePwd', savePwd, -1);
	if (this.savePwd) {
	    this.$cookies.set('userObj', userObj, "1d");
	} else {
	    this.$cookies.remove('userObj');
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值