大體思路就是通過存/取/刪cookie實現的;每次進入登錄頁,先去讀取cookie,如果瀏覽器的cookie中有賬號信息,就自動填充到登錄框中,存cookie是在登錄成功之后,判斷當前用戶是否勾選了記住密碼,如果勾選了,則把賬號信息存到cookie當中,效果圖如下:
瀏覽器中的cookie信息如下圖,注意這里的cookie的expire/Max-Age過期時間,這個時間是格林尼治標准時間GMT,世界統一的時間,GMT+8小時就是北京時間,初中地理沒學好,特地去查了下!!
實現代碼:
html

1 <template> 2 <div class="login-wrapper"> 3 <form class="form-signin" role="form"> 4 <h2 class="form-signin-heading">點播系統</h2> 5 <input class="form-control" v-model="userName" v-on:keydown="doLogin($event)" placeholder="用戶名" required autofocus> 6 <input type="password" class="form-control" v-model="passWord" v-on:keydown="doLogin($event)" placeholder="Password"> 7 <div class="checkbox remember-password-container"> 8 <input type="checkbox" value="remember-me" v-model="rememberPassword" id="remember-password-checkbox" v-on:click="doRememberPassword($event)"> 9 <label for="remember-password-checkbox"> 10 Remember me 11 </label> 12 </div> 13 <div class="btn btn-lg btn-primary btn-block" v-on:click="doLogin()">Sign in</div> 14 </form> 15 </div> 16 </template>
登錄處理邏輯js

1 export default { 2 // name: 'component2', 3 data () { 4 return { 5 userName: "", 6 passWord: "", 7 rememberPassword: false 8 } 9 }, 10 // 相當於init doAjax 11 beforeCreate() { 12 // console.log('login頁面 加載完成!') 13 }, 14 // 相當於ready 模板編譯掛載之后 15 mounted: function() { 16 //讀取cookie中的賬號信息,如果有accountInfo的話,則說明該用戶之前勾選了記住密碼的功能,則需要自動填上賬號密碼 17 this.loadAccountInfo(); 18 }, 19 methods: { 20 doLogin: function(event){ 21 22 var mySelf = this; 23 const router = this.$router; 24 // console.log(router) 25 // router.go(); 26 // router.push({path:'/index'}) 27 // console.log(pars.domain) 28 29 var mySelf = this; 30 var userName = mySelf.userName; 31 var userPwd = mySelf.passWord; 32 //記住密碼checkbox的勾選狀態 和賬號信息的字符串 33 var rememberStatus = mySelf.rememberPassword; 34 var accountInfo = ""; 35 accountInfo = userName + "&" + userPwd; 36 37 if (event && event.type == 'keydown' && event.keyCode != 13) { 38 return; 39 } 40 41 //console.log("用戶名:" + userName) 42 //console.log("密碼:" + userPwd) 43 44 if (userName == ""){ 45 util.showDialog('error','用戶名不能為空!'); 46 return; 47 } 48 if (userPwd == ""){ 49 util.showDialog('error','密碼不能為空!'); 50 return; 51 } 52 53 $.get( pars.domain + "xxx" + "&t=" + (new Date).getTime(), function(ret) { 54 55 if (ret.code == 0) { 56 //如果登錄成功,則把賬號信息保存在cookie當中 57 if (rememberStatus){ 58 console.log("勾選了記住密碼,現在開始寫入cookie"); 59 util.setCookie('accountInfo',accountInfo,1440*3); 60 } 61 else{ 62 console.log("沒有勾選記住密碼,現在開始刪除賬號cookie"); 63 util.delCookie('accountInfo'); 64 } 65 66 // 若為本地環境 則手寫cookie 67 if (window.location.href.indexOf('localhost') != -1){ 68 util.setCookie('token','zhaopeng_58e0cbbea951f0e79fafcee80da522b8',1440); 69 } 70 // console.log(window.location.href) 71 console.log('登錄的返回值為0'); 72 router.push({path:'/'}); 73 } else { 74 util.showDialog('error','賬號名或密碼錯誤!'); 75 } 76 }, "json"); 77 78 }, 79 doRememberPassword: function(event){ 80 let mySelf = this; 81 let rememberStatus = mySelf.rememberPassword; 82 // event.preventDefault(); 83 mySelf.rememberPassword = !rememberStatus; 84 //如果去掉勾選,則刪掉cookie 85 // if (!rememberStatus){ 86 87 // } 88 // mySelf.rememberPassword = false; 89 }, 90 loadAccountInfo: function(){ 91 92 let mySelf = this; 93 //zhaopeng&A15hOsu8YeGnCsjb 94 let accountInfo = util.getCookie('accountInfo'); 95 96 //如果cookie里沒有賬號信息 97 if(Boolean(accountInfo) == false){ 98 console.log('cookie中沒有檢測到賬號信息!'); 99 return false; 100 } 101 else{ 102 //如果cookie里有賬號信息 103 console.log('cookie中檢測到賬號信息!現在開始預填寫!'); 104 let userName = ""; 105 let passWord = ""; 106 let index = accountInfo.indexOf("&"); 107 108 userName = accountInfo.substring(0,index); 109 passWord = accountInfo.substring(index+1); 110 111 mySelf.userName = userName; 112 mySelf.passWord = passWord; 113 mySelf.rememberPassword = true; 114 } 115 } 116 117 } 118 }
操作cookie邏輯js

1 // 設置cookie 2 setCookie: function(c_name,value,expiremMinutes){ 3 var exdate = new Date(); 4 exdate.setTime(exdate.getTime() + expiremMinutes * 60 * 1000); 5 document.cookie= c_name + "=" + escape(value)+((expiremMinutes==null) ? "" : ";expires="+exdate.toGMTString()); 6 }, 7 8 // 讀取cookie 9 getCookie: function(c_name){ 10 if (document.cookie.length>0) 11 { 12 var c_start=document.cookie.indexOf(c_name + "="); 13 if (c_start!=-1) 14 { 15 c_start=c_start + c_name.length+1; 16 var c_end=document.cookie.indexOf(";",c_start); 17 if (c_end==-1) 18 c_end = document.cookie.length 19 return unescape(document.cookie.substring(c_start, c_end)) 20 } 21 } 22 return "" 23 }, 24 25 // 刪除cookie 26 delCookie: function(c_name){ 27 var exp = new Date(); 28 exp.setTime(exp.getTime() - 1); 29 var cval = this.getCookie(c_name); 30 if(cval!=null){ 31 document.cookie = c_name + "=" + cval + ";expires=" + exp.toGMTString(); 32 } 33 },