一般如果登錄頻繁,想要記住用戶名密碼的話,可設置cookie,以下是登錄方法與設置cookie,認真閱讀哦,希望對你有幫助。
1、先獻上一個簡單的頁面
<form class="form-signin" role="form" method="post" action="">
<div class="input-group" style="display: inline-block;">
<input type="text" class="srk" id="j_UserName" placeholder="用戶名" required="">
</div>
<div class="input-group has-feedback" style="display: inline-block;">
<input type="Password" class="srk ui-keyboard-input ui-corner-all" id="j_Password" aria-haspopup="true" role="textbox">
</div>
<div class="btnDiv" style="display: inline-block;position: absolute\9;padding-left: 1px;">
<button id="form-ok" class="btndl" type="button" @click="login">登 錄</button>
<button id="form-ok1" class="btndl" type="button" @click="register">注 冊</button>
</div>
<div style="margin: 20px 3px 10px 3px;">
<input type="checkbox" value="remember-me-nm" name="savenm" id="savenm" class="check">
<span class="song">記住用戶名</span>
<input type="checkbox" value="remember-me-pw" name="savepw" id="savepw" class="check">
<span class="song">記住密碼</span>
<span class="song" style="color: red;padding-left: 12%;">{{message}}</span>
</div>
</form>
2、具體方法
methods:{
//登錄方法
login(){
//判斷是否勾選記住密碼的選項
if (document.getElementById("savepw").checked == true) { //記住密碼
//傳入賬號名,密碼,和保存天數3個參數
this.setCookie($("#j_UserName").val(), $("#j_Password").val(), 7, true, true);
} else {
if (document.getElementById("savenm").checked == true) { //記住用戶名
this.setCookie($("#j_UserName").val(), "", 7, false, true);
} else {
console.log("清空Cookie");
//清空Cookie
this.clearCookie();
}
}
let data = {
'UserName': $("#j_UserName").val(), //用戶名
'Password': $("#j_Password").val() //密碼
}
/*接口請求*/
this.$http.post(this.GLOBAL.serverSrc + '/api/Login/LoginOn', data, {
header: {},
emulateJSON: true
}).then((res) => {
console.info(res);
}).catch(err => {
console.info(res);
})
}
//設置cookie
setCookie(c_name, c_pwd, exdays, check, checkNm) {
var exdate = new Date(); //獲取時間
exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays); //保存的天數
//字符串拼接cookie
window.document.cookie = "UserName" + "=" + c_name + ";path=/;expires=" + exdate.toGMTString();
if (document.getElementById("savenm").checked == true && document.getElementById("savepw").checked == false) {
window.document.cookie = "Password" + "=" + "" + ";path=/;expires=" + exdate.toGMTString();
}
if (document.getElementById("savepw").checked == true) {
window.document.cookie = "Password" + "=" + c_pwd + ";path=/;expires=" + exdate.toGMTString();
}
window.document.cookie = "checked" + "=" + check + ";path=/;expires=" + exdate.toGMTString();
window.document.cookie = "checkedNm" + "=" + checkNm + ";path=/;expires=" + exdate.toGMTString();
},
//讀取cookie
getCookie: function() {
if (document.cookie.length > 0) {
var arr = document.cookie.split('; '); //這裏顯示的格式需要切割一下自己可輸出看下
for (var i = 0; i < arr.length; i++) {
var arr2 = arr[i].split('='); //再次切割
//判斷查找相對應的值
if (arr2[0] == 'UserName') {
$("#j_UserName").val(arr2[1]); //保存到保存數據的地方
} else if (arr2[0] == 'Password') {
$("#j_Password").val(arr2[1]);
} else if (arr2[0] == 'checked') {
if (arr2[1] == "false") {
document.getElementById("savepw").checked = false;
} else {
document.getElementById("savepw").checked = arr2[1];
}
} else if (arr2[0] == 'checkedNm') {
document.getElementById("savenm").checked = arr2[1];
}
}
}
},
//清除cookie
clearCookie: function() {
this.setCookie("", "", "", "", -1); //修改4值都爲空,天數爲負1天就好了
}
}