@ -11,6 +11,7 @@ import (
yunxin "gitea.ddegame.cn/open/servicebase/pkg/common/netease/dto"
yunxin "gitea.ddegame.cn/open/servicebase/pkg/common/netease/dto"
"gitea.ddegame.cn/open/servicebase/pkg/htools"
"gitea.ddegame.cn/open/servicebase/pkg/htools"
"gitea.ddegame.cn/open/servicebase/pkg/log"
"gitea.ddegame.cn/open/servicebase/pkg/log"
"gitea.ddegame.cn/open/servicebase/pkg/tools"
"github.com/pkg/errors"
"github.com/pkg/errors"
"github.com/spf13/cast"
"github.com/spf13/cast"
@ -689,7 +690,6 @@ func (client *ImClient) SendAttachMsg(parameters map[string]string) error {
// CreateChatGroup 创建群组
// CreateChatGroup 创建群组
func ( client * ImClient ) CreateChatGroup ( ctx context . Context , ownerUserId string , groupName string , inviteMem [ ] string ) ( string , error ) {
func ( client * ImClient ) CreateChatGroup ( ctx context . Context , ownerUserId string , groupName string , inviteMem [ ] string ) ( string , error ) {
url := "https://open.yunxinapi.com/im/v2.1/teams"
url := "https://open.yunxinapi.com/im/v2.1/teams"
httpMethod := common . HTTP_METHOD_POST
httpMethod := common . HTTP_METHOD_POST
header := client . generateJsonHeader ( )
header := client . generateJsonHeader ( )
@ -736,6 +736,373 @@ func (client *ImClient) CreateChatGroup(ctx context.Context, ownerUserId string,
return cast . ToString ( body . Data . TeamInfo . TeamID ) , nil
return cast . ToString ( body . Data . TeamInfo . TeamID ) , nil
}
}
// GroupMsgSend 发送群消息
func ( client * ImClient ) GroupMsgSend ( from , tid , _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=" + "1" )
sb . Append ( "&" )
sb . Append ( "to=" + tid )
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 . Info ( "GroupMsgSend %s:" + err . Error ( ) )
return err
} else {
var resDTO CreateImRes
json . Unmarshal ( [ ] byte ( res ) , & resDTO )
if resDTO . Code != 200 {
log . Info ( "GroupMsgSend %s:" + resDTO . Desc )
} else {
log . Info ( "GroupMsgSend %s:" + 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
}
// 创建云信账户
// 创建高级群,创建时即可通过设置群成员列表邀请用户入群。
// 建群成功会返回 tid, 网易云信服务器产生, 群唯一标识, 该字段需要保存, 以便于加人与踢人等后续操作。
// 如果创建时邀请的成员中存在加群数量超过限制的情况,会返回 faccid( 加群失败成员的 IM 账号)。
// 每个用户可创建的群数量有限制,限制值由 IM 套餐的群组配置决定,具体可前往 网易云信控制台 查看。
func ( client * ImClient ) CreateProGroup ( tname , owner , announcement , avatar , msg string ) ( string , error ) {
url := "https://api.netease.im/nimserver/team/create.action"
httpMethod := common . HTTP_METHOD_POST
header := client . generateHeader ( )
sb := HyTools . NewStringBuilder ( )
sb . Append ( "tname=" + tname )
sb . Append ( "&" )
sb . Append ( "owner=" + owner )
sb . Append ( "&" )
sb . Append ( "announcement=" + announcement )
sb . Append ( "&" )
sb . Append ( "msg=" + msg )
sb . Append ( "&" )
sb . Append ( "icon=" + avatar )
sb . Append ( "&" )
sb . Append ( "members=[]" )
sb . Append ( "&" )
sb . Append ( "joinmode=" + "0" )
sb . Append ( "&" )
sb . Append ( "beinvitemode=" + "1" )
sb . Append ( "&" )
sb . Append ( "invitemode=" + "1" ) // 邀请权限, 即谁可以邀请他人入群0, 群主和管理员( 默认) 。1, 所有人
sb . Append ( "&" )
sb . Append ( "teamMemberLimit=" + "3000" ) // 最大群成员数(包含群主),[2, 200(默认)]
sb . Append ( "&" )
sb . Append ( "isNotifyCloseOnline=" + "0" ) // 是否关闭群通知消息在线发送0, 否。1, 是
sb . Append ( "&" )
sb . Append ( "isNotifyClosePersistent=" + "0" ) // 是否关闭存储离线/漫游/历史的群通知消息0, 否。1, 是
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 . Info ( "CreateProGroup %s:" + err . Error ( ) )
return "" , err
} else {
var resDTO CreateImRes
json . Unmarshal ( [ ] byte ( res ) , & resDTO )
if resDTO . Code != 200 {
log . Info ( "CreateProGroup_%s:" + resDTO . Desc )
} else {
log . Info ( "CreateProGroup_%s:" + 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 body [ "tid" ] . ( string ) , nil
}
// 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
header := client . generateJsonHeader ( )
reqBody := map [ string ] any {
"operator_id" : operator_id ,
"team_type" : 1 ,
"name" : name ,
"announcement" : announcement ,
// "intro": intro,
// "members_limit": members_limit,
// "join_mode": join_mode, 通过 SDK 侧操作申请入群的验证方式。 0( 默认) : 无需验证, 直接入群。 1: 需要群主或管理员验证通过才能入群。 2: 不允许任何人申请入群。
// "agree_mode": agree_mode, 邀请入群时是否需要被邀请人的同意。0( 默认) : 需要被邀请人同意才能入群。1: 不需要被邀请人同意, 直接入群。
// "invite_mode": invite_mode, 邀请权限, 即谁可以邀请他人入群。0( 默认) : 群主和管理员。1: 所有人。
}
bodyBytes , err := json . Marshal ( reqBody )
if err != nil {
return errors . WithStack ( err )
}
res , err := HyTools . HttpDo ( httpMethod , url , header , string ( bodyBytes ) )
if err != nil {
log . InfoF ( "GroupUpdate %s:" + err . Error ( ) , operator_id )
return errors . WithStack ( err )
}
log . InfoF ( "update group chat resp: %s" , res )
var body BaseRes [ CreateChatGroupRsp ]
err = json . Unmarshal ( [ ] byte ( res ) , & body )
if err != nil {
return errors . WithStack ( err )
}
if body . Code != 200 {
return errors . Errorf ( "GroupUpdate code(%d) not 200,msg: %s" , body . Code , body . Msg )
}
return nil
}
// 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
header := client . generateJsonHeader ( )
reqBody := map [ string ] any {
"team_type" : 1 ,
"new_owner_account_id" : new_owner ,
"leave" : 2 ,
"extension" : extension ,
}
bodyBytes , err := json . Marshal ( reqBody )
if err != nil {
return errors . WithStack ( err )
}
res , err := HyTools . HttpDo ( httpMethod , url , header , string ( bodyBytes ) )
if err != nil {
log . InfoF ( "GroupTransfer %s:" + err . Error ( ) , id )
return errors . WithStack ( err )
}
log . InfoF ( "GroupTransfer group chat resp: %s" , res )
var body BaseRes [ CreateChatGroupRsp ]
err = json . Unmarshal ( [ ] byte ( res ) , & body )
if err != nil {
return errors . WithStack ( err )
}
if body . Code != 200 {
return errors . Errorf ( "GroupTransfer code(%d) not 200,msg: %s" , body . Code , body . Msg )
}
return nil
}
// GroupManagerAdd
func ( client * ImClient ) GroupManagerAdd ( ctx context . Context , id , operator_id , extension string , managers [ ] string ) error {
url := "https://open.yunxinapi.com/im/v2.1/teams/" + id + "/actions/add_manager"
httpMethod := common . HTTP_METHOD_POST
header := client . generateJsonHeader ( )
reqBody := map [ string ] any {
"team_type" : 1 ,
"managers" : managers ,
"operator_id" : operator_id ,
"extension" : extension ,
}
bodyBytes , err := json . Marshal ( reqBody )
if err != nil {
return errors . WithStack ( err )
}
res , err := HyTools . HttpDo ( httpMethod , url , header , string ( bodyBytes ) )
if err != nil {
log . InfoF ( "GroupManagerAdd %s:" + err . Error ( ) , operator_id )
return errors . WithStack ( err )
}
log . InfoF ( "GroupManagerAdd group chat resp: %s" , res )
var body BaseRes [ CreateChatGroupRsp ]
err = json . Unmarshal ( [ ] byte ( res ) , & body )
if err != nil {
return errors . WithStack ( err )
}
if body . Code != 200 {
return errors . Errorf ( "GroupManagerAdd code(%d) not 200,msg: %s" , body . Code , body . Msg )
}
return nil
}
// 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
header := client . generateJsonHeader ( )
reqBody := map [ string ] any {
"team_type" : 1 ,
"managers" : managers ,
"operator_id" : operator_id ,
"extension" : extension ,
}
bodyBytes , err := json . Marshal ( reqBody )
if err != nil {
return errors . WithStack ( err )
}
res , err := HyTools . HttpDo ( httpMethod , url , header , string ( bodyBytes ) )
if err != nil {
log . InfoF ( "GroupManagerRemove %s:" + err . Error ( ) , operator_id )
return errors . WithStack ( err )
}
log . InfoF ( "GroupManagerRemove group chat resp: %s" , res )
var body BaseRes [ CreateChatGroupRsp ]
err = json . Unmarshal ( [ ] byte ( res ) , & body )
if err != nil {
return errors . WithStack ( err )
}
if body . Code != 200 {
return errors . Errorf ( "GroupManagerRemove code(%d) not 200,msg: %s" , body . Code , body . Msg )
}
return nil
}
// 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
header := client . generateJsonHeader ( )
reqBody := map [ string ] any {
"team_type" : 1 ,
"operator_id" : operator_id ,
"extension" : extension ,
}
bodyBytes , err := json . Marshal ( reqBody )
if err != nil {
return errors . WithStack ( err )
}
res , err := HyTools . HttpDo ( httpMethod , url , header , string ( bodyBytes ) )
if err != nil {
log . InfoF ( "GroupRemove %s:" + err . Error ( ) , operator_id )
return errors . WithStack ( err )
}
log . InfoF ( "GroupRemove group chat resp: %s" , res )
var body BaseRes [ CreateChatGroupRsp ]
err = json . Unmarshal ( [ ] byte ( res ) , & body )
if err != nil {
return errors . WithStack ( err )
}
if body . Code != 200 {
return errors . Errorf ( "GroupRemove code(%d) not 200,msg: %s" , body . Code , body . Msg )
}
return nil
}
// 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
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 ,
}
bodyBytes , err := json . Marshal ( reqBody )
if err != nil {
return errors . WithStack ( err )
}
res , err := HyTools . HttpDo ( httpMethod , url , header , string ( bodyBytes ) )
if err != nil {
log . InfoF ( "GroupMemberKick %s:" + err . Error ( ) , operator_id )
return errors . WithStack ( err )
}
log . InfoF ( "GroupMemberKick group chat resp: %s" , res )
var body BaseRes [ CreateChatGroupRsp ]
err = json . Unmarshal ( [ ] byte ( res ) , & body )
if err != nil {
return errors . WithStack ( err )
}
if body . Code != 200 {
return errors . Errorf ( "GroupMemberKick code(%d) not 200,msg: %s" , body . Code , body . Msg )
}
return nil
}
// 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
header := client . generateJsonHeader ( )
reqBody := map [ string ] any {
"team_type" : 1 ,
"account_id" : account_id ,
"team_id" : tools . StrToInt ( tid ) ,
"extension" : extension ,
}
bodyBytes , err := json . Marshal ( reqBody )
if err != nil {
return errors . WithStack ( err )
}
res , err := HyTools . HttpDo ( httpMethod , url , header , string ( bodyBytes ) )
if err != nil {
log . InfoF ( "GroupMemberLeave %s:" + err . Error ( ) , tid )
return errors . WithStack ( err )
}
log . InfoF ( "GroupMemberLeave group chat resp: %s" , res )
var body BaseRes [ CreateChatGroupRsp ]
err = json . Unmarshal ( [ ] byte ( res ) , & body )
if err != nil {
return errors . WithStack ( err )
}
if body . Code != 200 {
return errors . Errorf ( "GroupMemberLeave code(%d) not 200,msg: %s" , body . Code , body . Msg )
}
return nil
}
// GroupMemberJoin
func ( client * ImClient ) GroupMemberJoin ( ctx context . Context , tid , operator_id , msg , extension string , invite_account_ids [ ] string ) error {
url := "https://open.yunxinapi.com/im/v2/team_members"
httpMethod := common . HTTP_METHOD_POST
header := client . generateJsonHeader ( )
reqBody := map [ string ] any {
"team_type" : 1 ,
"operator_id" : operator_id ,
"team_id" : tools . StrToInt ( tid ) ,
"msg" : msg ,
"invite_account_ids" : invite_account_ids ,
"extension" : extension ,
}
bodyBytes , err := json . Marshal ( reqBody )
if err != nil {
return errors . WithStack ( err )
}
res , err := HyTools . HttpDo ( httpMethod , url , header , string ( bodyBytes ) )
if err != nil {
log . InfoF ( "GroupMemberJoin %s:" + err . Error ( ) , tid )
return errors . WithStack ( err )
}
log . InfoF ( "GroupMemberJoin group chat resp: %s" , res )
var body BaseRes [ CreateChatGroupRsp ]
err = json . Unmarshal ( [ ] byte ( res ) , & body )
if err != nil {
return errors . WithStack ( err )
}
if body . Code != 200 {
return errors . Errorf ( "GroupMemberJoin code(%d) not 200,msg: %s" , body . Code , body . Msg )
}
return nil
}
// ================ 私有方法 ========================
// ================ 私有方法 ========================
func generateBodyData ( parameters map [ string ] string ) string {
func generateBodyData ( parameters map [ string ] string ) string {
sb := htools . NewStringBuilder ( )
sb := htools . NewStringBuilder ( )