index.tsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. noLook?: boolean;
  14. offline?: boolean;
  15. };
  16. function ImageLazy({
  17. width = 100,
  18. height = 100,
  19. src,
  20. noLook,
  21. offline = false,
  22. }: Props) {
  23. // 默认不能预览图片,加载成功之后能预览
  24. const [lookImg, setLookImg] = useState(false);
  25. // 图片加载完成
  26. const onLoad = useCallback(() => {
  27. setLookImg(true);
  28. }, []);
  29. // 点击预览图片
  30. const lookBigImg = useCallback(() => {
  31. store.dispatch({
  32. type: "layout/lookBigImg",
  33. payload: { url: offline ? src : baseURL + src, show: true },
  34. });
  35. }, [offline, src]);
  36. return (
  37. <div className={styles.ImageLazy} style={{ width: width, height: height }}>
  38. <div className="lazyBox">
  39. <Image
  40. lazy
  41. onLoad={onLoad}
  42. src={src ? (offline ? src : baseURL + src) : ""}
  43. placeholder={<img src={imgLoding} alt="" />}
  44. fallback={<img src={imgErr} alt="" />}
  45. fit="cover"
  46. />
  47. {/* 图片预览 */}
  48. {noLook || !lookImg ? null : (
  49. <div className="lookImg" onClick={lookBigImg}>
  50. <EyeOutlined rev={undefined} />
  51. &nbsp;
  52. <div>预览</div>
  53. </div>
  54. )}
  55. </div>
  56. </div>
  57. );
  58. }
  59. const MemoImageLazy = React.memo(ImageLazy);
  60. export default MemoImageLazy;