|
@@ -1,12 +1,11 @@
|
|
-import React, { useEffect, useRef, useState } from "react";
|
|
|
|
|
|
+import React, { useCallback, useEffect, useState } from "react";
|
|
import styles from "./index.module.scss";
|
|
import styles from "./index.module.scss";
|
|
import Lazyimg from "react-lazyimg-component";
|
|
import Lazyimg from "react-lazyimg-component";
|
|
import { baseURL } from "@/utils/http";
|
|
import { baseURL } from "@/utils/http";
|
|
import imgLoding from "@/assets/img/login/loading.gif";
|
|
import imgLoding from "@/assets/img/login/loading.gif";
|
|
import imgErr from "@/assets/img/login/IMGerror.png";
|
|
import imgErr from "@/assets/img/login/IMGerror.png";
|
|
-import classNames from "classnames";
|
|
|
|
import { EyeOutlined } from "@ant-design/icons";
|
|
import { EyeOutlined } from "@ant-design/icons";
|
|
-import { Image } from "antd";
|
|
|
|
|
|
+import { useDispatch } from "react-redux";
|
|
|
|
|
|
type Props = {
|
|
type Props = {
|
|
width?: number;
|
|
width?: number;
|
|
@@ -16,74 +15,70 @@ type Props = {
|
|
};
|
|
};
|
|
|
|
|
|
function ImageLazy({ width = 100, height = 100, src, noLook }: Props) {
|
|
function ImageLazy({ width = 100, height = 100, src, noLook }: Props) {
|
|
- const [defaultUrl, setDefaultUrl] = useState(src ? imgLoding : imgErr);
|
|
|
|
- const [defaultShow, setDefaultShow] = useState(true);
|
|
|
|
|
|
+ const dispatch = useDispatch();
|
|
|
|
|
|
- const timeId = useRef(-1);
|
|
|
|
|
|
+ // 图片占位符
|
|
|
|
+ const [placeholderUrl, setPlaceholderUrl] = useState(
|
|
|
|
+ src ? imgLoding : imgErr
|
|
|
|
+ );
|
|
|
|
+
|
|
|
|
+ // 默认不能预览图片,加载成功之后能预览
|
|
|
|
+ const [lookImg, setLookImg] = useState(false);
|
|
|
|
|
|
useEffect(() => {
|
|
useEffect(() => {
|
|
- // 组件销毁的时候清理定时器
|
|
|
|
- return () => {
|
|
|
|
- clearTimeout(timeId.current);
|
|
|
|
- };
|
|
|
|
- }, []);
|
|
|
|
|
|
+ if (src) {
|
|
|
|
+ // 进页面查看图片的加载情况
|
|
|
|
+ // 创建一个img标签
|
|
|
|
+ const imgDom = document.createElement("img");
|
|
|
|
+ imgDom.src = baseURL + src;
|
|
|
|
+
|
|
|
|
+ // 不管图片加载成功或者失败,都删除掉,提高性能
|
|
|
|
+ // 图片加载成功
|
|
|
|
+ imgDom.onload = function () {
|
|
|
|
+ setLookImg(true);
|
|
|
|
+ imgDom.remove();
|
|
|
|
+ };
|
|
|
|
+ // 图片加载失败
|
|
|
|
+ imgDom.onerror = function () {
|
|
|
|
+ setPlaceholderUrl(imgErr);
|
|
|
|
+ imgDom.remove();
|
|
|
|
+ };
|
|
|
|
|
|
- const onLoad = (val: any) => {
|
|
|
|
- let flag = false;
|
|
|
|
- // console.log(val);
|
|
|
|
- val.target.onload = function () {
|
|
|
|
- setDefaultShow(false);
|
|
|
|
- flag = true;
|
|
|
|
- };
|
|
|
|
- // 5秒后图片还没有加载出来为错误
|
|
|
|
- timeId.current = window.setTimeout(() => {
|
|
|
|
- if (!flag) setDefaultUrl(imgErr);
|
|
|
|
- }, 5000);
|
|
|
|
- };
|
|
|
|
|
|
+ return () => {
|
|
|
|
+ // 离开页面也删掉掉元素
|
|
|
|
+ imgDom.remove();
|
|
|
|
+ };
|
|
|
|
+ }
|
|
|
|
+ }, [src]);
|
|
|
|
|
|
// 点击预览图片
|
|
// 点击预览图片
|
|
- const [visible, setVisible] = useState(false);
|
|
|
|
|
|
+ const lookBigImg = useCallback(() => {
|
|
|
|
+ dispatch({
|
|
|
|
+ type: "login/lookBigImg",
|
|
|
|
+ payload: { url: baseURL + src, show: true },
|
|
|
|
+ });
|
|
|
|
+ }, [dispatch, src]);
|
|
|
|
|
|
return (
|
|
return (
|
|
<div className={styles.ImageLazy} style={{ width: width, height: height }}>
|
|
<div className={styles.ImageLazy} style={{ width: width, height: height }}>
|
|
<div className="lazyBox">
|
|
<div className="lazyBox">
|
|
- {src ? (
|
|
|
|
- <Lazyimg
|
|
|
|
- onLoad={onLoad}
|
|
|
|
- src={baseURL + src}
|
|
|
|
- width={width}
|
|
|
|
- height={height}
|
|
|
|
- alt=""
|
|
|
|
- />
|
|
|
|
- ) : null}
|
|
|
|
|
|
+ <Lazyimg
|
|
|
|
+ src={src ? baseURL + src : ""}
|
|
|
|
+ width={width}
|
|
|
|
+ height={height}
|
|
|
|
+ placeholder={placeholderUrl}
|
|
|
|
+ alt=""
|
|
|
|
+ />
|
|
|
|
|
|
{/* 图片预览 */}
|
|
{/* 图片预览 */}
|
|
- {noLook ? null : (
|
|
|
|
- <div className="lookImg" onClick={() => setVisible(true)}>
|
|
|
|
|
|
+ {noLook || !lookImg ? null : (
|
|
|
|
+ <div className="lookImg" onClick={lookBigImg}>
|
|
<EyeOutlined />
|
|
<EyeOutlined />
|
|
|
|
|
|
<div>预览</div>
|
|
<div>预览</div>
|
|
</div>
|
|
</div>
|
|
)}
|
|
)}
|
|
</div>
|
|
</div>
|
|
-
|
|
|
|
- <img
|
|
|
|
- className={classNames("showImg", defaultShow ? "active" : "")}
|
|
|
|
- src={defaultUrl}
|
|
|
|
- alt=""
|
|
|
|
- />
|
|
|
|
-
|
|
|
|
- {/* 预览图片 */}
|
|
|
|
- <Image
|
|
|
|
- style={{ display: "none" }}
|
|
|
|
- preview={{
|
|
|
|
- visible,
|
|
|
|
- src: baseURL + src,
|
|
|
|
- onVisibleChange: (value) => {
|
|
|
|
- setVisible(value);
|
|
|
|
- },
|
|
|
|
- }}
|
|
|
|
- />
|
|
|
|
</div>
|
|
</div>
|
|
);
|
|
);
|
|
}
|
|
}
|