API接口,用户登录,获取用户信息,用户退出
这个是前端请求的用户相关接口。
路由:routers/apiRouters.go
func ApiRouter(router *gin.Engine) { //会员登录 router.POST("users/login", controllers.UserLogin) //使用JWT对用户的请求进行验证 user := router.Group("users/", middleware.CheckAuth) { //获取会员信息 user.POST("getuserinfo", controllers.GetUserInfo) user.POST("loginout", controllers.UserLoginout) } }
中间件:登录验证 api/middleware/usersMid.go
func CheckAuth(c *gin.Context) { token := c.PostForm("token") if token == "" { c.JSON(http.StatusOK, gin.H{"code": 500, "msg": "no token"}) c.Abort() //终止 return } claims := common.UserClaims{} _, err := jwt.ParseWithClaims(token, &claims, func(t *jwt.Token) (interface{}, error) { return common.JWTKEY, nil }) //验证失败 if err != nil { ve, _ := err.(*jwt.ValidationError) if ve.Errors == jwt.ValidationErrorExpired { c.JSON(http.StatusOK, gin.H{"code": 404, "msg": "token expired"}) } else { c.JSON(http.StatusOK, gin.H{"code": 405, "msg": "token invalid"}) } c.Abort() //终止 return } //获取redistoken,进行验证 idstr := strconv.Itoa(claims.UserId) red_token := common.GetRedisVal("token"+idstr) if red_token != token { c.JSON(http.StatusOK, gin.H{"code": 406, "msg": "token not exist"}) c.Abort() //终止 return } c.Set("cliaims", claims) //传递参数 }
控制器:api/controllers/users.go
// 定义结构体,接收表单提交的数据 type LoginUser struct { Name string `form:"name" binding:"required"` Password string `form:"password" binding:"required"` } //用户登录 func UserLogin(c *gin.Context) { //用户登录 var u LoginUser err := c.ShouldBind(&u) if err != nil { c.JSON(http.StatusOK, gin.H{"code": 500, "msg": err.Error()}) return } //密码验证 stat,msg,user := modes.UserLogin(u.Name,u.Password) if stat == false { c.JSON(http.StatusOK, gin.H{"code": 500, "msg": msg}) return } //登录成功,获取token返回给前端 token := common.GetToken(u.Name,user.Uid) c.JSON(http.StatusOK, gin.H{ "code": 0, "msg": "login success", "token": token, "user_id":user.Uid, }) return } //获取用户信息 func GetUserInfo(c *gin.Context) { var claims11 = common.UserClaims{} claims,_ := c.Get("cliaims") claims11 = claims.(common.UserClaims) uid := claims11.UserId //username := claims11.UserName user := modes.UsersItem(uid) //username := claims.UserName //判断token c.JSON(http.StatusOK, gin.H{ "code": 0, "msg": "GetUserInfo Success", "claims": claims, "uid":user.Uid, "username":user.Username, "usertype":user.Utype, }) } //退出 func UserLoginout(c *gin.Context) { var claims11 = common.UserClaims{} claims,_ := c.Get("cliaims") claims11 = claims.(common.UserClaims) //将token设置为空值,并过期 idstr := strconv.Itoa(claims11.UserId) common.SetRedisVal("token"+idstr,"",-1) c.JSON(http.StatusOK, gin.H{ "code": 0, "msg": "login out success", }) return }
模型:modes/UsersModel.go
//用户登录 func UserLogin(name string,pass string) (bool,string,Users) { var user = Users{} pass = common.MyMd5(pass) DB.Model(Users{}).Where("username=? and passwd = ?",name,pass).First(&user) if user.Uid == 0{ return false,"登录失败",user } if user.Ustatus == 2 { return false,"用户处于风控中,暂无法登录",user } if user.Ustatus == 2 { return false,"用户已被封禁,暂无法登录",user } return true,"登录成功",user }
但行好事,莫问前程!
本文来自博客园,作者:yangphp,转载请注明原文链接:https://www.cnblogs.com/ypeih/p/17351159.html