获取群组列表

onLoad(){
var aoptions = {
'Member_Account': loginInfo.identifier,
'Offset': 0,
'GroupBaseInfoFilter': [
'Type',
'Name',
'Introduction',
'Notification',
'FaceUrl',
'CreateTime',
'Owner_Account',
'LastInfoTime',
'LastMsgTime',
'NextMsgSeq',
'MemberNum',
'MaxMemberNum',
'ApplyJoinOption',
'ShutUpAllMember'
],
'SelfInfoFilter': [
'Role',
'JoinTime',
'MsgFlag',
'UnreadMsgNum'
]
};
webim.getJoinedGroupListHigh(
aoptions,
function(resp) {
if (!resp.GroupIdList || resp.GroupIdList.length == 0) {
alert('您目前还没有加入任何群组');
return;
}
var data = [];
console.log(resp)
for (var i = 0; i < resp.GroupIdList.length; i++) {
var group_id = resp.GroupIdList[i].GroupId;
var name = webim.Tool.formatText2Html(resp.GroupIdList[i].Name);
var type_en = resp.GroupIdList[i].Type;
var type = webim.Tool.groupTypeEn2Ch(resp.GroupIdList[i].Type);
var role_en = resp.GroupIdList[i].SelfInfo.Role;
var role = webim.Tool.groupRoleEn2Ch(resp.GroupIdList[i].SelfInfo.Role);
var msg_flag = webim.Tool.groupMsgFlagEn2Ch(
resp.GroupIdList[i].SelfInfo.MsgFlag);
var msg_flag_en = resp.GroupIdList[i].SelfInfo.MsgFlag;
var join_time = webim.Tool.formatTimeStamp(
resp.GroupIdList[i].SelfInfo.JoinTime);
var member_num = resp.GroupIdList[i].MemberNum;
var notification = webim.Tool.formatText2Html(
resp.GroupIdList[i].Notification);
var introduction = webim.Tool.formatText2Html(
resp.GroupIdList[i].Introduction);
var ShutUpAllMember = resp.GroupIdList[i].ShutUpAllMember;
data.push({
'GroupId': group_id,
'Name': name,
'TypeEn': type_en,
'Type': type,
'RoleEn': role_en,
'Role': role,
'MsgFlagEn': msg_flag_en,
'MsgFlag': msg_flag,
'MemberNum': member_num,
'Notification': notification,
'Introduction': introduction,
'JoinTime': join_time,
'ShutUpAllMember': ShutUpAllMember
});
}
console.log(data) ;
that.setData({
groupList: data
})
},
function(err) {
console.log(err.ErrorInfo);
}
);
}
获取群列表成功后,本地存储群组id(GroupId)。获取历史聊天记录。
//读取群组基本资料-高级接口
getGroupInfo(group_id, cbOK, cbErr) {
var options = {
'GroupIdList': [
group_id
],
'GroupBaseInfoFilter': [
'Type',
'Name',
'Introduction',
'Notification',
'FaceUrl',
'CreateTime',
'Owner_Account',
'LastInfoTime',
'LastMsgTime',
'NextMsgSeq',
'MemberNum',
'MaxMemberNum',
'ApplyJoinOption',
'ShutUpAllMember'
],
'MemberInfoFilter': [
'Account',
'Role',
'JoinTime',
'LastSendMsgTime',
'ShutUpUntil'
]
};
webim.getGroupInfo(
options,
function(resp) {
if (resp.GroupInfo[0].ShutUpAllMember == 'On') {
console.log('该群组已开启全局禁言');
}
if (cbOK) {
cbOK(resp);
}
},
function(err) {
console.log(err.ErrorInfo);
}
);
},
获取最新的群历史消息,用于切换群组聊天时,重新拉取群组的聊天消息
getLastGroupHistoryMsgs: function() {
var that = this;
var selToID = that.data.GroupId;
that.getGroupInfo(selToID, function(resp) {
var reqMsgCount = 20;
that.setData({
reqMsgSeq: resp.GroupInfo[0].NextMsgSeq - 1
})
//拉取最新的群历史消息
var options = {
'GroupId': selToID,
'ReqMsgSeq': resp.GroupInfo[0].NextMsgSeq - 1,
'ReqMsgNumber': reqMsgCount,
};
webim.syncGroupMsgs(
options,
function(msgList) {
if (msgList.length == 0) {
console.log("该群没有历史消息了:options=" + JSON.stringify(options));
return;
}
var msgLists = [];
for (var i = 0; i < msgList.length; i++) {
if (msgList[i].elems[0].content.opType==1){
var adminSubname='加入该群'
}else{
var adminSubname = '退出该群'
}
var array = {
name: msgList[i].fromAccountNick,
time: utils.js_date_time(msgList[i].time),
images: msgList[i].elems[0].content.ImageInfoArray,
text: msgList[i].elems[0].content.text,
type: msgList[i].elems[0].type,
isSend: msgList[i].isSend,
adminSubname: adminSubname,
adminText: msgList[i].elems[0].content.userIdList,
file: msgList[i].elems[0].content.downUrl,
filename: msgList[i].elems[0].content.name,
num: 0
}
msgLists.push(array);
}
that.setData({
msgList: msgLists
})
that.pageScrollToBottom();
},
function(err) {
console.log(err.ErrorInfo);
}
);
})
},
消息发送
//发送消息(文本或者表情)
onSendMsg() {
var that = this;
var selToID = that.data.selToID;
var selType = webim.SESSION_TYPE.GROUP;
if (!selToID) {
console.log("你还没有选中好友或者群组,暂不能聊天");
return;
}
//获取消息内容
var msgContent = that.data.message;
var msgLen = webim.Tool.getStrBytes(msgContent);
that.GetUserEntity(msgContent)
.then((res) => {
console.log(res)
}).catch((res) => {
console.log(res)
});
if (msgContent.length < 1) {
console.log("发送的消息不能为空!");
wx.showModal({
title: '提示',
content: '发送的消息不能为空!',
})
return;
}
var maxLen, errInfo;
if (selType == webim.SESSION_TYPE.C2C) {
maxLen = webim.MSG_MAX_LENGTH.C2C;
errInfo = "消息长度超出限制(最多" + Math.round(maxLen / 3) + "汉字)";
} else {
maxLen = webim.MSG_MAX_LENGTH.GROUP;
errInfo = "消息长度超出限制(最多" + Math.round(maxLen / 3) + "汉字)";
}
if (msgLen > maxLen) {
console.log(errInfo);
return;
}
//发消息处理
that.handleMsgSend(msgContent);
},
handleMsgSend(msgContent) {
var that = this;
that.pageScrollToBottom();
var selToID = that.data.selToID;
var selType = webim.SESSION_TYPE.GROUP;
var loginInfo = wx.getStorageSync('loginInfo')
var friendHeadUrl = '../../images/noavatar.png'
if (!selSess) {
var selSess = new webim.Session(selType, selToID, selToID, friendHeadUrl, Math.round(new Date().getTime() / 1000));
}
var isSend = true; //是否为自己发送
var seq = -1; //消息序列,-1表示sdk自动生成,用于去重
var random = Math.round(Math.random() * 4294967296); //消息随机数,用于去重
var msgTime = Math.round(new Date().getTime() / 1000); //消息时间戳
var subType; //消息子类型
if (selType == webim.SESSION_TYPE.C2C) {
subType = webim.C2C_MSG_SUB_TYPE.COMMON;
} else {
//webim.GROUP_MSG_SUB_TYPE.COMMON-普通消息,
//webim.GROUP_MSG_SUB_TYPE.LOVEMSG-点赞消息,优先级最低
//webim.GROUP_MSG_SUB_TYPE.TIP-提示消息(不支持发送,用于区分群消息子类型),
//webim.GROUP_MSG_SUB_TYPE.REDPACKET-红包消息,优先级最高
subType = webim.GROUP_MSG_SUB_TYPE.COMMON;
}
var msg = new webim.Msg(selSess, isSend, seq, random, msgTime, loginInfo.identifier, subType, loginInfo.identifierNick);
var text_obj, face_obj, tmsg, emotionIndex, emotion, restMsgIndex;
//解析文本和表情
var expr = /\[[^[\]]{1,3}\]/mg;
var emotions = msgContent.match(expr);
if (!emotions || emotions.length < 1) {
text_obj = new webim.Msg.Elem.Text(msgContent);
msg.addText(text_obj);
} else {
for (var i = 0; i < emotions.length; i++) {
tmsg = msgContent.substring(0, msgContent.indexOf(emotions[i]));
if (tmsg) {
text_obj = new webim.Msg.Elem.Text(tmsg);
msg.addText(text_obj);
}
emotionIndex = webim.EmotionDataIndexs[emotions[i]];
emotion = webim.Emotions[emotionIndex];
if (emotion) {
face_obj = new webim.Msg.Elem.Face(emotionIndex, emotions[i]);
msg.addFace(face_obj);
} else {
text_obj = new webim.Msg.Elem.Text(emotions[i]);
msg.addText(text_obj);
}
restMsgIndex = msgContent.indexOf(emotions[i]) + emotions[i].length;
msgContent = msgContent.substring(restMsgIndex);
}
if (msgContent) {
text_obj = new webim.Msg.Elem.Text(msgContent);
msg.addText(text_obj);
}
}
msg.PushInfo = {
"PushFlag": 0,
"Desc": '测试离线推送内容', //离线推送内容
"Ext": '测试离线推送透传内容', //离线推送透传内容
"AndroidInfo": {
"Sound": "android.mp3" //离线推送声音文件路径。
},
"ApnsInfo": {
"Sound": "apns.mp3", //离线推送声音文件路径。
"BadgeMode": 1
}
};
msg.PushInfoBoolean = true; //是否开启离线推送push同步
msg.sending = 1;
msg.originContent = msgContent;
webim.sendMsg(msg, function(resp) {
console.log(resp)
that.setData({
message: ''
})
}, function(err) {
//console.log(err.ErrorInfo);
wx.showModal({
title: '提示',
content: '消息发送失败,内容包含敏感字体哦~',
})
});
},
接下来处理一下,聊天页面增加一条消息。
上篇中有介绍到监听新消息事件。
//处理消息(私聊(包括普通消息和全员推送消息),普通群(非直播聊天室)消息) 我这里是只要私聊的
addMsg: function(msg) {
var that = this;
var fromAccount, fromAccountNick, sessType, subType;
fromAccount = msg.getFromAccount();
if (!fromAccount) {
fromAccount = '';
}
fromAccountNick = msg.getFromAccountNick();
if (!fromAccountNick) {
fromAccountNick = fromAccount;
}
//解析消息
//获取会话类型
//webim.SESSION_TYPE.GROUP-群聊,
//webim.SESSION_TYPE.C2C-私聊,
sessType = msg.getSession().type();
//获取消息子类型
//会话类型为群聊时,子类型为:webim.GROUP_MSG_SUB_TYPE
//会话类型为私聊时,子类型为:webim.C2C_MSG_SUB_TYPE
subType = msg.getSubType();
switch (sessType) {
case webim.SESSION_TYPE.GROUP: //私聊消息
switch (subType) {
case webim.GROUP_MSG_SUB_TYPE.COMMON: //c2c普通消息
that.convertMsg(msg); //解析方法
break;
case webim.GROUP_MSG_SUB_TYPE.TIP: //c2c普通消息
that.convertMsg(msg); //解析方法
break;
}
break;
}
},
convertMsg: function(msg) {
var that = this;
var elems, elem, type, content, isSelfSend;
var loginInfo = that.data.loginInfo; //自己的资料
var friendInfo = that.data.friendInfo; //对方的资料,这里要特别注意一下,消息里面是不会返回双方的头像和昵称的,只能通过指定的方法得到。
elems = msg.getElems();
isSelfSend = msg.getIsSend(); //消息是否为自己发的 true是自己发送,
var sess = msg.sess;
var currentMsg = {}; //设置消息数组,存消息
var currentMsgsArray = that.data.msgList;
var allChatList = that.data.allChatList;
for (var i in elems) {
elem = elems[i];
type = elem.getType();
content = elem.getContent();
switch (type) {
case webim.MSG_ELEMENT_TYPE.TEXT:
var msgContent = content.text;
console.log(content)
var msgTime = msg.getTime(); //得到当前消息发送的时间
//得到当天凌晨的时间戳
var timeStamp = new Date(new Date().setHours(0, 0, 0, 0)) / 1000;
var thisdate;
var d = new Date(msgTime * 1000); //根据时间戳生成的时间对象
var min = d.getMinutes();
var hour = d.getHours();
//得到时和分,分小于10时,只返回一位数
if (min < 10) {
min = "0" + min;
}
//得到月份和天 月份一般是从0开始,所以展示出来要+1
var month = d.getMonth();
var day = d.getDate();
//得到时间 当天时间应该只显示时分 当天以前显示日期+时间
thisdate = ('2019-' +(month + 1) + '-' + day + ' ' + hour + ":" + min);
currentMsg.text = msgContent; //当前消息的内容
currentMsg.type = type;
currentMsg.images = images;
currentMsg.time = thisdate;
currentMsg.isSend = isSelfSend;
currentMsg.name = msg.fromAccountNick;
if (msg.fromAccountHeadurl==null){
var avatarUrl = '../../images/noavatar.png';
}else{
var avatarUrl = msg.fromAccountHeadurl;
}
console.log(currentMsg)
//根据是否自己发送的消息,设置双方的头像
if (isSelfSend == true) {
currentMsg.avatarUrl = avatarUrl;
} else {
currentMsg.avatarUrl = avatarUrl;
}
//然后将每一条聊天消息push进数组
currentMsgsArray.push(currentMsg);
that.setData({
msgList: currentMsgsArray,
})
break;
}
}
},
萌新小白,文章多有不到之处,qq:215227670,一起交流。
网友评论