vue 登录时记住密码 + 加密

67 篇文章 7 订阅

思路 登录成功时  记录选中记住密码状态 到localstorage, 账号密码 加密后存到cookie。

npm i npm install crypto-js 
import CryptoJS from 'crypto-js'
export default {
  data () {
    return {
      loginForm: {
        account: '',
        password: ''
      },
      rememberPassword: false
    }
  },
  created () {
    this.getCookiePassword()
    if (localStorage.getItem('rememberPsw') === 'true') {
      this.rememberPassword = true
      this.getCookie()
    }
  },
  methods: {
    getCookiePassword () {
      if (document.cookie.length > 0) {
        var arr = document.cookie.split(';') // 这里显示的格式需要切割一下自己可输出看下
        for (var i = 0; i < arr.length; i++) {
          var arr2 = arr[i].split('=') // 再次切割
          // 判断查找相对应的值
          console.log(unescape(arr2[1]))
        }
      }
    },
    // 登录
    post () {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          console.log('成功')
          this.getLogin()
        } else {
          this.$message({
            showClose: true,
            message: '请确认账号密码后再登陆!',
            type: 'warning'
          })
        }
      })
    },
    async getLogin () {
      try {
        const res = await this.$store.dispatch('login/userLogin', this.loginForm)
        // 记住密码
        if (this.rememberPassword) {
          this.setCookie(this.loginForm.account, this.loginForm.password, 7)
        } else {
          this.clearCookie()
        }
        // 保存记住密码状态
        localStorage.setItem('rememberPsw', this.rememberPassword)
        this.$message({ showClose: true, message: res.msg, type: 'success' })
        res.data.userinfo.group_id === 3 ? this.$router.push('/consultantManagement') : this.$router.push('/')
      } catch (err) {
        this.$message({ showClose: true, message: err.message, type: 'error' })
      }
    },
    rememberAccountNumber (name, value, day) {
      console.log(name, value, day)
      const Days = day
      const exp = new Date()
      exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000)
      document.cookie = name + '=' + escape(value) + ';expires=' + exp.toGMTString()
    },
    // 设置cookie
    setCookie (portId, psw, exdays) {
      // Encrypt,加密账号密码
      var cipherPortId = CryptoJS.AES.encrypt(
        portId + '',
        'secretkey123'
      ).toString()
      var cipherPsw = CryptoJS.AES.encrypt(psw + '', 'secretkey123').toString()
      console.log(cipherPortId + '/' + cipherPsw)// 打印一下看看有没有加密成功

      var exdate = new Date() // 获取时间
      exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays) // 保存的天数
      // 字符串拼接cookie,为什么这里用了==,因为加密后的字符串也有个=号,影响下面getcookie的字符串切割,你也可以使用更炫酷的符号。
      window.document.cookie =
    'currentPortId' +
    '==' +
    cipherPortId +
    ';path=/;expires=' +
    exdate.toGMTString()
      window.document.cookie =
    'password' +
    '==' +
    cipherPsw +
    ';path=/;expires=' +
    exdate.toGMTString()
    },
    // 读取cookie
    getCookie: function () {
      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] === ' currentPortId') {
            // Decrypt,将解密后的内容赋值给账号
            const bytes = CryptoJS.AES.decrypt(arr2[1], 'secretkey123')
            this.loginForm.account = bytes.toString(CryptoJS.enc.Utf8)
          } else if (arr2[0] === ' password') {
            // Decrypt,将解密后的内容赋值给密码
            const bytes = CryptoJS.AES.decrypt(arr2[1], 'secretkey123')
            this.loginForm.password = bytes.toString(CryptoJS.enc.Utf8)
          }
        }
      }
    },
    // 清除cookie
    clearCookie: function () {
      this.setCookie('', '', -1)
    }
  }
}
</script>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值