1.安装JSEncrypt
npm install jsencrypt
2.在登陆页面引用
import { JSEncrypt } from "jsencrypt";
3.点击登录对表单验证
// 点击登录按钮
async loginClick(){
this.$refs.loginFormRef.validate(async valid=>{
if(!valid){
return
}
// 深拷贝表单对象
const form=JSON.parse(JSON.stringify(this.loginForm))
// 获取公钥
const res=await getPublicKey()
// 创建加密对象实例
let jsencrypt=new JSEncrypt()
// 设置公钥
jsencrypt.setPublicKey(res.data)
form.publicKey=res.data
// 对密码加密
form.password = jsencrypt.encrypt(form.password)
try {
const res=await login(qs.stringify(form))
if(res.status !==100010200 ){
this.getCodeData()
return this.$message.error(res.message)
}
console.log(res.message);
const { token}=res.data
this.$store.dispatch('router/setisLogin')
// 存储用户信息
this.$store.dispatch('router/SetUserInfo',res.data)
this.$message.success('登陆成功')
// 存储token
setToken(token)
// 跳转到首页
this.$router.push('/')
} catch (error) {
this.getCodeData()
}
})
}