package response import ( "encoding/json" "fmt" "git.sxidc.com/go-tools/api_binding/mqtt_binding/mqtt_client" "git.sxidc.com/go-tools/api_binding/mqtt_binding/mqtt_client/router" "git.sxidc.com/service-supports/fserr" "git.sxidc.com/service-supports/fslog" ) func NoResponse(_ *mqtt_client.MqttClient, _ *router.Item, _ Identifier, _ any, _ error) { return } func SendMsgResponse(c *mqtt_client.MqttClient, item *router.Item, responseIdentifier Identifier, _ any, err error) { msgResp := formMsgResponse(err) jsonResponse(c, item, responseIdentifier, msgResp) } type IDResponse[T IDType] struct { MsgResponse ID T `json:"id"` } func SendIDResponse[T IDType](c *mqtt_client.MqttClient, item *router.Item, responseIdentifier Identifier, id T, err error) { msgResp := formMsgResponse(err) jsonResponse(c, item, responseIdentifier, IDResponse[T]{ MsgResponse: msgResp, ID: id, }) } type InfoResponse[T any] struct { MsgResponse Info T `json:"info"` } func SendInfoResponse[T any](c *mqtt_client.MqttClient, item *router.Item, responseIdentifier Identifier, info T, err error) { msgResp := formMsgResponse(err) jsonResponse(c, item, responseIdentifier, InfoResponse[T]{ MsgResponse: msgResp, Info: info, }) } type InfosResponse[T any] struct { MsgResponse InfosData[T] } func SendInfosResponse[T any](c *mqtt_client.MqttClient, item *router.Item, responseIdentifier Identifier, data InfosData[T], err error) { msgResp := formMsgResponse(err) jsonResponse(c, item, responseIdentifier, InfosResponse[T]{ MsgResponse: msgResp, InfosData: data, }) } func StructToMap(originStruct any) map[string]any { jsonBytes, err := json.Marshal(originStruct) if err != nil { panic(err) } retMap := make(map[string]any) err = json.Unmarshal(jsonBytes, &retMap) if err != nil { panic(err) } return retMap } func SendMapResponse(c *mqtt_client.MqttClient, item *router.Item, responseIdentifier Identifier, data map[string]any, err error) { msgRespMap := formMapMsgResponse(err) for key, value := range data { msgRespMap[key] = value } jsonResponse(c, item, responseIdentifier, msgRespMap) } type MsgResponse struct { Success bool `json:"success"` ErrCode int `json:"errCode"` Msg string `json:"msg"` } func formMsgResponse(err error) MsgResponse { if err != nil { fslog.Error(err) serviceErr := fserr.ParseCode(err) return MsgResponse{ Success: false, ErrCode: serviceErr.BusinessCode, Msg: serviceErr.Msg, } } return MsgResponse{ Success: true, ErrCode: 0, Msg: "操作成功", } } func formMapMsgResponse(err error) map[string]any { resp := make(map[string]any) if err != nil { fslog.Error(err) serviceErr := fserr.ParseCode(err) resp["success"] = false resp["errCode"] = serviceErr.BusinessCode resp["msg"] = serviceErr.Msg return resp } resp["success"] = true resp["errCode"] = 0 resp["msg"] = "操作成功" return resp } func SendString(c *mqtt_client.MqttClient, item *router.Item, responseIdentifier Identifier, data string, err error) { if err != nil { bytesResponse(c, item, responseIdentifier, []byte(err.Error())) return } bytesResponse(c, item, responseIdentifier, []byte(data)) } func WriteBytes(c *mqtt_client.MqttClient, item *router.Item, responseIdentifier Identifier, bytes []byte, err error) { if err != nil { bytesResponse(c, item, responseIdentifier, []byte(err.Error())) return } bytesResponse(c, item, responseIdentifier, bytes) } func jsonResponse(c *mqtt_client.MqttClient, item *router.Item, responseIdentifier Identifier, obj any) { jsonBytes, err := json.Marshal(obj) if err != nil { panic(err) } err = c.Response(item, responseIdentifier.Identifier(), jsonBytes) if err != nil { fmt.Println("发送mqtt json响应失败: " + err.Error()) return } } func bytesResponse(c *mqtt_client.MqttClient, item *router.Item, responseIdentifier Identifier, obj []byte) { err := c.Response(item, responseIdentifier.Identifier(), obj) if err != nil { fmt.Println("发送mqtt bytes响应失败") return } }