index.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import { baseURL } from '@/utils/http'
  2. import classNames from 'classnames'
  3. import styles from './index.module.scss'
  4. import logoImg from '@/assets/img/logo.png'
  5. import { Input, Pagination, Select } from 'antd'
  6. import { SearchOutlined, ExclamationOutlined, CloseCircleOutlined } from '@ant-design/icons';
  7. import { useEffect, useRef, useState } from 'react'
  8. import { LazyLoadImage } from "react-lazy-load-image-component";
  9. import { useDispatch, useSelector } from 'react-redux';
  10. import { getListAction, getSortAction, getAgeAction } from '@/store/action/home';
  11. import { RootState } from '@/store';
  12. import Model from '@/components/Model'
  13. export default function Home() {
  14. const dispatch = useDispatch()
  15. // 进页面获取后端文物数据
  16. useEffect(() => {
  17. dispatch(getSortAction())
  18. dispatch(getAgeAction())
  19. }, [dispatch])
  20. // 滚动盒子ref
  21. const modelBoxRef = useRef<HTMLDivElement>(null)
  22. // 输入框变量
  23. const [value, setValue] = useState('')
  24. // 清空输入框
  25. const clearValueFu = () => {
  26. setValue('')
  27. setPageNum(1)
  28. const sroolDom = modelBoxRef.current
  29. sroolDom!.scrollTop = 0
  30. dispatch(getListAction({
  31. ...fromData.current, dictTextureId: fromData.current.dictTextureId === -1 ? null : fromData.current.dictTextureId,
  32. searchKey: '', pageNum: 1,
  33. dictAgeId: fromData.current.dictAgeId === -1 ? null : fromData.current.dictAgeId,
  34. }))
  35. }
  36. const searchKeyUpFu = (e: React.KeyboardEvent<HTMLInputElement>) => {
  37. if (e.key === 'Enter') searchBtnFu()
  38. }
  39. const searchBtnFu = () => {
  40. getModelList()
  41. }
  42. // 分类的数据和高亮
  43. const { sortList, modelInfo, ageList } = useSelector((state: RootState) => state.homeStore)
  44. const { list, total } = modelInfo
  45. // 分类
  46. const [sort, setSort] = useState(-1)
  47. // 分类下拉框改变
  48. const sortChangeFu = (value: number) => {
  49. setSort(value)
  50. }
  51. // 年代
  52. const [ageSerach, setAgeSerach] = useState(-1)
  53. // 年代下拉框改变
  54. const ageChangeFu = (value: number) => {
  55. setAgeSerach(value)
  56. fromData.current.dictAgeId = value
  57. fromData.current.pageNum = 1
  58. setPageNum(1)
  59. getModelList()
  60. }
  61. const [pageNum, setPageNum] = useState(1)
  62. const fromData = useRef({
  63. pageNum: 1,
  64. pageSize: 12,
  65. searchKey: '',
  66. dictTextureId: -1,
  67. dictAgeId: -1
  68. })
  69. const pageChangeFu = (page: number) => {
  70. fromData.current.pageNum = page
  71. setPageNum(page)
  72. getModelList()
  73. }
  74. // 封装一个发送请求获取列表的函数
  75. const getModelList = () => {
  76. const sroolDom = modelBoxRef.current
  77. sroolDom!.scrollTop = 0
  78. dispatch(getListAction({
  79. ...fromData.current, dictTextureId: fromData.current.dictTextureId === -1 ? null : fromData.current.dictTextureId, searchKey: value,
  80. dictAgeId: fromData.current.dictAgeId === -1 ? null : fromData.current.dictAgeId,
  81. }))
  82. }
  83. useEffect(() => {
  84. fromData.current.dictTextureId = sort
  85. fromData.current.pageNum = 1
  86. setPageNum(1)
  87. getModelList()
  88. // eslint-disable-next-line react-hooks/exhaustive-deps
  89. }, [sort])
  90. // 点击每个模型出来的页面
  91. const [modelId, setModelId] = useState(0)
  92. return (
  93. <div className={styles.Home}>
  94. {/* 点击单个模型出来的详情页面 */}
  95. <div className={classNames('modelTxtBox', modelId ? 'activeTxtBox' : '')}>
  96. <Model modelId={modelId} closeModel={() => setModelId(0)} />
  97. </div>
  98. <div className='title'>
  99. <div className='logo'>
  100. <img src={logoImg} alt="" />
  101. </div>
  102. </div>
  103. <div className='mainBox'>
  104. {/* 筛选的盒子 */}
  105. <div className='screenBox'>
  106. {/* 输入框 */}
  107. <div className='search'>
  108. <div className='searchBtn' onClick={searchBtnFu}></div>
  109. {value.length > 0 ? (<div className='searchClear' onClick={clearValueFu}>
  110. <CloseCircleOutlined />
  111. </div>) : null}
  112. <Input maxLength={10} placeholder='请输入文物名称' value={value} onChange={(e) => setValue(e.target.value)} onKeyUp={(e) => searchKeyUpFu(e)} prefix={<SearchOutlined />} />
  113. </div>
  114. {/* 分类下拉框 */}
  115. <div className='sortBox'>
  116. <Select style={{ width: '100%' }} value={sort} onChange={sortChangeFu} options={sortList} />
  117. </div>
  118. {/* 年代筛选下拉框 */}
  119. <div className='ageSeachBox'>
  120. <Select style={{ width: '100%' }} value={ageSerach} onChange={ageChangeFu} options={ageList} />
  121. </div>
  122. </div>
  123. {/* 页面渲染主体 */}
  124. <div className='modelBox' ref={modelBoxRef}>
  125. {
  126. list && list.length > 0 ?
  127. list.map(item => (
  128. <div className='modelBox_row' key={item.id} onClick={() => setModelId(item.id)}>
  129. {/* 鼠标悬浮出来的盒子 */}
  130. <div className='modelBox_row_hover'>{item.name}</div>
  131. <LazyLoadImage src={baseURL + item.thumb}
  132. width={300} height={260}
  133. alt=""
  134. />
  135. <p>
  136. {item.dictTextureName ? <span className='row_txt1'>{item.dictTextureName}</span> : null}
  137. <span className='row_txt2'>{item.name}</span>
  138. </p>
  139. </div>
  140. ))
  141. :
  142. <div className='noneList'>
  143. <div className='incoBox'>
  144. <ExclamationOutlined />
  145. </div>
  146. <p>暂无内容</p>
  147. </div>
  148. }
  149. </div>
  150. {/* 分页器 */}
  151. <div className='page'>
  152. <Pagination size="small" current={pageNum} total={total} pageSize={12} hideOnSinglePage={true} onChange={pageChangeFu} />
  153. </div>
  154. </div>
  155. </div>
  156. )
  157. }