Card.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. "use client";
  2. import { GameListRep, getGameDetailApi } from "@/api/home";
  3. import Box from "@/components/Box";
  4. import { useRouter } from "@/i18n";
  5. import { useGlobalStore } from "@/stores";
  6. import { brandList } from "@/utils/constant";
  7. import { Button, Popup, Toast } from "antd-mobile";
  8. import { useTranslations } from "next-intl";
  9. import { FC, PropsWithChildren, ReactNode, useEffect, useRef, useState } from "react";
  10. import styles from "./style.module.scss";
  11. export interface CardProps {
  12. item?: GameListRep;
  13. render?: (value: GameListRep) => ReactNode;
  14. }
  15. const Card: FC<PropsWithChildren<CardProps>> = (props) => {
  16. const { render, item } = props;
  17. const appRef = useRef<HTMLElement>();
  18. const t = useTranslations("Game");
  19. const urlRef = useRef<string>("");
  20. const brandRef = useRef<string>("");
  21. const [visible, setVisible] = useState(false);
  22. const state = useGlobalStore();
  23. const router = useRouter();
  24. const { token } = state;
  25. useEffect(() => {
  26. appRef.current! = document.querySelector("#app")!;
  27. }, []);
  28. const handler = (game: GameListRep) => {
  29. setVisible(true);
  30. brandRef.current = brandList.find((item) => item.gid === game.game_id)?.brand ?? "";
  31. if (!token) return;
  32. const params = {
  33. id: game.id,
  34. };
  35. getGameDetailApi(params).then((res) => {
  36. urlRef.current = res.data?.game_url;
  37. });
  38. };
  39. const playGameHandler = () => {
  40. if (!token) {
  41. router.push("/login?redirect=/withdraw");
  42. return;
  43. }
  44. if (urlRef.current) {
  45. window.open(`${urlRef.current}&brand=${brandRef.current}`);
  46. } else {
  47. Toast.show("数据错误");
  48. }
  49. };
  50. return (
  51. <>
  52. {render ? (
  53. render(item!)
  54. ) : (
  55. <div className={styles.cardWrap} onClick={() => handler(item!)}>
  56. <img src={item?.game_icon} alt={item?.game_name_cn} className={"h-1/1"} />
  57. </div>
  58. )}
  59. <Popup
  60. visible={visible}
  61. onMaskClick={() => {
  62. setVisible(false);
  63. }}
  64. onClose={() => {
  65. setVisible(false);
  66. }}
  67. showCloseButton={true}
  68. getContainer={() => document.getElementById("app")!}
  69. bodyStyle={{ background: "#1c1c1c" }}
  70. >
  71. <Box className={"w-1/1 flex w-[4.02rem] flex-1"}>
  72. <div className={styles.cardWrap} style={{ width: "1.1rem" }}>
  73. <img src={item?.game_icon} alt={item?.game_name_cn} />
  74. </div>
  75. <div className={styles.cardWrapGmeInfo}>
  76. <p className={"h-[0.6rem]"}>{item?.game_name_cn}</p>
  77. <div className={"flex w-[2.2rem] justify-around"}>
  78. {/*<Button*/}
  79. {/* onClick={playGameHandler}*/}
  80. {/* className={*/}
  81. {/* "h-[0.39rem] w-[0.89rem] rounded-[0.05rem] text-[0.15rem]" +*/}
  82. {/* " bg-[#3a3a3a]" +*/}
  83. {/* " font-bold"*/}
  84. {/* }*/}
  85. {/*>*/}
  86. {/* {t("demo")}*/}
  87. {/*</Button>*/}
  88. <Button
  89. onClick={playGameHandler}
  90. style={{
  91. "--background-color": "#009d80",
  92. "--border-color": "#009d80",
  93. }}
  94. className={`h-[0.39rem] w-[0.89rem] rounded-[0.05rem] bg-[#] text-[0.15rem] font-bold`}
  95. >
  96. {t("join")}
  97. </Button>
  98. </div>
  99. </div>
  100. </Box>
  101. </Popup>
  102. </>
  103. );
  104. };
  105. export default Card;