package response import ( "encoding/json" "git.sxidc.com/go-tools/api_binding/http_binding/binding_context" "git.sxidc.com/service-supports/fserr" "git.sxidc.com/service-supports/fslog" "git.sxidc.com/service-supports/websocket" "net/http" ) func NoResponse(_ *binding_context.Context, _ int, _ any, _ error) { return } func SendMsgResponse(c *binding_context.Context, statusCode int, _ any, err error) { msgResp := formMsgResponse(err) c.JSON(statusCode, msgResp) } type IDResponse[T IDType] struct { MsgResponse ID T `json:"id"` } func SendIDResponse[T IDType](c *binding_context.Context, statusCode int, id T, err error) { msgResp := formMsgResponse(err) c.JSON(statusCode, IDResponse[T]{ MsgResponse: msgResp, ID: id, }) } type InfoResponse[T any] struct { MsgResponse Info T `json:"info"` } func SendInfoResponse[T any](c *binding_context.Context, statusCode int, info T, err error) { msgResp := formMsgResponse(err) c.JSON(statusCode, InfoResponse[T]{ MsgResponse: msgResp, Info: info, }) } type InfosResponse[T any] struct { MsgResponse InfosData[T] } func SendInfosResponse[T any](c *binding_context.Context, statusCode int, data InfosData[T], err error) { msgResp := formMsgResponse(err) c.JSON(statusCode, 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 *binding_context.Context, statusCode int, data map[string]any, err error) { msgRespMap := formMapMsgResponse(err) for key, value := range data { msgRespMap[key] = value } c.JSON(statusCode, 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 WSSendMessage(groupID string, obj any) { message, err := json.Marshal(obj) if err != nil { err = fserr.ParseCode(err) fslog.Error(err) return } err = websocket.GetInstance().BroadCast(groupID, message) if err != nil { err = fserr.ParseCode(err) fslog.Error(err) return } } func SendString(c *binding_context.Context, statusCode int, data string, err error) { if err != nil { c.String(http.StatusOK, err.Error()) return } c.String(http.StatusOK, data) } func WriteBytes(c *binding_context.Context, statusCode int, bytes []byte, err error) { if err != nil { c.AbortWithStatus(statusCode) return } _, err = c.Writer.Write(bytes) if err != nil { c.AbortWithStatus(statusCode) return } c.Writer.Flush() } func SendWXOrderResponse(c *binding_context.Context, statusCode int, _ any, err error) { if err == nil { c.XML(statusCode, map[string]any{ "return_code": "FAIL", "return_msg": err.Error(), }) return } c.XML(statusCode, map[string]any{ "return_code": "SUCCESS", }) }