auto.ts 6.0 KB

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