auto.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 checkIsShowed = (data: PromotionRep) => {
  56. if (!data?.id || data.pop_type === 3) return false;
  57. if (data.pop_type === 2 && !getToken()) return false;
  58. let popData = Local.getKey(keyMap[data.pop_type]);
  59. if (!popData) return false;
  60. try {
  61. const isCurDay = dayjs(popData.date).add(1, "day").isAfter(dayjs(), "day");
  62. if (isCurDay && popData[userInfo.id][data.id]) {
  63. return true;
  64. }
  65. } catch {}
  66. return false;
  67. };
  68. const saveCache = (type: 1 | 2 | 3 | 0, popid: number) => {
  69. if (!userInfo?.id) return;
  70. const cachedData = Local.getKey(keyMap[type]);
  71. let popData: any = {
  72. date: Date.now(),
  73. };
  74. if (cachedData) {
  75. try {
  76. if (dayjs(cachedData.date).isSame(Date.now(), "day")) {
  77. popData = cachedData;
  78. } else {
  79. Local.removeKey(keyMap[type]);
  80. }
  81. } catch {}
  82. }
  83. if (!popData[userInfo.id]) {
  84. popData[userInfo.id] = {};
  85. }
  86. popData[userInfo.id][popid] = true;
  87. Local.setKey(keyMap[type], JSON.stringify(popData));
  88. };
  89. const saveShowIndex = (idx: number) => {
  90. sessionStorage.setItem("dialogShow", `${idx + 1}`);
  91. };
  92. const doShow = async () => {
  93. //记录点击的索引
  94. let showIndex = sessionStorage.getItem("dialogShow");
  95. let startShow = showIndex ? Number(showIndex) : 0;
  96. if (startShow > data.length - 1) {
  97. startShow = 0;
  98. }
  99. for (let i = startShow; i < data.length; i++) {
  100. const curData = data[i];
  101. // 一天只展示一次
  102. if (checkIsShowed(curData)) {
  103. saveShowIndex(i);
  104. continue;
  105. }
  106. // await dialogManage.showDialog("ReceiveGift", curData); //周返现
  107. // await dialogManage.showDialog('WheelSection', curData); //轮盘
  108. // await dialogManage.showDialog('SignInSection', curData); //签到
  109. try {
  110. switch (curData.content_type) {
  111. // 图片展示
  112. case 1:
  113. await dialogManage.showDialog("ImgDialog", curData);
  114. break;
  115. // 富文本展示
  116. case 2:
  117. await dialogManage.showDialog("TextDialog", curData);
  118. break;
  119. case 3:
  120. // 轮盘特殊处理 dialogManage.showDialog('WheelSection')
  121. console.log(wheelState.status);
  122. if (
  123. curData?.action_params?.includes("WheelSection") &&
  124. wheelState.status === 2
  125. ) {
  126. break;
  127. }
  128. if (curData.action_type === 5) {
  129. curData?.action_params ? await eval(curData?.action_params || "") : "";
  130. }
  131. break;
  132. }
  133. } catch (err) {
  134. saveShowIndex(i);
  135. continue;
  136. }
  137. // 记录当前展示到几个了
  138. saveShowIndex(i);
  139. if (curData.id) saveCache(curData.pop_type, curData.id);
  140. }
  141. };
  142. return null;
  143. };
  144. export default AutoShowDialog;