index.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import React, { useCallback, useEffect, useRef, useState } from "react";
  2. import styles from "./index.module.scss";
  3. import classNames from "classnames";
  4. import { A2getGoodsDataType } from "@/types";
  5. import { Input, Pagination } from "antd";
  6. import { SearchOutlined } from "@ant-design/icons";
  7. import { useDispatch, useSelector } from "react-redux";
  8. import { A2_APIgetGoodsList, A2_APIgetSelectData } from "@/store/action/A2Main";
  9. import { RootState } from "@/store";
  10. import { envUrl } from "@/utils/env";
  11. import ImageLazy from "@/components/ImageLazy";
  12. // 轮播图
  13. import { Swiper, SwiperSlide } from "swiper/react";
  14. import { FreeMode } from "swiper";
  15. // Import Swiper styles
  16. import "swiper/css";
  17. import "swiper/css/free-mode";
  18. import Tab4Info from "../Tab4Info";
  19. const typeArr = [
  20. { id: "img", name: "二维文物" },
  21. { id: "model", name: "三维文物" },
  22. ];
  23. function Tab4() {
  24. const dispatch = useDispatch();
  25. // 看看是不是还没有发送请求
  26. const [isDataFlag, setIsDataFlag] = useState(false);
  27. // 发送请求参数
  28. const [getData, setGetData] = useState<A2getGoodsDataType>({
  29. dictAge: "",
  30. dictTexture: "",
  31. searchKey: "",
  32. pageNum: 1,
  33. pageSize: 8,
  34. type: "img",
  35. });
  36. // 发送请求函数
  37. const getList = useCallback(() => {
  38. setIsDataFlag(true);
  39. let searchKey = "";
  40. if (inpValueRef.current) searchKey = inpValueRef.current.input.value;
  41. dispatch(A2_APIgetGoodsList({ ...getData, searchKey }));
  42. }, [dispatch, getData]);
  43. useEffect(() => {
  44. getList();
  45. }, [getList]);
  46. // 输入框
  47. const [inpValue, setInpValue] = useState("");
  48. const inpValueRef = useRef<any>(null);
  49. useEffect(() => {
  50. inpValueRef.current = inpValue;
  51. }, [inpValue]);
  52. useEffect(() => {
  53. // 发送请求拿到下拉框数据
  54. // dispatch(A2_APIgetSelectData("age"));
  55. dispatch(A2_APIgetSelectData("texture"));
  56. }, [dispatch]);
  57. // 获取下拉数据
  58. const selectData = useSelector(
  59. (state: RootState) => state.A2Main.selectData.texture
  60. );
  61. // 从仓库获取数据
  62. const goodsList = useSelector((state: RootState) => state.A2Main.goodsList);
  63. // 详情页 id
  64. const [infoId, setInfoId] = useState(0);
  65. return (
  66. <div
  67. className={styles.Tab4}
  68. style={{ backgroundImage: `url(${envUrl}/goods/bac.jpg)` }}
  69. >
  70. <div className="tab4Top">
  71. {/* 二维 三维的筛选 */}
  72. {typeArr.map((v) => (
  73. <div
  74. key={v.id}
  75. className={classNames(
  76. "tab4Top1",
  77. v.id === getData.type ? "tab4Top1Ac" : ""
  78. )}
  79. onClick={() => setGetData({ ...getData, type: v.id, pageNum: 1 })}
  80. >
  81. {v.name}
  82. </div>
  83. ))}
  84. {/* 输入框 */}
  85. <div className="tab4Top2">
  86. <div className="tab4Top2_1">
  87. <SearchOutlined />
  88. </div>
  89. <Input
  90. placeholder="请输入关键词"
  91. maxLength={30}
  92. value={inpValue}
  93. onChange={(e) => setInpValue(e.target.value.trim())}
  94. ref={inpValueRef}
  95. />
  96. <div
  97. className="tab4Top2_2"
  98. onClick={() => {
  99. if (getData.pageNum === 1) getList();
  100. else setGetData({ ...getData, pageNum: 1 });
  101. }}
  102. >
  103. 搜索
  104. </div>
  105. </div>
  106. </div>
  107. {/* 类型的筛选 */}
  108. <div className="tab4Top2">
  109. <Swiper
  110. modules={[FreeMode]}
  111. className="appSw"
  112. spaceBetween={0}
  113. slidesPerView="auto"
  114. freeMode={true}
  115. mousewheel={true}
  116. // onSlideChange={(e) => console.log("slide change", e)}
  117. // onSwiper={(swiper) => (mySwiperRef.current = swiper)}
  118. >
  119. {selectData.map((v) => (
  120. <SwiperSlide key={v.label}>
  121. <div
  122. onClick={(e) =>
  123. setGetData({
  124. ...getData,
  125. dictTexture: v.value as string,
  126. pageNum: 1,
  127. })
  128. }
  129. className={classNames(
  130. "row",
  131. getData.dictTexture === v.value ? "active" : ""
  132. )}
  133. >
  134. <span>{v.label}</span>
  135. </div>
  136. </SwiperSlide>
  137. ))}
  138. </Swiper>
  139. </div>
  140. {/* 主要内容 */}
  141. {goodsList.list.length <= 0 && isDataFlag ? (
  142. <div className="tav4MainNone">暂无数据</div>
  143. ) : (
  144. <div className="tab4Main">
  145. {goodsList.list.map((v) => (
  146. <div
  147. className="tab4Mrow"
  148. key={v.id}
  149. title={v.name}
  150. onClick={() => setInfoId(v.id)}
  151. >
  152. <div className="tab4MrowImg">
  153. <ImageLazy src={v.thumb} noLook width="100%" height="100%" />
  154. </div>
  155. <p>{v.name}</p>
  156. </div>
  157. ))}
  158. </div>
  159. )}
  160. {/* 分页器 */}
  161. <div className="tab4Page">
  162. <Pagination
  163. showQuickJumper
  164. current={getData.pageNum}
  165. total={goodsList.total}
  166. pageSize={15}
  167. hideOnSinglePage={true}
  168. onChange={(page) => setGetData({ ...getData, pageNum: page })}
  169. showSizeChanger={false}
  170. />
  171. </div>
  172. {/* 点击文物进入详情页 */}
  173. {infoId ? (
  174. <Tab4Info id={infoId} closePage={() => setInfoId(0)} isOpen={false} />
  175. ) : null}
  176. </div>
  177. );
  178. }
  179. const MemoTab4 = React.memo(Tab4);
  180. export default MemoTab4;