"use client"; import { PromotionRep } from "@/api/home"; import { usePathname } from "@/i18n/routing"; import { useUserInfoStore } from "@/stores/useUserInfoStore"; import useWheelStore from "@/stores/useWheelStore"; import { commonMask } from "@/utils"; 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 pathName = usePathname(); const [data, setData] = React.useState([]); const { userInfo } = useUserInfoStore(); const wheelState = useWheelStore((state) => state); const [cacheUserInfo, setCacheUserInfo] = React.useState({}); React.useEffect(() => { if (cacheUserInfo.id !== userInfo.id) { getData(); setCacheUserInfo(userInfo); } }, [userInfo, cacheUserInfo]); React.useEffect(() => { if (!data?.length) { return; } setTimeout(() => { doShow(); }, 300); // eslint-disable-next-line react-hooks/exhaustive-deps }, [data]); const getData = async () => { commonMask.show(); try { const res = await getPromotions(); if (res?.code === 200 && res?.data?.length) { setData(res.data); if (!res.data?.length) { commonMask.hide(); } } } catch { commonMask.hide(); } }; 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 true; 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 () => { setTimeout(() => { commonMask.hide(); }, 500); //记录点击的索引 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 (pathName !== "/") return; if (!checkIsCanShow(curData)) { saveShowIndex(i); continue; } // await dialogManage.showDialog("ReceiveGift", curData); //周返现 // await dialogManage.showDialog('WheelSection', curData); //轮盘 // await dialogManage.showDialog('SignInSection', curData); //签到 // await dialogManage.showDialog("SignInSection", curData); try { let res: any = ""; switch (curData.content_type) { // 图片展示 case 1: res = await dialogManage.showDialog("ImgDialog", curData); break; // 富文本展示 case 2: res = await dialogManage.showDialog("TextDialog", curData); break; case 3: // 轮盘特殊处理 dialogManage.showDialog('WheelSection') if (curData?.action_params?.includes("WheelSection")) { await wheelState.setWheel(); if (wheelState.status != 1) { break; } } if (curData.action_type === 5) { res = curData?.action_params ? await eval(curData?.action_params || "") : ""; } break; } if (res === "target") { return; } } catch (err) { saveShowIndex(i); continue; } // 记录当前展示到几个了 saveShowIndex(i); if (curData.id) saveCache(curData.pop_type, curData.id); } }; return null; }; export default AutoShowDialog;