@aochuang/client-sdk 1.0.405 → 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.405",
3
+ "version": "1.0.406",
4
4
  "description": "",
5
5
  "files": [
6
6
  "common",