hot_category.tsx 5.1 KB

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