美文网首页vue
vue记住密码功能

vue记住密码功能

作者: 明月几何8 | 来源:发表于2020-09-09 11:32 被阅读0次

依赖组件:base64

// 安装
npm install --save js-base64
// 引入
const Base64 = require('js-base64').Base64
<template>
  <form class="main">
    <!-- 账号 -->
    <div class="item">
      <label for="account">账号</label>
      <input type="text" style="display:none" />
      <input type="text" v-model="loginForm.account" id="account" />
    </div>
    <!--密码-->
    <div class="item">
      <label for="password">密码</label>
      <input type="password" style="display:none" />
      <input type="password" v-model="loginForm.password" id="password" />
    </div>
    <!-- 记住密码 -->
    <div class="item">
      <label>记住密码</label>
      <input type="checkbox" v-model="loginForm.remember" />
    </div>
    <!--登录按钮-->
    <button @click="submit">登录</button>
  </form>
</template>

<script>
// 引入base64
const Base64 = require("js-base64").Base64;
export default {
  data() {
    return {
      // 登陆表单
      loginForm: {
        account: "",
        password: "",
        remember: "",
      },
    };
  },
  created() {
    // 在页面加载时从cookie获取登录信息
    let account = this.getCookie("account");
    let password = Base64.decode(this.getCookie("password"));
    console.log(password);
    // 如果存在赋值给表单,并且将记住密码勾选
    if (account) {
      this.loginForm.account = account;
      this.loginForm.password = password;
      this.loginForm.remember = true;
    }
  },
  methods: {
    // 登录
    submit: function () {
      //   // 点击登陆向后台提交登陆信息
      //   axios.post("url", this.loginForm).then((res) => {
      //     // 储存token(需要封装拦截器,将token放入请求头中)
      //     this.setCookie("token", res.token);
      //     // 跳转到首页
      //     this.$router.push("/Index");
      //     // 储存登录信息
      //     this.setUserInfo();
      //   });
      // 储存token(需要封装拦截器,将token放入请求头中)
      this.setCookie("token", "1234567890");
      // 跳转到首页
      this.$router.push("/");
      // 储存登录信息
      this.setUserInfo();
    },
    // 储存表单信息
    setUserInfo: function () {
      // 判断用户是否勾选记住密码,如果勾选,向cookie中储存登录信息,
      // 如果没有勾选,储存的信息为空
      if (this.loginForm.remember) {
        this.setCookie("account", this.loginForm.account, 7);
        // base64加密密码
        let passWord = Base64.encode(this.loginForm.password);
        this.setCookie("password", passWord, 7);
        this.setCookie("remember", this.loginForm.remember, 7);
      } else {
        this.setCookie("account", "");
        this.setCookie("password", "");
      }
    },
    // 获取cookie
    getCookie: function (key) {
      if (document.cookie.length > 0) {
        var start = document.cookie.indexOf(key + "=");
        if (start !== -1) {
          start = start + key.length + 1;
          var end = document.cookie.indexOf(";", start);
          if (end === -1) end = document.cookie.length;
          return unescape(document.cookie.substring(start, end));
        }
      }
      return "";
    },
    // 保存cookie
    setCookie: function (cName, value, expiredays) {
      var exdate = new Date();
      exdate.setDate(exdate.getDate() + expiredays);
      document.cookie =
        cName +
        "=" +
        decodeURIComponent(value) +
        (expiredays == null ? "" : ";expires=" + exdate.toGMTString());
    },
  },
};
</script>

<style>
.main {
  width: 300px;
}
.main .item {
  display: flex;
  align-items: center;
  line-height: 30px;
}
.main .item label {
  width: 100px;
}
</style>

相关文章

  • vue记住密码功能

    依赖组件:base64

  • vue简单实现记住密码功能

    原理是在提交表单的时候把用户名和密码的值存入cookie中然后再次进入页面时读取cookie html部分 js部...

  • vue Cookie 实现记住密码功能

    注: 文章摘自 nxmin - 简书 功能 1.记住密码勾选,点登陆时,将账号和密码保存到cookie,下次登陆自...

  • Swift_使用SwiftyRSA对密码进行加密

    一般的APP都会有一个记住密码的功能,如果点击记住密码,等下次登录,密码是加密状态

  • vue项目实现记住密码到cookie功能

    一、前言 基于这种功能的实现网上已经有很多教程,我只是重新熟悉一下。毕竟好记性不如烂笔头,不喜勿喷 二、实现功能 ...

  • 浏览器“记住密码”的弊端

    现在主流浏览器都有记住密码功能,不过这个功能存在方便的同时存在一些弊端,例如随便一个被记住密码的网页,密码框一般都...

  • 前端记住密码

    需求:前端实现记住密码功能 实现: 其实就是根据用户有没有选中记住密码,来判断要不要把用户账号和密码存在cooki...

  • 禁止自动填充密码、提示密码问题

    控制密码显示与隐藏的功能已经完美实现。然而问题又来了:浏览器有个记住密码的功能,下次登录时会自动填充上次登录记住的...

  • 去掉web浏览器记住密码的功能

    去掉web浏览器记住密码的功能: 来一波解释默认的,浏览器会帮你记住密码,经过测试,只要你的页面中有密码框。 in...

  • 参考的文章

    验证码 图片验证码前端怎样获取后端生成的验证码图片,并且点击图片的时候改变验证码 记住密码 前端记住密码功能密码安...

网友评论

    本文标题:vue记住密码功能

    本文链接:https://www.haomeiwen.com/subject/qhleektx.html