first commit
This commit is contained in:
132
pkg/client/netease/IMService.go
Normal file
132
pkg/client/netease/IMService.go
Normal file
@ -0,0 +1,132 @@
|
||||
package Netease
|
||||
|
||||
import (
|
||||
"servicebase/pkg/common/HyTools"
|
||||
"encoding/json"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
Netease "servicebase/pkg/common/netease"
|
||||
|
||||
"github.com/anxpp/beego"
|
||||
"github.com/anxpp/beego/logs"
|
||||
)
|
||||
|
||||
const (
|
||||
// ChatRoomManagerUserId 聊天室消息管理员ID
|
||||
ChatRoomManagerUserId = "34cc7185d0b60eacf38f616b8aad8c51"
|
||||
SystemNoticeUserId = "x94b992dbb4e4dfca9c971200d797a02"
|
||||
P2pMessagesTypeSystemNotice = "104"
|
||||
TimeDefaultFormat = "Y-m-d H:i:s"
|
||||
)
|
||||
|
||||
type ImService struct {
|
||||
}
|
||||
|
||||
// ImMsgBodyAttach IM消息 body对象
|
||||
type ImMsgBodyAttach struct {
|
||||
TypeCode string `json:"TypeCode"`
|
||||
BizData interface{} `json:"BizData"`
|
||||
Msg string `json:"msg,omitempty"`
|
||||
}
|
||||
|
||||
// ChatRoomMsgAttach 聊天室自定义消息
|
||||
type ChatRoomMsgAttach struct {
|
||||
TypeCode string `json:"TypeCode"`
|
||||
BizData interface{} `json:"BizData"`
|
||||
}
|
||||
|
||||
// SendCustomMsgToChatroomByChatroomManager 向聊天室发消息
|
||||
func (s *ImService) SendCustomMsgToChatroomByChatroomManager(messageRoomId string, attachData ChatRoomMsgAttach) error {
|
||||
msgParameter := make(map[string]string)
|
||||
msgParameter["roomid"] = messageRoomId
|
||||
msgParameter["msgId"] = HyTools.GetUUID()
|
||||
msgParameter["fromAccid"] = ChatRoomManagerUserId
|
||||
msgParameter["msgType"] = "100" //自定义消息
|
||||
msgParameter["resendFlag"] = "0"
|
||||
bodyBytes, _ := json.Marshal(attachData)
|
||||
msgParameter["ext"] = ""
|
||||
msgParameter["attach"] = string(bodyBytes)
|
||||
imClient := Netease.NewImClient()
|
||||
return imClient.SendChatroomMsg(msgParameter)
|
||||
}
|
||||
|
||||
// SendCustomerMsgFromAToB 发IM点对点自定义消息 系统通知 用户关系变化
|
||||
func (s *ImService) SendCustomerMsgFromAToB(fromAccid, toAccid, pushContent, payloadJson string, bodyModel ImMsgBodyAttach, optionMap map[string]string) error {
|
||||
msgParameter := make(map[string]string)
|
||||
msgParameter["from"] = fromAccid
|
||||
msgParameter["ope"] = "0" //0=p2p 1=群消息
|
||||
msgParameter["to"] = toAccid
|
||||
msgParameter["type"] = "100" //0 表示文本消息, 1 表示图片, 2 表示语音, 3 表示视频, 4 表示地理位置信息, 6 表示文件, 100 自定义消息类型
|
||||
|
||||
bodyBytes, _ := json.Marshal(bodyModel)
|
||||
|
||||
msgParameter["body"] = string(bodyBytes) //最大长度5000字符,为一个JSON串
|
||||
msgParameter["antispam"] = "false" //对于对接了易盾反垃圾功能的应用,本消息是否需要指定经由易盾检测的内容 只对消息类型为:100 自定义消息类型 的消息生效。
|
||||
|
||||
optionBytes, _ := json.Marshal(optionMap)
|
||||
msgParameter["option"] = string(optionBytes) //发消息时特殊指定的行为选项,JSON格式,可用于指定消息的漫游,存云端历史,发送方多端同步,推送,消息抄送等特殊行为
|
||||
|
||||
//需要推送
|
||||
if optionMap["push"] == "true" {
|
||||
if len(pushContent) > 0 {
|
||||
msgParameter["pushcontent"] = pushContent // ios推送内容,不超过150字符,option选项中允许推送(push=true),此字段可以指定推送内容
|
||||
}
|
||||
|
||||
if len(payloadJson) > 0 {
|
||||
msgParameter["payload"] = payloadJson // ios 推送对应的payload,必须是JSON,不能超过2k字符
|
||||
}
|
||||
}
|
||||
imClient := Netease.NewImClient()
|
||||
return imClient.SendMsg(msgParameter)
|
||||
}
|
||||
|
||||
// SendSystemNoticeToUser 给用户发系统消息
|
||||
// _ = messageService.SendSystemNoticeToUser(userId, "厅补贴到账通知", msgContent, "", "")
|
||||
func (s *ImService) SendSystemNoticeToUser(toAccid, msgTitle, msgText, schemeUrl, imageUrl string) {
|
||||
option := GetDefaultMsgOption()
|
||||
fromUserId := SystemNoticeUserId
|
||||
logs.Info("SendSystemNoticeToUser from " + fromUserId + "to " + toAccid)
|
||||
pushContent := ""
|
||||
payloadJson := ""
|
||||
bizData := make(map[string]string, 0)
|
||||
bizData["Title"] = url.QueryEscape(msgTitle)
|
||||
bizData["Content"] = url.QueryEscape(msgText)
|
||||
bizData["Scheme"] = schemeUrl
|
||||
bizData["ImageUrl"] = imageUrl
|
||||
bizData["CreateTime"] = beego.Date(time.Now(), TimeDefaultFormat)
|
||||
//body的业务数据
|
||||
var bodyAttach ImMsgBodyAttach
|
||||
bodyAttach.TypeCode = P2pMessagesTypeSystemNotice
|
||||
bodyAttach.BizData = bizData
|
||||
bodyAttach.Msg = msgTitle
|
||||
err := s.SendCustomerMsgFromAToB(fromUserId, toAccid, pushContent, payloadJson, bodyAttach, option)
|
||||
if nil != err {
|
||||
logs.Error("通知发送失败!!!!")
|
||||
logs.Info(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// GetDefaultMsgOption 发送消息默认的配置
|
||||
func GetDefaultMsgOption() map[string]string {
|
||||
/*
|
||||
1. roam: 该消息是否需要漫游,默认true(需要app开通漫游消息功能);
|
||||
2. history: 该消息是否存云端历史,默认true;
|
||||
3. sendersync: 该消息是否需要发送方多端同步,默认true;
|
||||
4. push: 该消息是否需要APNS推送或安卓系统通知栏推送,默认true;
|
||||
5. route: 该消息是否需要抄送第三方;默认true (需要app开通消息抄送功能);
|
||||
6. badge:该消息是否需要计入到未读计数中,默认true;
|
||||
7. needPushNick: 推送文案是否需要带上昵称,不设置该参数时默认true;
|
||||
8. persistent: 是否需要存离线消息,不设置该参数时默认true。
|
||||
*/
|
||||
result := make(map[string]string)
|
||||
result["roam"] = "true"
|
||||
result["history"] = "true"
|
||||
result["sendersync"] = "false"
|
||||
result["push"] = "true"
|
||||
result["route"] = "true"
|
||||
result["badge"] = "true"
|
||||
result["needPushNick"] = "false"
|
||||
result["persistent"] = "true"
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user