123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- 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
- }
- }
|