今天讲的:
pycharm:
class LoginView(APIView): def post(self,request): account = request.data.get("account") password = request.data.get("password") rule = r"^1[3-9][0-9]{9}" if not re.findall(rule,account): user_info = User.objects.filter(username=account).first() else: user_info = User.objects.filter(mobile=account).first() if not user_info: return Response({ "code":400, "msg":"用户或者密码错误" }) if user_info.password != password: return Response({ "code": 400, "msg": "用户或者密码错误" }) import jwt,time from django.conf import settings payload = { "user_id":user_info.id, "username":user_info.username, "exp":int(time.time()+(60*60*24*30)), } token = jwt.encode(payload,key=settings.SECRET_KEY,algorithm="HS256") return Response({ "code": 200, "msg": "用户登录成功", "user":{ "name":user_info.username, "mobile":user_info.mobile, }, "token":token })
vue:
Login() {
// 通过element自定义表单校验规则,校验用户输入的用户信息
this.$refs["ruleForm"].validate(valid => {
//如果通过校验开始登录
if (valid) {
// TODO 登录
console.log("开始登录,用户名", this.LoginUser.name, "密码", this.LoginUser.pass)
this.$axios.post('/user/login/',{"account":this.LoginUser.name,"password":this.LoginUser.pass}).then(res=>{
if(res.data.code == 200){
this.LoginUser.name = ""
this.LoginUser.pass = ""
this.setShowLogin(false)
this.setUser({"userName":res.data.user.name})
localStorage.setItem("user",JSON.stringify(res.data.user))
localStorage.setItem("token",JSON.res.data.token)
this.notifySucceed(res.data.msg)
}else{
this.notifyError(res.data.msg)
}
}).catch(err=>{
console.log(err)
this.notifyError("页面错误了")
})
}
});
},