1.收集表单信息
loginForm:
{
loginName: 'wangwu',
password: '12345678',
validateCode:'',
code:''
},
2.封装接口,设置headers
//获取图形验证码
export function getImg(params) {
return service6.request({
method: 'get',
url: 'validatecode/image/',
params
})
}
//登录
export function login(data) {
return service6.request({
method: 'post',
url: 'urm/login',
data: data,
headers: {
"ValidateCode": JSON.stringify({
"type": 1,
"imageValidateCode": {
"code": data.code,
"validateCode": data.validateCode
}
})
}
})
}
3.调用接口
async getImgVal(){
const {data:res} = await getImg({})
this.imgUrl = res.validateCodeImage
this.loginForm.code=res.code
}
async login () {
const res1 = await login(this.loginForm)
if(res1.code === 0){
//登录成功后给用户添加token
// window.sessionStorage.setItem('token', res.data.token)
this.$message.success('登录成功!')
this.$router.push('/index')
}else{
return this.$message.error(res.message)
}
}