get_history_msg.js
7.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//获取历史消息(c2c或者group)成功回调函数
//msgList 为消息数组,结构为[Msg]
function getHistoryMsgCallback(msgList, prepage) {
var msg;
prepage = prepage || false;
//如果是加载前几页的消息,消息体需要prepend,所以先倒排一下
if (prepage) {
msgList.reverse();
}
for (var j in msgList) { //遍历新消息
msg = msgList[j];
if (msg.getSession().id() == selToID) { //为当前聊天对象的消息
selSess = msg.getSession();
//在聊天窗体中新增一条消息
addMsg(msg, prepage);
}
}
//消息已读上报,并将当前会话的消息设置成自动已读
webim.setAutoRead(selSess, true, true);
}
//获取最新的c2c历史消息,用于切换好友聊天,重新拉取好友的聊天消息
var getLastC2CHistoryMsgs = function(cbOk, cbError) {
if (selType == webim.SESSION_TYPE.GROUP) {
alert('当前的聊天类型为群聊天,不能进行拉取好友历史消息操作');
return;
}
if (!selToID || selToID == '@TIM#SYSTEM') {
alert('当前的聊天id非法,selToID=' + selToID);
return;
}
var lastMsgTime = 0; //第一次拉取好友历史消息时,必须传0
var msgKey = '';
var options = {
'Peer_Account': selToID, //好友帐号
'MaxCnt': reqMsgCount, //拉取消息条数
'LastMsgTime': lastMsgTime, //最近的消息时间,即从这个时间点向前拉取历史消息
'MsgKey': msgKey
};
selSess = null;
webim.MsgStore.delSessByTypeId(selType, selToID);
webim.getC2CHistoryMsgs(
options,
function(resp) {
var complete = resp.Complete; //是否还有历史消息可以拉取,1-表示没有,0-表示有
if (resp.MsgList.length == 0) {
webim.Log.warn("没有历史消息了:data=" + JSON.stringify(options));
return;
}
getPrePageC2CHistroyMsgInfoMap[selToID] = { //保留服务器返回的最近消息时间和消息Key,用于下次向前拉取历史消息
'LastMsgTime': resp.LastMsgTime,
'MsgKey': resp.MsgKey
};
//清空聊天界面
document.getElementsByClassName("msgflow")[0].innerHTML = "";
if (cbOk)
cbOk(resp.MsgList);
},
cbError
);
};
//向上翻页,获取更早的好友历史消息
var getPrePageC2CHistoryMsgs = function(cbOk, cbError) {
if (selType == webim.SESSION_TYPE.GROUP) {
alert('当前的聊天类型为群聊天,不能进行拉取好友历史消息操作');
return;
}
var tempInfo = getPrePageC2CHistroyMsgInfoMap[selToID]; //获取下一次拉取的c2c消息时间和消息Key
var lastMsgTime;
var msgKey;
if (tempInfo) {
lastMsgTime = tempInfo.LastMsgTime;
msgKey = tempInfo.MsgKey;
} else {
alert('获取下一次拉取的c2c消息时间和消息Key为空');
return;
}
var options = {
'Peer_Account': selToID, //好友帐号
'MaxCnt': reqMsgCount, //拉取消息条数
'LastMsgTime': lastMsgTime, //最近的消息时间,即从这个时间点向前拉取历史消息
'MsgKey': msgKey
};
webim.getC2CHistoryMsgs(
options,
function(resp) {
var complete = resp.Complete; //是否还有历史消息可以拉取,1-表示没有,0-表示有
if (resp.MsgList.length == 0) {
webim.Log.warn("没有历史消息了:data=" + JSON.stringify(options));
return;
}
getPrePageC2CHistroyMsgInfoMap[selToID] = { //保留服务器返回的最近消息时间和消息Key,用于下次向前拉取历史消息
'LastMsgTime': resp.LastMsgTime,
'MsgKey': resp.MsgKey
};
if (cbOk) {
cbOk(resp.MsgList);
} else {
getHistoryMsgCallback(resp.MsgList, true);
}
},
cbError
);
};
//获取最新的群历史消息,用于切换群组聊天时,重新拉取群组的聊天消息
var getLastGroupHistoryMsgs = function(cbOk) {
if (selType == webim.SESSION_TYPE.C2C) {
alert('当前的聊天类型为好友聊天,不能进行拉取群历史消息操作');
return;
}
getGroupInfo(selToID, function(resp) {
//拉取最新的群历史消息
var options = {
'GroupId': selToID,
'ReqMsgSeq': resp.GroupInfo[0].NextMsgSeq - 1,
'ReqMsgNumber': reqMsgCount
};
if (options.ReqMsgSeq == null || options.ReqMsgSeq == undefined || options.ReqMsgSeq <= 0) {
webim.Log.warn("该群还没有历史消息:options=" + JSON.stringify(options));
return;
}
selSess = null;
webim.MsgStore.delSessByTypeId(selType, selToID);
recentSessMap[webim.SESSION_TYPE.GROUP + selToID] = {};
recentSessMap[webim.SESSION_TYPE.GROUP + selToID].MsgGroupReadedSeq = resp.GroupInfo && resp.GroupInfo[0] && resp.GroupInfo[0].NextMsgSeq - 1;
webim.syncGroupMsgs(
options,
function(msgList) {
if (msgList.length == 0) {
webim.Log.warn("该群没有历史消息了:options=" + JSON.stringify(options));
return;
}
var msgSeq = msgList[0].seq - 1;
getPrePageGroupHistroyMsgInfoMap[selToID] = {
"ReqMsgSeq": msgSeq
};
//清空聊天界面
document.getElementsByClassName("msgflow")[0].innerHTML = "";
if (cbOk)
cbOk(msgList);
},
function(err) {
alert(err.ErrorInfo);
}
);
});
};
//向上翻页,获取更早的群历史消息
var getPrePageGroupHistoryMsgs = function(cbOk) {
if (selType == webim.SESSION_TYPE.C2C) {
alert('当前的聊天类型为好友聊天,不能进行拉取群历史消息操作');
return;
}
var tempInfo = getPrePageGroupHistroyMsgInfoMap[selToID]; //获取下一次拉取的群消息seq
var reqMsgSeq;
if (tempInfo) {
reqMsgSeq = tempInfo.ReqMsgSeq;
if (reqMsgSeq <= 0) {
webim.Log.warn('该群没有历史消息可拉取了');
return;
}
} else {
webim.Log.error('获取下一次拉取的群消息seq为空');
return;
}
var options = {
'GroupId': selToID,
'ReqMsgSeq': reqMsgSeq,
'ReqMsgNumber': reqMsgCount
};
webim.syncGroupMsgs(
options,
function(msgList) {
if (msgList.length == 0) {
webim.Log.warn("该群没有历史消息了:options=" + JSON.stringify(options));
return;
}
var msgSeq = msgList[0].seq - 1;
getPrePageGroupHistroyMsgInfoMap[selToID] = {
"ReqMsgSeq": msgSeq
};
if (cbOk) {
cbOk(msgList);
} else {
getHistoryMsgCallback(msgList, true);
}
},
function(err) {
alert(err.ErrorInfo);
}
);
};