yarn add jsencrypt

因为next.js在加载时是没有window对象的 这个库引用了window对象导致报错 无奈出此下策

export const pub = `
-----BEGIN PUBLIC KEY-----
xxxxxxxxxxxxxxxxxxxxxxxxxx
-----END PUBLIC KEY-----`;

const RASEncrypt = () => {
if (window.jsencrypt) {
return window.jsencrypt;
} else {
const { JSEncrypt } = require("jsencrypt");
const instance = new JSEncrypt();
instance.setPublicKey(pub);
window.jsencrypt = instance;
return instance;
}
};

export default RASEncrypt;

使用

const enPassword = RASEncrypt().encrypt(password);
const user = { username, password: enPassword };