From 8bde0f660d5f416ba7b2d7d87418d225a075c5b0 Mon Sep 17 00:00:00 2001 From: Yangtao Date: Tue, 2 Dec 2025 13:44:25 +0800 Subject: [PATCH] feat(app): update --- pkg/common/netease/ImClient.go | 118 +++++++++++++++++++++++++-------- 1 file changed, 91 insertions(+), 27 deletions(-) diff --git a/pkg/common/netease/ImClient.go b/pkg/common/netease/ImClient.go index 8ae1181..f1c45c0 100644 --- a/pkg/common/netease/ImClient.go +++ b/pkg/common/netease/ImClient.go @@ -736,6 +736,50 @@ func (client *ImClient) CreateChatGroup(ctx context.Context, ownerUserId string, return cast.ToString(body.Data.TeamInfo.TeamID), nil } +// P2PMsgSend 发送单聊消息 +func (client *ImClient) P2PMsgSend(from, to, _type, content, msg string) error { + url := "https://api.netease.im/nimserver/msg/sendMsg.action" + httpMethod := common.HTTP_METHOD_POST + header := client.generateHeader() + sb := HyTools.NewStringBuilder() + sb.Append("from=" + from) + sb.Append("&") + sb.Append("ope=" + "0") + sb.Append("&") + sb.Append("to=" + to) + sb.Append("&") + sb.Append("type=" + _type) // 0:文本消息1:图片消息2:语音消息3:视频消息4:地理位置消息6:文件消息10:提示消息100:自定义消息, + sb.Append("&") + sb.Append("body=" + content) + sb.Append("&") + sb.Append("antispam=true") + res, err := HyTools.HttpDo(httpMethod, url, header, sb.ToString()) + // code Integer 状态码 + // tid Long 网易云信服务器产生,群唯一标识 + // faccid String 入群失败的账号(accid)列表,格式为 JSONArray,如果创建时邀请的成员中存在加群数量超过限制的情况,会返回入群失败的 accid 以及附言(msg) + if err != nil { + // {"desc":"already register","code":414} + log.InfoF("GroupMsgSend error %s:", err.Error()) + return err + } else { + var resDTO CreateImRes + json.Unmarshal([]byte(res), &resDTO) + if resDTO.Code != 200 { + log.InfoF("GroupMsgSend code error %s", resDTO.Desc) + } else { + log.InfoF("GroupMsgSend success from=%s to=%s type=%s content=%s res=%s", from, to, _type, content, res) + } + } + var body map[string]any + if errJson := json.Unmarshal([]byte(res), &body); errJson != nil { + return errJson + } + if int(body["code"].(float64)) != 200 { + return errors.New(body["desc"].(string)) + } + return nil +} + // GroupMsgSend 发送群消息 func (client *ImClient) GroupMsgSend(from, tid, _type, content, msg string) error { url := "https://api.netease.im/nimserver/msg/sendMsg.action" @@ -759,15 +803,15 @@ func (client *ImClient) GroupMsgSend(from, tid, _type, content, msg string) erro // faccid String 入群失败的账号(accid)列表,格式为 JSONArray,如果创建时邀请的成员中存在加群数量超过限制的情况,会返回入群失败的 accid 以及附言(msg) if err != nil { // {"desc":"already register","code":414} - log.Info("GroupMsgSend %s:" + err.Error()) + log.InfoF("GroupMsgSend error %s:", err.Error()) return err } else { var resDTO CreateImRes json.Unmarshal([]byte(res), &resDTO) if resDTO.Code != 200 { - log.Info("GroupMsgSend %s:" + resDTO.Desc) + log.InfoF("GroupMsgSend code error %s", resDTO.Desc) } else { - log.Info("GroupMsgSend %s:" + res) + log.InfoF("GroupMsgSend success from=%s to=%s type=%s content=%s res=%s", from, tid, _type, content, res) } } var body map[string]any @@ -843,7 +887,7 @@ func (client *ImClient) CreateProGroup(tname, owner, announcement, avatar, msg s // GroupUpdate 更新群组 func (client *ImClient) GroupUpdate(ctx context.Context, id, operator_id, name, announcement, icon string) error { url := "https://open.yunxinapi.com/im/v2.1/teams/" + id - httpMethod := common.HTTP_METHOD_PATCH + httpMethod := "PATCH" header := client.generateJsonHeader() reqBody := map[string]any{ "operator_id": operator_id, @@ -880,7 +924,7 @@ func (client *ImClient) GroupUpdate(ctx context.Context, id, operator_id, name, // GroupTransfer 转让群组 func (client *ImClient) GroupTransfer(ctx context.Context, id, new_owner, extension string) error { url := "https://open.yunxinapi.com/im/v2.1/teams/" + id + "/actions/transfer_owner" - httpMethod := common.HTTP_METHOD_PATCH + httpMethod := "PATCH" header := client.generateJsonHeader() reqBody := map[string]any{ "team_type": 1, @@ -918,7 +962,7 @@ func (client *ImClient) GroupManagerAdd(ctx context.Context, id, operator_id, ex "team_type": 1, "managers": managers, "operator_id": operator_id, - "extension": extension, + // "extension": extension, } bodyBytes, err := json.Marshal(reqBody) if err != nil { @@ -944,14 +988,19 @@ func (client *ImClient) GroupManagerAdd(ctx context.Context, id, operator_id, ex // GroupManagerRemove func (client *ImClient) GroupManagerRemove(ctx context.Context, id, operator_id, extension string, managers []string) error { url := "https://open.yunxinapi.com/im/v2.1/teams/" + id + "/actions/remove_manager" - httpMethod := common.HTTP_METHOD_DELETE + httpMethod := "DELETE" header := client.generateJsonHeader() reqBody := map[string]any{ "team_type": 1, "managers": managers, "operator_id": operator_id, - "extension": extension, + // "extension": extension, } + // var params []string + // for k, v := range reqBody { + // params = append(params, fmt.Sprintf("%s=%v", k, v)) + // } + // url = fmt.Sprintf("%s?%s", url, strings.Join(params, "&")) bodyBytes, err := json.Marshal(reqBody) if err != nil { return errors.WithStack(err) @@ -968,7 +1017,7 @@ func (client *ImClient) GroupManagerRemove(ctx context.Context, id, operator_id, return errors.WithStack(err) } if body.Code != 200 { - return errors.Errorf("GroupManagerRemove code(%d) not 200,msg: %s", body.Code, body.Msg) + return errors.Errorf("GroupManagerRemove code(%d) not 200 url=%s id=%s operator=%s managers=%v,msg: %s", body.Code, url, id, operator_id, managers, body.Msg) } return nil } @@ -976,18 +1025,23 @@ func (client *ImClient) GroupManagerRemove(ctx context.Context, id, operator_id, // GroupRemove func (client *ImClient) GroupRemove(ctx context.Context, id, operator_id, extension string) error { url := "https://open.yunxinapi.com/im/v2.1/teams/" + id - httpMethod := common.HTTP_METHOD_DELETE + httpMethod := "DELETE" header := client.generateJsonHeader() reqBody := map[string]any{ "team_type": 1, "operator_id": operator_id, - "extension": extension, + // "extension": extension, } - bodyBytes, err := json.Marshal(reqBody) - if err != nil { - return errors.WithStack(err) + var params []string + for k, v := range reqBody { + params = append(params, fmt.Sprintf("%s=%v", k, v)) } - res, err := HyTools.HttpDo(httpMethod, url, header, string(bodyBytes)) + url = fmt.Sprintf("%s?%s", url, strings.Join(params, "&")) + // bodyBytes, err := json.Marshal(reqBody) + // if err != nil { + // return errors.WithStack(err) + // } + res, err := HyTools.HttpDo(httpMethod, url, header, "") if err != nil { log.InfoF("GroupRemove %s:"+err.Error(), operator_id) return errors.WithStack(err) @@ -1007,20 +1061,25 @@ func (client *ImClient) GroupRemove(ctx context.Context, id, operator_id, extens // GroupMemberKick func (client *ImClient) GroupMemberKick(ctx context.Context, tid, operator_id, extension string, members []string) error { url := "https://open.yunxinapi.com/im/v2/team_members/actions/kick_member" - httpMethod := common.HTTP_METHOD_DELETE + httpMethod := "DELETE" header := client.generateJsonHeader() reqBody := map[string]any{ "team_type": 1, "operator_id": operator_id, "team_id": tools.StrToInt(tid), "kick_account_ids": members, - "extension": extension, + // "extension": extension, } - bodyBytes, err := json.Marshal(reqBody) - if err != nil { - return errors.WithStack(err) + var params []string + for k, v := range reqBody { + params = append(params, fmt.Sprintf("%s=%v", k, v)) } - res, err := HyTools.HttpDo(httpMethod, url, header, string(bodyBytes)) + url = fmt.Sprintf("%s?%s", url, strings.Join(params, "&")) + // bodyBytes, err := json.Marshal(reqBody) + // if err != nil { + // return errors.WithStack(err) + // } + res, err := HyTools.HttpDo(httpMethod, url, header, "") if err != nil { log.InfoF("GroupMemberKick %s:"+err.Error(), operator_id) return errors.WithStack(err) @@ -1040,19 +1099,24 @@ func (client *ImClient) GroupMemberKick(ctx context.Context, tid, operator_id, e // GroupMemberLeave func (client *ImClient) GroupMemberLeave(ctx context.Context, tid, account_id, extension string) error { url := "https://open.yunxinapi.com/im/v2/team_members/actions/leave" - httpMethod := common.HTTP_METHOD_DELETE + httpMethod := "DELETE" header := client.generateJsonHeader() reqBody := map[string]any{ "team_type": 1, "account_id": account_id, "team_id": tools.StrToInt(tid), - "extension": extension, + // "extension": extension, } - bodyBytes, err := json.Marshal(reqBody) - if err != nil { - return errors.WithStack(err) + var params []string + for k, v := range reqBody { + params = append(params, fmt.Sprintf("%s=%v", k, v)) } - res, err := HyTools.HttpDo(httpMethod, url, header, string(bodyBytes)) + url = fmt.Sprintf("%s?%s", url, strings.Join(params, "&")) + // bodyBytes, err := json.Marshal(reqBody) + // if err != nil { + // return errors.WithStack(err) + // } + res, err := HyTools.HttpDo(httpMethod, url, header, "") if err != nil { log.InfoF("GroupMemberLeave %s:"+err.Error(), tid) return errors.WithStack(err)