user.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package tools
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. jwt "device-manage/pkg/jwtauth"
  6. )
  7. func ExtractClaims(c *gin.Context) jwt.MapClaims {
  8. claims, exists := c.Get(jwt.JwtPayloadKey)
  9. if !exists {
  10. return make(jwt.MapClaims)
  11. }
  12. return claims.(jwt.MapClaims)
  13. }
  14. func GetUserIdUint(c *gin.Context) uint {
  15. data := ExtractClaims(c)
  16. if data["identity"] != nil {
  17. return uint((data["identity"]).(float64))
  18. }
  19. fmt.Println(GetCurrentTimeStr() + " [WARING] " + c.Request.Method + " " + c.Request.URL.Path + " GetUserId 缺少identity")
  20. return 0
  21. }
  22. func GetUserId(c *gin.Context) int {
  23. data := ExtractClaims(c)
  24. if data["identity"] != nil {
  25. return int((data["identity"]).(float64))
  26. }
  27. fmt.Println(GetCurrentTimeStr() + " [WARING] " + c.Request.Method + " " + c.Request.URL.Path + " GetUserId 缺少identity")
  28. return 0
  29. }
  30. func GetUserIdStr(c *gin.Context) string {
  31. data := ExtractClaims(c)
  32. fmt.Println(">>>>>>>>>>>>")
  33. fmt.Println(data["identity"])
  34. if data["identity"] != nil {
  35. return Int64ToString(int64((data["identity"]).(float64)))
  36. }
  37. fmt.Println(GetCurrentTimeStr() + " [WARING] " + c.Request.Method + " " + c.Request.URL.Path + " GetUserIdStr 缺少identity")
  38. return ""
  39. }
  40. func GetUserName(c *gin.Context) string {
  41. data := ExtractClaims(c)
  42. if data["nice"] != nil {
  43. return (data["nice"]).(string)
  44. }
  45. fmt.Println(GetCurrentTimeStr() + " [WARING] " + c.Request.Method + " " + c.Request.URL.Path + " GetUserName 缺少nice")
  46. return ""
  47. }
  48. func GetRoleName(c *gin.Context) string {
  49. data := ExtractClaims(c)
  50. if data["rolekey"] != nil {
  51. return (data["rolekey"]).(string)
  52. }
  53. fmt.Println(GetCurrentTimeStr() + " [WARING] " + c.Request.Method + " " + c.Request.URL.Path + " GetRoleName 缺少rolekey")
  54. return ""
  55. }
  56. func GetRoleId(c *gin.Context) int {
  57. data := ExtractClaims(c)
  58. if data["roleid"] != nil {
  59. i := int((data["roleid"]).(float64))
  60. return i
  61. }
  62. fmt.Println(GetCurrentTimeStr() + " [WARING] " + c.Request.Method + " " + c.Request.URL.Path + " GetRoleId 缺少roleid")
  63. return 0
  64. }