|
@@ -1,12 +1,17 @@
|
|
|
"use client";
|
|
|
import { PromotionRep } from "@/api/home";
|
|
|
+import { useUserInfoStore } from "@/stores/useUserInfoStore";
|
|
|
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";
|
|
|
|
|
|
-//"ImgDialog","WheelSection", "SignInSection", "HomePromotion", "TextDialog"
|
|
|
+const keyMap: any = {
|
|
|
+ 1: "ban_pop_type_1",
|
|
|
+ 2: "pop_type_2",
|
|
|
+ 3: "pop_type_3",
|
|
|
+};
|
|
|
|
|
|
const getPromotions = async () => {
|
|
|
return server
|
|
@@ -21,10 +26,11 @@ const getPromotions = async () => {
|
|
|
|
|
|
const AutoShowDialog = () => {
|
|
|
const [data, setData] = React.useState<PromotionRep[]>([]);
|
|
|
+ const { userInfo } = useUserInfoStore();
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
getData();
|
|
|
- }, []);
|
|
|
+ }, [userInfo]);
|
|
|
React.useEffect(() => {
|
|
|
if (!data.length) return;
|
|
|
setTimeout(() => {
|
|
@@ -40,15 +46,45 @@ const AutoShowDialog = () => {
|
|
|
}
|
|
|
};
|
|
|
const checkIsShowed = (data: PromotionRep) => {
|
|
|
- const showTime = localStorage.getItem(`dialog_${data.id}`);
|
|
|
- if (showTime) {
|
|
|
- const nextDay = dayjs(Number(showTime)).add(1, "day").format("YYYY-MM-DD");
|
|
|
- if ((dayjs(nextDay).isAfter(dayjs()), "day")) {
|
|
|
+ if (!data?.id || data.pop_type === 3) return false;
|
|
|
+ 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 true;
|
|
|
}
|
|
|
- }
|
|
|
+ } catch {}
|
|
|
return false;
|
|
|
};
|
|
|
+
|
|
|
+ const saveCache = (type: 1 | 2 | 3, popid: number) => {
|
|
|
+ if (!userInfo?.id) return;
|
|
|
+ const cachedData = Local.getKey(keyMap[type]);
|
|
|
+ console.log(cachedData);
|
|
|
+ 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");
|
|
@@ -59,13 +95,8 @@ const AutoShowDialog = () => {
|
|
|
for (let i = startShow; i < data.length; i++) {
|
|
|
const curData = data[i];
|
|
|
// 一天只展示一次
|
|
|
- if (curData.pop_type === 1 && checkIsShowed(curData)) {
|
|
|
- sessionStorage.setItem("dialogShow", `${i + 1}`);
|
|
|
- continue;
|
|
|
- }
|
|
|
- // 未登录的用户不展示
|
|
|
- if (curData.pop_type === 2 && !getToken()) {
|
|
|
- sessionStorage.setItem("dialogShow", `${i + 1}`);
|
|
|
+ if (checkIsShowed(curData)) {
|
|
|
+ saveShowIndex(i);
|
|
|
continue;
|
|
|
}
|
|
|
|
|
@@ -86,15 +117,12 @@ const AutoShowDialog = () => {
|
|
|
break;
|
|
|
}
|
|
|
} catch (err) {
|
|
|
- sessionStorage.setItem("dialogShow", `${i + 1}`);
|
|
|
+ saveShowIndex(i);
|
|
|
continue;
|
|
|
}
|
|
|
- // 一天只展示一次
|
|
|
- if (curData.pop_type === 1) {
|
|
|
- localStorage.setItem(`dialog_${curData.id}`, Date.now().toString());
|
|
|
- }
|
|
|
// 记录当前展示到几个了
|
|
|
- sessionStorage.setItem("dialogShow", `${i + 1}`);
|
|
|
+ saveShowIndex(i);
|
|
|
+ if (curData.id) saveCache(curData.pop_type, curData.id);
|
|
|
}
|
|
|
};
|
|
|
|