@aochuang/client-sdk 1.0.345 → 1.0.346
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -203,7 +203,7 @@ export default class ClientChatAccount {
|
|
|
203
203
|
|
|
204
204
|
refreshAccountUnreadMessageNum (params) {
|
|
205
205
|
const { wechatAccountId, ...nextParams } = params
|
|
206
|
-
|
|
206
|
+
this.getAccountUnreadMessageNumMethod(Object.assign({
|
|
207
207
|
wechatAccountIds: [wechatAccountId]
|
|
208
208
|
}, nextParams)).then(rs => {
|
|
209
209
|
if (!rs || !rs.data || !rs.data.length) {
|
|
@@ -5,39 +5,20 @@ import { createPollRequest } from '../../../utils/util.js'
|
|
|
5
5
|
|
|
6
6
|
const ALL_WECHAT_ACCOUNT_ID = 'all'
|
|
7
7
|
|
|
8
|
-
const
|
|
9
|
-
const ASSIGN_SESSION_MERGE_EXCLUDE_FIELDS = [
|
|
10
|
-
'conversationId',
|
|
11
|
-
'wechatAccountId',
|
|
12
|
-
'lastMsgTime',
|
|
13
|
-
'lastUpdateTime',
|
|
14
|
-
'stickyOnTop',
|
|
15
|
-
'mute',
|
|
16
|
-
'lastAtMsgContent'
|
|
17
|
-
]
|
|
18
|
-
|
|
19
|
-
const loopLoadAssignSessionData = (fetchFn, params, onPageLoaded) => {
|
|
8
|
+
const loopLoadAssignSessionData = (fetchFn, params) => {
|
|
20
9
|
let lastPs = null
|
|
21
10
|
let lastRs = null
|
|
22
11
|
let result = []
|
|
23
|
-
let page = 0
|
|
24
12
|
const loopFn = (params, total) => {
|
|
25
13
|
const { taskId, size, ...nextParams } = params
|
|
26
|
-
page += 1
|
|
27
14
|
lastPs = fetchFn(Object.assign({}, nextParams, {
|
|
28
15
|
size,
|
|
29
16
|
taskId,
|
|
30
17
|
total
|
|
31
18
|
}))
|
|
32
19
|
lastRs = lastPs.then(rs => {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
onPageLoaded && onPageLoaded({
|
|
36
|
-
page,
|
|
37
|
-
pageSize: pageData.length,
|
|
38
|
-
total: result.length
|
|
39
|
-
})
|
|
40
|
-
if (pageData.length < size) {
|
|
20
|
+
result = result.concat(rs.data || [])
|
|
21
|
+
if (rs.data.length < size) {
|
|
41
22
|
return result
|
|
42
23
|
} else {
|
|
43
24
|
return loopFn(params, result.length)
|
|
@@ -207,7 +188,7 @@ export default class ClientChatSession {
|
|
|
207
188
|
* @params { Object } data - 会话数据集合
|
|
208
189
|
* @params { String } wechatAccountId - 如果传wechatAccountId那么后面addSession的时候如果传wechatAccountId那么才会更新lastUpdateTime或者lastMsgTime,本质的意思是只有通过reload和loadData才刷新lastUpdateTime或者lastMsgTime,避免这个时间乱套了
|
|
209
190
|
*/
|
|
210
|
-
setSessionData (data, wechatAccountId, reset = true
|
|
191
|
+
setSessionData (data, wechatAccountId, reset = true) {
|
|
211
192
|
if (reset) {
|
|
212
193
|
this.sessionMap = {}
|
|
213
194
|
this.data.sessionGroupData = {}
|
|
@@ -219,7 +200,7 @@ export default class ClientChatSession {
|
|
|
219
200
|
if (item.isDeleted) {
|
|
220
201
|
this.removeSession(item.conversationId)
|
|
221
202
|
} else {
|
|
222
|
-
this.addSession(item, wechatAccountId
|
|
203
|
+
this.addSession(item, wechatAccountId)
|
|
223
204
|
}
|
|
224
205
|
})
|
|
225
206
|
}
|
|
@@ -247,7 +228,11 @@ export default class ClientChatSession {
|
|
|
247
228
|
})
|
|
248
229
|
}
|
|
249
230
|
|
|
250
|
-
getSession (sessionId, serializeMethod) {
|
|
231
|
+
getSession (sessionId, serializeMethod, callback = null, isCache = true) {
|
|
232
|
+
if (typeof callback === 'boolean') {
|
|
233
|
+
isCache = callback
|
|
234
|
+
callback = null
|
|
235
|
+
}
|
|
251
236
|
if (this.sessionMap[sessionId]) {
|
|
252
237
|
return Promise.resolve({
|
|
253
238
|
data: this.sessionMap[sessionId]
|
|
@@ -255,7 +240,12 @@ export default class ClientChatSession {
|
|
|
255
240
|
}
|
|
256
241
|
// 这里需要轮询从后端获取会话,后端会话更新和创建是异步的,所以要多轮训请求几次
|
|
257
242
|
if (this.pollRequestMap[sessionId]) {
|
|
258
|
-
|
|
243
|
+
// 因为有的地方要复用缓存,有的地方不能缓存,比如onAddMessagSuccess的时候不能缓存
|
|
244
|
+
if (isCache) {
|
|
245
|
+
return this.pollRequestMap[sessionId].ps
|
|
246
|
+
} else {
|
|
247
|
+
this.pollRequestMap[sessionId].stop()
|
|
248
|
+
}
|
|
259
249
|
}
|
|
260
250
|
const pollRequest = createPollRequest(({ done, fail }) => {
|
|
261
251
|
return this.getSessionMethod({
|
|
@@ -272,7 +262,7 @@ export default class ClientChatSession {
|
|
|
272
262
|
rs = serializeMethod(rs)
|
|
273
263
|
}
|
|
274
264
|
if (rs.data) {
|
|
275
|
-
this.addSession(rs.data)
|
|
265
|
+
this.addSession(rs.data, null, callback)
|
|
276
266
|
return rs
|
|
277
267
|
} else {
|
|
278
268
|
return Promise.reject('未找到会话' + sessionId)
|
|
@@ -407,7 +397,7 @@ export default class ClientChatSession {
|
|
|
407
397
|
* "wechatAccountId": "2a39a725814acf07502299ab2285fff8d44377f5"
|
|
408
398
|
* }
|
|
409
399
|
*/
|
|
410
|
-
addSession (data, wechatAccountId,
|
|
400
|
+
addSession (data, wechatAccountId, callback) {
|
|
411
401
|
if (!data || !data.conversationId) {
|
|
412
402
|
throw new Error('缺少必须参数conversationId')
|
|
413
403
|
}
|
|
@@ -423,54 +413,11 @@ export default class ClientChatSession {
|
|
|
423
413
|
return
|
|
424
414
|
}
|
|
425
415
|
let nextData = data
|
|
426
|
-
let assignMergeLog = null
|
|
427
416
|
// 如果会话已经存在,那么取已经存在的,以后有需要可以用data更新已经存在的会话,保持引用关系
|
|
428
417
|
if (this.sessionMap[data.conversationId]) {
|
|
429
418
|
nextData = this.sessionMap[data.conversationId]
|
|
430
|
-
const unreadBefore = nextData.unreadMsgCount || 0
|
|
431
|
-
const muteBefore = !!nextData.mute
|
|
432
|
-
const changedFields = []
|
|
433
|
-
Object.keys(data).forEach(key => {
|
|
434
|
-
if (ASSIGN_SESSION_MERGE_EXCLUDE_FIELDS.indexOf(key) >= 0 || nextData[key] === data[key]) {
|
|
435
|
-
return
|
|
436
|
-
}
|
|
437
|
-
changedFields.push(key)
|
|
438
|
-
Vue.set(nextData, key, data[key])
|
|
439
|
-
})
|
|
440
|
-
if (context && context.source === 'assign-task') {
|
|
441
|
-
assignMergeLog = {
|
|
442
|
-
taskId: context.taskId,
|
|
443
|
-
conversationId: data.conversationId,
|
|
444
|
-
wechatAccountId: data.wechatAccountId,
|
|
445
|
-
existed: true,
|
|
446
|
-
changedFields,
|
|
447
|
-
unreadBefore,
|
|
448
|
-
unreadFromServer: data.unreadMsgCount || 0,
|
|
449
|
-
unreadAfter: nextData.unreadMsgCount || 0,
|
|
450
|
-
muteBefore,
|
|
451
|
-
muteFromServer: !!data.mute,
|
|
452
|
-
muteAfter: !!nextData.mute,
|
|
453
|
-
preservedFields: ASSIGN_SESSION_MERGE_EXCLUDE_FIELDS.slice()
|
|
454
|
-
}
|
|
455
|
-
}
|
|
456
419
|
} else {
|
|
457
420
|
this.sessionMap[data.conversationId] = nextData
|
|
458
|
-
if (context && context.source === 'assign-task') {
|
|
459
|
-
assignMergeLog = {
|
|
460
|
-
taskId: context.taskId,
|
|
461
|
-
conversationId: data.conversationId,
|
|
462
|
-
wechatAccountId: data.wechatAccountId,
|
|
463
|
-
existed: false,
|
|
464
|
-
changedFields: [],
|
|
465
|
-
unreadBefore: null,
|
|
466
|
-
unreadFromServer: data.unreadMsgCount || 0,
|
|
467
|
-
unreadAfter: data.unreadMsgCount || 0,
|
|
468
|
-
muteBefore: null,
|
|
469
|
-
muteFromServer: !!data.mute,
|
|
470
|
-
muteAfter: !!data.mute,
|
|
471
|
-
preservedFields: []
|
|
472
|
-
}
|
|
473
|
-
}
|
|
474
421
|
}
|
|
475
422
|
const topField = nextData.stickyOnTop ? 0 : 1 // 置顶
|
|
476
423
|
if (wechatAccountId && wechatAccountId !== ALL_WECHAT_ACCOUNT_ID && nextData.wechatAccountId !== wechatAccountId) {
|
|
@@ -551,12 +498,12 @@ export default class ClientChatSession {
|
|
|
551
498
|
if (virtualSessionIndex > -1) {
|
|
552
499
|
this.data.virtualSessionData.splice(virtualSessionIndex, 1)
|
|
553
500
|
}
|
|
501
|
+
callback && callback({
|
|
502
|
+
session: data
|
|
503
|
+
})
|
|
554
504
|
this.onAddSession && this.onAddSession({
|
|
555
|
-
session:
|
|
505
|
+
session: data
|
|
556
506
|
})
|
|
557
|
-
if (assignMergeLog) {
|
|
558
|
-
console.info(ASSIGN_SESSION_LOG_PREFIX, 'merge', assignMergeLog)
|
|
559
|
-
}
|
|
560
507
|
}
|
|
561
508
|
|
|
562
509
|
removeSession (conversationId) {
|
|
@@ -690,9 +637,7 @@ export default class ClientChatSession {
|
|
|
690
637
|
return true
|
|
691
638
|
}
|
|
692
639
|
})
|
|
693
|
-
|
|
694
|
-
this.data.sessionGroupData[groupId][topField][oldSortFields.day][oldSortFields.milliseconds].splice(index, 1)
|
|
695
|
-
}
|
|
640
|
+
this.data.sessionGroupData[groupId][topField][oldSortFields.day][oldSortFields.milliseconds].splice(index, 1)
|
|
696
641
|
}
|
|
697
642
|
})
|
|
698
643
|
})
|
|
@@ -752,9 +697,7 @@ export default class ClientChatSession {
|
|
|
752
697
|
return true
|
|
753
698
|
}
|
|
754
699
|
})
|
|
755
|
-
|
|
756
|
-
this.data.sessionGroupData[groupId][topField][sortFields.day][sortFields.milliseconds].splice(index, 1)
|
|
757
|
-
}
|
|
700
|
+
this.data.sessionGroupData[groupId][topField][sortFields.day][sortFields.milliseconds].splice(index, 1)
|
|
758
701
|
}
|
|
759
702
|
})
|
|
760
703
|
})
|
|
@@ -899,36 +842,14 @@ export default class ClientChatSession {
|
|
|
899
842
|
total: 0 , // 已获取总数
|
|
900
843
|
size: 50 // 每次获取数量
|
|
901
844
|
}, params)
|
|
902
|
-
const taskId = params.taskId
|
|
903
|
-
console.info(ASSIGN_SESSION_LOG_PREFIX, 'load-start', {
|
|
904
|
-
taskId,
|
|
905
|
-
total: params.total,
|
|
906
|
-
size: params.size
|
|
907
|
-
})
|
|
908
845
|
this.data.loadAssignTaskSessioning = true
|
|
909
846
|
return loopLoadAssignSessionData((nextParams) => {
|
|
910
847
|
return this.loadAssignTaskSessionDataMethod(nextParams)
|
|
911
|
-
}, params
|
|
912
|
-
console.info(ASSIGN_SESSION_LOG_PREFIX, 'page-loaded', Object.assign({ taskId }, pageInfo))
|
|
913
|
-
}).then(rs => {
|
|
848
|
+
}, params).then(rs => {
|
|
914
849
|
if (rs && rs.length) {
|
|
915
|
-
this.setSessionData(rs, null, false
|
|
916
|
-
source: 'assign-task',
|
|
917
|
-
taskId
|
|
918
|
-
})
|
|
850
|
+
this.setSessionData(rs, null, false)
|
|
919
851
|
}
|
|
920
|
-
console.info(ASSIGN_SESSION_LOG_PREFIX, 'load-complete', {
|
|
921
|
-
taskId,
|
|
922
|
-
sessionCount: rs ? rs.length : 0,
|
|
923
|
-
wechatAccountIds: Array.from(new Set((rs || []).map(session => session.wechatAccountId)))
|
|
924
|
-
})
|
|
925
852
|
return rs
|
|
926
|
-
}).catch(error => {
|
|
927
|
-
console.error(ASSIGN_SESSION_LOG_PREFIX, 'load-error', {
|
|
928
|
-
taskId,
|
|
929
|
-
message: error && error.message ? error.message : String(error)
|
|
930
|
-
})
|
|
931
|
-
throw error
|
|
932
853
|
}).finally(() => {
|
|
933
854
|
this.data.loadAssignTaskSessioning = false
|
|
934
855
|
})
|
|
@@ -15,7 +15,7 @@ const CONNECT_STATUS = {
|
|
|
15
15
|
Normal: 'normal',
|
|
16
16
|
Loading: 'loading',
|
|
17
17
|
Complete: 'complete'
|
|
18
|
-
}
|
|
18
|
+
}
|
|
19
19
|
|
|
20
20
|
function getAtMessageContent (data) {
|
|
21
21
|
if (!data || data.isSend || !data.content) {
|
|
@@ -43,136 +43,6 @@ function appendLastAtMsgContent (lastAtMsgContent, atMessage) {
|
|
|
43
43
|
return messages.concat(atMessage)
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
const ASSIGN_SESSION_LOG_PREFIX = '[client-chat][assign-session]'
|
|
47
|
-
|
|
48
|
-
function getAccountUnreadSnapshot (account, accountIds) {
|
|
49
|
-
const accountIdMap = accountIds && accountIds.reduce((result, accountId) => {
|
|
50
|
-
result[accountId] = true
|
|
51
|
-
return result
|
|
52
|
-
}, {})
|
|
53
|
-
return (account.accountUnreadMessageNumData || []).filter(item => {
|
|
54
|
-
return !accountIdMap || accountIdMap[item.id]
|
|
55
|
-
}).map(item => ({
|
|
56
|
-
wechatAccountId: item.id,
|
|
57
|
-
unread: item.num || 0,
|
|
58
|
-
muteUnread: item.muteNum || 0
|
|
59
|
-
}))
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function getAssignAccountIds (sessions) {
|
|
63
|
-
return Object.keys((sessions || []).reduce((result, session) => {
|
|
64
|
-
if (session && session.wechatAccountId) {
|
|
65
|
-
result[session.wechatAccountId] = true
|
|
66
|
-
}
|
|
67
|
-
return result
|
|
68
|
-
}, {}))
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function getSessionUnreadSnapshot (sessionInstance) {
|
|
72
|
-
return Object.keys(sessionInstance.sessionMap || {}).reduce((result, conversationId) => {
|
|
73
|
-
const session = sessionInstance.sessionMap[conversationId]
|
|
74
|
-
result[conversationId] = {
|
|
75
|
-
wechatAccountId: session.wechatAccountId,
|
|
76
|
-
unread: session.unreadMsgCount || 0,
|
|
77
|
-
mute: !!session.mute
|
|
78
|
-
}
|
|
79
|
-
return result
|
|
80
|
-
}, {})
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
function getAssignUnreadChanges (clientChat, sessions, sessionUnreadBefore, accountIdBeforeMap, taskId) {
|
|
84
|
-
const sessionMap = {}
|
|
85
|
-
const sessionData = sessions || []
|
|
86
|
-
sessionData.forEach(session => {
|
|
87
|
-
if (!session || !session.conversationId) {
|
|
88
|
-
return
|
|
89
|
-
}
|
|
90
|
-
if (sessionMap[session.conversationId]) {
|
|
91
|
-
console.warn(ASSIGN_SESSION_LOG_PREFIX, 'duplicate-session', {
|
|
92
|
-
taskId,
|
|
93
|
-
conversationId: session.conversationId,
|
|
94
|
-
wechatAccountId: session.wechatAccountId
|
|
95
|
-
})
|
|
96
|
-
}
|
|
97
|
-
sessionMap[session.conversationId] = session
|
|
98
|
-
})
|
|
99
|
-
const accountChangeMap = {}
|
|
100
|
-
Object.keys(sessionMap).forEach(conversationId => {
|
|
101
|
-
const serverSession = sessionMap[conversationId]
|
|
102
|
-
const localSession = clientChat.session.sessionMap[conversationId] || serverSession
|
|
103
|
-
const before = sessionUnreadBefore[conversationId]
|
|
104
|
-
const beforeUnread = before ? before.unread : 0
|
|
105
|
-
const beforeMute = before ? before.mute : false
|
|
106
|
-
const afterUnread = localSession.unreadMsgCount || 0
|
|
107
|
-
const afterMute = !!localSession.mute
|
|
108
|
-
const wechatAccountId = localSession.wechatAccountId
|
|
109
|
-
if (!accountChangeMap[wechatAccountId]) {
|
|
110
|
-
accountChangeMap[wechatAccountId] = {
|
|
111
|
-
wechatAccountId,
|
|
112
|
-
unreadDelta: 0,
|
|
113
|
-
muteDelta: 0,
|
|
114
|
-
isNewAccount: !accountIdBeforeMap[wechatAccountId],
|
|
115
|
-
sessionCount: 0
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
accountChangeMap[wechatAccountId].unreadDelta += afterUnread - beforeUnread
|
|
119
|
-
accountChangeMap[wechatAccountId].muteDelta += (afterMute ? afterUnread : 0) - (beforeMute ? beforeUnread : 0)
|
|
120
|
-
accountChangeMap[wechatAccountId].sessionCount += 1
|
|
121
|
-
})
|
|
122
|
-
return Object.keys(accountChangeMap).map(accountId => accountChangeMap[accountId])
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
function updateAssignAccountUnread (clientChat, changes, taskId, eventType) {
|
|
126
|
-
return Promise.all(changes.map(change => {
|
|
127
|
-
const accountUnreadBefore = getAccountUnreadSnapshot(clientChat.account, [change.wechatAccountId])[0] || {
|
|
128
|
-
wechatAccountId: change.wechatAccountId,
|
|
129
|
-
unread: 0,
|
|
130
|
-
muteUnread: 0
|
|
131
|
-
}
|
|
132
|
-
let promise
|
|
133
|
-
let strategy
|
|
134
|
-
if (change.isNewAccount) {
|
|
135
|
-
strategy = 'server-refresh'
|
|
136
|
-
promise = clientChat.account.refreshAccountUnreadMessageNum({
|
|
137
|
-
wechatAccountId: change.wechatAccountId
|
|
138
|
-
})
|
|
139
|
-
} else if (change.unreadDelta || change.muteDelta) {
|
|
140
|
-
strategy = 'delta'
|
|
141
|
-
promise = clientChat.account.updateAccountUnreadMessageNum(change.wechatAccountId, ({ unreadMesasgeNum, unreadMessageMuteNum }) => ({
|
|
142
|
-
num: Math.max(0, (unreadMesasgeNum || 0) + change.unreadDelta),
|
|
143
|
-
muteNum: Math.max(0, (unreadMessageMuteNum || 0) + change.muteDelta)
|
|
144
|
-
}))
|
|
145
|
-
} else {
|
|
146
|
-
strategy = 'no-change'
|
|
147
|
-
promise = Promise.resolve()
|
|
148
|
-
}
|
|
149
|
-
return promise.then(() => {
|
|
150
|
-
console.info(ASSIGN_SESSION_LOG_PREFIX, 'account-unread-updated', {
|
|
151
|
-
eventType,
|
|
152
|
-
taskId,
|
|
153
|
-
wechatAccountId: change.wechatAccountId,
|
|
154
|
-
sessionCount: change.sessionCount,
|
|
155
|
-
unreadDelta: change.unreadDelta,
|
|
156
|
-
muteDelta: change.muteDelta,
|
|
157
|
-
strategy,
|
|
158
|
-
before: accountUnreadBefore,
|
|
159
|
-
after: getAccountUnreadSnapshot(clientChat.account, [change.wechatAccountId])[0] || null
|
|
160
|
-
})
|
|
161
|
-
}).catch(error => {
|
|
162
|
-
console.error(ASSIGN_SESSION_LOG_PREFIX, 'account-unread-update-error', {
|
|
163
|
-
eventType,
|
|
164
|
-
taskId,
|
|
165
|
-
wechatAccountId: change.wechatAccountId,
|
|
166
|
-
unreadDelta: change.unreadDelta,
|
|
167
|
-
muteDelta: change.muteDelta,
|
|
168
|
-
strategy,
|
|
169
|
-
message: error && error.message ? error.message : String(error)
|
|
170
|
-
})
|
|
171
|
-
throw error
|
|
172
|
-
})
|
|
173
|
-
}))
|
|
174
|
-
}
|
|
175
|
-
|
|
176
46
|
class ClientChat {
|
|
177
47
|
data = Vue.observable({
|
|
178
48
|
connectStatus: CONNECT_STATUS.Normal
|
|
@@ -553,22 +423,45 @@ class ClientChat {
|
|
|
553
423
|
rs.data.lastUpdateTime = data.updateDate
|
|
554
424
|
}
|
|
555
425
|
return rs
|
|
556
|
-
}
|
|
426
|
+
}, ({ session }) => {
|
|
557
427
|
// 更新账号未读消息数量
|
|
558
|
-
this.account.updateAccountUnreadMessageNum(
|
|
559
|
-
if (
|
|
428
|
+
this.account.updateAccountUnreadMessageNum(session.wechatAccountId, ({ unreadMesasgeNum, unreadMessageMuteNum }) => {
|
|
429
|
+
if (session.mute) {
|
|
560
430
|
return {
|
|
561
|
-
num: (unreadMesasgeNum || 0) + (
|
|
562
|
-
muteNum: (unreadMessageMuteNum || 0) + (
|
|
431
|
+
num: (unreadMesasgeNum || 0) + (session.unreadMsgCount || 0),
|
|
432
|
+
muteNum: (unreadMessageMuteNum || 0) + (session.unreadMsgCount || 0)
|
|
563
433
|
}
|
|
564
434
|
} else {
|
|
565
435
|
return {
|
|
566
|
-
num: (unreadMesasgeNum || 0) + (
|
|
436
|
+
num: (unreadMesasgeNum || 0) + (session.unreadMsgCount || 0),
|
|
567
437
|
muteNum: unreadMessageMuteNum
|
|
568
438
|
}
|
|
569
439
|
}
|
|
570
440
|
})
|
|
571
|
-
}, (
|
|
441
|
+
}, false).then((rs => {
|
|
442
|
+
// 前面更新未读消息数量只是处理会话的时候,因为getSession是异步的,会导致addMessage多次执行到这里,不会倒下面更新会话的逻辑
|
|
443
|
+
// 比如连续发了3条消息,然后getSession后端返回可能只会返回2条未读数,所以这里还需要补偿一下
|
|
444
|
+
const session = rs.data || {}
|
|
445
|
+
const isNewerMessage = data.wechatTime > (session.lastMsgTime || 0)
|
|
446
|
+
if (!data.isSend && isNewerMessage) {
|
|
447
|
+
if (!(this.sessionManage.currentSession && this.sessionManage.currentSession.conversationId === sessionId && this.shouldMarkCurrentSessionRead(sessionId, data))) {
|
|
448
|
+
// 更新账号未读消息数量
|
|
449
|
+
this.account.updateAccountUnreadMessageNum(session.wechatAccountId, ({ unreadMesasgeNum, unreadMessageMuteNum }) => {
|
|
450
|
+
if (session.mute) {
|
|
451
|
+
return {
|
|
452
|
+
num: (unreadMesasgeNum || 0) + 1,
|
|
453
|
+
muteNum: (unreadMessageMuteNum || 0) + 1
|
|
454
|
+
}
|
|
455
|
+
} else {
|
|
456
|
+
return {
|
|
457
|
+
num: (unreadMesasgeNum || 0) + 1,
|
|
458
|
+
muteNum: unreadMessageMuteNum
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
})
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
}), (error) => {
|
|
572
465
|
this.socket.addLog({
|
|
573
466
|
group: 'client-chat-message:onAddMessageSuccess',
|
|
574
467
|
content: {
|
|
@@ -598,7 +491,7 @@ class ClientChat {
|
|
|
598
491
|
lastUpdateTime: data.updateDate,
|
|
599
492
|
unreadMsgCount: session.unreadMsgCount
|
|
600
493
|
}
|
|
601
|
-
|
|
494
|
+
if (!data.isSend && isNewerMessage) {
|
|
602
495
|
if (this.sessionManage.currentSession && this.sessionManage.currentSession.conversationId === sessionId && this.shouldMarkCurrentSessionRead(sessionId, data)) {
|
|
603
496
|
// 标记已读
|
|
604
497
|
this.session.setSessionRead(sessionId)
|
|
@@ -701,21 +594,44 @@ class ClientChat {
|
|
|
701
594
|
rs.data.lastAtMsgContent = appendLastAtMsgContent(rs.data.lastAtMsgContent, atMessage)
|
|
702
595
|
}
|
|
703
596
|
return rs
|
|
704
|
-
}
|
|
597
|
+
}, ({ session }) => {
|
|
705
598
|
// 更新账号未读消息数量
|
|
706
|
-
this.account.updateAccountUnreadMessageNum(
|
|
707
|
-
if (
|
|
599
|
+
this.account.updateAccountUnreadMessageNum(session.wechatAccountId, ({ unreadMesasgeNum, unreadMessageMuteNum }) => {
|
|
600
|
+
if (session.mute) {
|
|
708
601
|
return {
|
|
709
|
-
num: (unreadMesasgeNum || 0) + (
|
|
710
|
-
muteNum: (unreadMessageMuteNum || 0) + (
|
|
602
|
+
num: (unreadMesasgeNum || 0) + (session.unreadMsgCount || 0),
|
|
603
|
+
muteNum: (unreadMessageMuteNum || 0) + (session.unreadMsgCount || 0)
|
|
711
604
|
}
|
|
712
605
|
} else {
|
|
713
606
|
return {
|
|
714
|
-
num: (unreadMesasgeNum || 0) + (
|
|
607
|
+
num: (unreadMesasgeNum || 0) + (session.unreadMsgCount || 0),
|
|
715
608
|
muteNum: unreadMessageMuteNum
|
|
716
609
|
}
|
|
717
610
|
}
|
|
718
611
|
})
|
|
612
|
+
}, false).then(rs => {
|
|
613
|
+
// 前面更新未读消息数量只是处理会话的时候,因为getSession是异步的,会导致addMessage多次执行到这里,不会倒下面更新会话的逻辑
|
|
614
|
+
// 比如连续发了3条消息,然后getSession后端返回可能只会返回2条未读数,所以这里还需要补偿一下
|
|
615
|
+
const session = rs.data || {}
|
|
616
|
+
const isNewerMessage = data.wechatTime > (session.lastMsgTime || 0)
|
|
617
|
+
if (!data.isSend && isNewerMessage) {
|
|
618
|
+
if (!(this.sessionManage.currentSession && this.sessionManage.currentSession.conversationId === sessionId && this.shouldMarkCurrentSessionRead(sessionId, data))) {
|
|
619
|
+
// 更新账号未读消息数量
|
|
620
|
+
this.account.updateAccountUnreadMessageNum(session.wechatAccountId, ({ unreadMesasgeNum, unreadMessageMuteNum }) => {
|
|
621
|
+
if (session.mute) {
|
|
622
|
+
return {
|
|
623
|
+
num: (unreadMesasgeNum || 0) + 1,
|
|
624
|
+
muteNum: (unreadMessageMuteNum || 0) + 1
|
|
625
|
+
}
|
|
626
|
+
} else {
|
|
627
|
+
return {
|
|
628
|
+
num: (unreadMesasgeNum || 0) + 1,
|
|
629
|
+
muteNum: unreadMessageMuteNum
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
})
|
|
633
|
+
}
|
|
634
|
+
}
|
|
719
635
|
}, (error) => {
|
|
720
636
|
this.socket.addLog({
|
|
721
637
|
group: 'client-chat-message:onAddMessageSuccess',
|
|
@@ -996,16 +912,6 @@ class ClientChat {
|
|
|
996
912
|
},
|
|
997
913
|
// 好友有变化,如分配的情况
|
|
998
914
|
onCmdFriendInfoChanged: ({ data }) => {
|
|
999
|
-
const assignAccountUnreadBefore = data.changeType === 4 ? getAccountUnreadSnapshot(this.account) : null
|
|
1000
|
-
if (data.changeType === 4) {
|
|
1001
|
-
console.info(ASSIGN_SESSION_LOG_PREFIX, 'socket-event', {
|
|
1002
|
-
eventType: 'friend-info-changed',
|
|
1003
|
-
taskId: data.taskId,
|
|
1004
|
-
changeType: data.changeType,
|
|
1005
|
-
itemCount: Array.isArray(data.friends) ? data.friends.length : 0,
|
|
1006
|
-
accountUnreadBefore: assignAccountUnreadBefore
|
|
1007
|
-
})
|
|
1008
|
-
}
|
|
1009
915
|
if (!this.socketFriendInfoChangedSerialQueue) {
|
|
1010
916
|
this.socketFriendInfoChangedSerialQueue = createSerialQueue()
|
|
1011
917
|
}
|
|
@@ -1019,19 +925,14 @@ class ClientChat {
|
|
|
1019
925
|
accountIds: (this.account.accountData || []).map(v => v.id)
|
|
1020
926
|
})
|
|
1021
927
|
} else {
|
|
1022
|
-
const sessionUnreadBefore = getSessionUnreadSnapshot(this.session)
|
|
1023
|
-
const accountIdBeforeMap = Object.keys(this.account.accountMap || {}).reduce((result, accountId) => {
|
|
1024
|
-
result[accountId] = true
|
|
1025
|
-
return result
|
|
1026
|
-
}, {})
|
|
1027
928
|
// 增量拉取分配产生的会话
|
|
1028
929
|
return this.session.loadAssignTaskSessionData({
|
|
1029
930
|
taskId: data.taskId
|
|
1030
931
|
}).then((rs) => {
|
|
1031
|
-
const wechatAccountIds =
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
932
|
+
const wechatAccountIds = Object.keys((rs || []).reduce((rs, v) => {
|
|
933
|
+
rs[v.wechatAccountId] = true
|
|
934
|
+
return rs
|
|
935
|
+
}, {}))
|
|
1035
936
|
const changeFriendMap = {}
|
|
1036
937
|
// 删除changeType为3移除的好友,例如转移的场景
|
|
1037
938
|
if (data.friends && data.friends.length) {
|
|
@@ -1064,27 +965,25 @@ class ClientChat {
|
|
|
1064
965
|
return this.contacts.getFriend(sessionFriendId, true)
|
|
1065
966
|
})
|
|
1066
967
|
}
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
968
|
+
// 刷新工作微信号未读消息数量
|
|
969
|
+
if (rs && rs.length) {
|
|
970
|
+
rs.forEach(session => {
|
|
971
|
+
this.account.updateAccountUnreadMessageNum(session.wechatAccountId, ({ unreadMesasgeNum, unreadMessageMuteNum }) => {
|
|
972
|
+
if (session.mute) {
|
|
973
|
+
return {
|
|
974
|
+
num: (unreadMesasgeNum || 0) + (session.unreadMsgCount || 0),
|
|
975
|
+
muteNum: (unreadMessageMuteNum || 0) + (session.unreadMsgCount || 0)
|
|
976
|
+
}
|
|
977
|
+
} else {
|
|
978
|
+
return {
|
|
979
|
+
num: (unreadMesasgeNum || 0) + (session.unreadMsgCount || 0),
|
|
980
|
+
muteNum: unreadMessageMuteNum
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
})
|
|
1074
984
|
})
|
|
1075
|
-
|
|
1076
|
-
})
|
|
985
|
+
}
|
|
1077
986
|
return ps.finally(() => {
|
|
1078
|
-
console.info(ASSIGN_SESSION_LOG_PREFIX, 'socket-complete', {
|
|
1079
|
-
eventType: 'friend-info-changed',
|
|
1080
|
-
taskId: data.taskId,
|
|
1081
|
-
sessionCount: rs ? rs.length : 0,
|
|
1082
|
-
wechatAccountIds,
|
|
1083
|
-
accountUnreadChanges,
|
|
1084
|
-
accountUnreadBefore,
|
|
1085
|
-
accountUnreadAfter: getAccountUnreadSnapshot(this.account, wechatAccountIds),
|
|
1086
|
-
accountUnreadUpdated
|
|
1087
|
-
})
|
|
1088
987
|
// 这里应该标记那些工作微信号的通讯录修改了,后面要在界面打个提示,告诉用户通讯录变化了,让手动刷新一下
|
|
1089
988
|
// 同时也将缓存都清理一下,例如工作微信号分组统计数量等
|
|
1090
989
|
this.contacts.markModify() // 标记通讯录修改
|
|
@@ -1123,31 +1022,16 @@ class ClientChat {
|
|
|
1123
1022
|
},
|
|
1124
1023
|
// 群聊有变化,如分配的情况
|
|
1125
1024
|
onCmdChatroomInfoChanged: ({ data }) => {
|
|
1126
|
-
const assignAccountUnreadBefore = data.changeType === 4 ? getAccountUnreadSnapshot(this.account) : null
|
|
1127
|
-
if (data.changeType === 4) {
|
|
1128
|
-
console.info(ASSIGN_SESSION_LOG_PREFIX, 'socket-event', {
|
|
1129
|
-
eventType: 'chatroom-info-changed',
|
|
1130
|
-
taskId: data.taskId,
|
|
1131
|
-
changeType: data.changeType,
|
|
1132
|
-
itemCount: Array.isArray(data.chatrooms) ? data.chatrooms.length : 0,
|
|
1133
|
-
accountUnreadBefore: assignAccountUnreadBefore
|
|
1134
|
-
})
|
|
1135
|
-
}
|
|
1136
1025
|
// 配分群聊
|
|
1137
1026
|
if (data.changeType === 4) {
|
|
1138
|
-
const sessionUnreadBefore = getSessionUnreadSnapshot(this.session)
|
|
1139
|
-
const accountIdBeforeMap = Object.keys(this.account.accountMap || {}).reduce((result, accountId) => {
|
|
1140
|
-
result[accountId] = true
|
|
1141
|
-
return result
|
|
1142
|
-
}, {})
|
|
1143
1027
|
// 增量拉取分配产生的会话
|
|
1144
1028
|
return this.session.loadAssignTaskSessionData({
|
|
1145
1029
|
taskId: data.taskId
|
|
1146
1030
|
}).then((rs) => {
|
|
1147
|
-
const wechatAccountIds =
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1031
|
+
const wechatAccountIds = Object.keys((rs || []).reduce((rs, v) => {
|
|
1032
|
+
rs[v.wechatAccountId] = true
|
|
1033
|
+
return rs
|
|
1034
|
+
}, {}))
|
|
1151
1035
|
// 删除changeType为3移除的群聊,例如转移的场景
|
|
1152
1036
|
if (data.chatrooms && data.chatrooms.length) {
|
|
1153
1037
|
data.chatrooms.forEach(room => {
|
|
@@ -1163,27 +1047,25 @@ class ClientChat {
|
|
|
1163
1047
|
ps = this.account.reloadData()
|
|
1164
1048
|
}
|
|
1165
1049
|
}
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1050
|
+
// 刷新工作微信号未读消息数量
|
|
1051
|
+
if (rs && rs.length) {
|
|
1052
|
+
rs.forEach(session => {
|
|
1053
|
+
this.account.updateAccountUnreadMessageNum(session.wechatAccountId, ({ unreadMesasgeNum, unreadMessageMuteNum }) => {
|
|
1054
|
+
if (session.mute) {
|
|
1055
|
+
return {
|
|
1056
|
+
num: (unreadMesasgeNum || 0) + (session.unreadMsgCount || 0),
|
|
1057
|
+
muteNum: (unreadMessageMuteNum || 0) + (session.unreadMsgCount || 0)
|
|
1058
|
+
}
|
|
1059
|
+
} else {
|
|
1060
|
+
return {
|
|
1061
|
+
num: (unreadMesasgeNum || 0) + (session.unreadMsgCount || 0),
|
|
1062
|
+
muteNum: unreadMessageMuteNum
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
})
|
|
1173
1066
|
})
|
|
1174
|
-
|
|
1175
|
-
})
|
|
1067
|
+
}
|
|
1176
1068
|
return ps.finally(() => {
|
|
1177
|
-
console.info(ASSIGN_SESSION_LOG_PREFIX, 'socket-complete', {
|
|
1178
|
-
eventType: 'chatroom-info-changed',
|
|
1179
|
-
taskId: data.taskId,
|
|
1180
|
-
sessionCount: rs ? rs.length : 0,
|
|
1181
|
-
wechatAccountIds,
|
|
1182
|
-
accountUnreadChanges,
|
|
1183
|
-
accountUnreadBefore,
|
|
1184
|
-
accountUnreadAfter: getAccountUnreadSnapshot(this.account, wechatAccountIds),
|
|
1185
|
-
accountUnreadUpdated
|
|
1186
|
-
})
|
|
1187
1069
|
// 这里应该标记那些工作微信号的通讯录修改了,后面要在界面打个提示,告诉用户通讯录变化了,让手动刷新一下
|
|
1188
1070
|
// 同时也将缓存都清理一下,例如工作微信号分组统计数量等
|
|
1189
1071
|
this.contacts.markModify() // 标记通讯录修改
|
|
@@ -1258,9 +1140,13 @@ class ClientChat {
|
|
|
1258
1140
|
onCmdFriendAccepted: ({ data }) => {
|
|
1259
1141
|
// 刷新指定微信号请求数量
|
|
1260
1142
|
if (data.wechatAccountId) {
|
|
1261
|
-
this.account.
|
|
1262
|
-
|
|
1263
|
-
}
|
|
1143
|
+
if (!this.account.accountMap[data.wechatAccountId]) {
|
|
1144
|
+
this.account.reloadData()
|
|
1145
|
+
} else {
|
|
1146
|
+
this.account.getAccountGreetingUnreadMsgCount({
|
|
1147
|
+
wechatAccountId: data.wechatAccountId
|
|
1148
|
+
})
|
|
1149
|
+
}
|
|
1264
1150
|
}
|
|
1265
1151
|
// 刷新所有微信号请求数量
|
|
1266
1152
|
this.account.getAccountGreetingUnreadMsgCount()
|