123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- package response
- import (
- "encoding/json"
- "git.sxidc.com/service-supports/fserr"
- "git.sxidc.com/service-supports/fslog"
- "git.sxidc.com/service-supports/websocket"
- "lcp/api/binding/http_binding/binding_context"
- )
- 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
- }
- }
|