1、安裝jsencrypt
cnpm install jsencrypt --save
2、在main.js中引入jsencrypt
import?JSEncrypt?from?'jsencrypt'
const?PUBLIC_KEY?=?'你的公鑰字符串'
Vue.prototype.$getRsaCode?=?(str)?=>?{?//?注冊方法
??let?pubKey?=?`-----BEGIN?PUBLIC?KEY-----${PUBLIC_KEY}-----END?PUBLIC?KEY-----`?//?ES6?模板字符串?引用?rsa?公鑰
??let?encryptStr?=?new?JSEncrypt()
??encryptStr.setPublicKey(pubKey)?//?設(shè)置?加密公鑰
??let?data?=?encryptStr.encrypt(str.toString())?//?進(jìn)行加密
??return?data
}
3、調(diào)用登錄方法時(shí),調(diào)用getRsaCode 方法
login({
? ? ?name:?_this.username,
? ? ?pwd:?_this.$getRsaCode(_this.password)
}).then((res)?=>?{
? ? ? // ...
})