Popup.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <template>
  2. <div v-if="visiblePopups.length > 0" class="popup--container">
  3. <div
  4. v-for="(popup, index) in visiblePopups"
  5. :key="popup.id"
  6. class="popup--wrapper"
  7. :style="{
  8. top: popupPositions[index]?.top || popup.position_top + 'px',
  9. left: popupPositions[index]?.left || popup.position_left + 'px',
  10. width: popup.width + 'px',
  11. height: popup.type === 'image' ? 'auto' : popup.height + 'px',
  12. }"
  13. >
  14. <!-- 스크롤 가능한 콘텐츠 영역 -->
  15. <div class="popup--content">
  16. <!-- HTML 타입 팝업 -->
  17. <div
  18. v-if="popup.type === 'html'"
  19. class="popup--html"
  20. v-html="popup.content"
  21. ></div>
  22. <!-- 이미지 타입 팝업 -->
  23. <div v-else-if="popup.type === 'image'" class="popup--image">
  24. <a
  25. v-if="popup.link_url"
  26. :href="popup.link_url"
  27. :target="popup.link_target || '_blank'"
  28. :rel="popup.link_target === '_self' ? '' : 'noopener noreferrer'"
  29. >
  30. <img :src="popup.image_url" :alt="popup.title" />
  31. </a>
  32. <img v-else :src="popup.image_url" :alt="popup.title" />
  33. </div>
  34. </div>
  35. <!-- 하단 고정 푸터 -->
  36. <div class="popup--footer" :class="{ solo: popup.cookie_setting == 'none' }">
  37. <label class="popup--checkbox" v-if="popup.cookie_setting !== 'none'">
  38. <input
  39. type="checkbox"
  40. @change="(e) => handleDontShowToday(e, popup.id, popup.cookie_setting)"
  41. />
  42. <span v-if="popup.cookie_setting === 'today'">오늘 하루 창 띄우지 않음</span>
  43. <span v-else-if="popup.cookie_setting === 'forever'"
  44. >다시는 창을 띄우지 않음</span
  45. >
  46. </label>
  47. <button
  48. class="popup--close"
  49. @click.stop="closePopup(popup.id)"
  50. @mousedown.stop
  51. @touchstart.stop
  52. @touchend.stop="closePopup(popup.id)"
  53. aria-label="팝업 닫기"
  54. >
  55. 닫기
  56. </button>
  57. </div>
  58. </div>
  59. </div>
  60. </template>
  61. <script setup>
  62. import { ref, onMounted, onUnmounted } from "vue";
  63. import axios from "axios";
  64. const props = defineProps({
  65. site: {
  66. type: String,
  67. required: true,
  68. validator: (value) => ["ford", "lincoln"].includes(value),
  69. },
  70. });
  71. const config = useRuntimeConfig();
  72. const visiblePopups = ref([]);
  73. const popupPositions = ref([]);
  74. const { getImageUrl } = useImage();
  75. // 드래그 상태
  76. const dragState = ref({
  77. isDragging: false,
  78. currentIndex: null,
  79. startX: 0,
  80. startY: 0,
  81. initialLeft: 0,
  82. initialTop: 0,
  83. });
  84. // 쿠키 가져오기
  85. const getCookie = (name) => {
  86. if (typeof document === "undefined") return null;
  87. const value = `; ${document.cookie}`;
  88. const parts = value.split(`; ${name}=`);
  89. if (parts.length === 2) return parts.pop().split(";").shift();
  90. return null;
  91. };
  92. // 쿠키 설정
  93. const setCookie = (name, value, days) => {
  94. if (typeof document === "undefined") return;
  95. const date = new Date();
  96. date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
  97. const expires = `expires=${date.toUTCString()}`;
  98. document.cookie = `${name}=${value};${expires};path=/`;
  99. };
  100. // 팝업 데이터 가져오기
  101. const fetchPopups = async () => {
  102. try {
  103. const apiUrl = `${config.public.apiBase}/api/popup/active?site=${props.site}`;
  104. const response = await axios.get(apiUrl);
  105. if (response.data.success && response.data.data) {
  106. // 쿠키 설정 필드 정규화 (cookie_setting 또는 cookie_type 지원)
  107. const popups = response.data.data.map((popup) => ({
  108. ...popup,
  109. cookie_setting: popup.cookie_setting || popup.cookie_type || "none",
  110. }));
  111. // 1차 필터: 기간(시작/종료 시각 포함) 재검증 — 백엔드 버그/캐시 대비 안전장치
  112. const now = new Date();
  113. const inPeriod = popups.filter((popup) => {
  114. if (!popup.start_date || !popup.end_date) return false;
  115. const start = new Date(
  116. `${popup.start_date}T${popup.start_time || "00:00:00"}`
  117. );
  118. const end = new Date(
  119. `${popup.end_date}T${popup.end_time || "23:59:59"}`
  120. );
  121. const active = now >= start && now <= end;
  122. if (!active) {
  123. console.log(
  124. `[Popup] ID ${popup.id} "${popup.title}" - 기간 외 노출 차단`,
  125. { now, start, end }
  126. );
  127. }
  128. return active;
  129. });
  130. // 2차 필터: 쿠키로 숨긴 팝업 제외
  131. visiblePopups.value = inPeriod.filter((popup) => {
  132. const cookieName = `popup_hide_${popup.id}`;
  133. const isHidden = getCookie(cookieName);
  134. console.log(
  135. `[Popup] ID ${popup.id} "${popup.title}" - Hidden by cookie:`,
  136. !!isHidden
  137. );
  138. return !isHidden;
  139. });
  140. // 초기 위치 설정
  141. popupPositions.value = visiblePopups.value.map(() => ({}));
  142. }
  143. } catch (error) {
  144. console.error("[Popup] Failed to fetch popups:", error);
  145. }
  146. };
  147. // 드래그 시작
  148. const startDrag = (event, index) => {
  149. // 1024px 이하에서는 드래그 비활성화
  150. if (window.innerWidth <= 1024) return;
  151. event.preventDefault();
  152. const e = event.touches ? event.touches[0] : event;
  153. const popup = visiblePopups.value[index];
  154. dragState.value = {
  155. isDragging: true,
  156. currentIndex: index,
  157. startX: e.clientX,
  158. startY: e.clientY,
  159. initialLeft: parseInt(popupPositions.value[index]?.left || popup.position_left),
  160. initialTop: parseInt(popupPositions.value[index]?.top || popup.position_top),
  161. };
  162. document.addEventListener("mousemove", onDrag);
  163. document.addEventListener("mouseup", stopDrag);
  164. document.addEventListener("touchmove", onDrag);
  165. document.addEventListener("touchend", stopDrag);
  166. };
  167. // 드래그 중
  168. const onDrag = (event) => {
  169. if (!dragState.value.isDragging) return;
  170. const e = event.touches ? event.touches[0] : event;
  171. const deltaX = e.clientX - dragState.value.startX;
  172. const deltaY = e.clientY - dragState.value.startY;
  173. const newLeft = dragState.value.initialLeft + deltaX;
  174. const newTop = dragState.value.initialTop + deltaY;
  175. popupPositions.value[dragState.value.currentIndex] = {
  176. left: newLeft + "px",
  177. top: newTop + "px",
  178. };
  179. };
  180. // 드래그 종료
  181. const stopDrag = () => {
  182. dragState.value.isDragging = false;
  183. dragState.value.currentIndex = null;
  184. document.removeEventListener("mousemove", onDrag);
  185. document.removeEventListener("mouseup", stopDrag);
  186. document.removeEventListener("touchmove", onDrag);
  187. document.removeEventListener("touchend", stopDrag);
  188. };
  189. // 팝업 닫기 (모바일에서 연속 닫힘 방지)
  190. let lastCloseTime = 0;
  191. const closePopup = (popupId) => {
  192. const now = Date.now();
  193. // 300ms 이내 연속 닫기 시도 무시 (터치 이벤트 중복 방지)
  194. if (now - lastCloseTime < 300) return;
  195. lastCloseTime = now;
  196. const index = visiblePopups.value.findIndex((p) => p.id === popupId);
  197. if (index !== -1) {
  198. visiblePopups.value.splice(index, 1);
  199. popupPositions.value.splice(index, 1);
  200. }
  201. };
  202. // 오늘 하루 보지 않기
  203. const handleDontShowToday = (event, popupId, cookieSetting) => {
  204. // 체크박스가 체크된 경우에만 실행
  205. if (!event.target.checked) return;
  206. let days = 1; // 기본 1일
  207. if (cookieSetting === "forever") days = 365 * 10;
  208. // 10년 (사실상 영구)
  209. else if (cookieSetting === "today") days = 1;
  210. const cookieName = `popup_hide_${popupId}`;
  211. setCookie(cookieName, "true", days);
  212. closePopup(popupId);
  213. };
  214. onMounted(() => {
  215. console.log("[Popup] Component mounted, starting to fetch popups...");
  216. fetchPopups();
  217. });
  218. onUnmounted(() => {
  219. stopDrag();
  220. });
  221. </script>
  222. <style scoped>
  223. .popup--container {
  224. position: fixed;
  225. top: 0;
  226. left: 0;
  227. width: 100%;
  228. height: 100%;
  229. pointer-events: none;
  230. z-index: 999;
  231. }
  232. .popup--wrapper {
  233. position: absolute;
  234. pointer-events: auto;
  235. background: #fff;
  236. /* border: 1px solid #333; */
  237. box-shadow: 0 8px 32px rgba(0, 0, 0, 0.6);
  238. border-radius: 8px;
  239. overflow: hidden;
  240. display: flex;
  241. flex-direction: column;
  242. max-width: calc(100% - 40px);
  243. max-height: 90vh;
  244. }
  245. @media (max-width: 1024px) {
  246. .popup--wrapper {
  247. left: 20px !important;
  248. }
  249. }
  250. .lincoln .popup--header {
  251. background-color: #db784d;
  252. }
  253. .popup--header {
  254. display: flex;
  255. align-items: center;
  256. justify-content: space-between;
  257. padding: 12px 16px;
  258. background-color: #076fed;
  259. cursor: move;
  260. user-select: none;
  261. }
  262. .popup--header:active {
  263. cursor: grabbing;
  264. }
  265. .popup--title {
  266. font-size: 14px;
  267. font-weight: 600;
  268. color: #ffffff;
  269. flex: 1;
  270. overflow: hidden;
  271. text-overflow: ellipsis;
  272. white-space: nowrap;
  273. }
  274. .popup--close {
  275. height: 28px;
  276. background: rgba(255, 255, 255, 0.1);
  277. color: #000;
  278. border: 1px solid rgba(0, 0, 0, 0.2);
  279. border-radius: 4px;
  280. cursor: pointer;
  281. font-size: 12px;
  282. display: flex;
  283. align-items: center;
  284. justify-content: center;
  285. transition: all 0.2s;
  286. flex-shrink: 0;
  287. margin-left: 10px;
  288. padding: 0 20px;
  289. }
  290. .popup--close:hover {
  291. background: rgba(255, 255, 255, 0.2);
  292. border-color: rgba(255, 255, 255, 0.3);
  293. }
  294. .popup--content {
  295. flex: 1;
  296. overflow: auto;
  297. background: #fff;
  298. min-height: 0;
  299. }
  300. .popup--html,
  301. .popup--image {
  302. padding: 20px;
  303. color: #ffffff;
  304. }
  305. .popup--html {
  306. background: #fff;
  307. }
  308. .popup--html :deep(p),
  309. .popup--html :deep(div),
  310. .popup--html :deep(span) {
  311. color: #444;
  312. }
  313. .popup--html :deep(a) {
  314. color: #4a9eff;
  315. }
  316. .popup--image {
  317. padding: 0;
  318. display: flex;
  319. align-items: center;
  320. justify-content: center;
  321. background: #0a0a0a;
  322. }
  323. .popup--image img {
  324. max-width: 100%;
  325. max-height: 100%;
  326. object-fit: contain;
  327. }
  328. .popup--footer {
  329. padding: 12px 20px;
  330. border-top: 1px solid #333;
  331. flex-shrink: 0;
  332. color: #000;
  333. display: flex;
  334. align-items: center;
  335. justify-content: space-between;
  336. }
  337. .popup--footer.solo {
  338. justify-content: flex-end;
  339. }
  340. .popup--checkbox {
  341. display: flex;
  342. align-items: center;
  343. gap: 8px;
  344. font-size: 14px;
  345. cursor: pointer;
  346. user-select: none;
  347. color: #444;
  348. }
  349. .popup--checkbox input[type="checkbox"] {
  350. cursor: pointer;
  351. accent-color: #4a9eff;
  352. }
  353. .lincoln .popup--checkbox input[type="checkbox"] {
  354. accent-color: #db784d;
  355. }
  356. .popup--checkbox span {
  357. color: #444;
  358. }
  359. @media (max-width: 768px) {
  360. .popup--wrapper {
  361. left: 50% !important;
  362. transform: translateX(-50%);
  363. max-width: calc(100% - 40px);
  364. }
  365. }
  366. </style>