Vue+element+cookie记住密码功能的简单实现方法

 更新时间:2020年09月20日 09:09:14   作者:WByangqq  
这篇文章主要给大家介绍了Vue+element+cookie记住密码功能的简单实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

实现功能:

1、登录时勾选记住密码,用cookie保存账号和密码并对密码进行两次加密处理(纯前端),下次登录自动输入账号密码

2、登录时不勾选,清空cookie,下次登录需要输入

效果图:

=============================================================================================================================================================================================

Html

 <div class="login-form-item">
     <el-form :model="ValidateForm" ref="ValidateForm" label-width="100px" class="demo-ruleForm">
      <el-form-item

        prop="username"
        :rules="[{ required: true, message: '用户名不能为空'}
      ]">
       <span><i class="el-icon-user"></i></span><el-input type="username" v-model.number="ValidateForm.username" autocomplete="off" clearable placeholder="用户名"></el-input>
      </el-form-item>
      <br>
      <el-form-item

        prop="password"
        :rules="[{ required: true, message: '密码不能为空'},
      ]">
       <span><i class="el-icon-lock"></i></span><el-input type="password" v-model.number="ValidateForm.password" autocomplete="off" clearable show-password placeholder="密码"></el-input>
      </el-form-item>
      <br>
      <el-form-item

        prop="sidentify"
        :rules="[
{ required: true, message: '验证码不能为空'},]"
      >
       <el-row class="sidentify">
        <el-col :span="21">
         <el-input type="age" v-model="ValidateForm.sidentify" autocomplete="off" placeholder="验证码"></el-input>
        </el-col>
        <el-col :span="3" class="sidentify sidentify-img">
         <sidentify :changeCode.sync='identifyCode' ref="switchSidentify"></sidentify>
        </el-col>
       </el-row>
      </el-form-item>
      <el-form-item>
       <el-checkbox v-model="checked" class="sidentify">记住密码</el-checkbox>
      </el-form-item>
      <el-form-item>
       <el-button type="primary" @click="submitForm('ValidateForm')" class="login-btn">登录</el-button>
      </el-form-item>
     </el-form>
    </div>

加密方法我用的base64和CryptoJS 大家记得去下载

js部分:

登录

// 登录
submitForm(formName) {
 this.$refs[formName].validate((valid) => {
  if (valid) {
   let username=this.ValidateForm.username;
   let pwd=this.ValidateForm.password;
   let sidentify=this.ValidateForm.sidentify;
   // 验证码通过
   if (sidentify == this.identifyCode){
    this.request.post(this.api.login.logindo,{username:username,pwd:pwd}).then((res)=>{
     console.log(res);
     if (res.data.code == 200){
      this.$message({
       message : '登录成功!',
       type : 'success'
      })
      //调用check选中方法
      this.checkedPwd(username,pwd)
      this.$router.push({name:'Home'})
     }else {
      this.$message({
       message : '账号或密码错误,请重新输入!',
       type : 'error'
      })
      //清空
      this.resetForm('ValidateForm')
      //刷新验证码
      this.$refs.switchSidentify.changeCode()
     }
    })
   }else {
    this.$message({
     message : '验证码输入错误,请重新输入!',
     type : 'error'
    })
    this.$refs.switchSidentify.changeCode()
    this.resetForm('ValidateForm')
   }
  } else {
   return false;
  }
 });
},

check方法:

checkedPwd(username,pwd){
 // 记住密码进行cookie存储和密码加密
 if (this.checked){
  // base64 加密
  let base64Pwd=Base64.encode(pwd);
  // Encrypt 加密
  let cryptoJsPwd=CryptoJS.AES.encrypt(base64Pwd,key).toString()
  // 账号密码保存天数
  this.setCookie(username,cryptoJsPwd,7)
 }else{
  // 清空
  this.clearCookie()
 }
},

设置读取和清空cookie

// 设置cookie
setCookie(c_name, c_pwd, exdays) {
 var exdate = new Date(); // 获取时间
 exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays); // 保存的天数
 // 字符串拼接cookie
 window.document.cookie = "username" + "=" + c_name + ";path=/;expires=" + exdate.toGMTString();
 window.document.cookie = "password" + "=" + c_pwd + ";path=/;expires=" + exdate.toGMTString();
},
// 读取cookie
getCookie: function() {
 if (document.cookie.length > 0) {
  //checked为true
  this.checked=true
  var arr = document.cookie.split('; ');
  for (var i = 0; i < arr.length; i++) {
   var arr2 = arr[i].split('=');
   if (arr2[0] == 'username') {
    this.ValidateForm.username = arr2[1];
   } else if (arr2[0] == 'password') {
    // Decrypt 解密
    let bytes = CryptoJS.AES.decrypt(arr2[1],key)
    let originalText=bytes.toString(CryptoJS.enc.Utf8)
    // base64解密
    let pwd=Base64.decode(originalText)
    this.ValidateForm.password = pwd;
   }
  }
 }
},
// 清除cookie
clearCookie: function() {
 this.setCookie("", "", -1); // 修改2值都为空,天数为负1天就好了
},

一定要创建后读取cookie

created () {
 this.getCookie()
},

总结

到此这篇关于Vue+element+cookie记住密码功能的简单实现方法文章就介绍到这了,更多相关Vue+element+cookie记住密码功能内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • el-table表格排序(多列排序和远程排序)

    el-table表格排序(多列排序和远程排序)

    本文主要介绍了el-table表格排序(多列排序和远程排序),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-05-05
  • 解决axios post 后端无法接收数据的问题

    解决axios post 后端无法接收数据的问题

    今天小编就为大家分享一篇解决axios post 后端无法接收数据的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-10-10
  • vue如何使用cookie、localStorage和sessionStorage进行储存数据

    vue如何使用cookie、localStorage和sessionStorage进行储存数据

    这篇文章主要介绍了vue如何使用cookie、localStorage和sessionStorage进行储存数据,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-08-08
  • 图文详解如何在vue3+vite项目中使用svg

    图文详解如何在vue3+vite项目中使用svg

    SVG指可伸缩矢量图形,用来定义用于网络的基于矢量的图形,下面这篇文章主要给大家介绍了关于如何在vue3+vite项目中使用svg的相关资料,需要的朋友可以参考下
    2021-11-11
  • 在vue中配置不同的代理同时访问不同的后台操作

    在vue中配置不同的代理同时访问不同的后台操作

    这篇文章主要介绍了在vue中配置不同的代理同时访问不同的后台操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • vue3使用自定义指令实现el dialog拖拽功能示例详解

    vue3使用自定义指令实现el dialog拖拽功能示例详解

    这篇文章主要为大家介绍了vue3使用自定义指令实现el dialog拖拽功能示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09
  • Vue实现实时更新sessionStorage数据的示例代码

    Vue实现实时更新sessionStorage数据的示例代码

    这篇文章主要为大家详细介绍了Vue如何实现实时更新sessionStorage数据,文中的示例代码讲解详细,具有一定的参考价值,需要的可以参考一下
    2023-06-06
  • 基于vuejs+webpack的日期选择插件

    基于vuejs+webpack的日期选择插件

    这篇文章主要为大家详细介绍了基于vuejs+webpack的日期选择插件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-11-11
  • vue项目遇见事件冒泡该如何处理详解

    vue项目遇见事件冒泡该如何处理详解

    冒泡事件处理是由内而外发生的,例如有两个父子div,给他们分别写上点击事件,点击子div先响应的是子div,再是父div,下面这篇文章主要给大家介绍了关于vue项目遇见事件冒泡该如何处理的相关资料,需要的朋友可以参考下
    2023-04-04
  • 基于vue+h5实现车牌号输入框功能(demo)

    基于vue+h5实现车牌号输入框功能(demo)

    最近开发项目是学校校内车辆超速方面的统计检测方面的系统,在开发过程中发现有个小功能,就是用户移动端添加车牌号,刚开始想着输入框,提交时正则效验一下格式,最后感觉不方便,所以就简单自己手写了一个H5车牌号软键盘,对vue车牌号输入框实现代码感兴趣的朋友一起看看吧
    2025-03-03

最新评论