user.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package user
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "gogs.baozhida.cn/zoie/OAuth-core/pkg"
  6. jwt "gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth"
  7. )
  8. func ExtractClaims(c *gin.Context) jwt.MapClaims {
  9. claims, exists := c.Get(jwt.JwtPayloadKey)
  10. if !exists {
  11. return make(jwt.MapClaims)
  12. }
  13. return claims.(jwt.MapClaims)
  14. }
  15. func Get(c *gin.Context, key string) interface{} {
  16. data := ExtractClaims(c)
  17. if data[key] != nil {
  18. return data[key]
  19. }
  20. fmt.Println(pkg.GetCurrentTimeStr() + " [WARING] " + c.Request.Method + " " + c.Request.URL.Path + " Get 缺少 " + key)
  21. return nil
  22. }
  23. func GetUUID(c *gin.Context) string {
  24. data := ExtractClaims(c)
  25. if data["uuid"] != nil {
  26. return (data["uuid"]).(string)
  27. }
  28. fmt.Println(pkg.GetCurrentTimeStr() + " [WARING] " + c.Request.Method + " " + c.Request.URL.Path + " GetUUID 缺少 uuid")
  29. return ""
  30. }
  31. func GetUserId(c *gin.Context) int {
  32. data := ExtractClaims(c)
  33. if data["identity"] != nil {
  34. return int((data["identity"]).(float64))
  35. }
  36. fmt.Println(pkg.GetCurrentTimeStr() + " [WARING] " + c.Request.Method + " " + c.Request.URL.Path + " GetUserId 缺少 identity")
  37. return 0
  38. }
  39. func GetUserIdStr(c *gin.Context) string {
  40. data := ExtractClaims(c)
  41. if data["identity"] != nil {
  42. return pkg.Int64ToString(int64((data["identity"]).(float64)))
  43. }
  44. fmt.Println(pkg.GetCurrentTimeStr() + " [WARING] " + c.Request.Method + " " + c.Request.URL.Path + " GetUserIdStr 缺少 identity")
  45. return ""
  46. }
  47. func GetUserName(c *gin.Context) string {
  48. data := ExtractClaims(c)
  49. if data["username"] != nil {
  50. return (data["username"]).(string)
  51. }
  52. fmt.Println(pkg.GetCurrentTimeStr() + " [WARING] " + c.Request.Method + " " + c.Request.URL.Path + " GetUserName 缺少 username")
  53. return ""
  54. }
  55. func GetRoleName(c *gin.Context) string {
  56. data := ExtractClaims(c)
  57. if data["roleName"] != nil {
  58. return (data["roleName"]).(string)
  59. }
  60. fmt.Println(pkg.GetCurrentTimeStr() + " [WARING] " + c.Request.Method + " " + c.Request.URL.Path + " GetRoleName 缺少 roleName")
  61. return ""
  62. }
  63. func GetRoleKey(c *gin.Context) string {
  64. data := ExtractClaims(c)
  65. if data["roleKey"] != nil {
  66. return (data["roleKey"]).(string)
  67. }
  68. fmt.Println(pkg.GetCurrentTimeStr() + " [WARING] " + c.Request.Method + " " + c.Request.URL.Path + " GetRoleKey 缺少 roleKey")
  69. return ""
  70. }
  71. func GetRoleId(c *gin.Context) int {
  72. data := ExtractClaims(c)
  73. if data["roleId"] != nil {
  74. i := int((data["roleId"]).(float64))
  75. return i
  76. }
  77. fmt.Println(pkg.GetCurrentTimeStr() + " [WARING] " + c.Request.Method + " " + c.Request.URL.Path + " GetRoleId 缺少 roleId")
  78. return 0
  79. }
  80. func GetDataScope(c *gin.Context) int {
  81. data := ExtractClaims(c)
  82. if data["dataScope"] != nil {
  83. i := int((data["dataScope"]).(float64))
  84. return i
  85. }
  86. fmt.Println(pkg.GetCurrentTimeStr() + " [WARING] " + c.Request.Method + " " + c.Request.URL.Path + " GetDataScope 缺少 dataScope")
  87. return 0
  88. }
  89. func GetDeptId(c *gin.Context) int {
  90. data := ExtractClaims(c)
  91. if data["deptId"] != nil {
  92. return int((data["deptId"]).(float64))
  93. }
  94. fmt.Println(pkg.GetCurrentTimeStr() + " [WARING] " + c.Request.Method + " " + c.Request.URL.Path + " GetDeptId 缺少 deptId")
  95. return 0
  96. }
  97. func GetDeptName(c *gin.Context) string {
  98. data := ExtractClaims(c)
  99. if data["deptName"] != nil {
  100. return (data["deptName"]).(string)
  101. }
  102. fmt.Println(pkg.GetCurrentTimeStr() + " [WARING] " + c.Request.Method + " " + c.Request.URL.Path + " GetDeptName 缺少 deptName")
  103. return ""
  104. }