response.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package response
  2. import (
  3. "encoding/json"
  4. "git.sxidc.com/service-supports/fserr"
  5. "git.sxidc.com/service-supports/fslog"
  6. "git.sxidc.com/service-supports/websocket"
  7. "lcp/api/binding/http_binding/binding_context"
  8. )
  9. func SendMsgResponse(c *binding_context.Context, statusCode int, _ any, err error) {
  10. msgResp := formMsgResponse(err)
  11. c.JSON(statusCode, msgResp)
  12. }
  13. type IDResponse[T IDType] struct {
  14. MsgResponse
  15. ID T `json:"id"`
  16. }
  17. func SendIDResponse[T IDType](c *binding_context.Context, statusCode int, id T, err error) {
  18. msgResp := formMsgResponse(err)
  19. c.JSON(statusCode, IDResponse[T]{
  20. MsgResponse: msgResp,
  21. ID: id,
  22. })
  23. }
  24. type InfoResponse[T any] struct {
  25. MsgResponse
  26. Info T `json:"info"`
  27. }
  28. func SendInfoResponse[T any](c *binding_context.Context, statusCode int, info T, err error) {
  29. msgResp := formMsgResponse(err)
  30. c.JSON(statusCode, InfoResponse[T]{
  31. MsgResponse: msgResp,
  32. Info: info,
  33. })
  34. }
  35. type InfosResponse[T any] struct {
  36. MsgResponse
  37. InfosData[T]
  38. }
  39. func SendInfosResponse[T any](c *binding_context.Context, statusCode int, data InfosData[T], err error) {
  40. msgResp := formMsgResponse(err)
  41. c.JSON(statusCode, InfosResponse[T]{
  42. MsgResponse: msgResp,
  43. InfosData: data,
  44. })
  45. }
  46. func StructToMap(originStruct any) map[string]any {
  47. jsonBytes, err := json.Marshal(originStruct)
  48. if err != nil {
  49. panic(err)
  50. }
  51. retMap := make(map[string]any)
  52. err = json.Unmarshal(jsonBytes, &retMap)
  53. if err != nil {
  54. panic(err)
  55. }
  56. return retMap
  57. }
  58. func SendMapResponse(c *binding_context.Context, statusCode int, data map[string]any, err error) {
  59. msgRespMap := formMapMsgResponse(err)
  60. for key, value := range data {
  61. msgRespMap[key] = value
  62. }
  63. c.JSON(statusCode, msgRespMap)
  64. }
  65. type MsgResponse struct {
  66. Success bool `json:"success"`
  67. ErrCode int `json:"errCode"`
  68. Msg string `json:"msg"`
  69. }
  70. func formMsgResponse(err error) MsgResponse {
  71. if err != nil {
  72. fslog.Error(err)
  73. serviceErr := fserr.ParseCode(err)
  74. return MsgResponse{
  75. Success: false,
  76. ErrCode: serviceErr.BusinessCode,
  77. Msg: serviceErr.Msg,
  78. }
  79. }
  80. return MsgResponse{
  81. Success: true,
  82. ErrCode: 0,
  83. Msg: "操作成功",
  84. }
  85. }
  86. func formMapMsgResponse(err error) map[string]any {
  87. resp := make(map[string]any)
  88. if err != nil {
  89. fslog.Error(err)
  90. serviceErr := fserr.ParseCode(err)
  91. resp["success"] = false
  92. resp["errCode"] = serviceErr.BusinessCode
  93. resp["msg"] = serviceErr.Msg
  94. return resp
  95. }
  96. resp["success"] = true
  97. resp["errCode"] = 0
  98. resp["msg"] = "操作成功"
  99. return resp
  100. }
  101. func WSSendMessage(groupID string, obj any) {
  102. message, err := json.Marshal(obj)
  103. if err != nil {
  104. err = fserr.ParseCode(err)
  105. fslog.Error(err)
  106. return
  107. }
  108. err = websocket.GetInstance().BroadCast(groupID, message)
  109. if err != nil {
  110. err = fserr.ParseCode(err)
  111. fslog.Error(err)
  112. return
  113. }
  114. }