response.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package response
  2. import (
  3. "encoding/json"
  4. "git.sxidc.com/go-tools/api_binding/http_binding/binding_context"
  5. "git.sxidc.com/service-supports/fserr"
  6. "git.sxidc.com/service-supports/fslog"
  7. "git.sxidc.com/service-supports/websocket"
  8. "net/http"
  9. )
  10. func NoResponse(_ *binding_context.Context, _ int, _ any, _ error) {
  11. return
  12. }
  13. func SendMsgResponse(c *binding_context.Context, statusCode int, _ any, err error) {
  14. msgResp := formMsgResponse(err)
  15. c.JSON(statusCode, msgResp)
  16. }
  17. type IDResponse[T IDType] struct {
  18. MsgResponse
  19. ID T `json:"id"`
  20. }
  21. func SendIDResponse[T IDType](c *binding_context.Context, statusCode int, id T, err error) {
  22. msgResp := formMsgResponse(err)
  23. c.JSON(statusCode, IDResponse[T]{
  24. MsgResponse: msgResp,
  25. ID: id,
  26. })
  27. }
  28. type InfoResponse[T any] struct {
  29. MsgResponse
  30. Info T `json:"info"`
  31. }
  32. func SendInfoResponse[T any](c *binding_context.Context, statusCode int, info T, err error) {
  33. msgResp := formMsgResponse(err)
  34. c.JSON(statusCode, InfoResponse[T]{
  35. MsgResponse: msgResp,
  36. Info: info,
  37. })
  38. }
  39. type InfosResponse[T any] struct {
  40. MsgResponse
  41. InfosData[T]
  42. }
  43. func SendInfosResponse[T any](c *binding_context.Context, statusCode int, data InfosData[T], err error) {
  44. msgResp := formMsgResponse(err)
  45. c.JSON(statusCode, InfosResponse[T]{
  46. MsgResponse: msgResp,
  47. InfosData: data,
  48. })
  49. }
  50. func StructToMap(originStruct any) map[string]any {
  51. jsonBytes, err := json.Marshal(originStruct)
  52. if err != nil {
  53. panic(err)
  54. }
  55. retMap := make(map[string]any)
  56. err = json.Unmarshal(jsonBytes, &retMap)
  57. if err != nil {
  58. panic(err)
  59. }
  60. return retMap
  61. }
  62. func SendMapResponse(c *binding_context.Context, statusCode int, data map[string]any, err error) {
  63. msgRespMap := formMapMsgResponse(err)
  64. for key, value := range data {
  65. msgRespMap[key] = value
  66. }
  67. c.JSON(statusCode, msgRespMap)
  68. }
  69. func SendRawMapResponse(c *binding_context.Context, statusCode int, data map[string]any, err error) {
  70. c.JSON(statusCode, data)
  71. }
  72. type MsgResponse struct {
  73. Success bool `json:"success"`
  74. ErrCode int `json:"errCode"`
  75. Msg string `json:"msg"`
  76. }
  77. func formMsgResponse(err error) MsgResponse {
  78. if err != nil {
  79. fslog.Error(err)
  80. serviceErr := fserr.ParseCode(err)
  81. return MsgResponse{
  82. Success: false,
  83. ErrCode: serviceErr.BusinessCode,
  84. Msg: serviceErr.Msg,
  85. }
  86. }
  87. return MsgResponse{
  88. Success: true,
  89. ErrCode: 0,
  90. Msg: "操作成功",
  91. }
  92. }
  93. func formMapMsgResponse(err error) map[string]any {
  94. resp := make(map[string]any)
  95. if err != nil {
  96. fslog.Error(err)
  97. serviceErr := fserr.ParseCode(err)
  98. resp["success"] = false
  99. resp["errCode"] = serviceErr.BusinessCode
  100. resp["msg"] = serviceErr.Msg
  101. return resp
  102. }
  103. resp["success"] = true
  104. resp["errCode"] = 0
  105. resp["msg"] = "操作成功"
  106. return resp
  107. }
  108. func WSSendMessage(groupID string, obj any) {
  109. message, err := json.Marshal(obj)
  110. if err != nil {
  111. err = fserr.ParseCode(err)
  112. fslog.Error(err)
  113. return
  114. }
  115. err = websocket.GetInstance().BroadCast(groupID, message)
  116. if err != nil {
  117. err = fserr.ParseCode(err)
  118. fslog.Error(err)
  119. return
  120. }
  121. }
  122. func SendString(c *binding_context.Context, statusCode int, data string, err error) {
  123. if err != nil {
  124. c.String(http.StatusOK, err.Error())
  125. return
  126. }
  127. c.String(http.StatusOK, data)
  128. }
  129. func WriteBytes(c *binding_context.Context, statusCode int, bytes []byte, err error) {
  130. if err != nil {
  131. c.AbortWithStatus(statusCode)
  132. return
  133. }
  134. _, err = c.Writer.Write(bytes)
  135. if err != nil {
  136. c.AbortWithStatus(statusCode)
  137. return
  138. }
  139. c.Writer.Flush()
  140. }
  141. func SendWXOrderResponse(c *binding_context.Context, statusCode int, _ any, err error) {
  142. if err == nil {
  143. c.XML(statusCode, map[string]any{
  144. "return_code": "FAIL",
  145. "return_msg": err.Error(),
  146. })
  147. return
  148. }
  149. c.XML(statusCode, map[string]any{
  150. "return_code": "SUCCESS",
  151. })
  152. }