123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- 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()
- }
- type WXOrderResponse struct {
- ReturnCode string `xml:"return_code"`
- ReturnMsg string `xml:"return_msg"`
- }
- func SendWXOrderResponse(c *binding_context.Context, statusCode int, _ any, err error) {
- if err != nil {
- c.XML(statusCode, &WXOrderResponse{
- ReturnCode: "FAIL",
- ReturnMsg: err.Error(),
- })
- return
- }
- c.XML(statusCode, &WXOrderResponse{
- ReturnCode: "SUCCESS",
- })
- }
|