index.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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/login/loading.gif'
  5. import imgErr from '@/assets/img/login/IMGerror.png'
  6. import { EyeOutlined } from '@ant-design/icons'
  7. import { useDispatch } from 'react-redux'
  8. import { Image } from 'antd-mobile'
  9. type Props = {
  10. width?: number
  11. height?: number
  12. src: string
  13. srcDa: string
  14. noLook?: boolean
  15. }
  16. function ImageLazy({ width = 100, height = 100, src, noLook, srcDa }: Props) {
  17. const dispatch = useDispatch()
  18. // 默认不能预览图片,加载成功之后能预览
  19. const [lookImg, setLookImg] = useState(false)
  20. // 图片加载完成
  21. const onLoad = useCallback(() => {
  22. setLookImg(true)
  23. }, [])
  24. // 点击预览图片
  25. const lookBigImg = useCallback(() => {
  26. dispatch({
  27. type: 'login/lookBigImg',
  28. payload: { url: baseURL + (srcDa ? srcDa : src), show: true }
  29. })
  30. }, [dispatch, src, srcDa])
  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 ? 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 />
  46. &nbsp;
  47. <div>预览</div>
  48. </div>
  49. )}
  50. </div>
  51. </div>
  52. )
  53. }
  54. const MemoImageLazy = React.memo(ImageLazy)
  55. export default MemoImageLazy