Browse Source

修改错误单词,新增从gin.Context获取用户id,uuid,authority_id的函数

main
SliverHorn 4 years ago
parent
commit
eaedb37fce
  1. 45
      server/api/v1/sys_user.go

45
server/api/v1/sys_user.go

@ -1,6 +1,7 @@
package v1 package v1
import ( import (
"errors"
"fmt" "fmt"
"gin-vue-admin/global" "gin-vue-admin/global"
"gin-vue-admin/global/response" "gin-vue-admin/global/response"
@ -45,13 +46,12 @@ func Login(c *gin.Context) {
} else { } else {
response.FailWithMessage("验证码错误", c) response.FailWithMessage("验证码错误", c)
} }
} }
// 登录以后签发jwt // 登录以后签发jwt
func tokenNext(c *gin.Context, user model.SysUser) { func tokenNext(c *gin.Context, user model.SysUser) {
j := &middleware.JWT{SigningKey: []byte(global.GVA_CONFIG.JWT.SigningKey)} // 唯一签名 j := &middleware.JWT{SigningKey: []byte(global.GVA_CONFIG.JWT.SigningKey)} // 唯一签名
clams := request.CustomClaims{
claims := request.CustomClaims{
UUID: user.UUID, UUID: user.UUID,
ID: user.ID, ID: user.ID,
NickName: user.NickName, NickName: user.NickName,
@ -64,7 +64,7 @@ func tokenNext(c *gin.Context, user model.SysUser) {
Issuer: "qmPlus", // 签名的发行者 Issuer: "qmPlus", // 签名的发行者
}, },
} }
token, err := j.CreateToken(clams)
token, err := j.CreateToken(claims)
if err != nil { if err != nil {
response.FailWithMessage("获取token失败", c) response.FailWithMessage("获取token失败", c)
return return
@ -73,7 +73,7 @@ func tokenNext(c *gin.Context, user model.SysUser) {
response.OkWithData(resp.LoginResponse{ response.OkWithData(resp.LoginResponse{
User: user, User: user,
Token: token, Token: token,
ExpiresAt: clams.StandardClaims.ExpiresAt * 1000,
ExpiresAt: claims.StandardClaims.ExpiresAt * 1000,
}, c) }, c)
return return
} }
@ -86,7 +86,7 @@ func tokenNext(c *gin.Context, user model.SysUser) {
response.OkWithData(resp.LoginResponse{ response.OkWithData(resp.LoginResponse{
User: user, User: user,
Token: token, Token: token,
ExpiresAt: clams.StandardClaims.ExpiresAt * 1000,
ExpiresAt: claims.StandardClaims.ExpiresAt * 1000,
}, c) }, c)
} else if err != nil { } else if err != nil {
response.FailWithMessage(fmt.Sprintf("%v", err), c) response.FailWithMessage(fmt.Sprintf("%v", err), c)
@ -104,7 +104,7 @@ func tokenNext(c *gin.Context, user model.SysUser) {
response.OkWithData(resp.LoginResponse{ response.OkWithData(resp.LoginResponse{
User: user, User: user,
Token: token, Token: token,
ExpiresAt: clams.StandardClaims.ExpiresAt * 1000,
ExpiresAt: claims.StandardClaims.ExpiresAt * 1000,
}, c) }, c)
} }
} }
@ -272,3 +272,36 @@ func SetUserInfo(c *gin.Context) {
}, c) }, c)
} }
} }
// 从Gin的Context中获取从jwt解析出来的用户ID
func getUserID(c *gin.Context) (uint, error) {
if claims, exists := c.Get("claims"); !exists {
global.GVA_LOG.Error("从Gin的Context中获取从jwt解析出来的用户ID失败, 请检查路由是否使用jwt中间件")
return 0, errors.New("解析失败")
} else {
waitUse := claims.(*request.CustomClaims)
return waitUse.ID, nil
}
}
// 从Gin的Context中获取从jwt解析出来的用户UUID
func getUserUuid(c *gin.Context) (string, error) {
if claims, exists := c.Get("claims"); !exists {
global.GVA_LOG.Error("从Gin的Context中获取从jwt解析出来的用户UUID失败, 请检查路由是否使用jwt中间件")
return "", errors.New("解析失败")
} else {
waitUse := claims.(*request.CustomClaims)
return waitUse.UUID.String(), nil
}
}
// 从Gin的Context中获取从jwt解析出来的用户角色id
func getUserAuthorityId(c *gin.Context) (string, error) {
if claims, exists := c.Get("claims"); !exists {
global.GVA_LOG.Error("从Gin的Context中获取从jwt解析出来的用户UUID失败, 请检查路由是否使用jwt中间件")
return "", errors.New("解析失败")
} else {
waitUse := claims.(*request.CustomClaims)
return waitUse.AuthorityId, nil
}
}
Loading…
Cancel
Save