From 7d0b3d7d6df79e4fb27caebdf6f97f7525b83ba3 Mon Sep 17 00:00:00 2001 From: lwl0608 Date: Fri, 29 Nov 2024 14:37:44 +0800 Subject: [PATCH] online mids --- api/adapter/firenzeapi/api.go | 50 ++++++++++++++++++++++++++++++---- api/base/response.go | 6 ++++ api/proto/firenze/websocket.go | 16 ++++++++++- 3 files changed, 65 insertions(+), 7 deletions(-) diff --git a/api/adapter/firenzeapi/api.go b/api/adapter/firenzeapi/api.go index 7abc938e..b9625576 100644 --- a/api/adapter/firenzeapi/api.go +++ b/api/adapter/firenzeapi/api.go @@ -16,10 +16,11 @@ var ( sendBizMsgUrl = "https://wsdebug.tiefen.fun/send_biz_msg" batchSendBizMsgUrl = "https://wsdebug.tiefen.fun/batch_send_biz_msg" sendBroadcastMsgUrl = "https://wsdebug.tiefen.fun/send_broadcast_msg" + onlineMidsUrl = "https://wsdebug.tiefen.fun/online_mids" ) // 给指定mid发消息 -func SendBizMsg(param *firenzeproto.SendBizMsgParam) (*base.BaseResponse, error) { +func SendBizMsg(param *firenzeproto.SendBizMsgParam) (*base.BaseRespHead, error) { defer logger.Recover() ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*2000) @@ -46,7 +47,7 @@ func SendBizMsg(param *firenzeproto.SendBizMsgParam) (*base.BaseResponse, error) } defer resp.Body.Close() - res := new(base.BaseResponse) + res := new(base.BaseRespHead) bs, _ := ioutil.ReadAll(resp.Body) err = json.Unmarshal(bs, &res) if err != nil { @@ -56,7 +57,7 @@ func SendBizMsg(param *firenzeproto.SendBizMsgParam) (*base.BaseResponse, error) } // 给批量mid发消息 -func BatchSendBizMsg(param *firenzeproto.BatchSendBizMsgParam) (*base.BaseResponse, error) { +func BatchSendBizMsg(param *firenzeproto.BatchSendBizMsgParam) (*base.BaseRespHead, error) { defer logger.Recover() ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*2000) @@ -83,7 +84,7 @@ func BatchSendBizMsg(param *firenzeproto.BatchSendBizMsgParam) (*base.BaseRespon } defer resp.Body.Close() - res := new(base.BaseResponse) + res := new(base.BaseRespHead) bs, _ := ioutil.ReadAll(resp.Body) err = json.Unmarshal(bs, &res) if err != nil { @@ -93,7 +94,7 @@ func BatchSendBizMsg(param *firenzeproto.BatchSendBizMsgParam) (*base.BaseRespon } // 给所有用户广播消息 -func SendBroadcastMsg(param *firenzeproto.SendBroadcastMsgParam) (*base.BaseResponse, error) { +func SendBroadcastMsg(param *firenzeproto.SendBroadcastMsgParam) (*base.BaseRespHead, error) { defer logger.Recover() ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*2000) @@ -120,7 +121,44 @@ func SendBroadcastMsg(param *firenzeproto.SendBroadcastMsgParam) (*base.BaseResp } defer resp.Body.Close() - res := new(base.BaseResponse) + res := new(base.BaseRespHead) + bs, _ := ioutil.ReadAll(resp.Body) + err = json.Unmarshal(bs, &res) + if err != nil { + return nil, err + } + return res, nil +} + +// 获取所有在线的mid +func OnlineMids(param *firenzeproto.OnlineMidsParam) (*firenzeproto.OnlineMidsResp, error) { + defer logger.Recover() + + ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*2000) + defer cancel() + + jsonStr, err := json.Marshal(param) + if err != nil { + logger.Error("build data error: %v", err) + return nil, err + } + + req, err := http.NewRequestWithContext(ctx, "POST", onlineMidsUrl, bytes.NewBuffer(jsonStr)) + if err != nil { + logger.Error("NewRequest error %v", err) + return nil, err + } + req.Header.Set("Content-Type", "application/json") + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + logger.Error("failed to add case, err : %v", err) + return nil, err + } + defer resp.Body.Close() + + res := new(firenzeproto.OnlineMidsResp) bs, _ := ioutil.ReadAll(resp.Body) err = json.Unmarshal(bs, &res) if err != nil { diff --git a/api/base/response.go b/api/base/response.go index de463caa..35efeeb5 100644 --- a/api/base/response.go +++ b/api/base/response.go @@ -15,6 +15,12 @@ type BaseResponse struct { RequestId string `json:"request_id"` } +type BaseRespHead struct { + Ret int `json:"ret"` + Msg string `json:"msg"` + ErrCode errcode.ErrCode `json:"errcode"` +} + func ReplyOk(ctx *gin.Context, data any) { ctx.JSON(http.StatusOK, BaseResponse{ Ret: consts.RetCodeSuccess, diff --git a/api/proto/firenze/websocket.go b/api/proto/firenze/websocket.go index 04a57a71..c41e35ce 100644 --- a/api/proto/firenze/websocket.go +++ b/api/proto/firenze/websocket.go @@ -1,6 +1,9 @@ package firenze -import "encoding/json" +import ( + "encoding/json" + "service/api/base" +) // 单条消息 type SendBizMsgParam struct { @@ -20,3 +23,14 @@ type BatchSendBizMsgParam struct { Mids []int64 `json:"mids"` Msg json.RawMessage `json:"msg"` } + +type OnlineMidsParam struct{} + +type OnlineMidsData struct { + Mids []int64 `json:"mids"` +} + +type OnlineMidsResp struct { + base.BaseRespHead + Data *OnlineMidsData `json:"data"` +}