online mids

This commit is contained in:
lwl0608 2024-11-29 14:37:44 +08:00
parent 5485b76a50
commit 7d0b3d7d6d
3 changed files with 65 additions and 7 deletions

View File

@ -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 {

View File

@ -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,

View File

@ -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"`
}