vue中使用cookies和crypto-js实现记住密码和加密
生活随笔
收集整理的這篇文章主要介紹了
vue中使用cookies和crypto-js实现记住密码和加密
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
使用crypto-js加解密
第一步,安裝
npm install crypto-js
第二步,在你需要的vue組件內(nèi)import
import CryptoJS from “crypto-js”;
第三步,使用
// Encrypt 加密 var cipherText = CryptoJS.AES.encrypt("my message","secretkey123").toString();console.log(cipherText)// Decrypt 解密var bytes = CryptoJS.AES.decrypt(cipherText, "secretkey123");var originalText = bytes.toString(CryptoJS.enc.Utf8);console.log(originalText); // 'my message'注意這個(gè)mymessage是字符串,如果你要加密的用戶id(number類型)得先轉(zhuǎn)成字符串
更多使用請(qǐng)?jiān)L問官方文檔
記住密碼
其中保存使用setcookie方法,取出則使用getcookie方法。
ok,我們來編寫方法
登錄的方法如下:
login() {this.$http //請(qǐng)根據(jù)實(shí)際情況修改該方法.post(...).then(res => {if (res.data.code == "success") {if (this.rememberPsw == true) {//判斷用戶是否勾選了記住密碼選項(xiàng)rememberPsw,傳入保存的賬號(hào)currentPortId,密碼password,天數(shù)30this.setCookie(this.currentPortId, this.password, 30);}else{this.clearCookie();}//這里是因?yàn)橐赾reated中判斷,所以使用了localstorage比較簡(jiǎn)單,當(dāng)然你也可以直接根據(jù)cookie的長(zhǎng)度or其他騷操作來判斷有沒有記住密碼。localStorage.setItem("rememberPsw", this.rememberPsw);} else {//----}}).catch(err => {//----});},最后要在created鉤子函數(shù)內(nèi)判斷用戶是否記住了密碼來執(zhí)行相關(guān)的操作
//判斷是否記住密碼 //**注意這里的true是字符串格式,因?yàn)锽oolean存進(jìn)localstorage中會(huì)變成String**created() {//判斷是否記住密碼if (localStorage.getItem("rememberPsw") == 'true') {this.getCookie();}}最后,界面貼上,其中rememberPsw是記住密碼按鈕的v-model值,currentPortId是第一個(gè)框的v-model值,password就是第二個(gè)框的v-model值啦。
總結(jié)
以上是生活随笔為你收集整理的vue中使用cookies和crypto-js实现记住密码和加密的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: bilibili老版本_哔哩哔哩5.13
- 下一篇: Vue之$nextTick属性