@aochuang/client-sdk 1.0.334 → 1.0.336

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.
@@ -5,14 +5,15 @@
5
5
  <div class="sdk-session-item__avatar-image">
6
6
  <slot name="avatar"></slot>
7
7
  </div>
8
- <sdk-badge
9
- class="sdk-session-item__avatar-badge"
10
- :class="{'is-mute': data[muteField]}"
11
- v-if="data[unreadMsgCountField]"
12
- :is-dot="data[muteField]"
13
- :value="data[unreadMsgCountField]"
14
- :max="99"
15
- ></sdk-badge>
8
+ <span class="sdk-session-item__avatar-badge">
9
+ <sdk-badge
10
+ :class="{'is-mute': data[muteField]}"
11
+ v-if="data[unreadMsgCountField]"
12
+ :is-dot="data[muteField]"
13
+ :value="data[unreadMsgCountField]"
14
+ :max="99"
15
+ ></sdk-badge>
16
+ </span>
16
17
  </div>
17
18
  <div class="sdk-session-item__main">
18
19
  <div class="sdk-session-item__row">
@@ -142,7 +143,7 @@ export default {
142
143
  border-radius: 3px;
143
144
  }
144
145
  .sdk-session-item__avatar-badge{
145
- position: absolute;
146
+ position: absolute;
146
147
  &.is-mute {
147
148
  left: calc(~'100% - 7px');
148
149
  top: -6px;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aochuang/client-sdk",
3
- "version": "1.0.334",
3
+ "version": "1.0.336",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -628,6 +628,37 @@ export default class ClientChatContacts {
628
628
  return rs
629
629
  })
630
630
  }
631
+
632
+ /**
633
+ * 强制从服务端刷新好友资料,绕过本地缓存。
634
+ */
635
+ refreshFriend (friendId) {
636
+ if (!friendId) {
637
+ return Promise.reject('缺少好友id')
638
+ }
639
+ if (!this.getContactsFriendMethod) {
640
+ return Promise.reject(new Error('getContactsFriendMethod未定义'))
641
+ }
642
+ return this.getContactsFriendMethod({
643
+ id: friendId
644
+ }).then(rs => {
645
+ if (!rs || !rs.data) {
646
+ return Promise.reject('未找到好友' + friendId)
647
+ }
648
+ if (this.friendMap[friendId]) {
649
+ const nextKeys = Object.keys(rs.data)
650
+ Object.keys(this.friendMap[friendId]).forEach(key => {
651
+ if (!nextKeys.includes(key)) {
652
+ Vue.delete(this.friendMap[friendId], key)
653
+ }
654
+ })
655
+ this.updateFriend(friendId, rs.data)
656
+ } else {
657
+ this.addFriend(rs.data)
658
+ }
659
+ return rs
660
+ })
661
+ }
631
662
 
632
663
  setRoomMemberData (roomId, data) {
633
664
  if (!roomId) {
@@ -769,4 +800,4 @@ export default class ClientChatContacts {
769
800
  this.cancelRefreshRequest = null
770
801
  }
771
802
  }
772
- }
803
+ }
@@ -205,6 +205,35 @@ export default class ClientChatSession {
205
205
  getSessionByLocal (sessionId) {
206
206
  return this.sessionMap[sessionId]
207
207
  }
208
+
209
+ /**
210
+ * 强制从服务端刷新会话,绕过本地缓存。
211
+ */
212
+ refreshSession (sessionId) {
213
+ if (!sessionId) {
214
+ return Promise.reject('缺少会话id')
215
+ }
216
+ return this.getSessionMethod({
217
+ id: sessionId
218
+ }).then(rs => {
219
+ if (!rs || !rs.data) {
220
+ return Promise.reject('未找到会话' + sessionId)
221
+ }
222
+ // 事件计算出的 sessionId 是本地索引,始终保持与服务端返回对象一致。
223
+ const serverSession = Object.assign({}, rs.data, {
224
+ id: sessionId
225
+ })
226
+ const localSession = this.getSessionByLocal(sessionId)
227
+ if (localSession) {
228
+ this.updateSession(sessionId, serverSession)
229
+ } else {
230
+ this.addSession(serverSession)
231
+ }
232
+ return Object.assign({}, rs, {
233
+ data: serverSession
234
+ })
235
+ })
236
+ }
208
237
 
209
238
  findSession (findFn) {
210
239
  let result
@@ -926,4 +955,4 @@ export default class ClientChatSession {
926
955
  this.cancelRefreshRequest = null
927
956
  }
928
957
  }
929
- }
958
+ }
@@ -32,6 +32,7 @@ class ClientChat {
32
32
  getLoginUserIdMethod: null,
33
33
  getLoginUserTokenMethod: null,
34
34
  loadAccountDataMethod: null,
35
+ getLoginAccountIdMethod: null, // 因为1.0有帮聊逻辑,帮聊消息以及通讯录的accountId不是登录用户的accountId,会导致找到的会话出现问题,所以这里提供一个方法让外部传入
35
36
  loadSessionDataMethod: null,
36
37
  updateSessionReadMethod: null,
37
38
  getSessionMethod: null,
@@ -57,8 +58,9 @@ class ClientChat {
57
58
  }
58
59
  if (!options.loadRoomMessageDataMethod) {
59
60
  throw new Error('loadRoomMessageDataMethod不能为空')
60
- }
61
+ }
61
62
  this.onOverwriteFriendInfoChange = options.onOverwriteFriendInfoChange
63
+ this.getLoginAccountIdMethod = options.getLoginAccountIdMethod
62
64
  this.event = new Event({
63
65
  checkOnMethod: (name) => {
64
66
  if (name === 'ready') {
@@ -86,7 +88,8 @@ class ClientChat {
86
88
  sortField: 'sendTime',
87
89
  clientChat: this,
88
90
  onAddMessageSuccess: ({ data }) => {
89
- let sessionId = createSessionId(data.corpId, data.qwAccountId, data.contactId)
91
+ const accountId = this.getLoginAccountIdMethod ? this.getLoginAccountIdMethod() : data.qwAccountId
92
+ let sessionId = createSessionId(accountId, data.contactId)
90
93
  // 更新会话
91
94
  if (!sessionId) {
92
95
  return
@@ -155,7 +158,8 @@ class ClientChat {
155
158
  sortField: 'sendTime',
156
159
  clientChat: this,
157
160
  onAddMessageSuccess: ({ data }) => {
158
- let sessionId = createSessionId(data.corpId, data.qwAccountId, data.roomId)
161
+ const accountId = this.getLoginAccountIdMethod ? this.getLoginAccountIdMethod() : data.qwAccountId
162
+ let sessionId = createSessionId(accountId, data.roomId)
159
163
  // 更新会话
160
164
  if (!sessionId) {
161
165
  return
@@ -258,57 +262,46 @@ class ClientChat {
258
262
  })
259
263
  this.socket = new ClientChatSocket({
260
264
  socket: options.socket,
261
- onCmdQwContactInfoChanged: ({ data }) => {
262
- if (!data) {
265
+ onCmdQwContactInfoChanged: ({ data }) => {
266
+ const contacts = data && (Array.isArray(data.contacts)
267
+ ? data.contacts
268
+ : (data.contactId ? [data] : []))
269
+ if (!contacts || !contacts.length) {
263
270
  return
264
271
  }
265
- const contacts = Array.isArray(data.contacts)
266
- ? data.contacts
267
- : (data.contactId ? [data] : [])
268
- const removedSessionIds = {}
269
- const accountUnreadCountMap = {}
270
- let contactsModified = false
272
+ const tasks = []
273
+ const removedTasks = []
271
274
  contacts.forEach(contact => {
272
- if (!contact || Number(contact.changeType) !== 3 || !contact.contactId) {
273
- return
274
- }
275
- contactsModified = true
276
- this.contacts.removeFriend(contact.contactId)
277
- let session = this.session.findSession(({ session }) => {
278
- return String(session.type) === '0' &&
279
- session.targetId === contact.contactId &&
280
- (!contact.qwAccountId || session.qwAccountId === contact.qwAccountId)
281
- })
282
- if (!session) {
283
- const sessionId = createSessionId(
284
- contact.corpId || data.corpId,
285
- contact.qwAccountId,
286
- contact.contactId
287
- )
288
- session = sessionId && this.session.getSessionByLocal(sessionId)
289
- }
290
- if (!session || removedSessionIds[session.id]) {
275
+ if (!contact) {
291
276
  return
292
277
  }
293
- removedSessionIds[session.id] = true
294
- if (this.session.currentSession && this.session.currentSession.id === session.id) {
295
- this.session.setCurrentSession(null)
278
+ const changeType = Number(contact.changeType)
279
+ if (changeType === 1) {
280
+ tasks.push(this.handleQwContactAdded(contact, data))
281
+ } else if (changeType === 2) {
282
+ tasks.push(this.handleQwContactUpdated(contact))
283
+ } else if (changeType === 3) {
284
+ removedTasks.push(this.handleQwContactRemoved(contact, data))
296
285
  }
297
- const accountId = session.qwAccountId || contact.qwAccountId
298
- if (accountId) {
299
- accountUnreadCountMap[accountId] = (accountUnreadCountMap[accountId] || 0) + (session.unreadMsgCount || 0)
300
- }
301
- this.session.removeSession(session.id)
302
286
  })
303
- if (contactsModified) {
304
- this.contacts.markModify()
305
- }
306
- return Promise.all(Object.keys(accountUnreadCountMap).map(accountId => {
307
- const removedUnreadCount = accountUnreadCountMap[accountId]
308
- return this.account.updateAccountUnreadMessageNum(accountId, ({ unreadMesasgeNum }) => {
309
- return Math.max(0, unreadMesasgeNum - removedUnreadCount)
287
+ return Promise.all(tasks.concat(removedTasks)).then(results => {
288
+ const accountUnreadCountMap = {}
289
+ removedTasks.forEach((task, index) => {
290
+ const result = results[tasks.length + index]
291
+ if (!result || !result.accountId || !result.unreadCount) {
292
+ return
293
+ }
294
+ accountUnreadCountMap[result.accountId] = (accountUnreadCountMap[result.accountId] || 0) + result.unreadCount
310
295
  })
311
- }))
296
+ return Promise.all(Object.keys(accountUnreadCountMap).map(accountId => {
297
+ const removedUnreadCount = accountUnreadCountMap[accountId]
298
+ return this.account.updateAccountUnreadMessageNum(accountId, ({ unreadMesasgeNum }) => {
299
+ return Math.max(0, unreadMesasgeNum - removedUnreadCount)
300
+ }).catch(error => {
301
+ console.warn('[client-chat] update account unread count failed', error)
302
+ })
303
+ }))
304
+ })
312
305
  },
313
306
  // 接受新消息
314
307
  onCmdQwNewMessage: ({ data }) => {
@@ -343,6 +336,90 @@ class ClientChat {
343
336
  }
344
337
  })
345
338
  }
339
+
340
+ getQwAccountCorpId (qwAccountId, corpId) {
341
+ if (corpId || !qwAccountId || !this.account) {
342
+ return corpId
343
+ }
344
+ const account = this.account.getAccountData().find(item => item.id === qwAccountId)
345
+ return account && account.corpId
346
+ }
347
+
348
+ handleQwContactAdded (contact, eventData) {
349
+ const qwAccountId = contact.qwAccountId || (eventData && eventData.qwAccountId)
350
+ const corpId = this.getQwAccountCorpId(
351
+ qwAccountId,
352
+ contact.corpId || (eventData && eventData.corpId)
353
+ )
354
+ const contactId = contact.contactId
355
+ const accountId = this.getLoginAccountIdMethod ? this.getLoginAccountIdMethod() : qwAccountId
356
+ const sessionId = createSessionId(accountId, contactId)
357
+ if (!sessionId) {
358
+ return Promise.resolve()
359
+ }
360
+ const localSession = this.session.getSessionByLocal(sessionId)
361
+ const oldUnread = localSession ? (localSession.unreadMsgCount || 0) : 0
362
+ this.contacts.markModify()
363
+ return this.session.refreshSession(sessionId).then(rs => {
364
+ const serverSession = rs && rs.data
365
+ const newUnread = serverSession ? (serverSession.unreadMsgCount || 0) : 0
366
+ const accountId = (serverSession && serverSession.qwAccountId) || qwAccountId
367
+ const unreadDiff = newUnread - oldUnread
368
+ if (accountId && unreadDiff) {
369
+ return this.account.updateAccountUnreadMessageNum(accountId, ({ unreadMesasgeNum }) => {
370
+ return Math.max(0, unreadMesasgeNum + unreadDiff)
371
+ })
372
+ }
373
+ }).catch(error => {
374
+ console.warn('[client-chat] refresh added contact session failed', error)
375
+ })
376
+ }
377
+
378
+ handleQwContactUpdated (contact) {
379
+ if (!contact.contactId) {
380
+ return Promise.resolve()
381
+ }
382
+ return this.contacts.refreshFriend(contact.contactId).then(() => {
383
+ this.contacts.markModify()
384
+ }).catch(error => {
385
+ console.warn('[client-chat] refresh contact failed', error)
386
+ })
387
+ }
388
+
389
+ handleQwContactRemoved (contact, eventData) {
390
+ if (!contact.contactId) {
391
+ return Promise.resolve()
392
+ }
393
+ this.contacts.removeFriend(contact.contactId)
394
+ let session = this.session.findSession(({ session }) => {
395
+ return String(session.type) === '0' &&
396
+ session.targetId === contact.contactId &&
397
+ (!contact.qwAccountId || session.qwAccountId === contact.qwAccountId)
398
+ })
399
+ if (!session) {
400
+ const qwAccountId = contact.qwAccountId || (eventData && eventData.qwAccountId)
401
+ const accountId = this.getLoginAccountIdMethod ? this.getLoginAccountIdMethod() : qwAccountId
402
+ const sessionId = createSessionId(
403
+ accountId,
404
+ contact.contactId
405
+ )
406
+ session = sessionId && this.session.getSessionByLocal(sessionId)
407
+ }
408
+ this.contacts.markModify()
409
+ if (!session) {
410
+ return Promise.resolve()
411
+ }
412
+ if (this.session.currentSession && this.session.currentSession.id === session.id) {
413
+ this.session.setCurrentSession(null)
414
+ }
415
+ const accountId = session.qwAccountId || contact.qwAccountId
416
+ const unreadCount = session.unreadMsgCount || 0
417
+ this.session.removeSession(session.id)
418
+ return Promise.resolve({
419
+ accountId,
420
+ unreadCount
421
+ })
422
+ }
346
423
 
347
424
  connect () {
348
425
  if (this.data.connectStatus === CONNECT_STATUS.Complete) {
@@ -13,11 +13,11 @@ export function isUni () {
13
13
  * @param accountId - 客服id
14
14
  * @param targetId - wechatFriendId | wechatChatroomId
15
15
  */
16
- export function createSessionId (corpId, accountId, targetId) {
17
- if (!corpId || !accountId || !targetId) {
16
+ export function createSessionId (accountId, targetId) {
17
+ if (!accountId || !targetId) {
18
18
  return
19
19
  }
20
- return shajs('sha1').update(`${corpId}${accountId}${targetId}`).digest('hex')
20
+ return shajs('sha1').update(`${accountId}${targetId}`).digest('hex')
21
21
  }
22
22
 
23
23
  /**