@conecli/cone-render 0.10.1-shop3.52 → 0.10.1-shop3.53

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.
@@ -1 +1 @@
1
- import React, { useCallback, useEffect, useRef, useState } from 'react'
2
1
  getQualityImage,
3
2
  isH5AndJdShopView,
4
3
  isChartH5,
5
4
  isH5AndJdShopViewH5Scroll,
6
5
  isAppStowShop,
7
6
  getSgmCustomCode,
8
7
  sgmCustomReport,
9
8
  isImageOptimizeEnable,
10
9
  getAvifSupport,
11
10
  getWebpSupport,
12
11
  getNativePageScrollRes,
13
12
  latestFromNativeMsgStorage,
14
13
  'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAAXNSR0IB2cksfwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAANQTFRFAAAAp3o92gAAAAF0Uk5TAEDm2GYAAAAKSURBVHicY2QAAAAEAAIhZK1qAAAAAElFTkSuQmCC'
15
14
  'https://img14.360buyimg.com/imagetools/jfs/t1/222907/25/7012/5824/61c4797cEbcd17c7f/6c76fc71e4fdb5a5.png'
16
15
  const styleSheets = document.styleSheets
17
16
  const internalStyleSheets = []
18
17
  for (let i = 0; i < styleSheets.length; i++) {
19
18
  const sheet = styleSheets[i]
20
19
  const ownerNode = sheet.ownerNode
21
20
  if (ownerNode && ownerNode.nodeName === 'STYLE') {
22
21
  internalStyleSheets.push(sheet)
23
22
  }
24
23
  }
25
24
  console.warn(
26
25
  '🚗 ~~ file: index.h5.tsx:47 ~~ getRuleBySelector() ~~ selector:',
27
26
  selector,
28
27
  )
29
28
  for (let i = 0; i < internalStyleSheets.length; i++) {
30
29
  try {
31
30
  const sheet = internalStyleSheets[i]
32
31
  const rules = sheet.cssRules || sheet.rules
33
32
  for (const rule of rules) {
34
33
  if (rule?.selectorText?.match(new RegExp(selector + '$'))) {
35
34
  return rule
36
35
  }
37
36
  }
38
37
  } catch (e) {
39
38
  console.warn('因安全限制无法访问样式表:', e)
40
39
  }
41
40
  }
42
41
  return null
43
42
  const {
44
43
  src = null,
45
44
  lazyLoad = true,
46
45
  imagRenderingSet = true,
47
46
  width = null,
48
47
  height = null,
49
48
  className = null,
50
49
  isSkuImage = false,
51
50
  hideErrorImage = false,
52
51
  style = null,
53
52
  backgroundColor = null,
54
53
  errorSrc = null,
55
54
  onLoad = null,
56
55
  onError = null,
57
56
  ...otherOption
58
57
  } = props
59
58
  getNetWorkType === NetWorkTypeQuality.default &&
60
59
  (getNetWorkType = global.info.sysInfo.netWorkType)
61
60
  const [loadSuccess, setLoadSuccess] = useState(false)
62
61
  const [imageErrState, setImageErrState] = useState(false)
63
62
  const [componentShowState, setComponentShowState] = useState(false)
64
63
  const measureRef = useRef<HTMLElement | null>(null)
65
64
  const [measureComplete, setMeasureComplete] = useState(false)
66
65
  const [imgSrc, setImgSrc] = useState(src)
67
66
  const [hasRetrySuccess, setHasRetrySuccess] = useState(false)
68
67
  const hasRetryRef = useRef(false)
69
68
  const componentShowStateRef = useRef(false)
70
69
  const requestSrcRef = useRef(src)
71
70
  const needShowHighVersion =
72
71
  isH5AndJdShopViewH5Scroll &&
73
72
  !(
74
73
  global.info.queryInfo?.downgraded &&
75
74
  global.info.queryInfo.downgraded === 'true'
76
75
  )
77
76
  const enableAvifOptimize = isImageOptimizeEnable()
78
77
  const isVipShop = global?.info?.pageInfo?.isVipShop
79
78
  const avifSupport = getAvifSupport()
80
79
  const webpSupport = getWebpSupport()
81
80
 
82
81
  const getRequestSrc = useCallback(
83
82
  (src) => {
84
83
  const requestSrc = getQualityImage(imgSrc, {
85
84
  isSkuImage,
86
85
  size: measureRef?.current?.getBoundingClientRect()?.width,
87
86
  })
88
87
  requestSrcRef.current = requestSrc
89
88
  return requestSrc
90
89
  },
91
90
  [src],
92
91
  )
93
92
 
94
93
  const imageErrorRetry = (src) => {
95
94
  return new Promise((resolve, reject) => {
96
95
  if (fetch && window && window.Image) {
97
96
  fetch(src)
98
97
  .then((response) => {
99
98
  const { ok, status } = response
100
99
  if (ok) {
101
100
  const originUrl = src.replace(/\.(jpe?g|png).*/, '.$1')
102
101
  response
103
102
  .blob()
104
103
  .then((blob) => {
105
104
  if (URL) {
106
105
  const url = URL.createObjectURL(blob)
107
106
  const img = new window.Image()
108
107
  img.src = url
109
108
  img.onload = () => {
110
109
  reportSGM({
111
110
  status,
112
111
  text: '再次请求并且onload了',
113
112
  type: 'retryAndOnload',
114
113
  requestSrc: src,
115
114
  resolveUrl: url,
116
115
  })
117
116
  resolve({
118
117
  ok: true,
119
118
  url,
120
119
  })
121
120
  setTimeout(() => {
122
121
  URL.revokeObjectURL(url)
123
122
  }, 2000)
124
123
  }
125
124
  img.onerror = () => {
126
125
  reportSGM({
127
126
  status,
128
127
  text: '图片解析异常',
129
128
  type: 'imageParseError',
130
129
  requestSrc: src,
131
130
  })
132
131
  URL.revokeObjectURL(url)
133
132
  resolve({
134
133
  ok: true,
135
134
  url: originUrl,
136
135
  })
137
136
  }
138
137
  } else {
139
138
  resolve({
140
139
  ok: true,
141
140
  url: originUrl,
142
141
  })
143
142
  }
144
143
  })
145
144
  .catch((error) => {
146
145
  resolve({
147
146
  ok: true,
148
147
  url: originUrl,
149
148
  })
150
149
  console.error('LazyLoadImage imageErrorRetry() error:', error)
151
150
  })
152
151
  } else {
153
152
  if (status === 404) {
154
153
  resolve({
155
154
  ok: false,
156
155
  text: '访问图片不存在',
157
156
  type: 'noSuchUrlImage',
158
157
  })
159
158
  } else {
160
159
  resolve({
161
160
  ok: false,
162
161
  status: status,
163
162
  text: '其它图片问题',
164
163
  type: 'otherImageError',
165
164
  })
166
165
  }
167
166
  }
168
167
  })
169
168
  .catch((error) => {
170
169
  resolve({
171
170
  ok: false,
172
171
  text: '网络异常',
173
172
  type: 'networkError',
174
173
  })
175
174
  console.error('LazyLoadImage imageErrorRetry() error:', error)
176
175
  })
177
176
  } else {
178
177
  resolve({
179
178
  ok: false,
180
179
  text: '不支持重试',
181
180
  type: 'notSupportRetry',
182
181
  })
183
182
  }
184
183
  })
185
184
  }
186
185
 
187
186
  const reportSGM = ({
188
187
  status,
189
188
  text,
190
189
  type,
191
190
  requestSrc = imgSrc,
192
191
  resolveUrl = '',
193
192
  }) => {
194
193
  const { shopId, venderId } = global.info.queryInfo || {}
195
194
  sgmCustomReport({
196
195
  code: getSgmCustomCode(`${SgmCustomCode.IMAGE_LOAD}_${type}`),
197
196
  msg: {
198
197
  avifSupport,
199
198
  webpSupport,
200
199
  shopId,
201
200
  venderId,
202
201
  originSrc: src,
203
202
  requestSrc,
204
203
  resolveUrl,
205
204
  status,
206
205
  text,
207
206
  },
208
207
  })
209
208
  }
210
209
  const imageErrorHandle = useCallback(
211
210
  (e) => {
212
211
  console.log(' ==============> 图片加载错误', e)
213
212
  if (!hasRetryRef.current) {
214
213
  hasRetryRef.current = true
215
214
  if (src) {
216
215
  try {
217
216
  imageErrorRetry(requestSrcRef.current).then((result) => {
218
217
  const { status, ok, text, type, url } = result || {}
219
218
  if (ok) {
220
219
  setImgSrc(url)
221
220
  setHasRetrySuccess(true)
222
221
  } else {
223
222
  errorSrc && setImgSrc(errorSrc)
224
223
  hideErrorImage && setImageErrState(true)
225
224
  typeof onError === 'function' && onError(e, src, props)
226
225
  reportSGM({
227
226
  status,
228
227
  text,
229
228
  type,
230
229
  requestSrc: requestSrcRef.current,
231
230
  })
232
231
  }
233
232
  })
234
233
  } catch (e) {
235
234
  console.error('LazyLoadImage imageErrorHandle() error:', e)
236
235
  errorSrc && setImgSrc(errorSrc)
237
236
  hideErrorImage && setImageErrState(true)
238
237
  typeof onError === 'function' && onError(e, src, props)
239
238
  }
240
239
  }
241
240
  } else {
242
241
  reportSGM({
243
242
  status: '',
244
243
  text: '渲染原始图片渲染异常',
245
244
  type: 'renderOriginImageError',
246
245
  })
247
246
  if (imgSrc.includes('blob:')) {
248
247
  reportSGM({
249
248
  status: '',
250
249
  text: '渲染本地blob图片异常',
251
250
  type: 'renderBlobImageError',
252
251
  })
253
252
  } else {
254
253
  reportSGM({
255
254
  status: '',
256
255
  text: '渲染本地非blob图片异常',
257
256
  type: 'renderNoBlobImageError',
258
257
  })
259
258
  }
260
259
  }
261
260
  },
262
261
  [src, hasRetryRef.current],
263
262
  )
264
263
 
265
264
  const imageLoad = useCallback(
266
265
  (_src, event) => {
267
266
  setLoadSuccess(true)
268
267
  typeof onLoad === 'function' && onLoad(event, src, props)
269
268
  },
270
269
  [src],
271
270
  )
272
271
 
273
272
  const changeStyleIncludeWidthAndHeightAndBgColor = () => {
274
273
  const changeStyle = {}
275
274
  width && (changeStyle['width'] = width)
276
275
  height && (changeStyle['height'] = height)
277
276
  backgroundColor && (changeStyle['backgroundColor'] = backgroundColor)
278
277
  return changeStyle
279
278
  }
280
279
  useEffect(() => {
281
280
  try {
282
281
  const { mode } = otherOption
283
282
  if (mode && mode === 'heightFix') {
284
283
  className?.split(/\s/)?.forEach((item) => {
285
284
  const userDefinedClass = getRuleBySelector('.' + item)
286
285
  const userDefinedHeight = userDefinedClass?.style?.height
287
286
  if (userDefinedHeight === '' || userDefinedHeight === 'auto') {
288
287
  console.warn(
289
288
  "🚗 ~~ 发现一例用户使用图片组件设置了 mode='heightFix' 并且没有定义一个具体高度:",
290
289
  userDefinedHeight,
291
290
  {
292
291
  code: getSgmCustomCode(`imageUsageCheck`),
293
292
  msg: {
294
293
  userDefinedClass: item,
295
294
  userDefinedHeight,
296
295
  },
297
296
  },
298
297
  )
299
298
  sgmCustomReport({
300
299
  code: getSgmCustomCode(`imageUsageCheck`),
301
300
  msg: {
302
301
  userDefinedClass: item,
303
302
  userDefinedHeight,
304
303
  },
305
304
  })
306
305
  }
307
306
  })
308
307
  }
309
308
  } catch (e) {
310
309
  console.warn('imageUsageCheck error:', e)
311
310
  }
312
311
  setMeasureComplete(true)
313
312
  if (needShowHighVersion) return
314
313
  const latestRes =
315
314
  latestFromNativeMsgStorage[TaroEventType.PAGE_SCROLL] || {}
316
315
  !componentShowStateRef.current && dealPageScrollInfo(latestRes)
317
316
  Taro.eventCenter.on(TaroEventType.PAGE_SCROLL, (res) => {
318
317
  !componentShowStateRef.current && dealPageScrollInfo(res)
319
318
  })
320
319
  }, [])
321
320
 
322
321
  const dealPageScrollInfo = (res) => {
323
322
  const { displayHeight, offSetY } = getNativePageScrollRes(res) || {}
324
323
  if (typeof displayHeight === 'undefined' || typeof offSetY === 'undefined')
325
324
  return
326
325
  if (measureRef.current) {
327
326
  const eleClientRect = measureRef.current.getBoundingClientRect()
328
327
  const getContainerHeightOffSetY = displayHeight * 1.5 + offSetY
329
328
  const eleOffsetTop = Math.ceil(eleClientRect.top)
330
329
  const eleOffsetHeight = Math.ceil(eleClientRect.height)
331
330
  if (!componentShowStateRef.current) {
332
331
  if (getContainerHeightOffSetY > eleOffsetTop) {
333
332
  componentShowStateRef.current = true
334
333
  setComponentShowState(true)
335
334
  }
336
335
  }
337
336
  }
338
337
  }
339
338
  return isH5AndJdShopView &&
340
339
  global?.config?.needImageLazy !== false &&
341
340
  !needShowHighVersion &&
342
341
  !isAppStowShop ? (
343
342
  <View
344
343
  ref={measureRef}
345
344
  className={classNames(
346
345
  imageStyle['d-app-lazy-image'],
347
346
  {
348
347
  [imageStyle['d-lazy-sku-image']]: isSkuImage,
349
348
  },
350
349
  {
351
350
  [imageStyle['d-hide-image-error']]: imageErrState,
352
351
  },
353
352
  {
354
353
  [imageStyle['d-load-completed']]: loadSuccess,
355
354
  },
356
355
  {
357
356
  'd-imag-rendering-crisp-edges':
358
357
  !isVipShop && imagRenderingSet,
359
358
  },
360
359
  'J_html5ImageBg',
361
360
  className,
362
361
  )}
363
362
  style={{
364
363
  ...style,
365
364
  ...changeStyleIncludeWidthAndHeightAndBgColor(),
366
365
  }}
367
366
  {...otherOption}
368
367
  >
369
368
  {(componentShowState || lazyLoad === false) && (
370
369
  <img
371
370
  src={getQualityImage(
372
371
  imgSrc,
373
372
  isVipShop
374
373
  ? NetWorkTypeQuality['perfect']
375
374
  : NetWorkTypeQuality[getNetWorkType],
376
375
  )}
377
376
  onLoad={imageLoad.bind(this, imgSrc)}
378
377
  onError={imageErrorHandle}
379
378
  />
380
379
  )}
381
380
  </View>
382
381
  ) : enableAvifOptimize ? (
383
382
  [
384
383
  measureComplete ? (
385
384
  <Image
386
385
  key={hasRetrySuccess ? 'realImageRetry' : 'realImage'}
387
386
  style={{
388
387
  ...style,
389
388
  ...changeStyleIncludeWidthAndHeightAndBgColor(),
390
389
  }}
391
390
  className={classNames(
392
391
  imageStyle['d-lazy-image'],
393
392
  {
394
393
  [imageStyle['d-lazy-sku-image']]: isSkuImage,
395
394
  },
396
395
  {
397
396
  [imageStyle['d-hide-image-error']]: imageErrState,
398
397
  },
399
398
  {
400
399
  [imageStyle['d-load-completed']]: loadSuccess,
401
400
  },
402
401
  {
403
402
  'd-imag-rendering-crisp-edges': imagRenderingSet,
404
403
  },
405
404
  {
406
405
  [imageStyle["d-no-jd-dog"]]: isVipShop && !isSkuImage,
407
406
  },
408
407
  className,
409
408
  )}
410
409
  src={hasRetrySuccess ? imgSrc : getRequestSrc(imgSrc)}
411
410
  lazyLoad={isChartH5 ? false : lazyLoad}
412
411
  onError={imageErrorHandle}
413
412
  onLoad={imageLoad.bind(this, imgSrc)}
414
413
  {...otherOption}
415
414
  />
416
415
  ) : (
417
416
  <Image
418
417
  key="defaultImage"
419
418
  style={{
420
419
  ...style,
421
420
  ...changeStyleIncludeWidthAndHeightAndBgColor(),
422
421
  }}
423
422
  className={classNames(
424
423
  imageStyle['d-lazy-image'],
425
424
  {
426
425
  [imageStyle['d-lazy-sku-image']]: isSkuImage,
427
426
  },
428
427
  {
429
428
  [imageStyle['d-hide-image-error']]: imageErrState,
430
429
  },
431
430
  {
432
431
  [imageStyle['d-load-completed']]: loadSuccess,
433
432
  },
434
433
  {
435
434
  'd-imag-rendering-crisp-edges': imagRenderingSet,
436
435
  },
437
436
  {
438
437
  [imageStyle["d-no-jd-dog"]]: isVipShop && !isSkuImage,
439
438
  },
440
439
  className,
441
440
  )}
442
441
  src={isSkuImage ? DEFAULT_SKU_SRC : DEFAULT_SRC}
443
442
  />
444
443
  ),
445
444
  loadSuccess ? null : <View key="measureRef" ref={measureRef}></View>,
446
445
  ]
447
446
  ) : (
448
447
  <Image
449
448
  style={{
450
449
  ...style,
451
450
  ...changeStyleIncludeWidthAndHeightAndBgColor(),
452
451
  }}
453
452
  className={classNames(
454
453
  imageStyle['d-lazy-image'],
455
454
  {
456
455
  [imageStyle['d-lazy-sku-image']]: isSkuImage,
457
456
  },
458
457
  {
459
458
  [imageStyle['d-hide-image-error']]: imageErrState,
460
459
  },
461
460
  {
462
461
  [imageStyle['d-load-completed']]: loadSuccess,
463
462
  },
464
463
  {
465
464
  'd-imag-rendering-crisp-edges': imagRenderingSet,
466
465
  },
467
466
  className,
468
467
  )}
469
468
  src={getQualityImage(imgSrc, NetWorkTypeQuality[getNetWorkType])}
470
469
  lazyLoad={isChartH5 ? false : lazyLoad}
471
470
  onError={imageErrorHandle}
472
471
  onLoad={imageLoad.bind(this, imgSrc)}
473
472
  {...otherOption}
474
473
  />
475
474
  )
475
+ import React, { useCallback, useEffect, useRef, useState } from 'react'
476
476
  getQualityImage,
477
477
  isH5AndJdShopView,
478
478
  isChartH5,
479
479
  isH5AndJdShopViewH5Scroll,
480
480
  isAppStowShop,
481
481
  getSgmCustomCode,
482
482
  sgmCustomReport,
483
483
  isImageOptimizeEnable,
484
484
  getAvifSupport,
485
485
  getWebpSupport,
486
486
  getNativePageScrollRes,
487
487
  latestFromNativeMsgStorage,
488
488
  'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAAXNSR0IB2cksfwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAANQTFRFAAAAp3o92gAAAAF0Uk5TAEDm2GYAAAAKSURBVHicY2QAAAAEAAIhZK1qAAAAAElFTkSuQmCC'
489
489
  'https://img14.360buyimg.com/imagetools/jfs/t1/222907/25/7012/5824/61c4797cEbcd17c7f/6c76fc71e4fdb5a5.png'
490
490
  const styleSheets = document.styleSheets
491
491
  const internalStyleSheets = []
492
492
  for (let i = 0; i < styleSheets.length; i++) {
493
493
  const sheet = styleSheets[i]
494
494
  const ownerNode = sheet.ownerNode
495
495
  if (ownerNode && ownerNode.nodeName === 'STYLE') {
496
496
  internalStyleSheets.push(sheet)
497
497
  }
498
498
  }
499
499
  console.warn(
500
500
  '🚗 ~~ file: index.h5.tsx:47 ~~ getRuleBySelector() ~~ selector:',
501
501
  selector,
502
502
  )
503
503
  for (let i = 0; i < internalStyleSheets.length; i++) {
504
504
  try {
505
505
  const sheet = internalStyleSheets[i]
506
506
  const rules = sheet.cssRules || sheet.rules
507
507
  for (const rule of rules) {
508
508
  if (rule?.selectorText?.match(new RegExp(selector + '$'))) {
509
509
  return rule
510
510
  }
511
511
  }
512
512
  } catch (e) {
513
513
  console.warn('因安全限制无法访问样式表:', e)
514
514
  }
515
515
  }
516
516
  return null
517
517
  const {
518
518
  src = null,
519
519
  lazyLoad = true,
520
520
  imagRenderingSet = true,
521
521
  width = null,
522
522
  height = null,
523
523
  className = null,
524
524
  isSkuImage = false,
525
525
  hideErrorImage = false,
526
526
  style = null,
527
527
  backgroundColor = null,
528
528
  errorSrc = null,
529
529
  onLoad = null,
530
530
  onError = null,
531
531
  sizeScale = 1,
532
532
  ...otherOption
533
533
  } = props
534
534
  getNetWorkType === NetWorkTypeQuality.default &&
535
535
  (getNetWorkType = global.info.sysInfo.netWorkType)
536
536
  const [loadSuccess, setLoadSuccess] = useState(false)
537
537
  const [imageErrState, setImageErrState] = useState(false)
538
538
  const [componentShowState, setComponentShowState] = useState(false)
539
539
  const measureRef = useRef<HTMLElement | null>(null)
540
540
  const [measureComplete, setMeasureComplete] = useState(false)
541
541
  const [imgSrc, setImgSrc] = useState(src)
542
542
  const [hasRetrySuccess, setHasRetrySuccess] = useState(false)
543
543
  const hasRetryRef = useRef(false)
544
544
  const componentShowStateRef = useRef(false)
545
545
  const requestSrcRef = useRef(src)
546
546
  const needShowHighVersion =
547
547
  isH5AndJdShopViewH5Scroll &&
548
548
  !(
549
549
  global.info.queryInfo?.downgraded &&
550
550
  global.info.queryInfo.downgraded === 'true'
551
551
  )
552
552
  const enableAvifOptimize = isImageOptimizeEnable()
553
553
  const isVipShop = global?.info?.pageInfo?.isVipShop
554
554
  const avifSupport = getAvifSupport()
555
555
  const webpSupport = getWebpSupport()
556
556
 
557
557
  const getRequestSrc = useCallback(
558
558
  (src) => {
559
559
  const requestSrc = getQualityImage(imgSrc, {
560
560
  isSkuImage,
561
561
  size: measureRef?.current?.getBoundingClientRect()?.width,
562
562
  sizeScale
563
563
  })
564
564
  requestSrcRef.current = requestSrc
565
565
  return requestSrc
566
566
  },
567
567
  [src],
568
568
  )
569
569
 
570
570
  const imageErrorRetry = (src) => {
571
571
  return new Promise((resolve, reject) => {
572
572
  if (fetch && window && window.Image) {
573
573
  fetch(src)
574
574
  .then((response) => {
575
575
  const { ok, status } = response
576
576
  if (ok) {
577
577
  const originUrl = src.replace(/\.(jpe?g|png).*/, '.$1')
578
578
  response
579
579
  .blob()
580
580
  .then((blob) => {
581
581
  if (URL) {
582
582
  const url = URL.createObjectURL(blob)
583
583
  const img = new window.Image()
584
584
  img.src = url
585
585
  img.onload = () => {
586
586
  reportSGM({
587
587
  status,
588
588
  text: '再次请求并且onload了',
589
589
  type: 'retryAndOnload',
590
590
  requestSrc: src,
591
591
  resolveUrl: url,
592
592
  })
593
593
  resolve({
594
594
  ok: true,
595
595
  url,
596
596
  })
597
597
  setTimeout(() => {
598
598
  URL.revokeObjectURL(url)
599
599
  }, 2000)
600
600
  }
601
601
  img.onerror = () => {
602
602
  reportSGM({
603
603
  status,
604
604
  text: '图片解析异常',
605
605
  type: 'imageParseError',
606
606
  requestSrc: src,
607
607
  })
608
608
  URL.revokeObjectURL(url)
609
609
  resolve({
610
610
  ok: true,
611
611
  url: originUrl,
612
612
  })
613
613
  }
614
614
  } else {
615
615
  resolve({
616
616
  ok: true,
617
617
  url: originUrl,
618
618
  })
619
619
  }
620
620
  })
621
621
  .catch((error) => {
622
622
  resolve({
623
623
  ok: true,
624
624
  url: originUrl,
625
625
  })
626
626
  console.error('LazyLoadImage imageErrorRetry() error:', error)
627
627
  })
628
628
  } else {
629
629
  if (status === 404) {
630
630
  resolve({
631
631
  ok: false,
632
632
  text: '访问图片不存在',
633
633
  type: 'noSuchUrlImage',
634
634
  })
635
635
  } else {
636
636
  resolve({
637
637
  ok: false,
638
638
  status: status,
639
639
  text: '其它图片问题',
640
640
  type: 'otherImageError',
641
641
  })
642
642
  }
643
643
  }
644
644
  })
645
645
  .catch((error) => {
646
646
  resolve({
647
647
  ok: false,
648
648
  text: '网络异常',
649
649
  type: 'networkError',
650
650
  })
651
651
  console.error('LazyLoadImage imageErrorRetry() error:', error)
652
652
  })
653
653
  } else {
654
654
  resolve({
655
655
  ok: false,
656
656
  text: '不支持重试',
657
657
  type: 'notSupportRetry',
658
658
  })
659
659
  }
660
660
  })
661
661
  }
662
662
 
663
663
  const reportSGM = ({
664
664
  status,
665
665
  text,
666
666
  type,
667
667
  requestSrc = imgSrc,
668
668
  resolveUrl = '',
669
669
  }) => {
670
670
  const { shopId, venderId } = global.info.queryInfo || {}
671
671
  sgmCustomReport({
672
672
  code: getSgmCustomCode(`${SgmCustomCode.IMAGE_LOAD}_${type}`),
673
673
  msg: {
674
674
  avifSupport,
675
675
  webpSupport,
676
676
  shopId,
677
677
  venderId,
678
678
  originSrc: src,
679
679
  requestSrc,
680
680
  resolveUrl,
681
681
  status,
682
682
  text,
683
683
  },
684
684
  })
685
685
  }
686
686
  const imageErrorHandle = useCallback(
687
687
  (e) => {
688
688
  console.log(' ==============> 图片加载错误', e)
689
689
  if (!hasRetryRef.current) {
690
690
  hasRetryRef.current = true
691
691
  if (src) {
692
692
  try {
693
693
  imageErrorRetry(requestSrcRef.current).then((result) => {
694
694
  const { status, ok, text, type, url } = result || {}
695
695
  if (ok) {
696
696
  setImgSrc(url)
697
697
  setHasRetrySuccess(true)
698
698
  } else {
699
699
  errorSrc && setImgSrc(errorSrc)
700
700
  hideErrorImage && setImageErrState(true)
701
701
  typeof onError === 'function' && onError(e, src, props)
702
702
  reportSGM({
703
703
  status,
704
704
  text,
705
705
  type,
706
706
  requestSrc: requestSrcRef.current,
707
707
  })
708
708
  }
709
709
  })
710
710
  } catch (e) {
711
711
  console.error('LazyLoadImage imageErrorHandle() error:', e)
712
712
  errorSrc && setImgSrc(errorSrc)
713
713
  hideErrorImage && setImageErrState(true)
714
714
  typeof onError === 'function' && onError(e, src, props)
715
715
  }
716
716
  }
717
717
  } else {
718
718
  reportSGM({
719
719
  status: '',
720
720
  text: '渲染原始图片渲染异常',
721
721
  type: 'renderOriginImageError',
722
722
  })
723
723
  if (imgSrc.includes('blob:')) {
724
724
  reportSGM({
725
725
  status: '',
726
726
  text: '渲染本地blob图片异常',
727
727
  type: 'renderBlobImageError',
728
728
  })
729
729
  } else {
730
730
  reportSGM({
731
731
  status: '',
732
732
  text: '渲染本地非blob图片异常',
733
733
  type: 'renderNoBlobImageError',
734
734
  })
735
735
  }
736
736
  }
737
737
  },
738
738
  [src, hasRetryRef.current],
739
739
  )
740
740
 
741
741
  const imageLoad = useCallback(
742
742
  (_src, event) => {
743
743
  setLoadSuccess(true)
744
744
  typeof onLoad === 'function' && onLoad(event, src, props)
745
745
  },
746
746
  [src],
747
747
  )
748
748
 
749
749
  const changeStyleIncludeWidthAndHeightAndBgColor = () => {
750
750
  const changeStyle = {}
751
751
  width && (changeStyle['width'] = width)
752
752
  height && (changeStyle['height'] = height)
753
753
  backgroundColor && (changeStyle['backgroundColor'] = backgroundColor)
754
754
  return changeStyle
755
755
  }
756
756
  useEffect(() => {
757
757
  try {
758
758
  const { mode } = otherOption
759
759
  if (mode && mode === 'heightFix') {
760
760
  className?.split(/\s/)?.forEach((item) => {
761
761
  const userDefinedClass = getRuleBySelector('.' + item)
762
762
  const userDefinedHeight = userDefinedClass?.style?.height
763
763
  if (userDefinedHeight === '' || userDefinedHeight === 'auto') {
764
764
  console.warn(
765
765
  "🚗 ~~ 发现一例用户使用图片组件设置了 mode='heightFix' 并且没有定义一个具体高度:",
766
766
  userDefinedHeight,
767
767
  {
768
768
  code: getSgmCustomCode(`imageUsageCheck`),
769
769
  msg: {
770
770
  userDefinedClass: item,
771
771
  userDefinedHeight,
772
772
  },
773
773
  },
774
774
  )
775
775
  sgmCustomReport({
776
776
  code: getSgmCustomCode(`imageUsageCheck`),
777
777
  msg: {
778
778
  userDefinedClass: item,
779
779
  userDefinedHeight,
780
780
  },
781
781
  })
782
782
  }
783
783
  })
784
784
  }
785
785
  } catch (e) {
786
786
  console.warn('imageUsageCheck error:', e)
787
787
  }
788
788
  setMeasureComplete(true)
789
789
  if (needShowHighVersion) return
790
790
  const latestRes =
791
791
  latestFromNativeMsgStorage[TaroEventType.PAGE_SCROLL] || {}
792
792
  !componentShowStateRef.current && dealPageScrollInfo(latestRes)
793
793
  Taro.eventCenter.on(TaroEventType.PAGE_SCROLL, (res) => {
794
794
  !componentShowStateRef.current && dealPageScrollInfo(res)
795
795
  })
796
796
  }, [])
797
797
 
798
798
  const dealPageScrollInfo = (res) => {
799
799
  const { displayHeight, offSetY } = getNativePageScrollRes(res) || {}
800
800
  if (typeof displayHeight === 'undefined' || typeof offSetY === 'undefined')
801
801
  return
802
802
  if (measureRef.current) {
803
803
  const eleClientRect = measureRef.current.getBoundingClientRect()
804
804
  const getContainerHeightOffSetY = displayHeight * 1.5 + offSetY
805
805
  const eleOffsetTop = Math.ceil(eleClientRect.top)
806
806
  const eleOffsetHeight = Math.ceil(eleClientRect.height)
807
807
  if (!componentShowStateRef.current) {
808
808
  if (getContainerHeightOffSetY > eleOffsetTop) {
809
809
  componentShowStateRef.current = true
810
810
  setComponentShowState(true)
811
811
  }
812
812
  }
813
813
  }
814
814
  }
815
815
  return isH5AndJdShopView &&
816
816
  global?.config?.needImageLazy !== false &&
817
817
  !needShowHighVersion &&
818
818
  !isAppStowShop ? (
819
819
  <View
820
820
  ref={measureRef}
821
821
  className={classNames(
822
822
  imageStyle['d-app-lazy-image'],
823
823
  {
824
824
  [imageStyle['d-lazy-sku-image']]: isSkuImage,
825
825
  },
826
826
  {
827
827
  [imageStyle['d-hide-image-error']]: imageErrState,
828
828
  },
829
829
  {
830
830
  [imageStyle['d-load-completed']]: loadSuccess,
831
831
  },
832
832
  {
833
833
  'd-imag-rendering-crisp-edges':
834
834
  !isVipShop && imagRenderingSet,
835
835
  },
836
836
  'J_html5ImageBg',
837
837
  className,
838
838
  )}
839
839
  style={{
840
840
  ...style,
841
841
  ...changeStyleIncludeWidthAndHeightAndBgColor(),
842
842
  }}
843
843
  {...otherOption}
844
844
  >
845
845
  {(componentShowState || lazyLoad === false) && (
846
846
  <img
847
847
  src={getQualityImage(
848
848
  imgSrc,
849
849
  isVipShop
850
850
  ? NetWorkTypeQuality['perfect']
851
851
  : NetWorkTypeQuality[getNetWorkType],
852
852
  )}
853
853
  onLoad={imageLoad.bind(this, imgSrc)}
854
854
  onError={imageErrorHandle}
855
855
  />
856
856
  )}
857
857
  </View>
858
858
  ) : enableAvifOptimize ? (
859
859
  [
860
860
  measureComplete ? (
861
861
  <Image
862
862
  key={hasRetrySuccess ? 'realImageRetry' : 'realImage'}
863
863
  style={{
864
864
  ...style,
865
865
  ...changeStyleIncludeWidthAndHeightAndBgColor(),
866
866
  }}
867
867
  className={classNames(
868
868
  imageStyle['d-lazy-image'],
869
869
  {
870
870
  [imageStyle['d-lazy-sku-image']]: isSkuImage,
871
871
  },
872
872
  {
873
873
  [imageStyle['d-hide-image-error']]: imageErrState,
874
874
  },
875
875
  {
876
876
  [imageStyle['d-load-completed']]: loadSuccess,
877
877
  },
878
878
  {
879
879
  'd-imag-rendering-crisp-edges': imagRenderingSet,
880
880
  },
881
881
  {
882
882
  [imageStyle["d-no-jd-dog"]]: isVipShop && !isSkuImage,
883
883
  },
884
884
  className,
885
885
  )}
886
886
  src={hasRetrySuccess ? imgSrc : getRequestSrc(imgSrc)}
887
887
  lazyLoad={isChartH5 ? false : lazyLoad}
888
888
  onError={imageErrorHandle}
889
889
  onLoad={imageLoad.bind(this, imgSrc)}
890
890
  {...otherOption}
891
891
  />
892
892
  ) : (
893
893
  <Image
894
894
  key="defaultImage"
895
895
  style={{
896
896
  ...style,
897
897
  ...changeStyleIncludeWidthAndHeightAndBgColor(),
898
898
  }}
899
899
  className={classNames(
900
900
  imageStyle['d-lazy-image'],
901
901
  {
902
902
  [imageStyle['d-lazy-sku-image']]: isSkuImage,
903
903
  },
904
904
  {
905
905
  [imageStyle['d-hide-image-error']]: imageErrState,
906
906
  },
907
907
  {
908
908
  [imageStyle['d-load-completed']]: loadSuccess,
909
909
  },
910
910
  {
911
911
  'd-imag-rendering-crisp-edges': imagRenderingSet,
912
912
  },
913
913
  {
914
914
  [imageStyle["d-no-jd-dog"]]: isVipShop && !isSkuImage,
915
915
  },
916
916
  className,
917
917
  )}
918
918
  src={isSkuImage ? DEFAULT_SKU_SRC : DEFAULT_SRC}
919
919
  />
920
920
  ),
921
921
  loadSuccess ? null : <View key="measureRef" ref={measureRef}></View>,
922
922
  ]
923
923
  ) : (
924
924
  <Image
925
925
  style={{
926
926
  ...style,
927
927
  ...changeStyleIncludeWidthAndHeightAndBgColor(),
928
928
  }}
929
929
  className={classNames(
930
930
  imageStyle['d-lazy-image'],
931
931
  {
932
932
  [imageStyle['d-lazy-sku-image']]: isSkuImage,
933
933
  },
934
934
  {
935
935
  [imageStyle['d-hide-image-error']]: imageErrState,
936
936
  },
937
937
  {
938
938
  [imageStyle['d-load-completed']]: loadSuccess,
939
939
  },
940
940
  {
941
941
  'd-imag-rendering-crisp-edges': imagRenderingSet,
942
942
  },
943
943
  className,
944
944
  )}
945
945
  src={getQualityImage(imgSrc, NetWorkTypeQuality[getNetWorkType])}
946
946
  lazyLoad={isChartH5 ? false : lazyLoad}
947
947
  onError={imageErrorHandle}
948
948
  onLoad={imageLoad.bind(this, imgSrc)}
949
949
  {...otherOption}
950
950
  />
951
951
  )