response.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. type MsgResponse struct {
  70. Success bool `json:"success"`
  71. ErrCode int `json:"errCode"`
  72. Msg string `json:"msg"`
  73. }
  74. func formMsgResponse(err error) MsgResponse {
  75. if err != nil {
  76. fslog.Error(err)
  77. serviceErr := fserr.ParseCode(err)
  78. return MsgResponse{
  79. Success: false,
  80. ErrCode: serviceErr.BusinessCode,
  81. Msg: serviceErr.Msg,
  82. }
  83. }
  84. return MsgResponse{
  85. Success: true,
  86. ErrCode: 0,
  87. Msg: "操作成功",
  88. }
  89. }
  90. func formMapMsgResponse(err error) map[string]any {
  91. resp := make(map[string]any)
  92. if err != nil {
  93. fslog.Error(err)
  94. serviceErr := fserr.ParseCode(err)
  95. resp["success"] = false
  96. resp["errCode"] = serviceErr.BusinessCode
  97. resp["msg"] = serviceErr.Msg
  98. return resp
  99. }
  100. resp["success"] = true
  101. resp["errCode"] = 0
  102. resp["msg"] = "操作成功"
  103. return resp
  104. }
  105. func WSSendMessage(groupID string, obj any) {
  106. message, err := json.Marshal(obj)
  107. if err != nil {
  108. err = fserr.ParseCode(err)
  109. fslog.Error(err)
  110. return
  111. }
  112. err = websocket.GetInstance().BroadCast(groupID, message)
  113. if err != nil {
  114. err = fserr.ParseCode(err)
  115. fslog.Error(err)
  116. return
  117. }
  118. }
  119. func SendString(c *binding_context.Context, statusCode int, data string, err error) {
  120. if err != nil {
  121. c.String(http.StatusOK, err.Error())
  122. return
  123. }
  124. c.String(http.StatusOK, data)
  125. }
  126. func WriteBytes(c *binding_context.Context, statusCode int, bytes []byte, err error) {
  127. if err != nil {
  128. c.AbortWithStatus(statusCode)
  129. return
  130. }
  131. _, err = c.Writer.Write(bytes)
  132. if err != nil {
  133. c.AbortWithStatus(statusCode)
  134. return
  135. }
  136. c.Writer.Flush()
  137. }
  138. type WXOrderResponse struct {
  139. ReturnCode string `xml:"return_code"`
  140. ReturnMsg string `xml:"return_msg"`
  141. }
  142. func SendWXOrderResponse(c *binding_context.Context, statusCode int, _ any, err error) {
  143. if err != nil {
  144. c.XML(statusCode, &WXOrderResponse{
  145. ReturnCode: "FAIL",
  146. ReturnMsg: err.Error(),
  147. })
  148. return
  149. }
  150. c.XML(statusCode, &WXOrderResponse{
  151. ReturnCode: "SUCCESS",
  152. })
  153. }