亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Vue實(shí)現(xiàn)登錄記住賬號(hào)密碼功能的思路與過程

 更新時(shí)間:2021年11月30日 12:38:21   作者:CC葡葡萄  
最近在學(xué)習(xí)vue,發(fā)現(xiàn)了vue的好多坑,下面這篇文章主要給大家介紹了關(guān)于Vue實(shí)現(xiàn)登錄記住賬號(hào)密碼功能的思路與過程,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下

實(shí)現(xiàn)思路

用戶登錄時(shí)若勾選“記住我”功能選項(xiàng),則將登錄名和密碼(加密后)保存至本地緩存中,下次登錄頁(yè)面加載時(shí)自動(dòng)獲取保存好的賬號(hào)和密碼(需解密),回顯到登錄輸入框中。

這里有三種方法來存儲(chǔ)賬號(hào)密碼:

1. sessionStorage(不推薦)

1). 僅在當(dāng)前會(huì)話下有效,關(guān)閉瀏覽器窗口后就被清除了

2). 存放數(shù)據(jù)大小一般為5MB

3). 不與服務(wù)器進(jìn)行交互通信

2. localStorage

1). 除非主動(dòng)清除localStorage里的信息,否則將永遠(yuǎn)存在,關(guān)閉瀏覽器窗口后下次啟動(dòng)任然存在

2). 存放數(shù)據(jù)大小一般為5MB

3). 不與服務(wù)器進(jìn)行交互通信

3. cookies

1). 可以手動(dòng)設(shè)置過期時(shí)間,超過有效期則失效。未設(shè)置過期時(shí)間,關(guān)閉瀏覽器窗口后就被清除了

2). 存放數(shù)據(jù)大小一般為4K

3). 每次請(qǐng)求都會(huì)被傳送到服務(wù)器

這里主要介紹第二種和第三種的使用方法。

功能界面

<el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="100px" class="loginForm demo-ruleForm">
  <!-- 賬號(hào) -->
  <el-form-item label="賬號(hào)" prop="userId" autocomplete="on">
    <el-input v-model="loginForm.userId" placeholder="請(qǐng)輸入賬號(hào)"></el-input>
  </el-form-item>
  <!-- 密碼 -->
  <el-form-item label="密碼" prop="password">
    <el-input type="password" v-model="loginForm.password" placeholder="請(qǐng)輸入密碼" @keyup.enter="submitForm('loginForm')"></el-input>
  </el-form-item>
  <div class="tip">
    <!-- 記住我 -->
    <el-checkbox v-model="checked" class="rememberMe">記住我</el-checkbox>
    <!-- 找回密碼 -->
    <el-button type="text" @click="open()" class="forgetPw">忘記密碼?</el-button>
  </div>
  <!-- 登錄 -->
  <el-form-item>
    <el-button type="primary" @click="submitForm('loginForm')" class="submit-btn">登錄</el-button>
  </el-form-item>
</el-form>

記住賬號(hào)密碼功能的具體實(shí)現(xiàn)

密碼加密

為提高安全性,密碼存儲(chǔ)前需進(jìn)行加密處理。目前加密方式有很多種,我這里選用了base64。

npm安裝base64依賴

//安裝
npm install --save js-base64
//引入
const Base64 = require("js-base64").Base64

localStorage

export default {
  data() {
    return {
      loginForm: {
        userId: "",
        password: "",
      },
      checked: false,
    };
  },
  mounted() {
    let username = localStorage.getItem("userId");
    if (username) {
      this.loginForm.userId = localStorage.getItem("userId");
      this.loginForm.password = Base64.decode(localStorage.getItem("password"));// base64解密
      this.checked = true;
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          /* ------ 賬號(hào)密碼的存儲(chǔ) ------ */
          if (this.checked) {
            let password = Base64.encode(this.loginForm.password); // base64加密
            localStorage.setItem("userId", this.loginForm.userId);
            localStorage.setItem("password", password);
          } else {
            localStorage.removeItem("userId");
            localStorage.removeItem("password");
          }
          /* ------ http登錄請(qǐng)求 ------ */
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};

cookies

export default {
  data() {
    return {
      loginForm: {
        userId: "",
        password: "",
      },
      checked: false,
    };
  },
  mounted() {
    this.getCookie();
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          /* ------ 賬號(hào)密碼的存儲(chǔ) ------ */
          if (this.checked) {
            let password = Base64.encode(this.loginForm.password); // base64加密
            this.setCookie(this.loginForm.userId, password, 7);
          } else {
            this.setCookie("", "", -1); // 修改2值都為空,天數(shù)為負(fù)1天就好了
          }
          /* ------ http登錄請(qǐng)求 ------ */
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    
    // 設(shè)置cookie
    setCookie(userId, password, days) {
      let date = new Date(); // 獲取時(shí)間
      date.setTime(date.getTime() + 24 * 60 * 60 * 1000 * days); // 保存的天數(shù)
      // 字符串拼接cookie
      window.document.cookie =
        "userId" + "=" + userId + ";path=/;expires=" + date.toGMTString();
      window.document.cookie =
        "password" + "=" + password + ";path=/;expires=" + date.toGMTString();
    },
    
    // 讀取cookie 將用戶名和密碼回顯到input框中
    getCookie() {
      if (document.cookie.length > 0) {
        let arr = document.cookie.split("; "); //分割成一個(gè)個(gè)獨(dú)立的“key=value”的形式
        for (let i = 0; i < arr.length; i++) {
          let arr2 = arr[i].split("="); // 再次切割,arr2[0]為key值,arr2[1]為對(duì)應(yīng)的value
          if (arr2[0] === "userId") {
            this.loginForm.userId = arr2[1];
          } else if (arr2[0] === "password") {
            this.loginForm.password = Base64.decode(arr2[1]);// base64解密
            this.checked = true;
          }
        }
      }
    },
  },
};

總結(jié)

到此這篇關(guān)于Vue實(shí)現(xiàn)登錄記住賬號(hào)密碼功能的思路與過程的文章就介紹到這了,更多相關(guān)Vue實(shí)現(xiàn)登錄記住賬號(hào)密碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論