安装 `jsencrypt` 库
在你的 Vue.js 项目中运行以下命令来安装 `jsencrypt`:
bash
npm install jsencrypt --save
实现密码加密
// 引入 jsencrypt
import JSEncrypt from 'jsencrypt';
let Base64 = require("js-base64").Base64;
getPublicKey().then((resKey) => {
const encrypt = new JSEncrypt(); // 初始化 JSEncrypt
const publicKey = `${resKey.data}`; // 设置公钥(从后端获取并配置)
encrypt.setPublicKey(publicKey);
// 加密密码
this.encryptedUsername = encrypt.encrypt(this.loginForm.username);
this.encryptedPassword = encrypt.encrypt(this.loginForm.password);
let postBody = {
username: Base64.encode(this.encryptedUsername),
password: Base64.encode(this.encryptedPassword),
};
loginCheck(postBody).then((res) => {
this.phone = res.phoneNumber;
this.openView = true;
if (this.times == 60) {
this.sendPhoneCode();
} else {
this.timer = setInterval(() => {
this.times--;
if (this.times === 0) {
this.codeFlag = false;
this.times = 60;
clearInterval(this.timer);
}
}, 1000);
}
});
});

1770

被折叠的 条评论
为什么被折叠?



