cache.js
18.3 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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
// 数据缓存
// 建议开发者选择mvvm框架来通过数据来驱动UI变化
var Cache = (function(){
var Cache = function (argument) {
this.friendslist = [];
this.personlist = {};
// 用户订阅的事件同步
this.personSubscribes = {};
this.teamlist = [];
this.teamMembers = {};
this.teamMap = {};
this.msgs ={};
this.sessions=[];
this.blacklist = [];
this.mutelist = [];
this.sysMsgs = [];
this.customSysMsgs = [];
this.sysMsgCount = 0;
};
/**
* 根据account获取用户对象
* @param account: 用户account
*/
Cache.prototype.getUserById = function (account) {
if(this.personlist[account]){
return this.personlist[account];
}
return false;
};
// 用户对象相关
Cache.prototype.setPersonlist = function(list){
var item;
for (var i = list.length - 1; i >= 0; i--) {
item = list[i];
this.personlist[item.account] = item;
};
};
Cache.prototype.updateAvatar = function(url){
this.personlist[userUID].avatar = url;
};
Cache.prototype.updatePersonlist = function(list){
if(!this.personlist[list.account]){
this.personlist[list.account] = list;
}else{
for(var p in list){
this.personlist[list.account][p] = list[p];
}
}
};
Cache.prototype.getPersonlist = function(){
return this.personlist;
};
/**
* 好友相关
*/
Cache.prototype.setFriends = function(list){
this.friendslist = list;
};
Cache.prototype.getFriends = function(list){
return this.friendslist;
};
// 获取好友备注名
Cache.prototype.getFriendAlias = function(account){
for (var i = this.friendslist.length-1; i >= 0; i--) {
if(this.friendslist[i].account == account){
return this.friendslist[i].alias||"";
}
};
}
Cache.prototype.updateFriendAlias = function(account,alias){
for (var i = this.friendslist.length-1; i >= 0; i--) {
if(this.friendslist[i].account == account){
this.friendslist[i].alias = alias;
return;
}
};
}
Cache.prototype.addFriend = function(list){
if(!this.isFriend(list.account)){
this.friendslist.push(list);
}
};
Cache.prototype.removeFriend = function(account){
for (var i = this.friendslist.length-1; i >= 0; i--) {
if(this.friendslist[i].account == account){
this.friendslist.splice(i,1);
}
};
};
Cache.prototype.getFriendslist = function(){
var array = [];
for(var i =0;i<this.friendslist.length; i++){
array.push(this.getUserById(this.friendslist[i].account));
}
return array;
};
Cache.prototype.getFriendslistOnShow = function(){
var array = [];
for(var i =0;i<this.friendslist.length; i++){
if(!this.inBlacklist(this.friendslist[i].account)){
array.push(this.getUserById(this.friendslist[i].account));
}
}
return array;
};
Cache.prototype.isFriend = function(account){
for (var i = this.friendslist.length-1 ; i >= 0; i--) {
if(this.friendslist[i].account == account){
return true;
}
};
return false;
};
/**
* 设置会话列表
* @param {Array} sessions 会话对象
*/
Cache.prototype.setSessions = function(sessions){
this.sessions = sessions;
};
/**
* 获取当前会话
* @return {Array} 会话集合
*/
Cache.prototype.getSessions = function () {
return this.sessions;
};
/**
* 获取指定会话
* @return {Array} 会话集合
*/
Cache.prototype.findSession = function (id) {
for (var i = this.sessions.length - 1; i >= 0; i--) {
if(this.sessions[i].id ===id){
return this.sessions[i];
}
};
return false;
}
Cache.prototype.addMsgs = function(msgs) {
var item,
user;
if(!$.isArray(msgs)){
this.addMsg(msgs);
return;
}
for (var i = 0; i <msgs.length; i++) {
if(msgs[i].scene==="team"){
user = msgs[i].to;
if(!this.msgs["team-"+user]){
this.msgs["team-"+user] = [];
}
this.msgs["team-"+user].push(msgs[i]);
}else{
user = (msgs[i].from === userUID?msgs[i].to:msgs[i].from);
if(!this.msgs["p2p-"+user]){
this.msgs["p2p-"+user] = [];
}
this.msgs["p2p-"+user].push(msgs[i]);
}
};
};
Cache.prototype.addMsg = function(msg){
var user;
if(msg.scene==="team"){
user = "team-"+msg.to;
if(!this.msgs[user]){
this.msgs[user] = [];
}
this.msgs[user].push(msg);
}else{
user = "p2p-"+(msg.from === userUID?msg.to:msg.from);
if(!this.msgs[user]){
this.msgs[user] = [];
}
this.msgs[user].push(msg);
}
};
Cache.prototype.addMsgsByReverse = function(msgs) {
var item
var user
var cacheSession = {}
for (var i = 0; i <msgs.length; i++) {
if(msgs[i].scene==="team"){
user = msgs[i].to;
if(!this.msgs["team-"+user]){
this.msgs["team-"+user] = [];
}
this.msgs["team-"+user].unshift(msgs[i]);
cacheSession["team-" + user] = true
}else{
user = (msgs[i].from === userUID?msgs[i].to:msgs[i].from);
if(!this.msgs["p2p-"+user]){
this.msgs["p2p-"+user] = [];
}
this.msgs["p2p-"+user].unshift(msgs[i]);
cacheSession["p2p-" + user] = true
}
};
for (var sid in cacheSession) {
this.msgs[sid] = this.msgs[sid].sort(function (a, b) {
return a.time - b.time;
});
}
};
//查消息 session-id idClient
Cache.prototype.findMsg = function(sid, cid) {
var list = this.msgs[sid];
for (var i = list.length - 1; i >= 0; i--) {
if(list[i].idClient === cid){
return list[i];
}
};
return false
}
//设置消息用于重发状态变化 session-id idClient 消息
Cache.prototype.setMsg = function(sid, cid, msg) {
var list = this.msgs[sid];
for (var i = list.length - 1; i >= 0; i--) {
if(list[i].idClient === cid){
list.splice(i,1);
list.push(msg);
return;
}
};
}
//回撤消息,回撤的消息用tip替换
Cache.prototype.backoutMsg = function (sid, cid, msg) {
var list = this.msgs[sid];
if(!list){
this.msgs[sid] = [msg]
return;
}
for (var i = list.length - 1; i >= 0; i--) {
if(list[i].idClient === cid){
list[i] = msg;
return;
}
}
this.msgs[sid].push(msg)
}
//系统消息
Cache.prototype.setSysMsgs = function(data){
this.sysMsgs= data;
}
Cache.prototype.getSysMsgs = function(data){
return this.sysMsgs;
}
//自定义系统消息
Cache.prototype.addCustomSysMsgs = function(data){
for (var i = 0; i <data.length; i++) {
this.customSysMsgs.push(data[i]);
};
}
Cache.prototype.deleteCustomSysMsgs = function(){
this.customSysMsgs= [];
}
Cache.prototype.getCustomSysMsgs = function(data){
return this.customSysMsgs;
}
// 系统消息计数
Cache.prototype.getSysMsgCount = function(value){
return this.sysMsgCount;
}
Cache.prototype.setSysMsgCount = function(value){
this.sysMsgCount = value;
}
Cache.prototype.addSysMsgCount = function(value){
this.sysMsgCount= this.sysMsgCount + value;
}
// /**
// * 删除漫游消息/历史消息
// * @param {String} to 需移除的消息对象标识
// */
// Cache.prototype.rmMsgs = function(to) {
// if($.type(to) === "string"){
// if(!!this.msgs["p2p-"+to]){
// delete this.msgs["p2p-"+to];
// }
// }else{
// if(!!this.msgs["team-"+to]){
// delete this.msgs["team-"+to];
// }
// }
// };
/**
* 获取漫游/历史消息
* @return {Array}
*/
Cache.prototype.getMsgs = function(id) {
if(!!this.msgs[id]){
return this.msgs[id];
}else{
return [];
}
};
/**
* 根据映射名来获取消息对象集合 如"p2p-iostest"
* @param {String} name 名字
* @return {Array}
*/
Cache.prototype.getMsgsByUser = function (name) {
return this.msgs[name]||[];
}
/**
* 离线消息处理
* @param {Array} msgs
*/
Cache.prototype.addOfflineMsgs= function(msgs) {
for (var i = msgs.length - 1; i >= 0; i--) {
if (/text|image|file|audio|video|geo|custom|notification|deleteMsg/i.test(msgs[i].type)) {
this.addMsgs(msgs[i]);
}else{
continue;
}
};
};
/**
* 初始化群列表
* @param {array} list
*/
Cache.prototype.setTeamList = function(list) {
var item;
for (var i = list.length - 1; i >= 0; i--) {
item = list[i];
this.teamMap[item.teamId] = item;
};
this.teamlist = list;
};
Cache.prototype.addTeam = function(team) {
if(!this.hasTeam(team.teamId)){
this.teamMap[team.teamId] = team;
this.teamlist.push(team);
}
};
Cache.prototype.hasTeam = function(id) {
var item;
for (var i = this.teamlist.length - 1; i >= 0; i--) {
item = this.teamlist[i];
if(item.teamId===id){
return true;
}
};
return false;
};
/**
* 获取群列表
*/
Cache.prototype.getTeamlist = function() {
return this.teamlist;
};
/**
* 获取群对象
*/
Cache.prototype.getTeamMap = function() {
return this.teamMap;
};
Cache.prototype.addTeamMap = function(data) {
for (var i = data.length - 1; i >= 0; i--) {
item = data[i];
this.teamMap[item.teamId] = item;
};
};
/**
* 根据群id获取群对象
*/
Cache.prototype.getTeamById = function(teamId) {
if(this.hasTeam(teamId)){
return this.teamMap[teamId];
}
return null;
};
Cache.prototype.getTeamMapById = function(teamId) {
return this.teamMap[teamId]||null;
};
/**
* 根据群id删除群
*/
Cache.prototype.removeTeamById = function (id) {
for (var i in this.teamlist) {
if (this.teamlist[i].teamId === id) {
this.teamlist.splice(i, 1);
break;
}
}
};
/**
* 更变群名
*/
Cache.prototype.updateTeam= function (teamId,obj) {
for(var p in obj){
this.teamMap[teamId][p] = obj[p];
}
for (var i in this.teamlist) {
if (this.teamlist[i].teamId === teamId) {
for(var p in obj){
this.teamlist[i][p] = obj[p];
}
break;
}
}
};
Cache.prototype.setTeamMembers = function(id, list){
this.teamMembers[id] = list;
}
Cache.prototype.addTeamMembers = function(id, array){
if(!this.teamMembers[id]){
return;
}
for (var i = array.length - 1; i >= 0; i--) {
this.teamMembers[id].members.push(array[i])
};
}
Cache.prototype.removeTeamMembers = function(id, array){
var obj = this.teamMembers[id],
account;
if(obj){
for (var j = array.length - 1; j >= 0; j--) {
account = array[j];
for (var i = obj.members.length - 1; i >= 0; i--) {
if (obj.members[i].account === account) {
obj.members.splice(i,1);
break;
}
};
};
}
}
Cache.prototype.getTeamMembers = function(id){
return this.teamMembers[id];
}
Cache.prototype.getTeamMemberInfo = function(account,id){
var obj = this.teamMembers[id];
if(obj&&obj.members){
for (var i = obj.members.length - 1; i >= 0; i--) {
if (obj.members[i].account === account) {
return obj.members[i]
}
};
}
return false
}
Cache.prototype.isTeamManager = function (account,id) {
var obj = this.teamMembers[id];
if(obj){
for (var i = obj.members.length - 1; i >= 0; i--) {
if (obj.members[i].account === account&&(obj.members[i].type==='owner'||obj.members[i].type==='manager')) {
return true
}
};
}
return false
}
Cache.prototype.updateTeamMemberMute = function (id,account,mute) {
var obj = this.teamMembers[id];
if(obj){
for (var i = obj.members.length - 1; i >= 0; i--) {
if (obj.members[i].account === account) {
obj.members[i].mute =mute;
return;
}
};
}
}
Cache.prototype.setMutelist= function (data) {
this.mutelist = data;
};
Cache.prototype.getMutelist= function (data) {
return this.mutelist;
};
Cache.prototype.inMutelist = function(account){
for (var i = this.mutelist.length - 1; i >= 0; i--) {
if(this.mutelist[i].account == account){
return true;
}
};
return false;
};
Cache.prototype.addToMutelist= function (data) {
this.mutelist.push(data);
}
Cache.prototype.removeFromMutelist = function(account){
for (var i = this.mutelist.length - 1; i >= 0; i--) {
if(this.mutelist[i].account == account){
this.mutelist.splice(i,1);
return true;
}
};
return false;
}
Cache.prototype.setBlacklist= function (data) {
this.blacklist = data;
};
Cache.prototype.getBlacklist= function () {
return this.blacklist;
}
Cache.prototype.inBlacklist = function(account){
for (var i = this.blacklist.length - 1; i >= 0; i--) {
if(this.blacklist[i].account == account){
return true;
}
};
return false;
};
Cache.prototype.addToBlacklist= function (data) {
this.blacklist.push(data);
};
Cache.prototype.removeFromBlacklist = function(account){
for (var i = this.blacklist.length - 1; i >= 0; i--) {
if(this.blacklist[i].account == account){
this.blacklist.splice(i,1);
return true;
}
};
return false;
};
// 更新用户多端同步订阅状态
Cache.prototype.updatePersonSubscribe = function (data) {
if (data.account) {
var account = data.account
this.personSubscribes[account] = this.personSubscribes[account] || {}
this.personSubscribes[account][data.type] = {
account: data.account,
type: data.type,
value: data.value,
custom: data.custom,
clientType: data.clientType,
serverConfig: data.serverConfig,
time: data.time,
// 多端状态
multiPortStatus: ''
}
var tempData = this.personSubscribes[account][data.type]
function getMultiPortStatus (customType, custom) {
var netState = {
0: '',
1: 'Wifi',
2: 'WWAN',
3: '2G',
4: '3G',
5: '4G'
}
var onlineState = {
0: '',
1: '',
2: ''
}
var multiPortStatus = ''
var custom = custom || {}
if (customType !== 0) {
// 有serverConfig.online属性,已被赋值端名称
custom = custom[customType]
} else if (custom[4]) {
custom = custom[4]
multiPortStatus = '电脑'
} else if (custom[2]) {
custom = custom[2]
multiPortStatus = 'iOS'
} else if (custom[1]) {
custom = custom[1]
multiPortStatus = 'Android'
} else if (custom[16]) {
custom = custom[16]
multiPortStatus = 'Web'
} else if (custom[64]) {
custom = custom[64]
multiPortStatus = 'Mac'
}
if (custom) {
custom = JSON.parse(custom)
if (typeof custom['net_state'] === 'number') {
var tempNetState = netState[custom['net_state']]
if (tempNetState) {
multiPortStatus += ('-' + tempNetState)
}
}
// if (typeof custom['online_state'] === 'number') {
// multiPortStatus += onlineState[custom['online_state']]
// } else {
// multiPortStatus += '在线'
// }
}
return multiPortStatus
}
// demo自定义多端登录同步事件
if (+data.type === 1) {
if (+data.value === 1 || +data.value === 2 || +data.value === 3 || +data.value === 10001) {
var serverConfig = JSON.parse(tempData.serverConfig)
var customType = 0
tempData.multiPortStatus = ''
// 优先判断serverConfig字段
if (serverConfig.online) {
if (serverConfig.online.indexOf(4) >= 0) {
tempData.multiPortStatus = '电脑'
customType = 4
} else if (serverConfig.online.indexOf(2) >= 0) {
tempData.multiPortStatus = 'iOS'
customType = 2
} else if (serverConfig.online.indexOf(1) >= 0) {
tempData.multiPortStatus = 'Android'
customType = 1
} else if (serverConfig.online.indexOf(16) >= 0) {
tempData.multiPortStatus = 'Web'
customType = 16
} else if (serverConfig.online.indexOf(64) >= 0) {
tempData.multiPortStatus = 'Mac'
customType = 64
}
}
if (tempData.custom && (Object.keys(tempData.custom).length > 0)) {
var portStatus = getMultiPortStatus(customType, tempData.custom)
// 如果serverConfig里有属性而custom里没有对应属性值
if ((tempData.multiPortStatus !== '') && (portStatus === '')) {
tempData.multiPortStatus += '在线'
} else {
tempData.multiPortStatus += portStatus
}
} else if (customType !== 0) {
tempData.multiPortStatus += '在线'
} else {
// tempData.multiPortStatus = '离线'
}
}
}
return true
}
return false
}
// 获取特定账号的订阅状态
Cache.prototype.getMultiPortStatus = function (account) {
if (this.personSubscribes[account] && this.personSubscribes[account][1]) {
return this.personSubscribes[account][1].multiPortStatus
}
return ''
}
// 获取用户的订阅关系
Cache.prototype.getPersonSubscribes = function () {
return this.personSubscribes
}
return Cache;
})();