@aochuang/client-sdk 1.0.404 → 1.0.406

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.
@@ -29,9 +29,9 @@
29
29
  </div>
30
30
  </template>
31
31
  <template v-else>
32
- <div class="sdk-chat-control-image__control">
32
+ <div class="sdk-chat-control-image__control" @mouseenter="handleControlMouseEnter" @mouseleave="handleControlMouseLeave">
33
33
  <ui-image
34
- :src="innerSrc"
34
+ :src="displaySrc"
35
35
  :radius="0"
36
36
  :previewable="previewable"
37
37
  :width="innerWidth"
@@ -42,6 +42,7 @@
42
42
  :enable-thumb="innerOptimize"
43
43
  fit="contain"
44
44
  @error="handleError"
45
+ @load="handleImageLoad"
45
46
  @click.native="handleImageClick"
46
47
  @dragstart.native="handleImageDragStart"
47
48
  ></ui-image>
@@ -56,9 +57,9 @@
56
57
  </template>
57
58
  </template>
58
59
  <template v-else>
59
- <div class="sdk-chat-control-image__control">
60
+ <div class="sdk-chat-control-image__control" @mouseenter="handleControlMouseEnter" @mouseleave="handleControlMouseLeave">
60
61
  <ui-image
61
- :src="innerSrc"
62
+ :src="displaySrc"
62
63
  :radius="0"
63
64
  :previewable="previewable"
64
65
  :width="innerWidth"
@@ -69,6 +70,7 @@
69
70
  :enable-thumb="enableOptimize"
70
71
  fit="contain"
71
72
  @error="handleError"
73
+ @load="handleImageLoad"
72
74
  @click.native="handleImageClick"
73
75
  @dragstart.native="handleImageDragStart"
74
76
  ></ui-image>
@@ -81,6 +83,20 @@
81
83
  </template>
82
84
  </div>
83
85
  </template>
86
+ <template v-if="dragBase64Enabled">
87
+ <div v-if="dragStatus === 'loading'" class="sdk-chat-control-image__drag-mask">
88
+ <ui-icon name="loading" :size="20" color="#fff"></ui-icon>
89
+ <span class="sdk-chat-control-image__drag-mask-text">图片转化中</span>
90
+ </div>
91
+ <transition name="sdk-chat-control-image-fade">
92
+ <div v-if="dragTip" class="sdk-chat-control-image__drag-mask" :class="'is-' + dragTip.type">
93
+ <span class="sdk-chat-control-image__drag-tip">
94
+ <ui-icon :name="dragTip.type === 'success' ? 'checkbox-circle-fill' : 'information-line'" :size="20" color="#fff"></ui-icon>
95
+ <span class="sdk-chat-control-image__drag-mask-text">{{ dragTip.text }}</span>
96
+ </span>
97
+ </div>
98
+ </transition>
99
+ </template>
84
100
  </div>
85
101
  <image-dragable-confirm
86
102
  v-if="showDragOriginConfirm"
@@ -92,7 +108,17 @@
92
108
  </template>
93
109
  <script>
94
110
  import { Image as UiImage, Button as UiButton, Icon as UiIcon } from '@aochuang/common/components'
95
- import { isOriginImage, getNewImageSrc, isNewImage } from './util'
111
+ import { formatOssPath } from '@aochuang/common/utils/oss'
112
+ import {
113
+ isOriginImage,
114
+ getNewImageSrc,
115
+ isNewImage,
116
+ isGifImage,
117
+ loadImageDataURL,
118
+ decodeImage,
119
+ getDragBase64Cache,
120
+ setDragBase64Cache
121
+ } from './util'
96
122
  import BiTooltip from '@aochuang/common/common/tooltip'
97
123
  import ImageDragableConfirm from './image-dragable-confirm.vue'
98
124
 
@@ -185,6 +211,15 @@ export default {
185
211
  enableDragOriginImage: {
186
212
  type: Boolean,
187
213
  default: false
214
+ },
215
+ /**
216
+ * 拖拽前转base64。开启后鼠标悬停时会把图片转成base64替换显示地址,
217
+ * 解决私有oss代理地址、裁剪参数等导致拖拽到其他软件打不开的问题,转换过程中禁止拖拽。
218
+ * 只针对PC端开启,移动端不要开启
219
+ */
220
+ enableDragBase64: {
221
+ type: Boolean,
222
+ default: false
188
223
  }
189
224
  },
190
225
  data () {
@@ -192,7 +227,11 @@ export default {
192
227
  defrostMsg: null,
193
228
  errorMsg: null,
194
229
  correctSrc: null, // 纠正后的图片地址,一般是开启了enableOptimize,大图找不到的时候,会回退到小图地址
195
- showDragOriginConfirm: false
230
+ showDragOriginConfirm: false,
231
+ imageLoaded: false, // 当前显示的图片是否加载成功,加载成功才允许转base64
232
+ dragStatus: 'none', // none | loading | success | error
233
+ dragBase64Ready: false, // base64本身存在this._dragBase64上,不放进响应式数据
234
+ dragTip: null // 转换完成/失败的短暂提示 { type, text }
196
235
  }
197
236
  },
198
237
  computed: {
@@ -261,13 +300,36 @@ export default {
261
300
  },
262
301
  allowDefrost () {
263
302
  return !!this.defrostMethod
303
+ },
304
+ dragBase64Enabled () {
305
+ return this.enableDragBase64 && this.type === 'default'
306
+ },
307
+ /**
308
+ * 实际传给ui-image的地址
309
+ * base64以函数形式传入,ui-image对函数类型的src不会做裁剪和formatOssPath处理,
310
+ * 否则formatOssPath会把base64里的+替换成%2B,或者误判/oss/子串,导致数据被破坏
311
+ */
312
+ displaySrc () {
313
+ if (this.dragBase64Ready && this._dragBase64) {
314
+ const dataURL = this._dragBase64
315
+ return () => dataURL
316
+ }
317
+ return this.innerSrc
264
318
  }
265
319
  },
266
320
  watch: {
267
321
  src () {
268
322
  this.showDragOriginConfirm = false
323
+ },
324
+ innerSrc () {
325
+ // 地址变了(纠错回退、原图下载完成等),丢弃旧的base64,避免旧数据覆盖新图
326
+ this.resetDragBase64()
327
+ this.imageLoaded = false
269
328
  }
270
329
  },
330
+ beforeDestroy () {
331
+ this.resetDragBase64()
332
+ },
271
333
  methods: {
272
334
  handleClick () {
273
335
  this.$emit('click')
@@ -276,6 +338,20 @@ export default {
276
338
  this.imageClickMethod && this.imageClickMethod()
277
339
  },
278
340
  handleImageDragStart (event) {
341
+ if (this.dragBase64Enabled) {
342
+ // 转换中禁止拖拽
343
+ if (this.dragStatus === 'loading') {
344
+ event.preventDefault()
345
+ return
346
+ }
347
+ // 已经是base64了,去掉文本类拖拽数据,防止拖到文本输入框里贴出一大串base64
348
+ if (this.dragBase64Ready && event.dataTransfer) {
349
+ try {
350
+ event.dataTransfer.clearData('text/plain')
351
+ event.dataTransfer.clearData('text/uri-list')
352
+ } catch (e) {}
353
+ }
354
+ }
279
355
  // 先去掉这个逻辑,私有的oss或者大图先不管了,还是以前的逻辑,私有的我这里是可以的,黄哒哒电脑不行,这里逻辑都是和老的一样的,可能和系统有关系吧
280
356
  // 先保持和以前一样的逻辑,不做这样的弹出交互了,客户不接受
281
357
  // if (!(this.enableDragOriginImage && this.isOriginImage)) {
@@ -285,10 +361,123 @@ export default {
285
361
  // event.stopPropagation()
286
362
  // this.showDragOriginConfirm = true
287
363
  },
364
+ handleImageLoad () {
365
+ this.imageLoaded = true
366
+ },
367
+ handleControlMouseEnter () {
368
+ if (!this.dragBase64Enabled || this.dragBase64Ready || this.dragStatus === 'loading') {
369
+ return
370
+ }
371
+ clearTimeout(this._dragHoverTimer)
372
+ // 停留一会儿再转换,避免滚动、划过时大量请求
373
+ this._dragHoverTimer = setTimeout(() => {
374
+ this.transformDragBase64()
375
+ }, 200)
376
+ },
377
+ handleControlMouseLeave () {
378
+ clearTimeout(this._dragHoverTimer)
379
+ // 中途移开取消转换
380
+ if (this.dragStatus === 'loading') {
381
+ this.abortDragBase64Task()
382
+ this.dragStatus = 'none'
383
+ }
384
+ },
385
+ /**
386
+ * 获取用于转换的地址,和当前显示的保持一致,尽量命中浏览器缓存
387
+ * gif用不带裁剪参数的地址,避免裁剪后只剩第一帧
388
+ */
389
+ getDragFetchSrc () {
390
+ if (isGifImage(this.innerSrc)) {
391
+ return formatOssPath(this.innerSrc)
392
+ }
393
+ const img = this.$el && this.$el.querySelector('.ui-image__img')
394
+ const currentSrc = img && img.getAttribute('src')
395
+ if (currentSrc && !/^(data|blob):/i.test(currentSrc)) {
396
+ return currentSrc
397
+ }
398
+ return formatOssPath(this.innerSrc)
399
+ },
400
+ transformDragBase64 () {
401
+ if (!this.imageLoaded || !this.innerSrc || this.errorMsg || this.defrostMsg) {
402
+ return
403
+ }
404
+ if (/^(data|blob):/i.test(this.innerSrc)) {
405
+ return
406
+ }
407
+ const fetchSrc = this.getDragFetchSrc()
408
+ if (!fetchSrc) {
409
+ return
410
+ }
411
+ const cached = getDragBase64Cache(fetchSrc)
412
+ if (cached) {
413
+ this.applyDragBase64(cached, false)
414
+ return
415
+ }
416
+ this.abortDragBase64Task()
417
+ this.dragStatus = 'loading'
418
+ const task = loadImageDataURL(fetchSrc, {
419
+ maxSize: 10 * 1024 * 1024
420
+ })
421
+ this._dragBase64Task = task
422
+ task.promise.then((dataURL) => {
423
+ // 预解码,替换时避免闪烁
424
+ return decodeImage(dataURL).then(() => dataURL)
425
+ }).then((dataURL) => {
426
+ if (this._dragBase64Task !== task) {
427
+ return
428
+ }
429
+ this._dragBase64Task = null
430
+ setDragBase64Cache(fetchSrc, dataURL)
431
+ this.applyDragBase64(dataURL, true)
432
+ }).catch(() => {
433
+ if (this._dragBase64Task !== task) {
434
+ return
435
+ }
436
+ this._dragBase64Task = null
437
+ this.dragStatus = 'error'
438
+ this.showDragTip('error', '转换失败,可直接拖拽原图', 1500)
439
+ })
440
+ },
441
+ applyDragBase64 (dataURL, showTip) {
442
+ this._dragBase64 = dataURL
443
+ this.dragBase64Ready = true
444
+ this.dragStatus = 'success'
445
+ if (showTip) {
446
+ this.showDragTip('success', '可拖拽', 1200)
447
+ }
448
+ },
449
+ showDragTip (type, text, duration) {
450
+ clearTimeout(this._dragTipTimer)
451
+ this.dragTip = { type, text }
452
+ this._dragTipTimer = setTimeout(() => {
453
+ this.dragTip = null
454
+ }, duration)
455
+ },
456
+ abortDragBase64Task () {
457
+ if (this._dragBase64Task) {
458
+ this._dragBase64Task.abort()
459
+ this._dragBase64Task = null
460
+ }
461
+ },
462
+ resetDragBase64 () {
463
+ clearTimeout(this._dragHoverTimer)
464
+ clearTimeout(this._dragTipTimer)
465
+ this.abortDragBase64Task()
466
+ this._dragBase64 = null
467
+ this.dragBase64Ready = false
468
+ this.dragStatus = 'none'
469
+ this.dragTip = null
470
+ },
288
471
  handleDragOriginConfirmClosed () {
289
472
  this.showDragOriginConfirm = false
290
473
  },
291
474
  handleError () {
475
+ // base64显示失败,回退到原地址,不走下面的纠错逻辑
476
+ if (this.dragBase64Ready) {
477
+ this.resetDragBase64()
478
+ return
479
+ }
480
+ this.imageLoaded = false
292
481
  if (this.defrostMethod) {
293
482
  this.errorMsg = '加载失败,可能资源被归档~'
294
483
  return
@@ -361,6 +550,46 @@ export default {
361
550
  line-height: 1em;
362
551
  vertical-align: top;
363
552
  }
553
+ .sdk-chat-control-image__drag-mask{
554
+ position: absolute;
555
+ left: 0;
556
+ top: 0;
557
+ right: 0;
558
+ bottom: 0;
559
+ z-index: 9;
560
+ display: flex;
561
+ align-items: center;
562
+ justify-content: center;
563
+ background-color: rgba(0, 0, 0, .45);
564
+ color: #fff;
565
+ font-size: 12px;
566
+ pointer-events: none; // 不挡住点击查看大图
567
+ &.is-success{
568
+ // 成功后不再遮住图片,只保留中间一个小提示
569
+ background-color: transparent;
570
+ .sdk-chat-control-image__drag-tip{
571
+ padding: 6px 12px;
572
+ border-radius: 16px;
573
+ background-color: rgba(0, 0, 0, .55);
574
+ }
575
+ }
576
+ &.is-error{
577
+ background-color: rgba(0, 0, 0, .55);
578
+ }
579
+ }
580
+ .sdk-chat-control-image__drag-tip{
581
+ display: inline-flex;
582
+ align-items: center;
583
+ }
584
+ .sdk-chat-control-image__drag-mask .ui-icon + .sdk-chat-control-image__drag-mask-text{
585
+ margin-left: 4px;
586
+ }
587
+ .sdk-chat-control-image-fade-leave-active{
588
+ transition: opacity .3s;
589
+ }
590
+ .sdk-chat-control-image-fade-leave-to{
591
+ opacity: 0;
592
+ }
364
593
  .sdk-chat-control-image__hd{
365
594
  position: absolute;
366
595
  background-color: #3773f9;
@@ -26,4 +26,129 @@ export function getOriginImageSrc (url) {
26
26
  } else {
27
27
  return url
28
28
  }
29
- }
29
+ }
30
+
31
+ /**
32
+ * 拖拽用 base64 缓存(模块级,按最近使用淘汰),避免消息列表重新渲染时重复转换
33
+ */
34
+ const DRAG_BASE64_CACHE_LIMIT = 20
35
+ const dragBase64Cache = new Map()
36
+
37
+ export function getDragBase64Cache (src) {
38
+ if (!src || !dragBase64Cache.has(src)) {
39
+ return null
40
+ }
41
+ const value = dragBase64Cache.get(src)
42
+ // 重新插入,标记为最近使用
43
+ dragBase64Cache.delete(src)
44
+ dragBase64Cache.set(src, value)
45
+ return value
46
+ }
47
+
48
+ export function setDragBase64Cache (src, dataURL) {
49
+ if (!src || !dataURL) {
50
+ return
51
+ }
52
+ dragBase64Cache.delete(src)
53
+ dragBase64Cache.set(src, dataURL)
54
+ while (dragBase64Cache.size > DRAG_BASE64_CACHE_LIMIT) {
55
+ dragBase64Cache.delete(dragBase64Cache.keys().next().value)
56
+ }
57
+ }
58
+
59
+ export function isGifImage (url) {
60
+ if (!url) {
61
+ return false
62
+ }
63
+ return /\.gif([?#]|$)/i.test(url)
64
+ }
65
+
66
+ const IMAGE_MIME_MAP = {
67
+ jpg: 'image/jpeg',
68
+ jpeg: 'image/jpeg',
69
+ png: 'image/png',
70
+ gif: 'image/gif',
71
+ webp: 'image/webp',
72
+ bmp: 'image/bmp'
73
+ }
74
+
75
+ function guessImageMimeType (url) {
76
+ let decodedUrl = url || ''
77
+ try {
78
+ decodedUrl = decodeURIComponent(decodedUrl)
79
+ } catch (e) {}
80
+ const match = decodedUrl.match(/\.(jpe?g|png|gif|webp|bmp)(?=[?#&]|$)/i)
81
+ return (match && IMAGE_MIME_MAP[match[1].toLowerCase()]) || 'image/jpeg'
82
+ }
83
+
84
+ /**
85
+ * 将图片地址加载为 base64(dataURL),返回 { promise, abort }
86
+ * maxSize:超过该字节数直接失败,避免大图占用过多内存
87
+ */
88
+ export function loadImageDataURL (src, { maxSize } = {}) {
89
+ let xhr = null
90
+ let reader = null
91
+ let aborted = false
92
+ const promise = new Promise((resolve, reject) => {
93
+ xhr = new XMLHttpRequest()
94
+ xhr.open('GET', src, true)
95
+ xhr.responseType = 'blob'
96
+ xhr.onload = () => {
97
+ if (aborted) {
98
+ return
99
+ }
100
+ if (xhr.status < 200 || xhr.status >= 300) {
101
+ reject(new Error(`load image failed: ${xhr.status}`))
102
+ return
103
+ }
104
+ let blob = xhr.response
105
+ if (!blob) {
106
+ reject(new Error('response is empty'))
107
+ return
108
+ }
109
+ // 私有oss代理等可能返回 application/octet-stream,按地址扩展名修正类型,否则拖出去的文件类型不对
110
+ if (!blob.type || blob.type.indexOf('image/') !== 0) {
111
+ if (blob.type && /^(text|application\/json)/i.test(blob.type)) {
112
+ reject(new Error('response is not image'))
113
+ return
114
+ }
115
+ blob = new Blob([blob], { type: guessImageMimeType(src) })
116
+ }
117
+ if (maxSize && blob.size > maxSize) {
118
+ reject(new Error('image too large'))
119
+ return
120
+ }
121
+ reader = new FileReader()
122
+ reader.onload = () => {
123
+ if (!aborted) {
124
+ resolve(reader.result)
125
+ }
126
+ }
127
+ reader.onerror = reject
128
+ reader.readAsDataURL(blob)
129
+ }
130
+ xhr.onerror = reject
131
+ xhr.onabort = () => reject(new Error('aborted'))
132
+ xhr.send()
133
+ })
134
+ return {
135
+ promise,
136
+ abort () {
137
+ aborted = true
138
+ xhr && xhr.abort()
139
+ reader && reader.readyState === 1 && reader.abort()
140
+ }
141
+ }
142
+ }
143
+
144
+ /**
145
+ * 预解码图片,替换 src 时避免闪烁
146
+ */
147
+ export function decodeImage (src) {
148
+ return new Promise((resolve, reject) => {
149
+ const img = new Image()
150
+ img.onload = () => resolve()
151
+ img.onerror = reject
152
+ img.src = src
153
+ })
154
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aochuang/client-sdk",
3
- "version": "1.0.404",
3
+ "version": "1.0.406",
4
4
  "description": "",
5
5
  "files": [
6
6
  "common",
@@ -170,8 +170,11 @@ export default class ClientChatMessage {
170
170
  * "wechatChatroomId": "2089efcc7f8f40b00b7f05fa4c6dfd3bb642774d", //群的时候才有
171
171
  * "wechatTime": 1723710022000
172
172
  * })
173
+ * @param { Boolean } fireEvent - 为false时不触发onAddMessageSuccess和add-message事件
174
+ * @param { Object } options
175
+ * @param { String } options.replaceMessageId - 这条消息替换的临时占位消息id,会话更新时据此判断是否允许用它的时间覆盖会话时间
173
176
  */
174
- addMessage (data, fireEvent) {
177
+ addMessage (data, fireEvent, options) {
175
178
  if (!data || !data.id || !data[this.groupField] || !data[this.sortField]) {
176
179
  this.addDiagnosticLog('message_rejected', this.getMessageDiagnosticData(data, {
177
180
  reason: 'missing_required_field'
@@ -234,7 +237,8 @@ export default class ClientChatMessage {
234
237
  sessionUpdateResult = this.onAddMessageSuccess && this.onAddMessageSuccess({
235
238
  data,
236
239
  lastMessageTime,
237
- isBeforeLastMessage
240
+ isBeforeLastMessage,
241
+ replaceMessageId: options && options.replaceMessageId
238
242
  })
239
243
  } catch (error) {
240
244
  this.handleSessionUpdateError(data, error)
@@ -467,7 +471,11 @@ export default class ClientChatMessage {
467
471
  // 如果返回的是对象,证明发送成功了,如果返回的不是对象,是报错字符串,交给外面处理错误
468
472
  if (data && typeof data === 'object') {
469
473
  this.removeMessage(newMessage.id)
470
- this.addMessage(data)
474
+ // 临时消息的时间是浏览器本地时间,服务端消息的时间是服务端时钟,两者可能有偏差,
475
+ // 告诉会话这条消息是替换临时消息的,这样即使服务端时间比本地时间小,也能把会话时间校正回服务端时钟
476
+ this.addMessage(data, undefined, {
477
+ replaceMessageId: newMessage.id
478
+ })
471
479
  } else if (typeof data === 'string') {
472
480
  this.updateMessage(newMessage.id, {
473
481
  sendStatus: 2,
@@ -43,6 +43,17 @@ function appendLastAtMsgContent (lastAtMsgContent, atMessage) {
43
43
  return messages.concat(atMessage)
44
44
  }
45
45
 
46
+ /**
47
+ * 会话的最后一条消息是否就是被替换的那条消息
48
+ * 发送消息时先插入临时占位消息(时间是浏览器本地时间),之后服务端消息替换它、发送回执回刷真实时间(服务端时钟)。
49
+ * 两个时钟可能有偏差,服务端时间可能比本地时间小,如果只按"时间更新才更新会话"判断,会话时间会一直停留在本地时间,
50
+ * 导致对方紧接着发来的消息被判断为旧消息,不更新会话最后一条消息也不增加未读数量。
51
+ * 所以替换的是会话的最后一条消息时,允许用更小的时间覆盖,把会话时间校正回服务端时钟。
52
+ */
53
+ function isReplaceSessionLastMessage (session, replaceMessageId) {
54
+ return !!replaceMessageId && !!session && session.lastMsgId === replaceMessageId
55
+ }
56
+
46
57
  class ClientChat {
47
58
  data = Vue.observable({
48
59
  connectStatus: CONNECT_STATUS.Normal
@@ -449,7 +460,7 @@ class ClientChat {
449
460
  getSessionIdMethod: data => createSessionId(options.getLoginAccountIdMethod ? options.getLoginAccountIdMethod() : data && data.accountId, data && data.wechatFriendId),
450
461
  checkMessageSendResultMethod: options.checkMessageSendResultMethod,
451
462
  loadLossMessageDataMethod: options.loadLossFriendMessageDataMethod,
452
- onAddMessageSuccess: ({ data, isBeforeLastMessage }) => {
463
+ onAddMessageSuccess: ({ data, isBeforeLastMessage, replaceMessageId }) => {
453
464
  const accountId = options.getLoginAccountIdMethod ? options.getLoginAccountIdMethod() : data.accountId
454
465
  let sessionId = createSessionId(accountId, data.wechatFriendId)
455
466
  // 更新会话
@@ -472,7 +483,7 @@ class ClientChat {
472
483
  if (rs && rs.data) {
473
484
  const isAfterServerMessage = data.wechatTime > context.baseLastMsgTime
474
485
  const isNewerMessage = data.wechatTime > (rs.data.lastMsgTime || 0)
475
- if (isNewerMessage) {
486
+ if (isNewerMessage || isReplaceSessionLastMessage(rs.data, replaceMessageId)) {
476
487
  rs.data.lastMsgContent = data.content
477
488
  rs.data.lastMsgType = data.msgType
478
489
  rs.data.lastMsgTime = data.wechatTime
@@ -505,7 +516,7 @@ class ClientChat {
505
516
  // listChange/getSession may already include this message in unreadMsgCount.
506
517
  // Only count messages newer than the session watermark.
507
518
  const isNewerMessage = data.wechatTime > (session.lastMsgTime || 0)
508
- if (!isNewerMessage) {
519
+ if (!isNewerMessage && !isReplaceSessionLastMessage(session, replaceMessageId)) {
509
520
  return {}
510
521
  }
511
522
  const nextSession = {
@@ -573,7 +584,7 @@ class ClientChat {
573
584
  getSessionIdMethod: data => createSessionId(options.getLoginAccountIdMethod ? options.getLoginAccountIdMethod() : data && data.accountId, data && data.wechatChatroomId),
574
585
  checkMessageSendResultMethod: options.checkMessageSendResultMethod,
575
586
  loadLossMessageDataMethod: options.loadLossRoomMessageDataMethod,
576
- onAddMessageSuccess: ({ data, isBeforeLastMessage }) => {
587
+ onAddMessageSuccess: ({ data, isBeforeLastMessage, replaceMessageId }) => {
577
588
  const accountId = options.getLoginAccountIdMethod ? options.getLoginAccountIdMethod() : data.accountId
578
589
  let sessionId = createSessionId(accountId, data.wechatChatroomId)
579
590
  const atMessage = getAtMessageContent(data)
@@ -598,7 +609,7 @@ class ClientChat {
598
609
  if (rs && rs.data) {
599
610
  const isAfterServerMessage = data.wechatTime > context.baseLastMsgTime
600
611
  const isNewerMessage = data.wechatTime > (rs.data.lastMsgTime || 0)
601
- if (isNewerMessage) {
612
+ if (isNewerMessage || isReplaceSessionLastMessage(rs.data, replaceMessageId)) {
602
613
  rs.data.lastMsgContent = data.content
603
614
  rs.data.lastMsgType = data.msgType
604
615
  rs.data.lastMsgTime = data.wechatTime
@@ -634,7 +645,7 @@ class ClientChat {
634
645
  // listChange/getSession may already include this message in unreadMsgCount.
635
646
  // Only count messages newer than the session watermark.
636
647
  const isNewerMessage = data.wechatTime > (session.lastMsgTime || 0)
637
- if (!isNewerMessage) {
648
+ if (!isNewerMessage && !isReplaceSessionLastMessage(session, replaceMessageId)) {
638
649
  return {}
639
650
  }
640
651
  const nextSession = {
@@ -879,15 +890,32 @@ class ClientChat {
879
890
  // 发送消息结果,状态回刷
880
891
  onCmdSendMessageResult: ({ data }) => {
881
892
  const { cmdType, friendMessageId, chatroomMessageId, errMsg, seq, ...nextData } = data
882
- if (friendMessageId) {
883
- this.friendMessage.updateMessage(friendMessageId, Object.assign(nextData, {
884
- resultMsg: errMsg
885
- }))
886
- } else {
887
- this.roomMessage.updateMessage(chatroomMessageId, Object.assign(nextData, {
888
- resultMsg: errMsg
889
- }))
893
+ const messageManage = friendMessageId ? this.friendMessage : this.roomMessage
894
+ const messageId = friendMessageId || chatroomMessageId
895
+ const targetField = friendMessageId ? 'wechatFriendId' : 'wechatChatroomId'
896
+ messageManage.updateMessage(messageId, Object.assign(nextData, {
897
+ resultMsg: errMsg
898
+ }))
899
+ // 回执里的wechatTime是服务端时钟的真实时间,如果这条消息是会话的最后一条消息,需要把会话时间同步过去,
900
+ // 避免会话时间停留在临时消息的本地时间上(本地时钟偏快时,会导致对方紧接着的回复不更新会话也不加未读)
901
+ const message = messageManage.getMessage(messageId)
902
+ if (!message || !message.wechatTime || !message[targetField]) {
903
+ // 消息还没插入时回执会先缓存,等消息插入时会合并回执数据,再由onAddMessageSuccess更新会话
904
+ return
905
+ }
906
+ const accountId = options.getLoginAccountIdMethod ? options.getLoginAccountIdMethod() : message.accountId
907
+ const sessionId = createSessionId(accountId, message[targetField])
908
+ if (!sessionId) {
909
+ return
890
910
  }
911
+ this.session.updateSession(sessionId, ({ session }) => {
912
+ if (session.lastMsgId !== messageId || session.lastMsgTime === message.wechatTime) {
913
+ return {}
914
+ }
915
+ return {
916
+ lastMsgTime: message.wechatTime
917
+ }
918
+ })
891
919
  },
892
920
  onCmdChatroomMemberInfoChanged: ({ data }) => {
893
921
  // 如果没有changeType的时候,目前看应该是群成员或者群信息变化了,需要重新刷新一下群成员和群信息