ResultData.go 772 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package router
  2. import (
  3. "demo/share"
  4. "github.com/gin-gonic/gin"
  5. "strconv"
  6. )
  7. func CreateResult() gin.H {
  8. return gin.H{
  9. "code": 200,
  10. "msg": "success",
  11. }
  12. }
  13. func CreateResultData(Data any) gin.H {
  14. return gin.H{
  15. "code": 200,
  16. "msg": "success",
  17. "data": Data,
  18. }
  19. }
  20. func CreateResultError(errCode int, errMsg string) gin.H {
  21. return gin.H{
  22. "code": errCode,
  23. "msg": errMsg,
  24. }
  25. }
  26. func GetUserIdByToken(c *gin.Context) int {
  27. header := c.GetHeader("auth-sign")
  28. if header == "" {
  29. c.JSON(200, CreateResultError(401, "用户未登录"))
  30. return 0
  31. }
  32. claims, err := share.ParseToken(header)
  33. if err != nil {
  34. c.JSON(200, CreateResultError(401, "用户未登录"))
  35. return 0
  36. }
  37. i, err := strconv.ParseInt(claims["iss"].(string), 10, 32)
  38. return int(i)
  39. }