index.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import React, { useCallback, useEffect, useMemo, useState } from "react";
  2. import styles from "./index.module.scss";
  3. import H5Logo from "@/assets/img/H5Logo.png";
  4. import { useLocation } from "react-router";
  5. import { urlParameter } from "@/utils/history";
  6. import { useDispatch, useSelector } from "react-redux";
  7. import { getH5InfoAPI } from "@/store/action/h5code";
  8. import { RootState } from "@/store";
  9. import { baseURL } from "@/utils/http";
  10. import classNames from "classnames";
  11. import html2canvas from "html2canvas";
  12. import { Spin } from "antd";
  13. function H5Code() {
  14. useEffect(() => {
  15. const root: any = document.querySelector("#root");
  16. root.style.overflow = "hidden";
  17. root.style.overflow = "hidden";
  18. root.style.minHeight = "100vh";
  19. root.style.minWidth = "100vw";
  20. document.title = "徐州汉画像石艺术馆";
  21. }, []);
  22. // 获取地址栏参数
  23. const dispatch = useDispatch();
  24. const location = useLocation();
  25. useEffect(() => {
  26. const obj = urlParameter(location.search);
  27. if (obj.id) dispatch(getH5InfoAPI(obj.id));
  28. }, [dispatch, location.search]);
  29. // 从仓库获取详情
  30. const info = useSelector((state: RootState) => state.h5codeReducer.h5info);
  31. // 点击下载海报
  32. const [donImg, setDonImg] = useState(false);
  33. const dataURLToBlob = useCallback((dataurl: any) => {
  34. let arr = dataurl.split(",");
  35. let mime = arr[0].match(/:(.*?);/)[1];
  36. let bstr = atob(arr[1]);
  37. let n = bstr.length;
  38. let u8arr = new Uint8Array(n);
  39. while (n--) {
  40. u8arr[n] = bstr.charCodeAt(n);
  41. }
  42. return new Blob([u8arr], { type: mime });
  43. }, []);
  44. const donImgFu = useCallback(() => {
  45. setDonImg(true);
  46. window.setTimeout(() => {
  47. const canEle: HTMLDivElement = document.querySelector(".H5MainBoxInfo")!; //获取dom
  48. let a = document.createElement("a");
  49. html2canvas(canEle).then((canvas) => {
  50. let dom = document.body.appendChild(canvas);
  51. dom.style.display = "none";
  52. a.style.display = "none";
  53. document.body.removeChild(dom);
  54. let blob: any = dataURLToBlob(dom.toDataURL("image/png"));
  55. a.setAttribute("href", URL.createObjectURL(blob));
  56. //这块是保存图片操作 可以设置保存的图片的信息
  57. a.setAttribute("download", info.name + ".png");
  58. document.body.appendChild(a);
  59. a.click();
  60. URL.revokeObjectURL(blob);
  61. document.body.removeChild(a);
  62. setDonImg(false);
  63. });
  64. }, 100);
  65. }, [dataURLToBlob, info.name]);
  66. const infoTxt = useMemo(() => {
  67. let txt = info.description ? info.description : "-";
  68. if (txt.length >= 60 && donImg) txt = txt.substring(0, 60) + "...";
  69. return txt;
  70. }, [donImg, info.description]);
  71. return (
  72. <div className={styles.H5Code}>
  73. <div className="H5MainBox">
  74. <div className="H5MainBoxInfo">
  75. <div className="H5Img">
  76. {info.thumb ? <img src={baseURL + info.thumb} alt="" /> : null}
  77. </div>
  78. {/* 占位盒子 */}
  79. <div className="H5Loc"></div>
  80. {/* 文字信息 */}
  81. <div className="H5TxtBox">
  82. {/* 名字 */}
  83. <div className="name">
  84. <div>{info.name}</div>
  85. </div>
  86. {/* 简介信息 */}
  87. <div className="txtMain">
  88. <p>类别:{info.dictTexture}</p>
  89. <p>年代:{info.dictAge}</p>
  90. <p>级别:{info.dictLevel}</p>
  91. <div>简介:{infoTxt}</div>
  92. </div>
  93. </div>
  94. {/* logo */}
  95. <div className="H5Logo">
  96. <img src={H5Logo} alt="" />
  97. </div>
  98. </div>
  99. <div className="H5MainBoxButton">
  100. <div onClick={donImgFu}>下载海报</div>
  101. </div>
  102. </div>
  103. {/* 加载中的背景 */}
  104. <div
  105. className={classNames(
  106. "H5lodingBacBox",
  107. donImg ? "H5lodingBacBoxShow" : ""
  108. )}
  109. >
  110. <Spin size="large" />
  111. </div>
  112. </div>
  113. );
  114. }
  115. const MemoH5Code = React.memo(H5Code);
  116. export default MemoH5Code;