You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.7 KiB

3 years ago
  1. package utils
  2. import (
  3. "github.com/flipped-aurora/gin-vue-admin/global"
  4. systemReq "github.com/flipped-aurora/gin-vue-admin/model/system/request"
  5. "github.com/gin-gonic/gin"
  6. uuid "github.com/satori/go.uuid"
  7. )
  8. // 从Gin的Context中获取从jwt解析出来的用户ID
  9. func GetUserID(c *gin.Context) uint {
  10. if claims, exists := c.Get("claims"); !exists {
  11. global.GVA_LOG.Error("从Gin的Context中获取从jwt解析出来的用户ID失败, 请检查路由是否使用jwt中间件!")
  12. return 0
  13. } else {
  14. waitUse := claims.(*systemReq.CustomClaims)
  15. return waitUse.ID
  16. }
  17. }
  18. // 从Gin的Context中获取从jwt解析出来的用户UUID
  19. func GetUserUuid(c *gin.Context) uuid.UUID {
  20. if claims, exists := c.Get("claims"); !exists {
  21. global.GVA_LOG.Error("从Gin的Context中获取从jwt解析出来的用户UUID失败, 请检查路由是否使用jwt中间件!")
  22. return uuid.UUID{}
  23. } else {
  24. waitUse := claims.(*systemReq.CustomClaims)
  25. return waitUse.UUID
  26. }
  27. }
  28. // 从Gin的Context中获取从jwt解析出来的用户角色id
  29. func GetUserAuthorityId(c *gin.Context) string {
  30. if claims, exists := c.Get("claims"); !exists {
  31. global.GVA_LOG.Error("从Gin的Context中获取从jwt解析出来的用户UUID失败, 请检查路由是否使用jwt中间件!")
  32. return ""
  33. } else {
  34. waitUse := claims.(*systemReq.CustomClaims)
  35. return waitUse.AuthorityId
  36. }
  37. }
  38. // 从Gin的Context中获取从jwt解析出来的用户角色id
  39. func GetUserInfo(c *gin.Context) *systemReq.CustomClaims {
  40. if claims, exists := c.Get("claims"); !exists {
  41. global.GVA_LOG.Error("从Gin的Context中获取从jwt解析出来的用户UUID失败, 请检查路由是否使用jwt中间件!")
  42. return nil
  43. } else {
  44. waitUse := claims.(*systemReq.CustomClaims)
  45. return waitUse
  46. }
  47. }