国内市场双十一活动仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
5.6 KiB

3 months ago
3 months ago
3 months ago
3 months ago
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <link rel="icon" href="/favicon.ico" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  7. <title>新时代、新龙头、新节奏</title>
  8. <style>
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. box-sizing: border-box;
  13. }
  14. body {
  15. font-family: "Microsoft Yahei", sans-serif;
  16. }
  17. /* 落地页 */
  18. .landing-page {
  19. width: 375px;
  20. height: auto;
  21. margin: 0 auto;
  22. overflow-y: auto;
  23. overflow-x: hidden;
  24. }
  25. .landing-page img {
  26. width: 100%;
  27. height: auto;
  28. display: block;
  29. }
  30. /* 弹窗 */
  31. .popup {
  32. display: none;
  33. position: fixed;
  34. top: 0;
  35. left: 0;
  36. width: 100%;
  37. height: 100%;
  38. background: rgba(0, 0, 0, 0.7);
  39. justify-content: center;
  40. align-items: center;
  41. z-index: 999;
  42. }
  43. .popup.show {
  44. display: flex;
  45. }
  46. .popup-content {
  47. width: 320px;
  48. text-align: center;
  49. }
  50. .popup-content img {
  51. width: 100%;
  52. height: auto;
  53. margin-bottom: 15px;
  54. }
  55. .popup-content img#closeBtn {
  56. width: 60%;
  57. margin-bottom: 5px;
  58. }
  59. </style>
  60. </head>
  61. <body>
  62. <!-- 落地页 -->
  63. <div class="landing-page">
  64. <img id="landingImage" />
  65. </div>
  66. <!-- 弹窗样式 -->
  67. <div class="popup" id="popup">
  68. <div class="popup-content">
  69. <img id="popupImage" />
  70. <img src="./assent/开心收下.png" id="closeBtn"/>
  71. </div>
  72. </div>
  73. <script type="module">
  74. // 导入API函数
  75. import { getImageApi, acceptCardApi } from './src/api/member.js';
  76. // 获取弹窗元素
  77. const landingImage = document.getElementById('landingImage');
  78. const popupImage = document.getElementById('popupImage');
  79. const popup = document.getElementById("popup");
  80. const closeBtn = document.getElementById("closeBtn");
  81. // 防抖多次点击
  82. let isClickable = true;
  83. // 记录页面打开时间
  84. const pageOpenTime = getBeijingTime();
  85. // 获取北京时间(东八区)的函数
  86. function getBeijingTime() {
  87. const date = new Date();
  88. const beijingTime = new Date(date.getTime() + 8 * 60 * 60 * 1000);
  89. return beijingTime.toISOString();
  90. }
  91. // 缓存键名
  92. const CACHE_KEY = 'landing_image';
  93. // 从缓存获取落地图
  94. function getLandingImageFromCache() {
  95. const cached = localStorage.getItem(CACHE_KEY);
  96. return cached ? JSON.parse(cached) : null;
  97. }
  98. // 将落地图存入缓存
  99. function saveLandingImageToCache(imageUrl) {
  100. localStorage.setItem(CACHE_KEY, JSON.stringify({
  101. url: imageUrl,
  102. }));
  103. }
  104. // 获取图片方法
  105. async function getImage() {
  106. try {
  107. const res = await getImageApi();
  108. if (res.code == 200 && res.data) {
  109. return {
  110. landingImageUrl: res.data.landingImage,
  111. popupImageUrl: res.data.popupImage
  112. };
  113. } else {
  114. console.warn('网络错误,请稍后重试');
  115. return null;
  116. }
  117. } catch (error) {
  118. console.error('网络错误,请稍后重试');
  119. return {
  120. landingImageUrl: './assent/8折页面.png',
  121. popupImageUrl: './assent/弹窗样式2.png'
  122. };
  123. }
  124. }
  125. // 加载
  126. window.onload = async function () {
  127. try {
  128. // 缓存
  129. const cachedImage = getLandingImageFromCache();
  130. // 接口
  131. const imagePaths = await getImage();
  132. // 确定最终落地图和是否展示弹窗
  133. let finalLandingUrl;
  134. let shouldShowPopup = false;
  135. if (imagePaths?.landingImageUrl) {
  136. finalLandingUrl = imagePaths.landingImageUrl;
  137. if (!cachedImage || cachedImage.url !== finalLandingUrl) {
  138. shouldShowPopup = true;
  139. saveLandingImageToCache(finalLandingUrl);
  140. popupImage.src = imagePaths.popupImageUrl;
  141. }
  142. } else {
  143. // 接口失败时使用缓存(如果有)
  144. finalLandingUrl = cachedImage?.url;
  145. }
  146. // 4. 设置落地图
  147. landingImage.src = finalLandingUrl;
  148. // 5. 新增:根据对比结果决定是否展示弹窗
  149. if (shouldShowPopup) {
  150. setTimeout(() => {
  151. popup.classList.add("show");
  152. }, 500);
  153. }
  154. } catch (err) {
  155. console.error('页面初始化失败');
  156. }
  157. };
  158. // 点击"开心收下",关闭弹窗
  159. closeBtn.addEventListener('click', closePopupAndSendData);
  160. // 点击弹窗空白处,关闭弹窗
  161. popup.addEventListener('click', function(e) {
  162. if (e.target === popup) {
  163. closePopupAndSendData();
  164. }
  165. });
  166. // 关闭弹窗并发送数据
  167. async function closePopupAndSendData() {
  168. if (!isClickable) {
  169. return;
  170. }
  171. // 关闭弹窗
  172. popup.classList.remove("show");
  173. // 发送页面打开时间到后端
  174. try {
  175. await acceptCardApi({
  176. openTime: pageOpenTime,
  177. });
  178. console.log('发送成功', pageOpenTime);
  179. } catch (error) {
  180. console.error('发送失败', pageOpenTime);
  181. }
  182. // 设置点击冷却
  183. isClickable = false;
  184. setTimeout(() => {
  185. isClickable = true;
  186. }, 10000);
  187. }
  188. </script>
  189. </body>
  190. </html>