response.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package common
  2. import (
  3. "git.shuncheng.lu/bigthing/gocommon/pkg/cerror"
  4. "git.shuncheng.lu/bigthing/gocommon/pkg/internal/util"
  5. "git.shuncheng.lu/bigthing/gocommon/pkg/net/engines"
  6. )
  7. const (
  8. SuccessCode = util.SuccessCode
  9. FailCode = util.FailCode
  10. HttpStatus = util.HttpStatus
  11. )
  12. func SuccessJson(c *engines.Context, data interface{}) {
  13. err := c.JSON(
  14. HttpStatus,
  15. util.NewSuccessHttpResponse(data),
  16. )
  17. if err != nil {
  18. util.Errorf("[Http-Response] SuccessJson find err, err: %v", err)
  19. }
  20. }
  21. func FailJson(c *engines.Context, err cerror.Cerror) {
  22. rerr := c.JSON(
  23. HttpStatus,
  24. util.NewFailHttpResponse(err),
  25. )
  26. if rerr != nil {
  27. util.Errorf("[Http-Response] FailJson find err, err: %v", err)
  28. }
  29. }
  30. func FailJsonWithMsg(c *engines.Context, message string) {
  31. err := c.JSON(
  32. HttpStatus,
  33. util.NewFailMessageHttpResponse(message),
  34. )
  35. if err != nil {
  36. util.Errorf("[Http-Response] FailJsonWithMsg find err, err: %v", err)
  37. }
  38. }
  39. /**
  40. MiddlewareBind 中绑定的数据,key(header)->value(header_data)
  41. */
  42. func GetMiddlewareBindHeaderData(c *engines.Context) map[string]string {
  43. result, _ := c.Value(util.HeaderData).(map[string]string)
  44. if result == nil {
  45. return map[string]string{}
  46. }
  47. return result
  48. }