hot_category.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { BetaSchemaForm, useToken } from "@ant-design/pro-components";
  2. import styles from './index.less'
  3. import Header from "./header";
  4. import { useModel } from "@umijs/max";
  5. import { Key, useEffect, useMemo, useState } from "react";
  6. import { Config } from "@/models/appPageConifg";
  7. import { useAjax } from "@/Hook/useAjax";
  8. import useApi from "@/Hook/useApi";
  9. import { Empty } from "antd";
  10. // 热门书
  11. export function HotCategory(props: {
  12. data: {
  13. appComponentId: number | string;
  14. componentType: string;
  15. showRightButton?: boolean;
  16. componentName?: string;
  17. remark?: string;
  18. configs?: any[];
  19. }
  20. }) {
  21. const { token } = useToken()
  22. const [tag, setTag] = useState<any>(null)
  23. let { initialState } = useModel("@@initialState")
  24. let { state } = useModel("appPageConifg")
  25. const { data: { configs, componentName, showRightButton } } = props
  26. const [bookData, setBookData] = useState([])
  27. let publicData = useMemo(() => {
  28. return {
  29. appId: initialState?.selectApp?.id || "",
  30. distributorId: initialState?.currentUser?.distributorId,
  31. appType: initialState?.selectApp?.appType || "",
  32. workDirection: state.tabs
  33. }
  34. }, [initialState?.selectApp, initialState?.currentUser?.distributorId, state.index])
  35. let apiobj = useApi(initialState?.selectApp?.appCategory || 1)
  36. let getList = useAjax((params) => apiobj.getBookPageList(params))
  37. useEffect(() => {
  38. if (!tag) {
  39. setTag(configs?.[0]?.categoryId)
  40. }
  41. if (tag) {
  42. getList.run({ ...publicData, categoryId: tag, pageNum: 1, pageSize: 8 }).then(res => {
  43. if (res.code === 200) {
  44. setBookData(res?.data?.records)
  45. }
  46. })
  47. }
  48. if (configs) {
  49. let isExist = configs?.find(i => i.categoryId == tag)
  50. if (!isExist) {
  51. setTag(configs?.[0]?.categoryId)
  52. getList.run({ ...publicData, categoryId: configs?.[0]?.categoryId, pageNum: 1, pageSize: 8 }).then(res => {
  53. if (res.code === 200) {
  54. setBookData(res?.data?.records)
  55. }
  56. })
  57. }
  58. if (configs.length === 0) {
  59. setTag(null)
  60. setBookData([])
  61. }
  62. }
  63. }, [tag, configs, publicData])
  64. return <div className={`${styles.hot_category}`}>
  65. <Header title={componentName || "热门分类"} btnText={"全部分类"} showBtn={showRightButton} />
  66. <div className={styles.tags}>
  67. {
  68. configs?.map(item => {
  69. return <div
  70. className={`${styles.tag} ${tag === item.categoryId ? styles.tag_ac : ""}`}
  71. key={item.categoryId}
  72. onClick={() => {
  73. setTag(item.categoryId || "")
  74. }}
  75. >
  76. {item?.categoryInfo?.name}
  77. </div>
  78. })
  79. }
  80. </div>
  81. <div className={styles.list}>
  82. {
  83. configs?.find(i => i.categoryId == tag)?.bookIds ? configs?.find(i => i.categoryId == tag)?.bookInfos?.map((item: any) => {
  84. return <div key={item.id} className={styles.box}>
  85. <img src={item?.picUrl} onError={(e: any) => {
  86. e.target.src = localStorage.getItem("nocover")
  87. }} />
  88. <span>{item?.bookName}</span>
  89. </div>
  90. }) : bookData?.length > 0 ? bookData?.map((item: any) => {
  91. return <div key={item} className={styles.box}>
  92. <img src={item?.longBookInfo?.picUrl || item?.shortBookInfoVO?.picUrl} onError={(e: any) => {
  93. e.target.src = localStorage.getItem("nocover")
  94. }} />
  95. <span>{item?.longBookInfo?.bookName || item?.shortBookInfoVO?.bookName}</span>
  96. </div>
  97. }) : <div style={{justifyContent:'center',display:'flex',width:'100%'}}><Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description="暂无数据请选中后设置"/></div>
  98. }
  99. </div>
  100. </div>
  101. }