|
|
<!DOCTYPE html><html lang="zh-CN"> <head> <meta charset="UTF-8" /> <link rel="icon" href="/favicon.ico" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <title></title> <style> * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: "Microsoft Yahei", sans-serif; } /* 落地页 */ .landing-page { width: 375px; height: auto; margin: 0 auto; overflow-y: auto; overflow-x: hidden; } .landing-page img { width: 100%; height: auto; display: block; } /* 弹窗 */ .popup { display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0, 0, 0, 0.7); justify-content: center; align-items: center; z-index: 999; } .popup.show { display: flex; } .popup-content { width: 320px; text-align: center; } .popup-content img { width: 100%; height: auto; margin-bottom: 15px; } .popup-content img#closeBtn { width: 60%; margin-bottom: 5px; } </style> </head> <body> <!-- 落地页 --> <div class="landing-page"> <img id="landingImage" /> </div>
<!-- 弹窗样式 --> <div class="popup" id="popup"> <div class="popup-content"> <img id="popupImage" /> <img src="./assent/开心收下.png" id="closeBtn"/> </div> </div>
<script type="module"> // 导入API函数 import { getImageApi, acceptCardApi, getStateApi } from './src/api/member.js';
const userAgent = navigator.userAgent; // 获取弹窗元素 const landingImage = document.getElementById('landingImage'); const popupImage = document.getElementById('popupImage'); const popup = document.getElementById("popup"); const closeBtn = document.getElementById("closeBtn");
// 防抖多次点击 let isClickable = true;
// 记录页面打开时间 const pageOpenTime = getBeijingTime();
// 获取北京时间(东八区)的函数 function getBeijingTime() { const date = new Date(); const beijingTime = new Date(date.getTime() + 8 * 60 * 60 * 1000); return beijingTime.toISOString(); }
// 缓存键名 const CACHE_KEY = 'landing_image';
// 从缓存获取落地图 function getLandingImageFromCache() { const cached = localStorage.getItem(CACHE_KEY); return cached ? JSON.parse(cached) : null; }
// 将落地图存入缓存 function saveLandingImageToCache(imageUrl) { localStorage.setItem(CACHE_KEY, JSON.stringify({ url: imageUrl, })); }
// 获取图片方法 async function getImage() { try { const res = await getImageApi({ id:3 }); console.log(res.data) if (res.code == 200 && res.data) { return { landingImageUrl: res.data.list[0].landing_page, popupImageUrl: res.data.list[0].landing_page_popup, title: res.data.list[0].name }; } else { return { msg:res.msg }; } } catch (error) { console.error('网络错误,请稍后重试'); return null } }
// 加载 window.onload = async function () { try { await acceptCardApi({ user_info:userAgent, state: 0, }); // 缓存 const cachedImage = getLandingImageFromCache();
// 接口 const imagePaths = await getImage();
// 确定最终落地图和是否展示弹窗 let finalLandingUrl; let shouldShowPopup = false; document.title = imagePaths.title?imagePaths.title:imagePaths.msg if(imagePaths?.msg){ document.body.innerHTML = ` <div style=" display:flex; align-items:center; justify-content:center; height:100vh; font-size:40px; color:#333; font-family:'PingFang SC', 'Microsoft Yahei', Arial, sans-serif; text-align:center; padding:20px; box-sizing:border-box; "> ${imagePaths.msg} </div> `; return } if (imagePaths?.landingImageUrl) { finalLandingUrl = imagePaths.landingImageUrl; // if (!cachedImage || cachedImage.url !== finalLandingUrl) { // shouldShowPopup = true; // saveLandingImageToCache(finalLandingUrl); // popupImage.src = imagePaths.popupImageUrl; // } } else { // 接口失败时使用缓存(如果有) finalLandingUrl = cachedImage?.url; } // 4. 设置落地图 landingImage.src = finalLandingUrl; const stateRes = await getStateApi({ user_info:userAgent }); if(stateRes.code == 200 ){ if(stateRes.data > 0){ shouldShowPopup = false } else { shouldShowPopup = true popupImage.src = imagePaths.popupImageUrl; } } // 5. 新增:根据对比结果决定是否展示弹窗 if (shouldShowPopup) { setTimeout(() => { popup.classList.add("show"); }, 500); } } catch (err) { console.error('页面初始化失败'); } };
// 点击"开心收下",关闭弹窗 closeBtn.addEventListener('click', closePopupAndSendData);
// 点击弹窗空白处,关闭弹窗 popup.addEventListener('click', function(e) { if (e.target === popup) { closePopupAndSendData(); } });
// 关闭弹窗并发送数据 async function closePopupAndSendData() { if (!isClickable) { return; }
// 关闭弹窗 popup.classList.remove("show"); // 发送页面打开时间到后端 try { await acceptCardApi({ user_info:userAgent, state: 1, }); } catch (error) { console.error('发送失败', pageOpenTime); } // 设置点击冷却 isClickable = false; setTimeout(() => { isClickable = true; }, 10000); } </script> </body></html>
|