@aochuang/client-sdk 1.0.400 → 1.0.402

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.
@@ -4,6 +4,8 @@ import { createSessionId } from './utils.js'
4
4
  import { createPollRequest } from '../../../utils/util.js'
5
5
 
6
6
  const ALL_WECHAT_ACCOUNT_ID = 'all'
7
+ const GET_SESSION_POLL_SPAN_TIME = 5 * 1000
8
+ const GET_SESSION_POLL_TIMEOUT = 10 * 60 * 1000
7
9
 
8
10
  const loopLoadAssignSessionData = (fetchFn, params) => {
9
11
  let lastPs = null
@@ -133,9 +135,11 @@ export default class ClientChatSession {
133
135
  getSessionMethod: null,
134
136
  onGetSessionLog: null,
135
137
  updateSessionReadMethod: null,
138
+ updateSessionUnreadMethod: null,
136
139
  starSessionMethod: null,
137
140
  unstarSessionMethod: null,
138
141
  onSessionReadChange: null,
142
+ onSessionUnreadChange: null,
139
143
  onUpdateSession: null,
140
144
  onRemoveSession: null,
141
145
  onDeleteSession: null,
@@ -166,6 +170,7 @@ export default class ClientChatSession {
166
170
  this.onAddSession = options.onAddSession
167
171
  this.loadSessionData = options.loadSessionDataMethod
168
172
  this.updateSessionRead = options.updateSessionReadMethod
173
+ this.updateSessionUnread = options.updateSessionUnreadMethod
169
174
  this.getSessionMethod = options.getSessionMethod
170
175
  this.onGetSessionLog = options.onGetSessionLog
171
176
  this.loadAssignTaskSessionDataMethod = options.loadAssignTaskSessionDataMethod
@@ -175,6 +180,7 @@ export default class ClientChatSession {
175
180
  this.unstarSessionMethod = options.unstarSessionMethod
176
181
  this.deleteSessionMethod = options.deleteSessionMethod
177
182
  this.onSessionReadChange = options.onSessionReadChange
183
+ this.onSessionUnreadChange = options.onSessionUnreadChange
178
184
  this.onUpdateSession = options.onUpdateSession
179
185
  this.onRemoveSession = options.onRemoveSession
180
186
  this.onDeleteSession = options.onDeleteSession
@@ -230,20 +236,21 @@ export default class ClientChatSession {
230
236
  })
231
237
  }
232
238
 
233
- _addGetSessionLog (title, data) {
239
+ _addGetSessionLog (event, data) {
234
240
  if (this.onGetSessionLog) {
235
241
  this.onGetSessionLog({
236
- group: 'client-chat-session:getSession',
237
- content: {
238
- title,
239
- data
240
- }
242
+ group: `diagnostic:${event}`,
243
+ content: Object.assign({ event }, data)
241
244
  })
242
245
  }
243
246
  }
244
247
 
245
248
  getSession (sessionId, serializeMethod) {
246
249
  if (this.sessionMap[sessionId]) {
250
+ this._addGetSessionLog('session_resolved', {
251
+ sessionId,
252
+ source: 'memory'
253
+ })
247
254
  return Promise.resolve({
248
255
  data: this.sessionMap[sessionId]
249
256
  })
@@ -255,6 +262,10 @@ export default class ClientChatSession {
255
262
  if (serializeMethod) {
256
263
  pollRequest.serializeMethods.push(serializeMethod)
257
264
  }
265
+ this._addGetSessionLog('session_poll_joined', {
266
+ sessionId,
267
+ mergedCount: pollRequest.serializeMethods.length
268
+ })
258
269
  return pollRequest.ps
259
270
  }
260
271
 
@@ -273,7 +284,7 @@ export default class ClientChatSession {
273
284
  pollRequest.lastRequestError = error && (error.message || error.errMsg || error.toString && error.toString()) || error
274
285
  return Promise.reject(error)
275
286
  })
276
- })
287
+ }, GET_SESSION_POLL_SPAN_TIME, GET_SESSION_POLL_TIMEOUT)
277
288
  pollRequest.serializeMethods = serializeMethod ? [serializeMethod] : []
278
289
  pollRequest.requestCount = 0
279
290
  pollRequest.emptyResponseCount = 0
@@ -281,8 +292,10 @@ export default class ClientChatSession {
281
292
  pollRequest.lastRequestError = null
282
293
  pollRequest.startedAt = Date.now()
283
294
  this.pollRequestMap[sessionId] = pollRequest
284
- this._addGetSessionLog('创建会话轮询', {
285
- sessionId
295
+ this._addGetSessionLog('session_poll_started', {
296
+ sessionId,
297
+ spanTime: GET_SESSION_POLL_SPAN_TIME,
298
+ timeout: GET_SESSION_POLL_TIMEOUT
286
299
  })
287
300
  pollRequest.ps = pollRequest.start().then(rs => {
288
301
  if (!rs.data) {
@@ -308,7 +321,7 @@ export default class ClientChatSession {
308
321
  unreadDelta: context.accountUnreadDelta
309
322
  })
310
323
  const wechatAccountId = rs.data.wechatAccountId
311
- this._addGetSessionLog('会话轮询合并完成', {
324
+ this._addGetSessionLog('session_poll_completed', {
312
325
  sessionId,
313
326
  requestCount: pollRequest.requestCount,
314
327
  emptyResponseCount: pollRequest.emptyResponseCount,
@@ -326,8 +339,10 @@ export default class ClientChatSession {
326
339
  })
327
340
  return rs
328
341
  }).catch((error) => {
329
- this._addGetSessionLog('会话轮询失败', {
342
+ this._addGetSessionLog('session_poll_failed', {
330
343
  sessionId,
344
+ isTimeout: error === 'timeout',
345
+ timeout: GET_SESSION_POLL_TIMEOUT,
331
346
  requestCount: pollRequest.requestCount,
332
347
  emptyResponseCount: pollRequest.emptyResponseCount,
333
348
  requestErrorCount: pollRequest.requestErrorCount,
@@ -924,6 +939,16 @@ export default class ClientChatSession {
924
939
  }
925
940
  }
926
941
  if (changeProps && Object.keys(changeProps).length) {
942
+ if (changeProps.unreadMsgCount) {
943
+ this._addGetSessionLog('session_unread_changed', {
944
+ sessionId,
945
+ wechatAccountId: session.wechatAccountId,
946
+ source: 'updateSession',
947
+ before: changeProps.unreadMsgCount.before,
948
+ after: changeProps.unreadMsgCount.after,
949
+ delta: (changeProps.unreadMsgCount.after || 0) - (changeProps.unreadMsgCount.before || 0)
950
+ })
951
+ }
927
952
  this.onUpdateSession && this.onUpdateSession({
928
953
  session,
929
954
  changeProps,
@@ -943,11 +968,126 @@ export default class ClientChatSession {
943
968
  if (!session) {
944
969
  return
945
970
  }
971
+ const oldUnreadMsgCount = session.unreadMsgCount || 0
972
+ this._addGetSessionLog('session_read_requested', {
973
+ sessionId,
974
+ wechatAccountId: session.wechatAccountId,
975
+ oldUnreadMsgCount
976
+ })
946
977
  return this.updateSessionRead({
947
978
  conversationId: sessionId
948
979
  }).then(() => {
949
980
  this.markSessionRead(sessionId)
981
+ this._addGetSessionLog('session_read_applied', {
982
+ sessionId,
983
+ wechatAccountId: session.wechatAccountId,
984
+ oldUnreadMsgCount,
985
+ unreadMsgCount: 0
986
+ })
987
+ }).catch(error => {
988
+ this._addGetSessionLog('session_read_failed', {
989
+ sessionId,
990
+ wechatAccountId: session.wechatAccountId,
991
+ oldUnreadMsgCount,
992
+ error: error && (error.message || error.errMsg || error.toString && error.toString()) || error
993
+ })
994
+ return Promise.reject(error)
995
+ })
996
+ }
997
+
998
+ /**
999
+ * 设置会话未读
1000
+ */
1001
+ setSessionUnread (sessionId) {
1002
+ let session = this.sessionMap[sessionId]
1003
+ if (!session) {
1004
+ session = this.data.virtualSessionData.find(v => v.conversationId === sessionId)
1005
+ }
1006
+ if (!session) {
1007
+ return Promise.reject(new Error('未找到会话' + sessionId))
1008
+ }
1009
+ if (!this.updateSessionUnread) {
1010
+ return Promise.reject(new Error('缺少updateSessionUnreadMethod配置'))
1011
+ }
1012
+ return this.updateSessionUnread({
1013
+ conversationId: sessionId
1014
+ }).then(rs => {
1015
+ if (rs && rs.data) {
1016
+ return this.mergeMarkUnreadResult(sessionId, rs.data)
1017
+ }
1018
+ // 兼容旧服务端成功后返回空 body,详情仍经过防旧合并。
1019
+ return this.getSessionMethod({
1020
+ id: sessionId
1021
+ }).then(detailRs => {
1022
+ if (!detailRs || !detailRs.data) {
1023
+ return Promise.reject(new Error('未找到会话' + sessionId))
1024
+ }
1025
+ return this.mergeMarkUnreadResult(sessionId, detailRs.data)
1026
+ })
1027
+ })
1028
+ }
1029
+
1030
+ /**
1031
+ * 合并标记未读返回的会话快照
1032
+ */
1033
+ mergeMarkUnreadResult (sessionId, dto) {
1034
+ let session = this.sessionMap[sessionId]
1035
+ if (!session) {
1036
+ session = this.data.virtualSessionData.find(v => v.conversationId === sessionId)
1037
+ }
1038
+ // 请求期间会话可能已被删除,不重新加回本地。
1039
+ if (!session) {
1040
+ return {
1041
+ applied: false,
1042
+ reason: 'session-not-found'
1043
+ }
1044
+ }
1045
+ if (!dto || dto.conversationId !== sessionId) {
1046
+ throw new Error('标记未读返回的会话不匹配')
1047
+ }
1048
+ if (typeof dto.unreadMsgCount !== 'number') {
1049
+ throw new Error('标记未读返回的未读数无效')
1050
+ }
1051
+ const localLastMsgTime = session.lastMsgTime || 0
1052
+ const serverLastMsgTime = dto.lastMsgTime || 0
1053
+ if (serverLastMsgTime < localLastMsgTime) {
1054
+ return {
1055
+ applied: false,
1056
+ reason: 'stale-last-message'
1057
+ }
1058
+ }
1059
+ const beforeUnreadMsgCount = session.unreadMsgCount || 0
1060
+ const patch = {
1061
+ unreadMsgCount: dto.unreadMsgCount
1062
+ }
1063
+ const lastMessageFields = [
1064
+ 'lastMsgId',
1065
+ 'lastMsgTime',
1066
+ 'lastMsgType',
1067
+ 'lastMsgContent',
1068
+ 'senderWechatIdHash'
1069
+ ]
1070
+ lastMessageFields.forEach(field => {
1071
+ if (typeof dto[field] !== 'undefined' && dto[field] !== null) {
1072
+ patch[field] = dto[field]
1073
+ }
950
1074
  })
1075
+ this.updateSession(sessionId, patch)
1076
+ const unreadChangeResult = this.onSessionUnreadChange && this.onSessionUnreadChange({
1077
+ session,
1078
+ beforeUnreadMsgCount,
1079
+ afterUnreadMsgCount: dto.unreadMsgCount
1080
+ })
1081
+ const result = {
1082
+ applied: true,
1083
+ session,
1084
+ beforeUnreadMsgCount,
1085
+ afterUnreadMsgCount: dto.unreadMsgCount
1086
+ }
1087
+ if (unreadChangeResult && typeof unreadChangeResult.then === 'function') {
1088
+ return unreadChangeResult.then(() => result)
1089
+ }
1090
+ return result
951
1091
  }
952
1092
 
953
1093
  /**
@@ -1137,6 +1277,13 @@ export default class ClientChatSession {
1137
1277
  lastUpdateTime: null // 最后更新时间
1138
1278
  })
1139
1279
  this.data.loading = true
1280
+ const startedAt = Date.now()
1281
+ this._addGetSessionLog('session_list_requested', {
1282
+ mode: 'reload',
1283
+ wechatAccountId: params.wechatAccountId,
1284
+ lastUpdateTime: params.lastUpdateTime,
1285
+ size: params.size
1286
+ })
1140
1287
  const ps1 = this.loadSessionData(params, {
1141
1288
  lastSession: null,
1142
1289
  lastUpdateSession: null,
@@ -1144,7 +1291,7 @@ export default class ClientChatSession {
1144
1291
  })
1145
1292
  ps1.then((rs) => {
1146
1293
  this.setSessionData(rs.data, params.wechatAccountId || ALL_WECHAT_ACCOUNT_ID)
1147
- })
1294
+ }, () => {})
1148
1295
  if (this.cancelLoadDataRequest) {
1149
1296
  this.cancelLoadDataRequest()
1150
1297
  }
@@ -1153,9 +1300,21 @@ export default class ClientChatSession {
1153
1300
  }
1154
1301
  return Promise.all([ps1]).then((rs) => {
1155
1302
  this.data.loading = false
1303
+ this._addGetSessionLog('session_list_applied', {
1304
+ mode: 'reload',
1305
+ wechatAccountId: params.wechatAccountId,
1306
+ count: rs[0] && rs[0].data && rs[0].data.length || 0,
1307
+ duration: Date.now() - startedAt
1308
+ })
1156
1309
  return rs[0]
1157
1310
  }, err => {
1158
1311
  this.data.loading = false
1312
+ this._addGetSessionLog('session_list_failed', {
1313
+ mode: 'reload',
1314
+ wechatAccountId: params.wechatAccountId,
1315
+ duration: Date.now() - startedAt,
1316
+ error: err && (err.message || err.errMsg || err.toString && err.toString()) || err
1317
+ })
1159
1318
  return Promise.reject(err)
1160
1319
  })
1161
1320
  }
@@ -1167,6 +1326,13 @@ export default class ClientChatSession {
1167
1326
  size: 20 // 本次要获取的会话条数
1168
1327
  }, params)
1169
1328
  this.data.loading = true
1329
+ const startedAt = Date.now()
1330
+ this._addGetSessionLog('session_list_requested', {
1331
+ mode: 'load_more',
1332
+ wechatAccountId: params.wechatAccountId,
1333
+ lastUpdateTime: params.lastUpdateTime,
1334
+ size: params.size
1335
+ })
1170
1336
  let lastSession = this._getLastSessionBySessionGroup(params.wechatAccountId || ALL_WECHAT_ACCOUNT_ID)
1171
1337
  const ps1 = this.loadSessionData(params, {
1172
1338
  lastSession,
@@ -1184,9 +1350,21 @@ export default class ClientChatSession {
1184
1350
  }
1185
1351
  return Promise.all([ps1]).then((rs) => {
1186
1352
  this.data.loading = false
1353
+ this._addGetSessionLog('session_list_applied', {
1354
+ mode: 'load_more',
1355
+ wechatAccountId: params.wechatAccountId,
1356
+ count: rs[0] && rs[0].data && rs[0].data.length || 0,
1357
+ duration: Date.now() - startedAt
1358
+ })
1187
1359
  return rs[0]
1188
1360
  }, err => {
1189
1361
  this.data.loading = false
1362
+ this._addGetSessionLog('session_list_failed', {
1363
+ mode: 'load_more',
1364
+ wechatAccountId: params.wechatAccountId,
1365
+ duration: Date.now() - startedAt,
1366
+ error: err && (err.message || err.errMsg || err.toString && err.toString()) || err
1367
+ })
1190
1368
  return Promise.reject(err)
1191
1369
  })
1192
1370
  }
@@ -1,5 +1,48 @@
1
1
  import Socket from '../../socket'
2
2
 
3
+ const LOG_CONTENT_MAX_LENGTH = 100
4
+
5
+ function cloneLogData (data) {
6
+ const cloneData = Object.assign({}, data)
7
+ ;['content', 'contBody'].forEach(field => {
8
+ if (typeof cloneData[field] === 'string') {
9
+ const contentLength = cloneData[field].length
10
+ if (contentLength > LOG_CONTENT_MAX_LENGTH) {
11
+ cloneData[field] = `${cloneData[field].substr(0, LOG_CONTENT_MAX_LENGTH)}...+${contentLength - LOG_CONTENT_MAX_LENGTH}`
12
+ }
13
+ } else if (cloneData[field] !== undefined) {
14
+ cloneData[`${field}Type`] = typeof cloneData[field]
15
+ delete cloneData[field]
16
+ }
17
+ })
18
+ delete cloneData.accessToken
19
+ delete cloneData.token
20
+ return cloneData
21
+ }
22
+
23
+ function getMessageDiagnosticData (data) {
24
+ const message = data && (data.friendMessage || data.chatroomMessage) || {}
25
+ return {
26
+ messageId: message.id || data && data.id,
27
+ msgSvrId: message.msgSvrId || data && data.msgSvrId
28
+ }
29
+ }
30
+
31
+ export function getSocketMessageLogData (data) {
32
+ const cloneData = Object.assign({}, data)
33
+ ;['friendMessage', 'chatroomMessage'].forEach(field => {
34
+ if (!cloneData[field] || typeof cloneData[field] !== 'object') {
35
+ return
36
+ }
37
+ cloneData[field] = Object.assign({}, cloneData[field])
38
+ delete cloneData[field].accessToken
39
+ delete cloneData[field].token
40
+ })
41
+ delete cloneData.accessToken
42
+ delete cloneData.token
43
+ return cloneData
44
+ }
45
+
3
46
  export default class Websocket {
4
47
  get connectStatus () {
5
48
  return this.socket.connectStatus
@@ -12,6 +55,7 @@ export default class Websocket {
12
55
  constructor (options) {
13
56
  options = Object.assign({
14
57
  socket: null,
58
+ log: null,
15
59
  // 检查socket事件通知方法,有些场景不需要后端推送的命令执行,比如管理后台
16
60
  checkSocketEventNotifyMethod: null,
17
61
  // 执行回调事件前的方法
@@ -40,7 +84,8 @@ export default class Websocket {
40
84
  throw new Error('socket实例不能为空')
41
85
  }
42
86
  this.socket = options.socket
43
- this.socket.log.onBeforeAdd(({ data }) => {
87
+ this.log = options.log || this.socket.log || null
88
+ this.log && this.log.onBeforeAdd && this.log.onBeforeAdd(({ data }) => {
44
89
  // 收消息需要拦截一些,因为下面有了
45
90
  if (data && ['socket:sendMessage'].includes(data.group)) {
46
91
  return false
@@ -55,23 +100,35 @@ export default class Websocket {
55
100
  }
56
101
  })
57
102
  this.socket.event.on('onCmdNewMessage', (evt) => {
58
- if (!options.checkSocketEventNotifyMethod || options.checkSocketEventNotifyMethod(evt) !== false) {
59
- const cloneMsg = Object.assign({}, evt.data)
60
- const newMsg = Object.assign({}, cloneMsg.chatroomMessage || cloneMsg.friendMessage)
61
- if (typeof newMsg.content === 'string') {
62
- newMsg.content = newMsg.content.substr(0, 100) // 只保留前100个字符,怕数据太多了
103
+ const messageLogData = getSocketMessageLogData(evt && evt.data)
104
+ if (options.checkSocketEventNotifyMethod && options.checkSocketEventNotifyMethod(evt) === false) {
105
+ this.addLog({
106
+ group: 'diagnostic:message_received_suppressed',
107
+ content: Object.assign({
108
+ event: 'message_received_suppressed',
109
+ reason: 'notify_filter',
110
+ data: messageLogData
111
+ }, getMessageDiagnosticData(evt && evt.data))
112
+ })
113
+ } else {
114
+ let processingError
115
+ try {
116
+ options.onCmdNewMessage(evt)
117
+ } catch (error) {
118
+ processingError = error
63
119
  }
64
- delete cloneMsg.chatroomMessage
65
- delete cloneMsg.friendMessage
66
- cloneMsg.message = newMsg
67
- this.socket.logEnable && this.socket.log.addLog({
68
- group: 'client-chat-socket:receiveMessage',
69
- content: {
70
- title: 'socket收到消息',
71
- data: cloneMsg
72
- }
120
+ this.addLog({
121
+ group: 'diagnostic:message_received',
122
+ content: Object.assign({
123
+ event: 'message_received',
124
+ result: processingError ? 'failed' : 'success',
125
+ errorType: processingError && (processingError.name || processingError.code) || undefined,
126
+ data: messageLogData
127
+ }, getMessageDiagnosticData(evt && evt.data))
73
128
  })
74
- options.onCmdNewMessage(evt)
129
+ if (processingError) {
130
+ throw processingError
131
+ }
75
132
  }
76
133
  })
77
134
  this.socket.event.on('onCmdSendMessageResult', (evt) => {
@@ -134,14 +191,11 @@ export default class Websocket {
134
191
  options.onHeartbeatSuccess && options.onHeartbeatSuccess(evt)
135
192
  })
136
193
  this.socket.event.on('onSendMessage', (evt) => {
137
- const cloneData = Object.assign({}, evt.data)
138
- if (cloneData && typeof cloneData.content === 'string') {
139
- cloneData.content = cloneData.content.substr(0, 100)
140
- }
194
+ const cloneData = cloneLogData(evt.data)
141
195
  if (evt.data.cmdType === 'CmdRequestWechatAccountsAliveStatus') {
142
196
  delete cloneData.wechatAccountIds
143
197
  }
144
- this.socket.logEnable && this.socket.log.addLog({
198
+ this.addLog({
145
199
  group: 'client-chat-socket:sendMessage',
146
200
  content: {
147
201
  title: '发送消息',
@@ -222,13 +276,17 @@ export default class Websocket {
222
276
  }
223
277
 
224
278
  addLog (data) {
225
- this.socket.logEnable && this.socket.log.addLog(data)
279
+ this.socket.logEnable && this.log && this.log.addLog(data)
226
280
  }
227
281
 
228
282
  exportLogData () {
229
283
  return this.socket.exportLogData()
230
284
  }
231
285
 
286
+ exportLogFiles () {
287
+ return this.socket.exportLogFiles()
288
+ }
289
+
232
290
  checkAndSync () {
233
291
  return this.socket.checkAndSync()
234
292
  }
@@ -55,6 +55,7 @@ class ClientChat {
55
55
  constructor (options) {
56
56
  options = Object.assign({
57
57
  socket: null,
58
+ log: null,
58
59
  checkSocketEventNotifyMethod: null,
59
60
  getLoginUserIdMethod: null,
60
61
  getLoginUserTokenMethod: null,
@@ -82,6 +83,7 @@ class ClientChat {
82
83
  loadSessionDataMethod: null,
83
84
  loadAssignTaskSessionDataMethod: null,
84
85
  updateSessionReadMethod: null,
86
+ updateSessionUnreadMethod: null,
85
87
  loadStarSessionDataMethod: null,
86
88
  loadUnreadSessionDataMethod: null,
87
89
  starSessionMethod: null,
@@ -101,6 +103,7 @@ class ClientChat {
101
103
  if (!options.loadRoomMessageDataMethod) {
102
104
  throw new Error('loadRoomMessageDataMethod不能为空')
103
105
  }
106
+ this.log = options.log || (options.socket && options.socket.log) || null
104
107
  this.onOverwriteFriendInfoChange = options.onOverwriteFriendInfoChange
105
108
  this.shouldMarkCurrentSessionReadMethod = options.shouldMarkCurrentSessionReadMethod
106
109
  this.event = new Event({
@@ -308,6 +311,29 @@ class ClientChat {
308
311
  }
309
312
  }, 'onSessionReadChange')
310
313
  }
314
+ options.onSessionUnreadChange = ({ session, beforeUnreadMsgCount, afterUnreadMsgCount }) => {
315
+ if (instance !== this.session && !this.session.sessionMap[session.conversationId]) {
316
+ // 搜索等独立列表可能先加载到会话,补入主列表时不重复累加账号未读。
317
+ this.session.addSession(Object.assign({}, session), session.wechatAccountId)
318
+ }
319
+ const delta = afterUnreadMsgCount - beforeUnreadMsgCount
320
+ if (!delta) {
321
+ return
322
+ }
323
+ return this.account.updateAccountUnreadMessageNum(session.wechatAccountId, ({ unreadMesasgeNum, unreadMessageMuteNum }) => {
324
+ const nextUnreadMessageNum = Math.max(0, (unreadMesasgeNum || 0) + delta)
325
+ if (session.mute) {
326
+ return {
327
+ num: nextUnreadMessageNum,
328
+ muteNum: Math.max(0, (unreadMessageMuteNum || 0) + delta)
329
+ }
330
+ }
331
+ return {
332
+ num: nextUnreadMessageNum,
333
+ muteNum: unreadMessageMuteNum || 0
334
+ }
335
+ }, 'onSessionUnreadChange')
336
+ }
311
337
  }
312
338
  this.session = new ClientChatSession({
313
339
  groupField: 'wechatAccountId',
@@ -316,6 +342,7 @@ class ClientChat {
316
342
  loadAssignTaskSessionDataMethod: options.loadAssignTaskSessionDataMethod,
317
343
  loadSessionDataMethod: options.loadSessionDataMethod,
318
344
  updateSessionReadMethod: options.updateSessionReadMethod,
345
+ updateSessionUnreadMethod: options.updateSessionUnreadMethod,
319
346
  getSessionMethod: options.getSessionMethod,
320
347
  onGetSessionLog: data => this.socket && this.socket.addLog(data),
321
348
  stickyTopSessionMethod: options.stickyTopSessionMethod,
@@ -335,6 +362,7 @@ class ClientChat {
335
362
  loadAssignTaskSessionDataMethod: options.loadAssignTaskSessionDataMethod,
336
363
  loadSessionDataMethod: options.loadSessionDataMethod,
337
364
  updateSessionReadMethod: options.updateSessionReadMethod,
365
+ updateSessionUnreadMethod: options.updateSessionUnreadMethod,
338
366
  getSessionMethod: options.getSessionMethod,
339
367
  stickyTopSessionMethod: options.stickyTopSessionMethod,
340
368
  muteNotificationSessionMethod: options.muteNotificationSessionMethod,
@@ -359,6 +387,7 @@ class ClientChat {
359
387
  return options.loadStarSessionDataMethod(params, context)
360
388
  },
361
389
  updateSessionReadMethod: options.updateSessionReadMethod,
390
+ updateSessionUnreadMethod: options.updateSessionUnreadMethod,
362
391
  getSessionMethod: options.getSessionMethod,
363
392
  stickyTopSessionMethod: options.stickyTopSessionMethod,
364
393
  muteNotificationSessionMethod: options.muteNotificationSessionMethod,
@@ -383,6 +412,7 @@ class ClientChat {
383
412
  return options.loadUnreadSessionDataMethod(params, context)
384
413
  },
385
414
  updateSessionReadMethod: options.updateSessionReadMethod,
415
+ updateSessionUnreadMethod: options.updateSessionUnreadMethod,
386
416
  getSessionMethod: options.getSessionMethod,
387
417
  stickyTopSessionMethod: options.stickyTopSessionMethod,
388
418
  muteNotificationSessionMethod: options.muteNotificationSessionMethod,
@@ -416,6 +446,7 @@ class ClientChat {
416
446
  groupField: 'wechatFriendId',
417
447
  sortField: 'wechatTime',
418
448
  clientChat: this,
449
+ getSessionIdMethod: data => createSessionId(options.getLoginAccountIdMethod ? options.getLoginAccountIdMethod() : data && data.accountId, data && data.wechatFriendId),
419
450
  checkMessageSendResultMethod: options.checkMessageSendResultMethod,
420
451
  loadLossMessageDataMethod: options.loadLossFriendMessageDataMethod,
421
452
  onAddMessageSuccess: ({ data, isBeforeLastMessage }) => {
@@ -539,6 +570,7 @@ class ClientChat {
539
570
  groupField: 'wechatChatroomId',
540
571
  sortField: 'wechatTime',
541
572
  clientChat: this,
573
+ getSessionIdMethod: data => createSessionId(options.getLoginAccountIdMethod ? options.getLoginAccountIdMethod() : data && data.accountId, data && data.wechatChatroomId),
542
574
  checkMessageSendResultMethod: options.checkMessageSendResultMethod,
543
575
  loadLossMessageDataMethod: options.loadLossRoomMessageDataMethod,
544
576
  onAddMessageSuccess: ({ data, isBeforeLastMessage }) => {
@@ -803,6 +835,7 @@ class ClientChat {
803
835
  })
804
836
  this.socket = new ClientChatSocket({
805
837
  socket: options.socket,
838
+ log: this.log,
806
839
  getLoginUserIdMethod: options.getLoginUserIdMethod,
807
840
  getLoginUserTokenMethod: options.getLoginUserTokenMethod,
808
841
  checkSocketEventNotifyMethod: options.checkSocketEventNotifyMethod,
@@ -1134,22 +1167,65 @@ class ClientChat {
1134
1167
 
1135
1168
  },
1136
1169
  onSocketOpen: () => {
1137
- const ps = []
1138
- if (this.friendMessage.lastReceiveMessageTime) {
1139
- ps.push(this.friendMessage.loadLossMessageData())
1140
- }
1141
- if (this.roomMessage.lastReceiveMessageTime) {
1142
- ps.push(this.roomMessage.loadLossMessageData())
1170
+ const reconnectTime = Date.now()
1171
+ const syncTasks = []
1172
+ const syncLossMessages = (type, messageInstance) => {
1173
+ const watermarkBefore = messageInstance.lastReceiveMessageTime
1174
+ if (!watermarkBefore) {
1175
+ messageInstance.setLastReceiveMessageTime(reconnectTime)
1176
+ this.socket.addLog({
1177
+ group: 'diagnostic:loss_sync_result',
1178
+ content: {
1179
+ event: 'loss_sync_result',
1180
+ type,
1181
+ result: 'initialized',
1182
+ watermarkBefore,
1183
+ watermarkAfter: messageInstance.lastReceiveMessageTime
1184
+ }
1185
+ })
1186
+ return
1187
+ }
1188
+ const startedAt = Date.now()
1189
+ const task = messageInstance.loadLossMessageData().then(result => {
1190
+ messageInstance.setLastReceiveMessageTime(reconnectTime)
1191
+ this.socket.addLog({
1192
+ group: 'diagnostic:loss_sync_result',
1193
+ content: Object.assign({
1194
+ event: 'loss_sync_result',
1195
+ type,
1196
+ result: 'success',
1197
+ watermarkBefore,
1198
+ watermarkAfter: messageInstance.lastReceiveMessageTime,
1199
+ durationMs: Date.now() - startedAt
1200
+ }, result)
1201
+ })
1202
+ return true
1203
+ }, error => {
1204
+ this.socket.addLog({
1205
+ group: 'diagnostic:loss_sync_result',
1206
+ content: {
1207
+ event: 'loss_sync_result',
1208
+ type,
1209
+ result: 'failed',
1210
+ watermarkBefore,
1211
+ watermarkAfter: messageInstance.lastReceiveMessageTime,
1212
+ durationMs: Date.now() - startedAt,
1213
+ errorType: error && (error.name || error.code) || typeof error
1214
+ }
1215
+ })
1216
+ return false
1217
+ })
1218
+ syncTasks.push(task)
1143
1219
  }
1144
- Promise.all(ps).finally(() => {
1145
- this.friendMessage.setLastReceiveMessageTime(Date.now())
1146
- this.roomMessage.setLastReceiveMessageTime(Date.now())
1220
+ syncLossMessages('friend', this.friendMessage)
1221
+ syncLossMessages('room', this.roomMessage)
1222
+ Promise.all(syncTasks).then(() => {
1147
1223
  // 断线期间本地增量可能和服务端产生偏差(其他端已读、消息延迟等),重连后以服务端为准全量校准一次
1148
- if (ps.length) {
1224
+ if (syncTasks.length) {
1149
1225
  this.account.refreshAllAccountUnreadMesageNum()
1150
1226
  }
1151
1227
  })
1152
- },
1228
+ },
1153
1229
  onSocketClose: (evt) => {
1154
1230
  },
1155
1231
  onSocketKickOut: () => {