index.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. "use client";
  2. import { ActionType } from "@/api/home";
  3. import { lredPacketApi, redPacketApi } from "@/api/promo";
  4. import RedPacketModal, { RedPacketModalProps } from "@/components/Box/RedPacketModal";
  5. import { useRouter } from "@/i18n/routing";
  6. import { isHttpUrl } from "@/utils";
  7. import { getToken } from "@/utils/Cookies";
  8. import { Toast } from "antd-mobile";
  9. import clsx from "clsx";
  10. import { CSSProperties, FC, PropsWithChildren, useMemo, useRef } from "react";
  11. import { twMerge } from "tailwind-merge";
  12. import DesktopModal, { DesktopRefProps } from "./Desktop";
  13. import { ModalEnum } from "./types";
  14. interface Props {
  15. pt?: boolean;
  16. pb?: boolean;
  17. pl?: boolean;
  18. pr?: boolean;
  19. none?: boolean;
  20. className?: string;
  21. style?: CSSProperties;
  22. Tag?: "section" | "div";
  23. // 点击功能, 1:无功能,2:跳转外部链接,3:跳转页面,4:跳转弹窗,5:跳转打开游戏,6:跳转Live Chat客服,7:跳转内部文档
  24. action?: ActionType;
  25. actionData?: unknown;
  26. }
  27. /**
  28. * @description
  29. * 1`:接受padding属性, 有默认值, 有自定义值, 默认显示, 可自定并且合并
  30. * todo 2: 可通过 clo / row 定义flex 布局
  31. * todo 3: 可设置背景
  32. */
  33. /**
  34. * @description 跳转外部链接
  35. */
  36. const openWindow = (url: string) => {
  37. if (isHttpUrl(url)) {
  38. window.open(url);
  39. } else {
  40. Toast.show("非法地址");
  41. }
  42. };
  43. /**
  44. * @description 红包雨验证
  45. */
  46. const isStartPacketsHandler = async () => {
  47. if (getToken()) {
  48. let redPacketInfo = await lredPacketApi();
  49. return redPacketInfo.data?.red_packets || [];
  50. } else {
  51. let redPacketInfo = await redPacketApi();
  52. return redPacketInfo.data || [];
  53. }
  54. };
  55. const Box: FC<PropsWithChildren<Props>> = (props) => {
  56. const router = useRouter();
  57. const packetModalRef = useRef<RedPacketModalProps>(null);
  58. const pwaModalRef = useRef<DesktopRefProps>(null);
  59. const pwaModal = useMemo(() => <DesktopModal ref={pwaModalRef} source={"components"} />, []);
  60. /**
  61. * @description 弹窗类型
  62. */
  63. const popupHandler = (actionData: keyof typeof ModalEnum) => {
  64. switch (actionData) {
  65. case ModalEnum.red_packet:
  66. isStartPacketsHandler().then((data) => {
  67. if (data.length) {
  68. packetModalRef.current?.onOpen(data);
  69. } else {
  70. Toast.show("The event is not open");
  71. }
  72. });
  73. break;
  74. case ModalEnum.pwa_install:
  75. pwaModalRef.current?.onOpen();
  76. break;
  77. default:
  78. Toast.show("Unknown parameters");
  79. }
  80. };
  81. const {
  82. className,
  83. children,
  84. pt = true,
  85. pb = true,
  86. pl = true,
  87. pr = true,
  88. none = false,
  89. style,
  90. Tag = "div",
  91. action = undefined,
  92. actionData = undefined,
  93. } = props;
  94. const cls = clsx(
  95. !none && {
  96. ["pt-[0.08rem]"]: pt,
  97. ["pb-[0.08rem]"]: pb,
  98. ["px-[0.12rem]"]: pl || pr,
  99. },
  100. className
  101. );
  102. const handler = () => {
  103. if (!action) return;
  104. switch (action) {
  105. case 1:
  106. return;
  107. case 2:
  108. openWindow(actionData as string);
  109. break;
  110. case 3:
  111. router.push(actionData as string);
  112. break;
  113. case 4:
  114. popupHandler(actionData as keyof typeof ModalEnum);
  115. break;
  116. case 5:
  117. break;
  118. case 6:
  119. console.log(`🎯🎯🎯🎯🎯-> in index.tsx on 65`);
  120. break;
  121. case 7:
  122. console.log(`🎯🎯🎯🎯🎯-> in index.tsx on 68`);
  123. break;
  124. default:
  125. console.log(`🎯🎯🎯🎯🎯-> in index.tsx on 71`);
  126. break;
  127. }
  128. };
  129. return (
  130. <>
  131. <Tag className={twMerge(cls)} style={style} onClick={handler}>
  132. {children}
  133. </Tag>
  134. <RedPacketModal ref={packetModalRef} />
  135. {pwaModal}
  136. </>
  137. );
  138. };
  139. export default Box;