"use client"; import { PromotionRep } from "@/api/home"; import { useUserInfoStore } from "@/stores/useUserInfoStore"; import useWheelStore from "@/stores/useWheelStore"; import { server } from "@/utils/client"; import { getToken } from "@/utils/Cookies"; import { Local } from "@/utils/storage"; import dayjs from "dayjs"; import React from "react"; import dialogManage from "./manager"; const keyMap: any = { 0: "ban_pop_type_0", 1: "ban_pop_type_1", 2: "pop_type_2", 3: "pop_type_3", }; const getPromotions = async () => { return server .request({ url: "/v1/api/front/pop_list", method: "POST", }) .then((res) => { if (res.code === 200) return res; }); }; const AutoShowDialog = () => { const [data, setData] = React.useState([]); const { userInfo } = useUserInfoStore(); const wheelState = useWheelStore((state) => state); React.useEffect(() => { getData(); }, [userInfo]); React.useEffect(() => { if (!data.length) return; setTimeout(() => { doShow(); }, 300); // eslint-disable-next-line react-hooks/exhaustive-deps }, [data]); const getData = async () => { const res = await getPromotions(); if (res?.code === 200 && res?.data?.length) { setData(res.data); } }; React.useEffect(() => { data.forEach((item) => { if (item.content_type === 1) { const img = new Image(); if (item?.content?.image) img.src = item?.content?.image; } }); }, [data]); const checkIsCanShow = (data: PromotionRep) => { if (!data?.id) return false; if (data.pop_type === 2 && !getToken()) return false; if (data.pop_type === 1) { let popData = Local.getKey(keyMap[data.pop_type]); if (!popData) return false; try { const isCurDay = dayjs(popData.date).add(1, "day").isAfter(dayjs(), "day"); if (isCurDay && popData[userInfo.id][data.id]) { return false; } } catch {} } return true; }; const saveCache = (type: 1 | 2 | 3 | 0, popid: number) => { if (!userInfo?.id) return; const cachedData = Local.getKey(keyMap[type]); let popData: any = { date: Date.now(), }; if (cachedData) { try { if (dayjs(cachedData.date).isSame(Date.now(), "day")) { popData = cachedData; } else { Local.removeKey(keyMap[type]); } } catch {} } if (!popData[userInfo.id]) { popData[userInfo.id] = {}; } popData[userInfo.id][popid] = true; Local.setKey(keyMap[type], JSON.stringify(popData)); }; const saveShowIndex = (idx: number) => { sessionStorage.setItem("dialogShow", `${idx + 1}`); }; const doShow = async () => { //记录点击的索引 let showIndex = sessionStorage.getItem("dialogShow"); let startShow = showIndex ? Number(showIndex) : 0; if (startShow > data.length - 1) { startShow = 0; } for (let i = startShow; i < data.length; i++) { const curData = data[i]; if (!checkIsCanShow(curData)) { saveShowIndex(i); continue; } // await dialogManage.showDialog("ReceiveGift", curData); //周返现 // await dialogManage.showDialog('WheelSection', curData); //轮盘 // await dialogManage.showDialog('SignInSection', curData); //签到 try { switch (curData.content_type) { // 图片展示 case 1: await dialogManage.showDialog("ImgDialog", curData); break; // 富文本展示 case 2: await dialogManage.showDialog("TextDialog", curData); break; case 3: // 轮盘特殊处理 dialogManage.showDialog('WheelSection') console.log(wheelState.status); if ( curData?.action_params?.includes("WheelSection") && wheelState.status === 2 ) { break; } if (curData.action_type === 5) { curData?.action_params ? await eval(curData?.action_params || "") : ""; } break; } } catch (err) { saveShowIndex(i); continue; } // 记录当前展示到几个了 saveShowIndex(i); if (curData.id) saveCache(curData.pop_type, curData.id); } }; return null; }; export default AutoShowDialog;