response.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package response
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "git.sxidc.com/go-tools/api_binding/mqtt_binding/mqtt_client"
  6. "git.sxidc.com/go-tools/api_binding/mqtt_binding/mqtt_client/router"
  7. "git.sxidc.com/service-supports/fserr"
  8. "git.sxidc.com/service-supports/fslog"
  9. )
  10. func NoResponse(_ *mqtt_client.MqttClient, _ *router.Item, _ Identifier, _ any, _ error) {
  11. return
  12. }
  13. func SendMsgResponse(c *mqtt_client.MqttClient, item *router.Item, responseIdentifier Identifier, _ any, err error) {
  14. msgResp := formMsgResponse(err)
  15. jsonResponse(c, item, responseIdentifier, msgResp)
  16. }
  17. type IDResponse[T IDType] struct {
  18. MsgResponse
  19. ID T `json:"id"`
  20. }
  21. func SendIDResponse[T IDType](c *mqtt_client.MqttClient, item *router.Item, responseIdentifier Identifier, id T, err error) {
  22. msgResp := formMsgResponse(err)
  23. jsonResponse(c, item, responseIdentifier, 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 *mqtt_client.MqttClient, item *router.Item, responseIdentifier Identifier, info T, err error) {
  33. msgResp := formMsgResponse(err)
  34. jsonResponse(c, item, responseIdentifier, 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 *mqtt_client.MqttClient, item *router.Item, responseIdentifier Identifier, data InfosData[T], err error) {
  44. msgResp := formMsgResponse(err)
  45. jsonResponse(c, item, responseIdentifier, 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 *mqtt_client.MqttClient, item *router.Item, responseIdentifier Identifier, data map[string]any, err error) {
  63. msgRespMap := formMapMsgResponse(err)
  64. for key, value := range data {
  65. msgRespMap[key] = value
  66. }
  67. jsonResponse(c, item, responseIdentifier, 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 SendString(c *mqtt_client.MqttClient, item *router.Item, responseIdentifier Identifier, data string, err error) {
  106. if err != nil {
  107. bytesResponse(c, item, responseIdentifier, []byte(err.Error()))
  108. return
  109. }
  110. bytesResponse(c, item, responseIdentifier, []byte(data))
  111. }
  112. func WriteBytes(c *mqtt_client.MqttClient, item *router.Item, responseIdentifier Identifier, bytes []byte, err error) {
  113. if err != nil {
  114. bytesResponse(c, item, responseIdentifier, []byte(err.Error()))
  115. return
  116. }
  117. bytesResponse(c, item, responseIdentifier, bytes)
  118. }
  119. func jsonResponse(c *mqtt_client.MqttClient, item *router.Item, responseIdentifier Identifier, obj any) {
  120. jsonBytes, err := json.Marshal(obj)
  121. if err != nil {
  122. panic(err)
  123. }
  124. err = c.Response(item, responseIdentifier.Identifier(), jsonBytes)
  125. if err != nil {
  126. fmt.Println("发送mqtt json响应失败: " + err.Error())
  127. return
  128. }
  129. }
  130. func bytesResponse(c *mqtt_client.MqttClient, item *router.Item, responseIdentifier Identifier, obj []byte) {
  131. err := c.Response(item, responseIdentifier.Identifier(), obj)
  132. if err != nil {
  133. fmt.Println("发送mqtt bytes响应失败")
  134. return
  135. }
  136. }