@aochuang/client-sdk 1.0.405 → 1.0.407
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/common/image/image.vue +317 -0
- package/common/image/index.js +2 -2
- package/common/image/util.js +140 -0
- package/components/wechat-chat-control/channel-live/channel-live.vue +19 -1
- package/components/wechat-chat-control/channel-video/channel-video.vue +18 -2
- package/components/wechat-chat-control/image/image.vue +20 -6
- package/components/wechat-chat-control/image/util.js +1 -1
- package/components/wechat-chat-control/url/url-miniprogram.vue +17 -3
- package/components/wechat-chat-control/url/url.vue +9 -1
- package/package.json +1 -1
- package/services/client-chat/client-chat-wechat/client-chat.js +32 -6
- package/services/client-chat/client-chat-wechat/docs/README.md +36 -0
- package/services/client-chat/client-chat-wechat/docs//344/271/261/345/272/217/346/266/210/346/201/257/346/234/252/350/257/273/346/225/260/346/274/217/350/256/241/351/227/256/351/242/230/344/277/256/345/244/215.md +128 -0
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<!-- 根节点直接是ui-image,不额外包裹元素,避免影响现有布局和样式 -->
|
|
3
|
+
<ui-image
|
|
4
|
+
v-bind="$attrs"
|
|
5
|
+
:src="displaySrc"
|
|
6
|
+
v-on="innerListeners"
|
|
7
|
+
@mouseenter.native="handleMouseEnter"
|
|
8
|
+
@mouseleave.native="handleMouseLeave"
|
|
9
|
+
@dragstart.native="handleDragStart"
|
|
10
|
+
>
|
|
11
|
+
<template v-for="name in getForwardSlotNames()" #[name]="scope">
|
|
12
|
+
<slot :name="name" v-bind="scope"></slot>
|
|
13
|
+
</template>
|
|
14
|
+
<!-- 遮罩放在ui-image的默认插槽里,渲染在图片区域内;这里不要用div,避免被外部 xxx div 之类的后代选择器命中 -->
|
|
15
|
+
<template v-if="enableDragBase64">
|
|
16
|
+
<span v-if="dragStatus === 'loading'" class="sdk-image__drag-mask">
|
|
17
|
+
<ui-icon name="loading" :size="20" color="#fff"></ui-icon>
|
|
18
|
+
<span class="sdk-image__drag-text">图片转化中</span>
|
|
19
|
+
</span>
|
|
20
|
+
<transition name="sdk-image-fade">
|
|
21
|
+
<span v-if="dragTip" class="sdk-image__drag-mask" :class="'is-' + dragTip.type">
|
|
22
|
+
<span class="sdk-image__drag-tip">
|
|
23
|
+
<ui-icon :name="dragTip.type === 'success' ? 'checkbox-circle-fill' : 'information-line'" :size="dragTip.type === 'success' ? 14 : 20" color="#fff"></ui-icon>
|
|
24
|
+
<span class="sdk-image__drag-text">{{ dragTip.text }}</span>
|
|
25
|
+
</span>
|
|
26
|
+
</span>
|
|
27
|
+
</transition>
|
|
28
|
+
</template>
|
|
29
|
+
<slot></slot>
|
|
30
|
+
</ui-image>
|
|
31
|
+
</template>
|
|
32
|
+
<script>
|
|
33
|
+
import { Image as UiImage, Icon as UiIcon } from '@aochuang/common/components'
|
|
34
|
+
import { formatOssPath } from '@aochuang/common/utils/oss'
|
|
35
|
+
import {
|
|
36
|
+
isGifImage,
|
|
37
|
+
loadImageDataURL,
|
|
38
|
+
decodeImage,
|
|
39
|
+
getDragBase64Cache,
|
|
40
|
+
setDragBase64Cache
|
|
41
|
+
} from './util'
|
|
42
|
+
|
|
43
|
+
const DRAG_MAX_SIZE = 10 * 1024 * 1024
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 在ui-image基础上增加“拖拽前转base64”能力,其他属性、事件、插槽全部透传给ui-image
|
|
47
|
+
* 默认不开启,不开启时和直接使用ui-image完全一致
|
|
48
|
+
*/
|
|
49
|
+
export default {
|
|
50
|
+
name: 'SdkImage',
|
|
51
|
+
inheritAttrs: false,
|
|
52
|
+
components: {
|
|
53
|
+
UiImage,
|
|
54
|
+
UiIcon
|
|
55
|
+
},
|
|
56
|
+
props: {
|
|
57
|
+
src: {
|
|
58
|
+
type: [String, File, Function]
|
|
59
|
+
},
|
|
60
|
+
/**
|
|
61
|
+
* 拖拽前转base64。开启后鼠标悬停时会把图片转成base64替换显示地址,
|
|
62
|
+
* 解决私有oss代理地址、裁剪参数、微信CDN等导致拖拽到其他软件打不开的问题,转换过程中禁止拖拽。
|
|
63
|
+
* 只针对PC端开启,移动端不要开启
|
|
64
|
+
*/
|
|
65
|
+
enableDragBase64: {
|
|
66
|
+
type: Boolean,
|
|
67
|
+
default: false
|
|
68
|
+
},
|
|
69
|
+
/**
|
|
70
|
+
* 用于转base64的地址,不传则使用当前显示的图片地址
|
|
71
|
+
* 例如显示用的是微信CDN地址(不支持跨域读取),可以传后端转存的oss地址
|
|
72
|
+
*/
|
|
73
|
+
dragSrc: {
|
|
74
|
+
type: String
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
data () {
|
|
78
|
+
return {
|
|
79
|
+
imageLoaded: false, // 当前显示的图片是否加载成功,加载成功才允许转base64
|
|
80
|
+
dragStatus: 'none', // none | loading | success | error
|
|
81
|
+
dragBase64Ready: false, // base64本身存在this._dragBase64上,不放进响应式数据
|
|
82
|
+
dragTip: null // 转换完成/失败的短暂提示 { type, text }
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
computed: {
|
|
86
|
+
/**
|
|
87
|
+
* 实际传给ui-image的地址
|
|
88
|
+
* base64以函数形式传入,ui-image对函数类型的src不会做裁剪和formatOssPath处理,
|
|
89
|
+
* 否则formatOssPath会把base64里的+替换成%2B,或者误判/oss/子串,导致数据被破坏
|
|
90
|
+
*/
|
|
91
|
+
displaySrc () {
|
|
92
|
+
if (this.dragBase64Ready && this._dragBase64) {
|
|
93
|
+
const dataURL = this._dragBase64
|
|
94
|
+
return () => dataURL
|
|
95
|
+
}
|
|
96
|
+
return this.src
|
|
97
|
+
},
|
|
98
|
+
innerListeners () {
|
|
99
|
+
return Object.assign({}, this.$listeners, {
|
|
100
|
+
load: this.handleLoad,
|
|
101
|
+
error: this.handleError
|
|
102
|
+
})
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
watch: {
|
|
106
|
+
src () {
|
|
107
|
+
// 地址变了,丢弃旧的base64,避免旧数据覆盖新图
|
|
108
|
+
this.resetDragBase64()
|
|
109
|
+
this.imageLoaded = false
|
|
110
|
+
},
|
|
111
|
+
dragSrc () {
|
|
112
|
+
this.resetDragBase64()
|
|
113
|
+
},
|
|
114
|
+
enableDragBase64 (val) {
|
|
115
|
+
if (!val) {
|
|
116
|
+
this.resetDragBase64()
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
beforeDestroy () {
|
|
121
|
+
this.resetDragBase64()
|
|
122
|
+
},
|
|
123
|
+
methods: {
|
|
124
|
+
// $scopedSlots不是响应式的,不能放computed里缓存,每次渲染时取
|
|
125
|
+
getForwardSlotNames () {
|
|
126
|
+
return Object.keys(this.$scopedSlots).filter(name => name !== 'default')
|
|
127
|
+
},
|
|
128
|
+
handleLoad (...args) {
|
|
129
|
+
this.imageLoaded = true
|
|
130
|
+
this.$emit('load', ...args)
|
|
131
|
+
},
|
|
132
|
+
handleError (...args) {
|
|
133
|
+
// base64显示失败,回退到原地址,不向外抛错误,避免外部误判为原图加载失败
|
|
134
|
+
if (this.dragBase64Ready) {
|
|
135
|
+
this.resetDragBase64()
|
|
136
|
+
return
|
|
137
|
+
}
|
|
138
|
+
this.imageLoaded = false
|
|
139
|
+
this.$emit('error', ...args)
|
|
140
|
+
},
|
|
141
|
+
handleDragStart (event) {
|
|
142
|
+
if (!this.enableDragBase64) {
|
|
143
|
+
return
|
|
144
|
+
}
|
|
145
|
+
// 转换中禁止拖拽
|
|
146
|
+
if (this.dragStatus === 'loading') {
|
|
147
|
+
event.preventDefault()
|
|
148
|
+
return
|
|
149
|
+
}
|
|
150
|
+
// 已经是base64了,去掉文本类拖拽数据,防止拖到文本输入框里贴出一大串base64
|
|
151
|
+
if (this.dragBase64Ready && event.dataTransfer) {
|
|
152
|
+
try {
|
|
153
|
+
event.dataTransfer.clearData('text/plain')
|
|
154
|
+
event.dataTransfer.clearData('text/uri-list')
|
|
155
|
+
} catch (e) {}
|
|
156
|
+
}
|
|
157
|
+
},
|
|
158
|
+
handleMouseEnter () {
|
|
159
|
+
if (!this.enableDragBase64 || this.dragBase64Ready || this.dragStatus === 'loading') {
|
|
160
|
+
return
|
|
161
|
+
}
|
|
162
|
+
clearTimeout(this._dragHoverTimer)
|
|
163
|
+
// 停留一会儿再转换,避免滚动、划过时大量请求
|
|
164
|
+
this._dragHoverTimer = setTimeout(() => {
|
|
165
|
+
this.transformDragBase64()
|
|
166
|
+
}, 200)
|
|
167
|
+
},
|
|
168
|
+
handleMouseLeave () {
|
|
169
|
+
clearTimeout(this._dragHoverTimer)
|
|
170
|
+
// 中途移开取消转换
|
|
171
|
+
if (this.dragStatus === 'loading') {
|
|
172
|
+
this.abortDragBase64Task()
|
|
173
|
+
this.dragStatus = 'none'
|
|
174
|
+
}
|
|
175
|
+
},
|
|
176
|
+
/**
|
|
177
|
+
* 获取用于转换的地址
|
|
178
|
+
* 1. 传了dragSrc用dragSrc
|
|
179
|
+
* 2. gif用不带裁剪参数的地址,避免裁剪后只剩第一帧
|
|
180
|
+
* 3. 其他和当前显示的保持一致,尽量命中浏览器缓存
|
|
181
|
+
*/
|
|
182
|
+
getDragFetchSrc () {
|
|
183
|
+
if (this.dragSrc) {
|
|
184
|
+
return formatOssPath(this.dragSrc)
|
|
185
|
+
}
|
|
186
|
+
if (typeof this.src !== 'string') {
|
|
187
|
+
return null
|
|
188
|
+
}
|
|
189
|
+
if (isGifImage(this.src)) {
|
|
190
|
+
return formatOssPath(this.src)
|
|
191
|
+
}
|
|
192
|
+
const img = this.$el && this.$el.querySelector('.ui-image__img')
|
|
193
|
+
const currentSrc = img && img.getAttribute('src')
|
|
194
|
+
if (currentSrc && !/^(data|blob):/i.test(currentSrc)) {
|
|
195
|
+
return currentSrc
|
|
196
|
+
}
|
|
197
|
+
return formatOssPath(this.src)
|
|
198
|
+
},
|
|
199
|
+
transformDragBase64 () {
|
|
200
|
+
if (!this.enableDragBase64 || !this.imageLoaded) {
|
|
201
|
+
return
|
|
202
|
+
}
|
|
203
|
+
const fetchSrc = this.getDragFetchSrc()
|
|
204
|
+
if (!fetchSrc || /^(data|blob):/i.test(fetchSrc)) {
|
|
205
|
+
return
|
|
206
|
+
}
|
|
207
|
+
const cached = getDragBase64Cache(fetchSrc)
|
|
208
|
+
if (cached) {
|
|
209
|
+
this.applyDragBase64(cached, false)
|
|
210
|
+
return
|
|
211
|
+
}
|
|
212
|
+
this.abortDragBase64Task()
|
|
213
|
+
this.dragStatus = 'loading'
|
|
214
|
+
const task = loadImageDataURL(fetchSrc, {
|
|
215
|
+
maxSize: DRAG_MAX_SIZE
|
|
216
|
+
})
|
|
217
|
+
this._dragBase64Task = task
|
|
218
|
+
task.promise.then((dataURL) => {
|
|
219
|
+
// 预解码,替换时避免闪烁
|
|
220
|
+
return decodeImage(dataURL).then(() => dataURL)
|
|
221
|
+
}).then((dataURL) => {
|
|
222
|
+
if (this._dragBase64Task !== task) {
|
|
223
|
+
return
|
|
224
|
+
}
|
|
225
|
+
this._dragBase64Task = null
|
|
226
|
+
setDragBase64Cache(fetchSrc, dataURL)
|
|
227
|
+
this.applyDragBase64(dataURL, true)
|
|
228
|
+
}).catch((err) => {
|
|
229
|
+
if (this._dragBase64Task !== task) {
|
|
230
|
+
return
|
|
231
|
+
}
|
|
232
|
+
// 方便排查转换失败原因(跨域、超过大小、请求失败等)
|
|
233
|
+
console.warn('[SdkImage] 图片转base64失败', fetchSrc, err)
|
|
234
|
+
this._dragBase64Task = null
|
|
235
|
+
this.dragStatus = 'error'
|
|
236
|
+
this.showDragTip('error', '转换失败,可直接拖拽原图', 1500)
|
|
237
|
+
})
|
|
238
|
+
},
|
|
239
|
+
applyDragBase64 (dataURL, showTip) {
|
|
240
|
+
this._dragBase64 = dataURL
|
|
241
|
+
this.dragBase64Ready = true
|
|
242
|
+
this.dragStatus = 'success'
|
|
243
|
+
if (showTip) {
|
|
244
|
+
this.showDragTip('success', '可拖拽', 1200)
|
|
245
|
+
}
|
|
246
|
+
},
|
|
247
|
+
showDragTip (type, text, duration) {
|
|
248
|
+
clearTimeout(this._dragTipTimer)
|
|
249
|
+
this.dragTip = { type, text }
|
|
250
|
+
this._dragTipTimer = setTimeout(() => {
|
|
251
|
+
this.dragTip = null
|
|
252
|
+
}, duration)
|
|
253
|
+
},
|
|
254
|
+
abortDragBase64Task () {
|
|
255
|
+
if (this._dragBase64Task) {
|
|
256
|
+
this._dragBase64Task.abort()
|
|
257
|
+
this._dragBase64Task = null
|
|
258
|
+
}
|
|
259
|
+
},
|
|
260
|
+
resetDragBase64 () {
|
|
261
|
+
clearTimeout(this._dragHoverTimer)
|
|
262
|
+
clearTimeout(this._dragTipTimer)
|
|
263
|
+
this.abortDragBase64Task()
|
|
264
|
+
this._dragBase64 = null
|
|
265
|
+
this.dragBase64Ready = false
|
|
266
|
+
this.dragStatus = 'none'
|
|
267
|
+
this.dragTip = null
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
</script>
|
|
272
|
+
<style lang="less">
|
|
273
|
+
.sdk-image__drag-mask{
|
|
274
|
+
position: absolute;
|
|
275
|
+
left: 0;
|
|
276
|
+
top: 0;
|
|
277
|
+
right: 0;
|
|
278
|
+
bottom: 0;
|
|
279
|
+
z-index: 9;
|
|
280
|
+
display: flex;
|
|
281
|
+
align-items: center;
|
|
282
|
+
justify-content: center;
|
|
283
|
+
background-color: rgba(0, 0, 0, .45);
|
|
284
|
+
color: #fff;
|
|
285
|
+
font-size: 12px;
|
|
286
|
+
line-height: 1.5;
|
|
287
|
+
pointer-events: none; // 不挡住点击
|
|
288
|
+
&.is-success{
|
|
289
|
+
// 成功后不再遮住图片,只保留中间一个小提示
|
|
290
|
+
background-color: transparent;
|
|
291
|
+
.sdk-image__drag-tip{
|
|
292
|
+
height: 22px;
|
|
293
|
+
padding: 0 10px;
|
|
294
|
+
line-height: 22px;
|
|
295
|
+
border-radius: 11px; // 高度的一半,左右两端为半圆
|
|
296
|
+
background-color: rgba(0, 0, 0, .55);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
&.is-error{
|
|
300
|
+
background-color: rgba(0, 0, 0, .55);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
.sdk-image__drag-tip{
|
|
304
|
+
display: inline-flex;
|
|
305
|
+
align-items: center;
|
|
306
|
+
}
|
|
307
|
+
.sdk-image__drag-mask .ui-icon + .sdk-image__drag-text{
|
|
308
|
+
margin-left: 4px;
|
|
309
|
+
white-space: nowrap;
|
|
310
|
+
}
|
|
311
|
+
.sdk-image-fade-leave-active{
|
|
312
|
+
transition: opacity .3s;
|
|
313
|
+
}
|
|
314
|
+
.sdk-image-fade-leave-to{
|
|
315
|
+
opacity: 0;
|
|
316
|
+
}
|
|
317
|
+
</style>
|
package/common/image/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import
|
|
1
|
+
import Image from './image.vue'
|
|
2
2
|
|
|
3
|
-
export default Image
|
|
3
|
+
export default Image
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 拖拽用 base64 缓存(模块级,按最近使用淘汰),避免消息列表重新渲染时重复转换
|
|
3
|
+
*/
|
|
4
|
+
const DRAG_BASE64_CACHE_LIMIT = 20
|
|
5
|
+
const dragBase64Cache = new Map()
|
|
6
|
+
|
|
7
|
+
export function getDragBase64Cache (src) {
|
|
8
|
+
if (!src || !dragBase64Cache.has(src)) {
|
|
9
|
+
return null
|
|
10
|
+
}
|
|
11
|
+
const value = dragBase64Cache.get(src)
|
|
12
|
+
// 重新插入,标记为最近使用
|
|
13
|
+
dragBase64Cache.delete(src)
|
|
14
|
+
dragBase64Cache.set(src, value)
|
|
15
|
+
return value
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function setDragBase64Cache (src, dataURL) {
|
|
19
|
+
if (!src || !dataURL) {
|
|
20
|
+
return
|
|
21
|
+
}
|
|
22
|
+
dragBase64Cache.delete(src)
|
|
23
|
+
dragBase64Cache.set(src, dataURL)
|
|
24
|
+
while (dragBase64Cache.size > DRAG_BASE64_CACHE_LIMIT) {
|
|
25
|
+
dragBase64Cache.delete(dragBase64Cache.keys().next().value)
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function isGifImage (url) {
|
|
30
|
+
if (!url) {
|
|
31
|
+
return false
|
|
32
|
+
}
|
|
33
|
+
return /\.gif([?#]|$)/i.test(url)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const IMAGE_MIME_MAP = {
|
|
37
|
+
jpg: 'image/jpeg',
|
|
38
|
+
jpeg: 'image/jpeg',
|
|
39
|
+
png: 'image/png',
|
|
40
|
+
gif: 'image/gif',
|
|
41
|
+
webp: 'image/webp',
|
|
42
|
+
bmp: 'image/bmp'
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function guessImageMimeType (url) {
|
|
46
|
+
let decodedUrl = url || ''
|
|
47
|
+
try {
|
|
48
|
+
decodedUrl = decodeURIComponent(decodedUrl)
|
|
49
|
+
} catch (e) {}
|
|
50
|
+
const match = decodedUrl.match(/\.(jpe?g|png|gif|webp|bmp)(?=[?#&]|$)/i)
|
|
51
|
+
return (match && IMAGE_MIME_MAP[match[1].toLowerCase()]) || 'image/jpeg'
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 将图片地址加载为 base64(dataURL),返回 { promise, abort }
|
|
56
|
+
* maxSize:超过该字节数直接失败,避免大图占用过多内存
|
|
57
|
+
*
|
|
58
|
+
* 使用 fetch + cache: 'no-store',整个重定向链路都不读写浏览器缓存:
|
|
59
|
+
* 私有oss的 /oss/view 会302到oss签名地址(跨域),签名精确到秒,同一秒内多次请求拿到的地址相同,
|
|
60
|
+
* 如果复用缓存,可能拿到 <img> 加载时缓存的、没有跨域头的响应(oss没有返回 Vary: Origin),
|
|
61
|
+
* 或者被取消请求留下的不完整缓存,导致跨域校验失败
|
|
62
|
+
*/
|
|
63
|
+
export function loadImageDataURL (src, { maxSize } = {}) {
|
|
64
|
+
const controller = typeof AbortController !== 'undefined' ? new AbortController() : null
|
|
65
|
+
let reader = null
|
|
66
|
+
let aborted = false
|
|
67
|
+
const checkAborted = () => {
|
|
68
|
+
if (aborted) {
|
|
69
|
+
throw new Error('aborted')
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
const promise = Promise.resolve().then(() => {
|
|
73
|
+
if (typeof fetch !== 'function') {
|
|
74
|
+
throw new Error('fetch is not supported')
|
|
75
|
+
}
|
|
76
|
+
return fetch(src, {
|
|
77
|
+
method: 'GET',
|
|
78
|
+
mode: 'cors',
|
|
79
|
+
credentials: 'same-origin',
|
|
80
|
+
cache: 'no-store',
|
|
81
|
+
signal: controller ? controller.signal : undefined
|
|
82
|
+
})
|
|
83
|
+
}).then((response) => {
|
|
84
|
+
checkAborted()
|
|
85
|
+
if (!response.ok) {
|
|
86
|
+
throw new Error(`load image failed: ${response.status}`)
|
|
87
|
+
}
|
|
88
|
+
// Content-Length 是跨域安全的响应头,超过大小提前取消,不用下载完
|
|
89
|
+
const contentLength = Number(response.headers.get('content-length'))
|
|
90
|
+
if (maxSize && contentLength && contentLength > maxSize) {
|
|
91
|
+
controller && controller.abort()
|
|
92
|
+
throw new Error('image too large')
|
|
93
|
+
}
|
|
94
|
+
return response.blob()
|
|
95
|
+
}).then((blob) => {
|
|
96
|
+
checkAborted()
|
|
97
|
+
if (!blob) {
|
|
98
|
+
throw new Error('response is empty')
|
|
99
|
+
}
|
|
100
|
+
// 私有oss代理等可能返回 application/octet-stream,按地址扩展名修正类型,否则拖出去的文件类型不对
|
|
101
|
+
if (!blob.type || blob.type.indexOf('image/') !== 0) {
|
|
102
|
+
if (blob.type && /^(text|application\/json)/i.test(blob.type)) {
|
|
103
|
+
throw new Error('response is not image')
|
|
104
|
+
}
|
|
105
|
+
blob = new Blob([blob], { type: guessImageMimeType(src) })
|
|
106
|
+
}
|
|
107
|
+
if (maxSize && blob.size > maxSize) {
|
|
108
|
+
throw new Error('image too large')
|
|
109
|
+
}
|
|
110
|
+
return new Promise((resolve, reject) => {
|
|
111
|
+
reader = new FileReader()
|
|
112
|
+
reader.onload = () => resolve(reader.result)
|
|
113
|
+
reader.onerror = () => reject(reader.error || new Error('read image failed'))
|
|
114
|
+
reader.onabort = () => reject(new Error('aborted'))
|
|
115
|
+
reader.readAsDataURL(blob)
|
|
116
|
+
})
|
|
117
|
+
})
|
|
118
|
+
return {
|
|
119
|
+
promise,
|
|
120
|
+
abort () {
|
|
121
|
+
aborted = true
|
|
122
|
+
controller && controller.abort()
|
|
123
|
+
if (reader && reader.readyState === 1) {
|
|
124
|
+
reader.abort()
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* 预解码图片,替换 src 时避免闪烁
|
|
132
|
+
*/
|
|
133
|
+
export function decodeImage (src) {
|
|
134
|
+
return new Promise((resolve, reject) => {
|
|
135
|
+
const img = new Image()
|
|
136
|
+
img.onload = () => resolve()
|
|
137
|
+
img.onerror = reject
|
|
138
|
+
img.src = src
|
|
139
|
+
})
|
|
140
|
+
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
<div class="sdk-wechat-chat-panel-control-channel-live" :class="['sdk-wechat-chat-panel-control-channel-live--' + type, {'is-border': border}]">
|
|
3
3
|
<template v-if="type === 'default' || type === 'thumb'">
|
|
4
4
|
<div class="sdk-wechat-chat-panel-control-channel-live__cover">
|
|
5
|
-
<
|
|
5
|
+
<sdk-image :src="cover" :enable-drag-base64="dragBase64Enabled" :drag-src="dragSrc" width="100%" height="100%" fit="cover" :radius="0"></sdk-image>
|
|
6
6
|
</div>
|
|
7
7
|
<div class="sdk-wechat-chat-panel-control-channel-live__main">
|
|
8
8
|
<div class="sdk-wechat-chat-panel-control-channel-live__rows">
|
|
@@ -28,10 +28,21 @@
|
|
|
28
28
|
<script>
|
|
29
29
|
import { parse, parseXml } from '../../../utils/json'
|
|
30
30
|
import { formatOssPath } from '@aochuang/common/utils/oss'
|
|
31
|
+
import SdkImage from '../../../common/image'
|
|
31
32
|
|
|
32
33
|
export default {
|
|
33
34
|
name: 'SdkChatControlChannelLive',
|
|
35
|
+
components: {
|
|
36
|
+
SdkImage
|
|
37
|
+
},
|
|
34
38
|
props: {
|
|
39
|
+
/**
|
|
40
|
+
* 拖拽前转base64(只针对PC端开启),用后端转存的oss地址转换,没有oss地址时不处理
|
|
41
|
+
*/
|
|
42
|
+
enableDragBase64: {
|
|
43
|
+
type: Boolean,
|
|
44
|
+
default: false
|
|
45
|
+
},
|
|
35
46
|
id: {
|
|
36
47
|
type: String
|
|
37
48
|
},
|
|
@@ -77,6 +88,13 @@ export default {
|
|
|
77
88
|
cover () {
|
|
78
89
|
return formatOssPath(this.nextContent.coverUrl)
|
|
79
90
|
},
|
|
91
|
+
// 显示用的coverUrl是微信CDN地址,不支持跨域读取,转base64用后端转存的oss地址(老的xml格式消息没有)
|
|
92
|
+
dragSrc () {
|
|
93
|
+
return this.nextContent.coverOssUrl || null
|
|
94
|
+
},
|
|
95
|
+
dragBase64Enabled () {
|
|
96
|
+
return this.enableDragBase64 && this.type === 'default' && !!this.dragSrc
|
|
97
|
+
},
|
|
80
98
|
avatar () {
|
|
81
99
|
return formatOssPath(this.nextContent.headUrl)
|
|
82
100
|
},
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
<div class="sdk-chat-control-channel-video" :class="['sdk-chat-control-channel-video--' + type, {'is-border': border}]">
|
|
3
3
|
<template v-if="type === 'default' || type === 'thumb'">
|
|
4
4
|
<div class="sdk-chat-control-channel-video__cover">
|
|
5
|
-
<
|
|
5
|
+
<sdk-image :src="cover" :enable-drag-base64="dragBase64Enabled" :drag-src="dragSrc" width="100%" height="100%" fit="cover" :radius="0"></sdk-image>
|
|
6
6
|
</div>
|
|
7
7
|
<div class="sdk-chat-control-channel-video__main">
|
|
8
8
|
<div class="sdk-chat-control-channel-video__rows">
|
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
</template>
|
|
28
28
|
<script>
|
|
29
29
|
import { Avatar as UiAvatar, Image as UiImage } from '@aochuang/common/components'
|
|
30
|
+
import SdkImage from '../../../common/image'
|
|
30
31
|
import { parse } from '../../../utils/json'
|
|
31
32
|
import { formatOssPath } from '@aochuang/common/utils/oss'
|
|
32
33
|
|
|
@@ -34,7 +35,8 @@ export default {
|
|
|
34
35
|
name: 'SdkChatControlChannelVideo',
|
|
35
36
|
components: {
|
|
36
37
|
UiAvatar,
|
|
37
|
-
UiImage
|
|
38
|
+
UiImage,
|
|
39
|
+
SdkImage
|
|
38
40
|
},
|
|
39
41
|
inject: {
|
|
40
42
|
SdkChatPanelBox: {
|
|
@@ -42,6 +44,13 @@ export default {
|
|
|
42
44
|
}
|
|
43
45
|
},
|
|
44
46
|
props: {
|
|
47
|
+
/**
|
|
48
|
+
* 拖拽前转base64(只针对PC端开启),用后端转存的oss地址转换,没有oss地址时不处理
|
|
49
|
+
*/
|
|
50
|
+
enableDragBase64: {
|
|
51
|
+
type: Boolean,
|
|
52
|
+
default: false
|
|
53
|
+
},
|
|
45
54
|
id: {
|
|
46
55
|
type: String
|
|
47
56
|
},
|
|
@@ -63,6 +72,13 @@ export default {
|
|
|
63
72
|
cover () {
|
|
64
73
|
return formatOssPath(this.nextContent.thumbUrl || this.nextContent.thumbOssUrl)
|
|
65
74
|
},
|
|
75
|
+
// 显示优先用的thumbUrl是微信CDN地址,不支持跨域读取,转base64用后端转存的oss地址
|
|
76
|
+
dragSrc () {
|
|
77
|
+
return this.nextContent.thumbOssUrl || null
|
|
78
|
+
},
|
|
79
|
+
dragBase64Enabled () {
|
|
80
|
+
return this.enableDragBase64 && this.type === 'default' && !!this.dragSrc
|
|
81
|
+
},
|
|
66
82
|
avatar () {
|
|
67
83
|
return formatOssPath(this.nextContent.avatar)
|
|
68
84
|
},
|
|
@@ -30,8 +30,9 @@
|
|
|
30
30
|
</template>
|
|
31
31
|
<template v-else>
|
|
32
32
|
<div class="sdk-chat-control-image__control">
|
|
33
|
-
<
|
|
33
|
+
<sdk-image
|
|
34
34
|
:src="innerSrc"
|
|
35
|
+
:enable-drag-base64="dragBase64Enabled"
|
|
35
36
|
:radius="0"
|
|
36
37
|
:previewable="previewable"
|
|
37
38
|
:width="innerWidth"
|
|
@@ -44,7 +45,7 @@
|
|
|
44
45
|
@error="handleError"
|
|
45
46
|
@click.native="handleImageClick"
|
|
46
47
|
@dragstart.native="handleImageDragStart"
|
|
47
|
-
></
|
|
48
|
+
></sdk-image>
|
|
48
49
|
<template v-if="isOriginImage && innerOptimize">
|
|
49
50
|
<span class="sdk-chat-control-image__hd">
|
|
50
51
|
<bi-tooltip text="该图片已经从手机下载过原图">
|
|
@@ -57,8 +58,9 @@
|
|
|
57
58
|
</template>
|
|
58
59
|
<template v-else>
|
|
59
60
|
<div class="sdk-chat-control-image__control">
|
|
60
|
-
<
|
|
61
|
+
<sdk-image
|
|
61
62
|
:src="innerSrc"
|
|
63
|
+
:enable-drag-base64="dragBase64Enabled"
|
|
62
64
|
:radius="0"
|
|
63
65
|
:previewable="previewable"
|
|
64
66
|
:width="innerWidth"
|
|
@@ -71,7 +73,7 @@
|
|
|
71
73
|
@error="handleError"
|
|
72
74
|
@click.native="handleImageClick"
|
|
73
75
|
@dragstart.native="handleImageDragStart"
|
|
74
|
-
></
|
|
76
|
+
></sdk-image>
|
|
75
77
|
<template v-if="isOriginImage && innerOptimize">
|
|
76
78
|
<span class="sdk-chat-control-image__hd">
|
|
77
79
|
<bi-tooltip text="该图片已经从手机下载过原图">
|
|
@@ -91,8 +93,9 @@
|
|
|
91
93
|
</div>
|
|
92
94
|
</template>
|
|
93
95
|
<script>
|
|
94
|
-
import {
|
|
96
|
+
import { Button as UiButton, Icon as UiIcon } from '@aochuang/common/components'
|
|
95
97
|
import { isOriginImage, getNewImageSrc, isNewImage } from './util'
|
|
98
|
+
import SdkImage from '../../../common/image'
|
|
96
99
|
import BiTooltip from '@aochuang/common/common/tooltip'
|
|
97
100
|
import ImageDragableConfirm from './image-dragable-confirm.vue'
|
|
98
101
|
|
|
@@ -100,7 +103,7 @@ export default {
|
|
|
100
103
|
name: 'SdkChatControlImage',
|
|
101
104
|
components: {
|
|
102
105
|
UiIcon,
|
|
103
|
-
|
|
106
|
+
SdkImage,
|
|
104
107
|
UiButton,
|
|
105
108
|
BiTooltip,
|
|
106
109
|
ImageDragableConfirm
|
|
@@ -185,6 +188,13 @@ export default {
|
|
|
185
188
|
enableDragOriginImage: {
|
|
186
189
|
type: Boolean,
|
|
187
190
|
default: false
|
|
191
|
+
},
|
|
192
|
+
/**
|
|
193
|
+
* 拖拽前转base64,具体逻辑见 common/image。只针对PC端开启,移动端不要开启
|
|
194
|
+
*/
|
|
195
|
+
enableDragBase64: {
|
|
196
|
+
type: Boolean,
|
|
197
|
+
default: false
|
|
188
198
|
}
|
|
189
199
|
},
|
|
190
200
|
data () {
|
|
@@ -261,6 +271,10 @@ export default {
|
|
|
261
271
|
},
|
|
262
272
|
allowDefrost () {
|
|
263
273
|
return !!this.defrostMethod
|
|
274
|
+
},
|
|
275
|
+
dragBase64Enabled () {
|
|
276
|
+
// 引用、缩略等小图不需要
|
|
277
|
+
return this.enableDragBase64 && this.type === 'default'
|
|
264
278
|
}
|
|
265
279
|
},
|
|
266
280
|
watch: {
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
{{title}}
|
|
14
14
|
</div>
|
|
15
15
|
<div class="sdk-chat-panel-control-url-miniprogram__content">
|
|
16
|
-
<
|
|
16
|
+
<sdk-image v-if="cover" :style="{width: '100%', height: '100%'}" :src="cover" :enable-drag-base64="dragBase64Enabled" :drag-src="dragSrc"></sdk-image>
|
|
17
17
|
</div>
|
|
18
18
|
</div>
|
|
19
19
|
<div class="sdk-chat-panel-control-url-miniprogram__foot">小程序</div>
|
|
@@ -50,14 +50,14 @@
|
|
|
50
50
|
</div>
|
|
51
51
|
</template>
|
|
52
52
|
<script>
|
|
53
|
-
import { Image as UiImage } from '@aochuang/common/components'
|
|
54
53
|
import { parse, parseXml } from '../../../utils/json'
|
|
55
54
|
import { formatOssPath } from '@aochuang/common/utils/oss'
|
|
55
|
+
import SdkImage from '../../../common/image'
|
|
56
56
|
|
|
57
57
|
export default {
|
|
58
58
|
name: 'SdkChatPanelControlUrlMiniprogram',
|
|
59
59
|
components: {
|
|
60
|
-
|
|
60
|
+
SdkImage
|
|
61
61
|
},
|
|
62
62
|
props: {
|
|
63
63
|
id: {
|
|
@@ -68,6 +68,13 @@ export default {
|
|
|
68
68
|
},
|
|
69
69
|
content: {
|
|
70
70
|
type: [Object, String]
|
|
71
|
+
},
|
|
72
|
+
/**
|
|
73
|
+
* 拖拽前转base64(只针对PC端开启),用后端转存的oss地址转换,没有oss地址时不处理
|
|
74
|
+
*/
|
|
75
|
+
enableDragBase64: {
|
|
76
|
+
type: Boolean,
|
|
77
|
+
default: false
|
|
71
78
|
}
|
|
72
79
|
},
|
|
73
80
|
inject: {
|
|
@@ -112,6 +119,13 @@ export default {
|
|
|
112
119
|
}
|
|
113
120
|
return cover
|
|
114
121
|
},
|
|
122
|
+
// 显示优先用的thumburl是微信CDN地址,不支持跨域读取,转base64用后端转存的oss地址
|
|
123
|
+
dragSrc () {
|
|
124
|
+
return (this.nextContent && this.nextContent.previewImage) || null
|
|
125
|
+
},
|
|
126
|
+
dragBase64Enabled () {
|
|
127
|
+
return this.enableDragBase64 && this.type === 'default' && !!this.dragSrc
|
|
128
|
+
},
|
|
115
129
|
border () {
|
|
116
130
|
return !this.SdkChatPanelBox
|
|
117
131
|
}
|
|
@@ -15,6 +15,13 @@ export default {
|
|
|
15
15
|
content: {
|
|
16
16
|
type: [Object, String]
|
|
17
17
|
},
|
|
18
|
+
/**
|
|
19
|
+
* 拖拽前转base64,目前只透传给小程序
|
|
20
|
+
*/
|
|
21
|
+
enableDragBase64: {
|
|
22
|
+
type: Boolean,
|
|
23
|
+
default: false
|
|
24
|
+
},
|
|
18
25
|
updateChatBoxProp: {
|
|
19
26
|
type: Function
|
|
20
27
|
},
|
|
@@ -70,7 +77,8 @@ export default {
|
|
|
70
77
|
props: {
|
|
71
78
|
id: props.id,
|
|
72
79
|
type: props.type,
|
|
73
|
-
content
|
|
80
|
+
content,
|
|
81
|
+
enableDragBase64: props.enableDragBase64
|
|
74
82
|
},
|
|
75
83
|
key: c.data.key
|
|
76
84
|
})
|
package/package.json
CHANGED
|
@@ -467,9 +467,6 @@ class ClientChat {
|
|
|
467
467
|
if (!sessionId) {
|
|
468
468
|
return
|
|
469
469
|
}
|
|
470
|
-
if (isBeforeLastMessage) {
|
|
471
|
-
return
|
|
472
|
-
}
|
|
473
470
|
if (isAutoReadMessage(data)) {
|
|
474
471
|
return this.handleAutoReadMessageSession(sessionId)
|
|
475
472
|
}
|
|
@@ -512,6 +509,10 @@ class ClientChat {
|
|
|
512
509
|
return rs
|
|
513
510
|
}).catch(() => {})
|
|
514
511
|
} else {
|
|
512
|
+
// 乱序消息只影响未读状态,不能覆盖较新的会话摘要。
|
|
513
|
+
if (isBeforeLastMessage) {
|
|
514
|
+
return this.handleOutOfOrderMessageSession(sessionId, data, 'onOutOfOrderFriendMessage')
|
|
515
|
+
}
|
|
515
516
|
this.session.updateSession(sessionId, ({ session }) => {
|
|
516
517
|
// listChange/getSession may already include this message in unreadMsgCount.
|
|
517
518
|
// Only count messages newer than the session watermark.
|
|
@@ -592,9 +593,6 @@ class ClientChat {
|
|
|
592
593
|
if (!sessionId) {
|
|
593
594
|
return
|
|
594
595
|
}
|
|
595
|
-
if (isBeforeLastMessage) {
|
|
596
|
-
return
|
|
597
|
-
}
|
|
598
596
|
if (isAutoReadMessage(data)) {
|
|
599
597
|
return this.handleAutoReadMessageSession(sessionId)
|
|
600
598
|
}
|
|
@@ -641,6 +639,10 @@ class ClientChat {
|
|
|
641
639
|
return rs
|
|
642
640
|
}).catch(() => {})
|
|
643
641
|
} else {
|
|
642
|
+
// 乱序消息只影响未读状态,不能覆盖较新的会话摘要。
|
|
643
|
+
if (isBeforeLastMessage) {
|
|
644
|
+
return this.handleOutOfOrderMessageSession(sessionId, data, 'onOutOfOrderRoomMessage')
|
|
645
|
+
}
|
|
644
646
|
this.session.updateSession(sessionId, ({ session }) => {
|
|
645
647
|
// listChange/getSession may already include this message in unreadMsgCount.
|
|
646
648
|
// Only count messages newer than the session watermark.
|
|
@@ -1328,6 +1330,30 @@ class ClientChat {
|
|
|
1328
1330
|
return true
|
|
1329
1331
|
}
|
|
1330
1332
|
|
|
1333
|
+
handleOutOfOrderMessageSession (sessionId, data, source) {
|
|
1334
|
+
return this.session.updateSession(sessionId, ({ session }) => {
|
|
1335
|
+
if (this.sessionManage.currentSession && this.sessionManage.currentSession.conversationId === sessionId && this.shouldMarkCurrentSessionRead(sessionId, data)) {
|
|
1336
|
+
this.session.setSessionRead(sessionId)
|
|
1337
|
+
return {}
|
|
1338
|
+
}
|
|
1339
|
+
this.account.updateAccountUnreadMessageNum(session.wechatAccountId, ({ unreadMesasgeNum, unreadMessageMuteNum }) => {
|
|
1340
|
+
if (session.mute) {
|
|
1341
|
+
return {
|
|
1342
|
+
num: (unreadMesasgeNum || 0) + 1,
|
|
1343
|
+
muteNum: (unreadMessageMuteNum || 0) + 1
|
|
1344
|
+
}
|
|
1345
|
+
}
|
|
1346
|
+
return {
|
|
1347
|
+
num: (unreadMesasgeNum || 0) + 1,
|
|
1348
|
+
muteNum: unreadMessageMuteNum
|
|
1349
|
+
}
|
|
1350
|
+
}, source)
|
|
1351
|
+
return {
|
|
1352
|
+
unreadMsgCount: (session.unreadMsgCount || 0) + 1
|
|
1353
|
+
}
|
|
1354
|
+
})
|
|
1355
|
+
}
|
|
1356
|
+
|
|
1331
1357
|
handleAutoReadMessageSession (sessionId) {
|
|
1332
1358
|
if (!sessionId) {
|
|
1333
1359
|
return
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# ClientChat WeChat 问题修复记录
|
|
2
|
+
|
|
3
|
+
本目录用于记录 `client-chat-wechat` 已修复问题的业务场景、根因、实现约束和回归范围。代码只能说明当前怎么做,问题文档还需要说明为什么这样做,以及哪些边界不能在后续修改中被破坏。
|
|
4
|
+
|
|
5
|
+
## 文档索引
|
|
6
|
+
|
|
7
|
+
| 问题 | 文档 | 核心结论 |
|
|
8
|
+
|---|---|---|
|
|
9
|
+
| 工作微信号未读与会话未读不一致 | [账号未读数量与会话未读数量不一致问题修复.md](./账号未读数量与会话未读数量不一致问题修复.md) | 区分账号未读账本与会话未读账本,按消息来源合并增量并在无法本地计算时向服务端校准 |
|
|
10
|
+
| 图片等消息乱序到达后未读漏计 | [乱序消息未读数漏计问题修复.md](./乱序消息未读数漏计问题修复.md) | 消息时间只控制会话摘要;已有本地会话的首次乱序入站消息仍需更新未读 |
|
|
11
|
+
|
|
12
|
+
## 新文档模板
|
|
13
|
+
|
|
14
|
+
每次问题修复至少填写以下内容:
|
|
15
|
+
|
|
16
|
+
```md
|
|
17
|
+
# <问题名称>
|
|
18
|
+
|
|
19
|
+
- 日期:YYYY-MM-DD
|
|
20
|
+
- 涉及文件:
|
|
21
|
+
- 相关提交:
|
|
22
|
+
|
|
23
|
+
## 1. 问题现象与复现步骤
|
|
24
|
+
|
|
25
|
+
## 2. 日志或状态证据
|
|
26
|
+
|
|
27
|
+
## 3. 根因
|
|
28
|
+
|
|
29
|
+
## 4. 修复方案与不变量
|
|
30
|
+
|
|
31
|
+
## 5. 副作用与未处理边界
|
|
32
|
+
|
|
33
|
+
## 6. 回归测试场景
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
记录时应优先写可验证事实,例如消息 ID、发送顺序、接收顺序、时间水位、修改前后的未读值和关键分支。不要只写“优化逻辑”或“修复问题”。
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# 乱序消息未读数漏计问题修复
|
|
2
|
+
|
|
3
|
+
- 日期:2026-11-19
|
|
4
|
+
- 涉及文件:`services/client-chat/client-chat-wechat/client-chat.js`
|
|
5
|
+
- 相关提交:`5080e19 fix: count unread out-of-order messages`
|
|
6
|
+
- 适用入口:好友消息、群聊消息
|
|
7
|
+
|
|
8
|
+
## 1. 问题现象与复现步骤
|
|
9
|
+
|
|
10
|
+
客服当前查看客户 B,客户 A 连续发送文本、图片、文本。客户端实时未读只从 0 增加到 2,刷新页面后服务端会话快照显示未读为 3。
|
|
11
|
+
|
|
12
|
+
本次日志确认的实际顺序:
|
|
13
|
+
|
|
14
|
+
| 顺序 | 消息 | `msgId` | 微信消息时间 | 客户端处理时间 | 结果 |
|
|
15
|
+
|---|---|---:|---:|---:|---|
|
|
16
|
+
| 发送 1 / 接收 1 | 文本“等等就觉得” | 1931 | 14:17:22 | 14:17:23 | 未读 0 -> 1 |
|
|
17
|
+
| 发送 3 / 接收 2 | 文本“谁好久好久” | 1933 | 14:17:27 | 14:17:28 | 未读 1 -> 2 |
|
|
18
|
+
| 发送 2 / 接收 3 | 图片 | 1932 | 14:17:26 | 14:17:30 | 被判定为乱序,未读仍为 2 |
|
|
19
|
+
|
|
20
|
+
图片实际先于第二条文本发送,但图片处理和推送较慢,导致客户端先收到时间为 14:17:27 的文本,再收到时间为 14:17:26 的图片。刷新后服务端未读为 3,证明消息已正常入库,漏计发生在客户端实时未读更新链路。
|
|
21
|
+
|
|
22
|
+
## 2. 根因
|
|
23
|
+
|
|
24
|
+
`ClientChatMessage.addMessage` 使用消息时间和当前消息水位比较:
|
|
25
|
+
|
|
26
|
+
```js
|
|
27
|
+
const isBeforeLastMessage = !data.isSend && lastMessageTime && data.wechatTime < lastMessageTime
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
消息 ID 1932 到达时,当前水位已经被 ID 1933 推进到 14:17:27,因此 `isBeforeLastMessage=true`。
|
|
31
|
+
|
|
32
|
+
好友和群聊的 `onAddMessageSuccess` 原来都在入口直接返回:
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
if (isBeforeLastMessage) {
|
|
36
|
+
return
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
这个判断原本是为了防止旧消息覆盖较新的 `lastMsgContent`、`lastMsgId` 和 `lastMsgTime`,但入口直接返回同时跳过了未读数更新。也就是说,一个“是否更新会话摘要”的时间条件,被错误地用于决定“是否存在一条新的未读消息”。
|
|
41
|
+
|
|
42
|
+
消息本身已经先写入 `messageMap`,同一页面实例内相同消息 ID 也已在进入回调前去重,所以本次图片是首次接收的有效消息,不应该因为时间较早而丢失未读语义。
|
|
43
|
+
|
|
44
|
+
## 3. 修复原则
|
|
45
|
+
|
|
46
|
+
本次采用最小修复,不引入额外的服务端对账请求:
|
|
47
|
+
|
|
48
|
+
1. 消息时间继续决定是否更新会话摘要和列表排序。
|
|
49
|
+
2. 已有本地会话的首次有效入站消息,未读处理不受消息时间影响。
|
|
50
|
+
3. 乱序消息不能修改任何 `lastMsg*` 字段。
|
|
51
|
+
4. 本地没有会话时,继续使用现有 `getSession` 服务端快照和 `accountUnreadDelta` 合并,客户端不能额外无条件 `+1`。
|
|
52
|
+
5. 自动已读、忽略会话、删除消息和屏蔽消息的过滤必须先于乱序未读处理。
|
|
53
|
+
|
|
54
|
+
## 4. 具体实现
|
|
55
|
+
|
|
56
|
+
好友和群聊消息都删除了 `onAddMessageSuccess` 入口处的乱序直接返回。在完成特殊消息过滤并查找本地会话后分流:
|
|
57
|
+
|
|
58
|
+
### 4.1 本地会话不存在
|
|
59
|
+
|
|
60
|
+
保持原有 `getSession` 逻辑:
|
|
61
|
+
|
|
62
|
+
- 会话未读以服务端返回的 `unreadMsgCount` 为基础合并。
|
|
63
|
+
- 只有消息晚于轮询基准水位时,才给会话快照额外 `+1`。
|
|
64
|
+
- 账号未读继续通过 `context.accountUnreadDelta` 累加本轮新增消息。
|
|
65
|
+
|
|
66
|
+
该路径不能直接本地 `+1`,因为服务端返回的会话快照可能已经包含当前消息,再加一次会产生重复计数。
|
|
67
|
+
|
|
68
|
+
### 4.2 本地会话存在且消息乱序
|
|
69
|
+
|
|
70
|
+
统一调用 `handleOutOfOrderMessageSession`:
|
|
71
|
+
|
|
72
|
+
- 当前正在查看该会话且允许自动已读:调用 `setSessionRead(sessionId)`,不增加未读。
|
|
73
|
+
- 其他情况:
|
|
74
|
+
- `session.unreadMsgCount = (session.unreadMsgCount || 0) + 1`
|
|
75
|
+
- 账号 `num + 1`
|
|
76
|
+
- 会话免打扰时同时 `muteNum + 1`
|
|
77
|
+
- 更新后立即结束,不进入后面的 `lastMsgContent`、`lastMsgType`、`lastMsgTime`、`lastMsgId`、`lastUpdateTime` 更新逻辑。
|
|
78
|
+
|
|
79
|
+
本次日志中的正确结果是:图片 ID 1932 把未读从 2 增加到 3,会话摘要仍保留时间更晚的文本 ID 1933。
|
|
80
|
+
|
|
81
|
+
## 5. 为什么不直接删除入口 return 后复用正常分支
|
|
82
|
+
|
|
83
|
+
正常分支仍用 `data.wechatTime > session.lastMsgTime` 判断是否更新。乱序消息不满足该条件,直接复用会再次返回空更新,未读仍不会增加。
|
|
84
|
+
|
|
85
|
+
如果反过来取消时间判断,则图片 ID 1932 会覆盖文本 ID 1933,导致会话摘要倒退、列表排序变化。因此乱序消息必须是独立的“只处理未读”分支。
|
|
86
|
+
|
|
87
|
+
## 6. 副作用与未处理边界
|
|
88
|
+
|
|
89
|
+
### 6.1 相同时间戳尚未处理
|
|
90
|
+
|
|
91
|
+
当 `data.wechatTime === lastMsgTime` 时,`isBeforeLastMessage=false`,但正常分支的 `isNewerMessage` 也为 false。这种同时间戳消息仍可能漏计。本次日志对应的是严格小于场景,不在本次最小修复中扩大范围。
|
|
92
|
+
|
|
93
|
+
### 6.2 外部新消息事件仍被抑制
|
|
94
|
+
|
|
95
|
+
`ClientChatMessage` 对 `isBeforeLastMessage` 仍会以 `reason=out_of_order` 抑制 `add-message` 事件。因此本次只修复会话和账号未读,不改变声音提醒、桌面通知或依赖 `add-message` 的外部缓存行为。
|
|
96
|
+
|
|
97
|
+
### 6.3 去重范围是当前消息实例
|
|
98
|
+
|
|
99
|
+
`messageMap` 能阻止同一页面实例内相同消息 ID 重复计数,但页面刷新或消息实例重建后会清空。当前修复没有增加跨刷新幂等账本。
|
|
100
|
+
|
|
101
|
+
### 6.4 断线补拉与服务端并发刷新
|
|
102
|
+
|
|
103
|
+
断线补拉也可能通过 `addMessage(item)` 进入实时回调。当前方案依赖消息 ID 去重,并假设首次进入乱序分支时本地会话未读尚未由另一条服务端刷新链路包含该消息。如果以后增加并发的会话快照刷新,需要重新检查是否可能本地重复 `+1`,必要时引入消息来源或服务端 revision。
|
|
104
|
+
|
|
105
|
+
### 6.5 群聊 @ 标记
|
|
106
|
+
|
|
107
|
+
乱序群聊消息本次只更新未读,不追加 `lastAtMsgContent`。这与“只修未读”的最小范围一致;如果产品要求乱序 @ 消息仍显示 @ 标记,需要单独设计,不能顺带修改 `lastMsg*` 摘要。
|
|
108
|
+
|
|
109
|
+
## 7. 回归测试场景
|
|
110
|
+
|
|
111
|
+
| 场景 | 预期 |
|
|
112
|
+
|---|---|
|
|
113
|
+
| 非当前好友会话依次接收时间 22、27、26 的三条不同 ID 消息 | 未读 0 -> 1 -> 2 -> 3;最后消息仍为时间 27 的消息 |
|
|
114
|
+
| 非当前群聊以相同顺序接收消息 | 会话与账号未读均增加到 3;最后消息不倒退 |
|
|
115
|
+
| 当前会话收到乱序消息且允许自动已读 | 调用 `setSessionRead`,会话未读保持 0 |
|
|
116
|
+
| `shouldMarkCurrentSessionRead` 返回 false | 即使是当前会话,乱序入站消息仍增加未读 |
|
|
117
|
+
| 免打扰会话收到乱序消息 | 会话未读、账号 `num` 和账号 `muteNum` 都增加 1 |
|
|
118
|
+
| 同一消息 ID 重复推送 | 第二次在消息层被去重,不重复增加未读 |
|
|
119
|
+
| 本地没有对应会话 | 继续走 `getSession`,不进入本地乱序 `+1` 分支 |
|
|
120
|
+
| 自动已读、忽略会话、deleted 或 blocked 消息 | 保持原有过滤行为,不增加未读 |
|
|
121
|
+
| 乱序消息处理前后比较会话摘要 | 所有 `lastMsg*` 字段保持不变 |
|
|
122
|
+
| 刷新页面重新取服务端会话 | 实时未读和刷新后的服务端未读一致 |
|
|
123
|
+
|
|
124
|
+
## 8. 验证记录
|
|
125
|
+
|
|
126
|
+
- 使用 Babel parser 对修改后的 `client-chat.js` 完成语法解析。
|
|
127
|
+
- `git diff --check` 通过。
|
|
128
|
+
- 仓库没有可直接运行的该模块单元测试,本次依赖上述场景进行人工或集成回归。
|