index.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import React, {
  2. useCallback,
  3. useEffect,
  4. useMemo,
  5. useRef,
  6. useState,
  7. } from "react";
  8. import styles from "./index.module.scss";
  9. import H5Logo from "@/assets/img/H5Logo.png";
  10. import { useLocation } from "react-router";
  11. import { urlParameter } from "@/utils/history";
  12. import { useDispatch, useSelector } from "react-redux";
  13. import { getH5InfoAPI } from "@/store/action/h5code";
  14. import { RootState } from "@/store";
  15. import { baseURL } from "@/utils/http";
  16. import classNames from "classnames";
  17. import html2canvas from "html2canvas";
  18. import { Spin } from "antd";
  19. function H5Code() {
  20. const timeRef = useRef(-1);
  21. useEffect(() => {
  22. const root: any = document.querySelector("#root");
  23. root.style.overflow = "hidden";
  24. root.style.overflow = "hidden";
  25. root.style.minHeight = "100vh";
  26. root.style.minWidth = "100vw";
  27. document.title = "徐州汉画像石艺术馆";
  28. const dom: any = document.querySelector("#H5Code");
  29. dom.style.height = window.innerHeight + "px";
  30. window.addEventListener(
  31. "resize",
  32. () => {
  33. if (dom) {
  34. clearTimeout(timeRef.current);
  35. timeRef.current = window.setTimeout(() => {
  36. dom.style.height = window.innerHeight + "px";
  37. }, 500);
  38. }
  39. },
  40. true
  41. );
  42. }, []);
  43. // 获取地址栏参数
  44. const dispatch = useDispatch();
  45. const location = useLocation();
  46. useEffect(() => {
  47. const obj = urlParameter(location.search);
  48. if (obj.id) dispatch(getH5InfoAPI(obj.id));
  49. }, [dispatch, location.search]);
  50. // 从仓库获取详情
  51. const info = useSelector((state: RootState) => state.h5codeReducer.h5info);
  52. // 点击下载海报
  53. const [donImg, setDonImg] = useState(false);
  54. const dataURLToBlob = useCallback((dataurl: any) => {
  55. let arr = dataurl.split(",");
  56. let mime = arr[0].match(/:(.*?);/)[1];
  57. let bstr = atob(arr[1]);
  58. let n = bstr.length;
  59. let u8arr = new Uint8Array(n);
  60. while (n--) {
  61. u8arr[n] = bstr.charCodeAt(n);
  62. }
  63. return new Blob([u8arr], { type: mime });
  64. }, []);
  65. const donImgFu = useCallback(() => {
  66. setDonImg(true);
  67. window.setTimeout(() => {
  68. const canEle: HTMLDivElement = document.querySelector(".H5MainBoxInfo")!; //获取dom
  69. let a = document.createElement("a");
  70. html2canvas(canEle, {
  71. backgroundColor: "transparent",
  72. allowTaint: true,
  73. useCORS: true,
  74. }).then((canvas) => {
  75. let dom = document.body.appendChild(canvas);
  76. dom.style.display = "none";
  77. a.style.display = "none";
  78. document.body.removeChild(dom);
  79. let blob: any = dataURLToBlob(dom.toDataURL("image/jpeg"));
  80. a.setAttribute("href", URL.createObjectURL(blob));
  81. //这块是保存图片操作 可以设置保存的图片的信息
  82. a.setAttribute("download", info.name + ".jpg");
  83. document.body.appendChild(a);
  84. a.click();
  85. URL.revokeObjectURL(blob);
  86. document.body.removeChild(a);
  87. setDonImg(false);
  88. });
  89. }, 100);
  90. }, [dataURLToBlob, info.name]);
  91. const infoTxt = useMemo(() => {
  92. let txt = info.description ? info.description : "-";
  93. const dom = document.querySelector(".txtMain>div");
  94. if (dom) {
  95. // 获取简介盒子的宽高,判断能装多少字符
  96. const width = dom.clientWidth;
  97. const height = dom.clientHeight;
  98. // 计算最多多少列
  99. const maxH = Math.floor(height / 20);
  100. // 一个字的面积
  101. // const oneZ = 18 * 20;
  102. // 盒子宽度下一行最多显示多少字
  103. const maxW = Math.floor(width / 16);
  104. // 这个盒子最多装多少字
  105. const maxString = maxH * maxW;
  106. // console.log("------", maxString);
  107. if (txt.length >= maxString && donImg)
  108. txt = txt.substring(0, maxString) + "...";
  109. }
  110. return txt;
  111. }, [donImg, info.description]);
  112. return (
  113. <div className={styles.H5Code} id="H5Code">
  114. <div className="H5MainBox">
  115. <div className="H5MainBoxInfo">
  116. <div className="H5Img">
  117. {info.thumb ? <img src={baseURL + info.thumb} alt="" /> : null}
  118. </div>
  119. {/* 占位盒子 */}
  120. <div className="H5Loc"></div>
  121. {/* 文字信息 */}
  122. <div className="H5TxtBox">
  123. {/* 名字 */}
  124. <div className="name">
  125. <div>{info.name}</div>
  126. </div>
  127. {/* 简介信息 */}
  128. <div className="txtMain">
  129. <p>类别:{info.dictTexture}</p>
  130. <p>年代:{info.dictAge}</p>
  131. <p>级别:{info.dictLevel}</p>
  132. <div>简介:{infoTxt}</div>
  133. </div>
  134. </div>
  135. {/* logo */}
  136. <div className="H5Logo">
  137. <img src={H5Logo} alt="" />
  138. </div>
  139. </div>
  140. <div className="H5MainBoxButton">
  141. <div onClick={donImgFu}>下载海报</div>
  142. </div>
  143. </div>
  144. {/* 加载中的背景 */}
  145. <div
  146. className={classNames(
  147. "H5lodingBacBox",
  148. donImg ? "H5lodingBacBoxShow" : ""
  149. )}
  150. >
  151. <Spin size="large" />
  152. </div>
  153. </div>
  154. );
  155. }
  156. const MemoH5Code = React.memo(H5Code);
  157. export default MemoH5Code;