auto.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. "use client";
  2. import { PromotionRep } from "@/api/home";
  3. import { useUserInfoStore } from "@/stores/useUserInfoStore";
  4. import useWheelStore from "@/stores/useWheelStore";
  5. import { server } from "@/utils/client";
  6. import { getToken } from "@/utils/Cookies";
  7. import { Local } from "@/utils/storage";
  8. import dayjs from "dayjs";
  9. import React from "react";
  10. import dialogManage from "./manager";
  11. const keyMap: any = {
  12. 0: "ban_pop_type_0",
  13. 1: "ban_pop_type_1",
  14. 2: "pop_type_2",
  15. 3: "pop_type_3",
  16. };
  17. const getPromotions = async () => {
  18. return server
  19. .request<PromotionRep[], { summery: { showType: 1 | 2 } }>({
  20. url: "/v1/api/front/pop_list",
  21. method: "POST",
  22. })
  23. .then((res) => {
  24. if (res.code === 200) return res;
  25. });
  26. };
  27. const AutoShowDialog = () => {
  28. const [data, setData] = React.useState<PromotionRep[]>([]);
  29. const { userInfo } = useUserInfoStore();
  30. const wheelState = useWheelStore((state) => state);
  31. React.useEffect(() => {
  32. getData();
  33. }, [userInfo]);
  34. React.useEffect(() => {
  35. if (!data.length) return;
  36. setTimeout(() => {
  37. doShow();
  38. }, 300);
  39. // eslint-disable-next-line react-hooks/exhaustive-deps
  40. }, [data]);
  41. const getData = async () => {
  42. const res = await getPromotions();
  43. if (res?.code === 200 && res?.data?.length) {
  44. setData(res.data);
  45. }
  46. };
  47. React.useEffect(() => {
  48. data.forEach((item) => {
  49. if (item.content_type === 1) {
  50. const img = new Image();
  51. if (item?.content?.image) img.src = item?.content?.image;
  52. }
  53. });
  54. }, [data]);
  55. const checkIsCanShow = (data: PromotionRep) => {
  56. if (!data?.id) return false;
  57. if (data.pop_type === 2 && !getToken()) return false;
  58. if (data.pop_type === 1) {
  59. let popData = Local.getKey(keyMap[data.pop_type]);
  60. if (!popData) return false;
  61. try {
  62. const isCurDay = dayjs(popData.date).add(1, "day").isAfter(dayjs(), "day");
  63. if (isCurDay && popData[userInfo.id][data.id]) {
  64. return false;
  65. }
  66. } catch {}
  67. }
  68. return true;
  69. };
  70. const saveCache = (type: 1 | 2 | 3 | 0, popid: number) => {
  71. if (!userInfo?.id) return;
  72. const cachedData = Local.getKey(keyMap[type]);
  73. let popData: any = {
  74. date: Date.now(),
  75. };
  76. if (cachedData) {
  77. try {
  78. if (dayjs(cachedData.date).isSame(Date.now(), "day")) {
  79. popData = cachedData;
  80. } else {
  81. Local.removeKey(keyMap[type]);
  82. }
  83. } catch {}
  84. }
  85. if (!popData[userInfo.id]) {
  86. popData[userInfo.id] = {};
  87. }
  88. popData[userInfo.id][popid] = true;
  89. Local.setKey(keyMap[type], JSON.stringify(popData));
  90. };
  91. const saveShowIndex = (idx: number) => {
  92. sessionStorage.setItem("dialogShow", `${idx + 1}`);
  93. };
  94. const doShow = async () => {
  95. //记录点击的索引
  96. let showIndex = sessionStorage.getItem("dialogShow");
  97. let startShow = showIndex ? Number(showIndex) : 0;
  98. if (startShow > data.length - 1) {
  99. startShow = 0;
  100. }
  101. for (let i = startShow; i < data.length; i++) {
  102. const curData = data[i];
  103. if (!checkIsCanShow(curData)) {
  104. saveShowIndex(i);
  105. continue;
  106. }
  107. // await dialogManage.showDialog("ReceiveGift", curData); //周返现
  108. // await dialogManage.showDialog('WheelSection', curData); //轮盘
  109. // await dialogManage.showDialog('SignInSection', curData); //签到
  110. try {
  111. switch (curData.content_type) {
  112. // 图片展示
  113. case 1:
  114. await dialogManage.showDialog("ImgDialog", curData);
  115. break;
  116. // 富文本展示
  117. case 2:
  118. await dialogManage.showDialog("TextDialog", curData);
  119. break;
  120. case 3:
  121. // 轮盘特殊处理 dialogManage.showDialog('WheelSection')
  122. console.log(wheelState.status);
  123. if (
  124. curData?.action_params?.includes("WheelSection") &&
  125. wheelState.status === 2
  126. ) {
  127. break;
  128. }
  129. if (curData.action_type === 5) {
  130. curData?.action_params ? await eval(curData?.action_params || "") : "";
  131. }
  132. break;
  133. }
  134. } catch (err) {
  135. saveShowIndex(i);
  136. continue;
  137. }
  138. // 记录当前展示到几个了
  139. saveShowIndex(i);
  140. if (curData.id) saveCache(curData.pop_type, curData.id);
  141. }
  142. };
  143. return null;
  144. };
  145. export default AutoShowDialog;