message.js
14.6 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
* @Author: 消息逻辑
*/
'use strict'
YX.fn.message = function () {
this.$sendBtn = $('#sendBtn')
this.$messageText = $('#messageText')
this.$chooseFileBtn = $('#chooseFileBtn')
this.$fileInput = $('#uploadFile')
this.$sendBtn.on('click', this.sendTextMessage.bind(this))
this.$messageText.on('keydown', this.inputMessage.bind(this))
this.$chooseFileBtn.on('click', 'a', this.chooseFile.bind(this))
this.$fileInput.on('change', this.uploadFile.bind(this))
//消息重发
this.$chatContent.delegate('.j-resend', 'click', this.doResend.bind(this))
//语音播发
this.$chatContent.delegate('.j-mbox', 'click', this.playAudio)
//聊天面板右键菜单
$.contextMenu({
selector: '.j-msg',
callback: function (key, options) {
if (key === 'delete') {
var id = options.$trigger.parent().data('id')
var msg = this.cache.findMsg(this.crtSession, id)
if (!msg || options.$trigger.hasClass('j-msg')) {
}
if (msg.flow !== 'out' && msg.scene === 'p2p') {
alert('点对点场景,只能撤回自己发的消息')
return
}
if (!this.cache.isCurSessionTeamManager && msg.flow !== 'out' && msg.scene === 'team') {
alert('群会话场景,非管理员不能撤回别人发的消息')
return
}
options.$trigger.removeClass('j-msg')
this.nim.deleteMsg({
msg: msg,
done: function (err) {
options.$trigger.addClass('j-msg')
if (err) {
if (err.code === 508) {
alert('发送时间超过2分钟的消息,不能被撤回')
} else {
alert(err.message || '操作失败')
}
} else {
msg.opeAccount = userUID
this.backoutMsg(id, {msg: msg})
}
}.bind(this)
})
}
}.bind(this),
items: {
"delete": { name: "撤回", icon: "delete" }
}
})
//表情贴图模块
this.initEmoji()
}
/**
* 处理收到的消息
* @param {Object} msg
* @return
*/
YX.fn.doMsg = function (msg) {
var that = this,
who = msg.to === userUID ? msg.from : msg.to,
updateContentUI = function () {
//如果当前消息对象的会话面板打开
if (that.crtSessionAccount === who) {
that.sendMsgRead(who, msg.scene)
var msgHtml = appUI.updateChatContentUI(msg, that.cache)
that.$chatContent.find('.no-msg').remove()
that.$chatContent.append(msgHtml).scrollTop(99999)
}
}
//非群通知消息处理
if (/text|image|file|audio|video|geo|custom|tip|robot/i.test(msg.type)) {
this.cache.addMsgs(msg)
var account = (msg.scene === "p2p" ? who : msg.from)
//用户信息本地没有缓存,需存储
if (!this.cache.getUserById(account)) {
this.mysdk.getUser(account, function (err, data) {
if (!err) {
that.cache.updatePersonlist(data)
updateContentUI()
}
})
} else {
this.buildSessions()
updateContentUI()
}
} else {
// 群消息处理
this.messageHandler(msg, updateContentUI)
}
}
/*****************************************************************
* emoji模块
****************************************************************/
YX.fn.initEmoji = function () {
this.$showEmoji = $('#showEmoji')
this.$showEmoji.on('click', this.showEmoji.bind(this))
var that = this,
emojiConfig = {
'emojiList': emojiList, //普通表情
'pinupList': pinupList, //贴图
'width': 500,
'height': 300,
'imgpath': './images/',
'callback': function (result) {
that.cbShowEmoji(result)
}
}
this.$emNode = new CEmojiEngine($('#emojiTag')[0], emojiConfig)
this.$emNode._$hide();
}
/**
* 选择表情回调
* @param {objcet} result 点击表情/贴图返回的数据
*/
YX.fn.cbShowEmoji = function (result) {
if (!!result) {
var scene = this.crtSessionType,
to = this.crtSessionAccount
// 贴图,发送自定义消息体
if (result.type === "pinup") {
var index = Number(result.emoji) + 1
var content = {
type: 3,
data: {
catalog: result.category,
chartlet: result.category + '0' + (index >= 10 ? index : '0' + index)
}
}
this.mysdk.sendCustomMessage(scene, to, content, this.sendMsgDone.bind(this))
} else {
// 表情,内容直接加到输入框
this.$messageText[0].value = this.$messageText[0].value + result.emoji
}
}
}
YX.fn.showEmoji = function () {
this.$emNode._$show()
}
/*************************************************************************
* 发送消息逻辑
*
************************************************************************/
YX.fn.uploadFile = function () {
var that = this,
scene = this.crtSessionType,
to = this.crtSessionAccount,
fileInput = this.$fileInput.get(0)
if (fileInput.files[0].size == 0) {
alert("不能传空文件")
return
}
this.mysdk.sendFileMessage(scene, to, fileInput, this.sendMsgDone.bind(this))
}
YX.fn.chooseFile = function () {
this.$fileInput.click()
}
YX.fn.sendTextMessage = function () {
var scene = this.crtSessionType,
to = this.crtSessionAccount,
text = this.$messageText.val().trim()
if (!!to && !!text) {
if (text.length > 500) {
alert('消息长度最大为500字符')
} else if (text.length === 0) {
return
} else {
this.mysdk.sendTextMessage(scene, to, text, false, this.sendMsgDone.bind(this))
}
}
}
/**
* 发送消息完毕后的回调
* @param error:消息发送失败的原因
* @param msg:消息主体,类型分为文本、文件、图片、地理位置、语音、视频、自定义消息,通知等
*/
YX.fn.sendMsgDone = function (error, msg) {
console.log(msg)
if (error && error.code === 7101) {
alert('被拉黑')
msg.blacked = true
}
this.cache.addMsgs(msg)
this.$messageText.val('')
this.$chatContent.find('.no-msg').remove()
var msgHtml = appUI.updateChatContentUI(msg, this.cache)
this.$chatContent.append(msgHtml).scrollTop(99999)
$('#uploadForm').get(0).reset()
}
YX.fn.inputMessage = function (e) {
var ev = e || window.event
if ($.trim(this.$messageText.val()).length > 0) {
if (ev.keyCode === 13 && ev.ctrlKey) {
this.$messageText.val(this.$messageText.val() + '\r\n')
} else if (ev.keyCode === 13 && !ev.ctrlKey) {
this.sendTextMessage()
}
}
}
// 重发
YX.fn.doResend = function (evt) {
var $node
if (evt.target.tagName.toLowerCase() === 'span') {
$node = $(evt.target)
} else {
$node = $(evt.target.parentNode)
}
var sessionId = $node.data("session")
var idClient = $node.data("id")
var msg = this.cache.findMsg(sessionId, idClient)
this.mysdk.resendMsg(msg, function (err, data) {
if (err) {
alert(err.message || '发送失败')
} else {
this.cache.setMsg(sessionId, idClient, data)
var msgHtml = appUI.buildChatContentUI(sessionId, this.cache)
this.$chatContent.html(msgHtml).scrollTop(99999)
$('#uploadForm').get(0).reset()
}
}.bind(this))
}
/************************************************************
* 获取当前会话消息
* @return {void}
*************************************************************/
YX.fn.getHistoryMsgs = function (scene, account) {
var id = scene + "-" + account;
var sessions = this.cache.findSession(id)
var msgs = this.cache.getMsgs(id)
//标记已读回执
this.sendMsgRead(account, scene)
if (!!sessions) {
// if (sessions.unread >= msgs.length) {
var end = (msgs.length > 0) ? msgs[0].time : false
this.mysdk.getLocalMsgs(id, end, this.getLocalMsgsDone.bind(this))
return
// }
}
this.doChatUI(id)
}
//拿到历史消息后聊天面板UI呈现
YX.fn.doChatUI = function (id) {
var temp = appUI.buildChatContentUI(id, this.cache)
this.$chatContent.html(temp)
this.$chatContent.scrollTop(9999)
//已读回执UI处理
this.markMsgRead(id)
}
YX.fn.getLocalMsgsDone = function (err, data) {
if (!err) {
this.cache.addMsgsByReverse(data.msgs)
var id = data.sessionId
var array = getAllAccount(data.msgs)
var that = this
this.checkUserInfo(array, function () {
that.doChatUI(id)
})
} else {
alert("获取历史消息失败")
}
}
//检查用户信息有木有本地缓存 没的话就去拿拿好后在执行回调
YX.fn.checkUserInfo = function (array, callback) {
var arr = []
var that = this
for (var i = array.length - 1; i >= 0; i--) {
if (!this.cache.getUserById(array[i])) {
arr.push(array[i])
}
}
if (arr.length > 0) {
this.mysdk.getUsers(arr, function (error, data) {
if (!error) {
that.cache.setPersonlist(data)
callback()
} else {
alert("获取用户信息失败")
}
})
} else {
callback()
}
}
//发送已读回执
YX.fn.sendMsgRead = function (account, scene) {
if (scene === "p2p") {
var id = scene + "-" + account
var sessions = this.cache.findSession(id)
this.mysdk.sendMsgReceipt(sessions.lastMsg, function (err, data) {
if (err) {
console.log(err)
}
})
}
}
//UI上标记消息已读
YX.fn.markMsgRead = function (id) {
if (!id || this.crtSession !== id) {
return
}
var msgs = this.cache.getMsgs(id)
for (var i = msgs.length - 1; i >= 0; i--) {
var message = msgs[i]
// 目前不支持群已读回执
if (message.scene === "team") {
return
}
if(message.type !== 'tip' && window.nim.isMsgRemoteRead(message)){
$(".item.item-me.read").removeClass("read")
$("#" + message.idClient).addClass("read")
break
}
}
}
//撤回消息
YX.fn.backoutMsg = function (id, data) {
var msg = data ? data.msg : this.cache.findMsg(this.crtSession, id)
var to = msg.target
var session = msg.sessionId
var opeAccount = msg.opeAccount || msg.from
var opeNick = getNick(opeAccount)
if (msg.scene === 'team') {
var teamId = msg.to || this.crtSessionAccount
var teamInfo = this.cache.getTeamById(teamId)
if (teamInfo && opeAccount !== msg.from) {
if (teamInfo.owner === opeAccount) {
opeNick = '群主' + opeNick
} else if (teamInfo.type === 'advanced') {
opeNick = '管理员' + opeNick
}
}
}
this.nim.sendTipMsg({
isLocal: true,
scene: msg.scene,
to: to,
tip: (userUID === opeAccount ? '你' : opeNick) + '撤回了一条消息',
time: msg.time,
done: function (err, data) {
if (!err) {
this.cache.backoutMsg(session, id, data)
if (this.crtSession === session) {
var msgHtml = appUI.buildChatContentUI(this.crtSession, this.cache)
this.$chatContent.html(msgHtml).scrollTop(99999)
//已读回执UI处理
this.markMsgRead(this.crtSession)
}
} else {
alert('操作失败')
}
}.bind(this)
})
}
/*********************************多人音视频模块********************************* */
/** 发送群视频tip消息
* @param {object} option
* @param {string} option.teamId 群id
* @param {string} option.account 发送群视频的uid
* @param {string} option.message tip消息
*/
YX.fn.sendTeamNetCallTip = function (option) {
var tmpUser = this.cache.getTeamMemberInfo(option.account, option.teamId);
option.nick = tmpUser.nickInTeam || getNick(option.account);
option.isLocal = option.isLocal === undefined ? true : option.isLocal;
/** 远程 先禁掉 */
this.nim.sendTipMsg({
isLocal: option.isLocal,
scene: 'team',
to: option.teamId,
tip: getNick(option.nick) + option.message,
time: Date.now(),
isPushable: false,
isHistoryable: false,
isRoamingable: false,
done: function (err, data) {
// err && console.log(err)
// this.buildSessions();
// var msgHtml = appUI.buildChatContentUI(this.crtSession, this.cache)
this.cache.addMsgs(data)
var msgHtml = appUI.updateChatContentUI(data, this.cache)
this.$chatContent.append(msgHtml).scrollTop(99999)
}.bind(this)
});
}
/** 对列表用户进行点对点发送自定义系统通知
* @param {Array} list
* @param {object} option
* @param {string} option.caller 主叫人
* @param {string} option.type 视频还是音频, 如果为空,则取消呼叫!
* @param {string} option.list 被呼叫uid的列表
* @param {string} option.teamId 群id
* @param {string} option.channelName 房间id
*/
YX.fn.sendCustomMessage = function (option) {
var that = this;
option.list = option.list || [];
var tmpUser = this.cache.getTeamMemberInfo(option.caller, option.teamId);
option.nick = tmpUser.nickInTeam || getNick(option.caller);
option.list.forEach(function (uid) {
// this.mysdk.sendCustomMessage('p2p', item, content, this.sendMsgDone.bind(this))
that.nim.sendCustomSysMsg({
scene: 'p2p',
to: uid,
enablePushNick: false,
content: JSON.stringify({ "id": 3, "members": option.list, "teamId": option.teamId, "room": option.channelName, type: option.type }),
isPushable: true,
sendToOnlineUsersOnly: false,
apnsText: option.nick + '正在呼叫您',
done: function (error, msg) {
console.log(msg)
}
});
});
}