index.tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import React, { useCallback, useState } from 'react'
  2. import styles from './index.module.scss'
  3. import { baseURL } from '@/utils/http'
  4. import imgLoding from '@/assets/img/loading.gif'
  5. import imgErr from '@/assets/img/IMGerror.png'
  6. import { EyeOutlined } from '@ant-design/icons'
  7. import store from '@/store'
  8. import { Image } from 'antd-mobile'
  9. type Props = {
  10. width?: number | string
  11. height?: number | string
  12. src: string
  13. srcBig?: string
  14. noLook?: boolean
  15. offline?: boolean
  16. }
  17. function ImageLazy({ width = 100, height = 100, src, srcBig, noLook, offline = false }: Props) {
  18. // 默认不能预览图片,加载成功之后能预览
  19. const [lookImg, setLookImg] = useState(false)
  20. // 图片加载完成
  21. const onLoad = useCallback(() => {
  22. setLookImg(true)
  23. }, [])
  24. // 点击预览图片
  25. const lookBigImg = useCallback(() => {
  26. store.dispatch({
  27. type: 'layout/lookBigImg',
  28. payload: { url: offline ? src : srcBig ? baseURL + srcBig : baseURL + src, show: true }
  29. })
  30. }, [offline, src, srcBig])
  31. return (
  32. <div className={styles.ImageLazy} style={{ width: width, height: height }}>
  33. <div className='lazyBox'>
  34. <Image
  35. lazy
  36. onLoad={onLoad}
  37. src={src ? (offline ? src : baseURL + src) : ''}
  38. placeholder={<img src={imgLoding} alt='' />}
  39. fallback={<img src={imgErr} alt='' />}
  40. fit='cover'
  41. />
  42. {/* 图片预览 */}
  43. {noLook || !lookImg ? null : (
  44. <div className='lookImg' onClick={lookBigImg}>
  45. <EyeOutlined rev={undefined} />
  46. &nbsp;
  47. <div>预览</div>
  48. </div>
  49. )}
  50. </div>
  51. </div>
  52. )
  53. }
  54. const MemoImageLazy = React.memo(ImageLazy)
  55. export default MemoImageLazy