前端vue登录记住账号密码并反显

1.页面所需属性(只展示所需按钮)

<template>
<Checkbox v-model="single" size="large" @on-change="singleChange">记住账号密码</Checkbox>
<Button @click="passWordlogOn()">登 录</Button>
</template>

 2.方法以及属性定义()

账号密码进行了加密解密操作 具体看文章http://t.csdn.cn/LvCBG

<script>
import { encryptionCode, decryptCode } from '@/utils/common';
 data() {
    return {
      queryFormPassword: {
        account: '',
        pabcd: ''
      },
      single: false,
    };
  },
  created() {
    // 首先查看是否记住密码,有则直接渲染到页面中
    if (this.getCookie('phone') && this.getCookie('pabcd')) {
      //对账号密码进行解密
      let phone = decryptCode(this.getCookie('phone'), '01234567789abcdefg');
      let pabcd = decryptCode(this.getCookie('pabcd'), '01234567789abcdefg');
      this.queryFormPassword.account = phone;
      this.queryFormPassword.pabcd = pabcd;
      this.single = true;
    } else {
      this.queryFormPassword.account = '';
      this.queryFormPassword.pabcd = '';
      this.single = false;
    }
  },
  methods: {

    //记住密码
    singleChange(val) {
      this.single = val;
    },

    //密码登录
    passWordlogOn() {
         //组件表单验证
      this.$refs.queryFormPassword.validate((valid) => {
        if (valid) {
          getKey({ phone: this.queryFormPassword.account }).then((res) => {
            if (res.code == 200) {
              const params = {
                phone: this.queryFormPassword.account,
                // pabcd: encrypt(this.queryFormPassword.pabcd, res.data)
                pabcd: this.queryFormPassword.pabcd
              };
              this.$store.dispatch('user/login', params);
              //延时一秒通过token判断是否登录成功
              setTimeout(() => {
                const token = this.$store.getters.token;
                if (token) {
                  if (this.single) {
                    //对账号密码进行加密
                    let phone =                                             
                    encryptionCode(this.queryFormPassword.account,'01234567789abcdefg');          
                    let pabcd = 
                    encryptionCode(this.queryFormPassword.pabcd, '01234567789abcdefg');
                    // 记住密码
                    this.setCookie('phone', phone, 7); //保存帐号到cookie,有效期7天
                    this.setCookie('pabcd', pabcd, 7); //保存密码到cookie,有效期7天
                  } else {
                    // 清除已记住的密码
                    this.delCookie('phone');
                    this.delCookie('pabcd');
                  }
                }
              }, 1000);
            } else {
            }
          });

        }
      });
    },

    //存放cookie
    setCookie(name, value, day) {
      let date = new Date();
      // setDate:用于设置一个月的某一天
      // getDate:通过时间戳返回月份的某一天
      // expires: cookie存放到指定日期
      date.setDate(date.getDate() + day);
      document.cookie = name + '=' + value + ';expires=' + date;
    },

    // 获取cookie
    getCookie(name) {
      //规定要匹配的格式
      let reg = RegExp(name + '=([^;]+)');
      // 通过格式用match(指定的值或一个以及多个正则表达式)函数 查找
      let arr = document.cookie.match(reg);
      //通过判断返回指定值
      if (arr) {
        return arr[1];
      } else {
        return '';
      }
    },

    //删除cookie
    delCookie(name) {
      this.setCookie(name, null, -1);
    }
  }
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值