这个是我在用Vue脚手架的时候写的,JS可以稍微改一下
//设置cookie方法
setCookie(portId, psw, exdays) {
// Encrypt,加密账号密码
var cipherPortId = CryptoJS.AES.encrypt(
portId + "",
"secretkey123"
).toString();
var cipherPsw = CryptoJS.AES.encrypt(psw + "", "secretkey123").toString();
var exdate = new Date(); //获取时间
exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays); //保存的天数
//字符串拼接cookie,为什么这里用了==,因为加密后的字符串也有个=号,影响下面getcookie的字符串切割,你也可以使用更炫酷的符号。
window.document.cookie =
"username" +
"==" +
cipherPortId +
";path=/;expires=" +
exdate.toGMTString();
window.document.cookie =
"password" +
"==" +
cipherPsw +
";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") {
// Decrypt,将解密后的内容赋值给账号
var bytes = CryptoJS.AES.decrypt(arr2[1], "secretkey123");
this.username = bytes.toString(CryptoJS.enc.Utf8) - 0;
} else if (arr2[0] == "password") {
// Decrypt,将解密后的内容赋值给密码
var bytes = CryptoJS.AES.decrypt(arr2[1], "secretkey123");
this.password = bytes.toString(CryptoJS.enc.Utf8);
}
}
}
},
//清除cookie
clearCookie() {
this.setCookie("", "", 0); //账号密码置空,天数置0
}
© 版权声明
站内部分资源由网友投稿或收集于网络,若侵犯了您的合法权益,请联系我们删除
THE END
暂无评论内容