Popup.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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
  16. class="popup--header"
  17. @mousedown="startDrag($event, index)"
  18. @touchstart="startDrag($event, index)"
  19. >
  20. <span class="popup--title">{{ popup.title }}</span>
  21. <button
  22. class="popup--close"
  23. @click="closePopup(popup.id)"
  24. aria-label="팝업 닫기"
  25. >
  26. </button>
  27. </div>
  28. <!-- 스크롤 가능한 콘텐츠 영역 -->
  29. <div class="popup--content">
  30. <!-- HTML 타입 팝업 -->
  31. <div v-if="popup.type === 'html'" class="popup--html" v-html="popup.content"></div>
  32. <!-- 이미지 타입 팝업 -->
  33. <div v-else-if="popup.type === 'image'" class="popup--image">
  34. <a
  35. v-if="popup.link_url"
  36. :href="popup.link_url"
  37. target="_blank"
  38. rel="noopener noreferrer"
  39. >
  40. <img :src="getImageUrl(popup.image_url)" :alt="popup.title" />
  41. </a>
  42. <img v-else :src="getImageUrl(popup.image_url)" :alt="popup.title" />
  43. </div>
  44. </div>
  45. <!-- 하단 고정 푸터 -->
  46. <div v-if="popup.cookie_setting !== 'none'" class="popup--footer">
  47. <label class="popup--checkbox">
  48. <input
  49. type="checkbox"
  50. @change="(e) => handleDontShowToday(e, popup.id, popup.cookie_setting)"
  51. />
  52. <span v-if="popup.cookie_setting === 'today'">오늘 하루 창 띄우지 않음</span>
  53. <span v-else-if="popup.cookie_setting === 'forever'">다시는 창을 띄우지 않음</span>
  54. </label>
  55. </div>
  56. </div>
  57. </div>
  58. </template>
  59. <script setup>
  60. import { ref, onMounted, onUnmounted } from 'vue'
  61. import axios from 'axios'
  62. const API_BASE_URL = 'https://gojinaudi.mycafe24.com/api'
  63. const visiblePopups = ref([])
  64. const popupPositions = ref([])
  65. const { getImageUrl } = useImage()
  66. // 드래그 상태
  67. const dragState = ref({
  68. isDragging: false,
  69. currentIndex: null,
  70. startX: 0,
  71. startY: 0,
  72. initialLeft: 0,
  73. initialTop: 0
  74. })
  75. // 쿠키 가져오기
  76. const getCookie = (name) => {
  77. if (typeof document === 'undefined') return null
  78. const value = `; ${document.cookie}`
  79. const parts = value.split(`; ${name}=`)
  80. if (parts.length === 2) return parts.pop().split(';').shift()
  81. return null
  82. }
  83. // 쿠키 설정
  84. const setCookie = (name, value, days) => {
  85. if (typeof document === 'undefined') return
  86. const date = new Date()
  87. date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000)
  88. const expires = `expires=${date.toUTCString()}`
  89. document.cookie = `${name}=${value};${expires};path=/`
  90. }
  91. // 팝업 데이터 가져오기
  92. const fetchPopups = async () => {
  93. try {
  94. const response = await axios.get(`${API_BASE_URL}/popup/active`)
  95. if (response.data.success && response.data.data) {
  96. // 쿠키 설정 필드 정규화 (cookie_setting 또는 cookie_type 지원)
  97. const popups = response.data.data.map(popup => ({
  98. ...popup,
  99. cookie_setting: popup.cookie_setting || popup.cookie_type || 'none'
  100. }))
  101. // 쿠키로 숨긴 팝업 필터링
  102. visiblePopups.value = popups.filter((popup) => {
  103. const cookieName = `popup_hide_${popup.id}`
  104. return !getCookie(cookieName)
  105. })
  106. // 초기 위치 설정
  107. popupPositions.value = visiblePopups.value.map(() => ({}))
  108. }
  109. } catch (error) {
  110. console.error('[Popup] Failed to fetch popups:', error)
  111. }
  112. }
  113. // 드래그 시작
  114. const startDrag = (event, index) => {
  115. event.preventDefault()
  116. const e = event.touches ? event.touches[0] : event
  117. const popup = visiblePopups.value[index]
  118. dragState.value = {
  119. isDragging: true,
  120. currentIndex: index,
  121. startX: e.clientX,
  122. startY: e.clientY,
  123. initialLeft: parseInt(popupPositions.value[index]?.left || popup.position_left),
  124. initialTop: parseInt(popupPositions.value[index]?.top || popup.position_top)
  125. }
  126. document.addEventListener('mousemove', onDrag)
  127. document.addEventListener('mouseup', stopDrag)
  128. document.addEventListener('touchmove', onDrag)
  129. document.addEventListener('touchend', stopDrag)
  130. }
  131. // 드래그 중
  132. const onDrag = (event) => {
  133. if (!dragState.value.isDragging) return
  134. const e = event.touches ? event.touches[0] : event
  135. const deltaX = e.clientX - dragState.value.startX
  136. const deltaY = e.clientY - dragState.value.startY
  137. const newLeft = dragState.value.initialLeft + deltaX
  138. const newTop = dragState.value.initialTop + deltaY
  139. popupPositions.value[dragState.value.currentIndex] = {
  140. left: newLeft + 'px',
  141. top: newTop + 'px'
  142. }
  143. }
  144. // 드래그 종료
  145. const stopDrag = () => {
  146. dragState.value.isDragging = false
  147. dragState.value.currentIndex = null
  148. document.removeEventListener('mousemove', onDrag)
  149. document.removeEventListener('mouseup', stopDrag)
  150. document.removeEventListener('touchmove', onDrag)
  151. document.removeEventListener('touchend', stopDrag)
  152. }
  153. // 팝업 닫기
  154. const closePopup = (popupId) => {
  155. const index = visiblePopups.value.findIndex(p => p.id === popupId)
  156. if (index !== -1) {
  157. visiblePopups.value.splice(index, 1)
  158. popupPositions.value.splice(index, 1)
  159. }
  160. }
  161. // 오늘 하루 보지 않기
  162. const handleDontShowToday = (event, popupId, cookieSetting) => {
  163. // 체크박스가 체크된 경우에만 실행
  164. if (!event.target.checked) return
  165. let days = 1 // 기본 1일
  166. if (cookieSetting === 'forever') days = 365 * 10 // 10년 (사실상 영구)
  167. else if (cookieSetting === 'today') days = 1
  168. const cookieName = `popup_hide_${popupId}`
  169. setCookie(cookieName, 'true', days)
  170. closePopup(popupId)
  171. }
  172. onMounted(() => {
  173. fetchPopups()
  174. })
  175. onUnmounted(() => {
  176. stopDrag()
  177. })
  178. </script>
  179. <style scoped>
  180. .popup--container {
  181. position: fixed;
  182. top: 0;
  183. left: 0;
  184. width: 100%;
  185. height: 100%;
  186. pointer-events: none;
  187. z-index: 9999;
  188. }
  189. .popup--wrapper {
  190. position: absolute;
  191. pointer-events: auto;
  192. background: #1a1a1a;
  193. border: 1px solid #333;
  194. box-shadow: 0 8px 32px rgba(0, 0, 0, 0.6);
  195. border-radius: 8px;
  196. overflow: hidden;
  197. display: flex;
  198. flex-direction: column;
  199. max-height: 90vh;
  200. }
  201. .popup--header {
  202. display: flex;
  203. align-items: center;
  204. justify-content: space-between;
  205. padding: 12px 16px;
  206. background: linear-gradient(to bottom, #2a2a2a, #1f1f1f);
  207. border-bottom: 1px solid #333;
  208. cursor: move;
  209. user-select: none;
  210. }
  211. .popup--header:active {
  212. cursor: grabbing;
  213. }
  214. .popup--title {
  215. font-size: 14px;
  216. font-weight: 600;
  217. color: #ffffff;
  218. flex: 1;
  219. overflow: hidden;
  220. text-overflow: ellipsis;
  221. white-space: nowrap;
  222. }
  223. .popup--close {
  224. width: 28px;
  225. height: 28px;
  226. background: rgba(255, 255, 255, 0.1);
  227. color: #ffffff;
  228. border: 1px solid rgba(255, 255, 255, 0.2);
  229. border-radius: 4px;
  230. cursor: pointer;
  231. font-size: 18px;
  232. display: flex;
  233. align-items: center;
  234. justify-content: center;
  235. transition: all 0.2s;
  236. flex-shrink: 0;
  237. margin-left: 10px;
  238. }
  239. .popup--close:hover {
  240. background: rgba(255, 255, 255, 0.2);
  241. border-color: rgba(255, 255, 255, 0.3);
  242. }
  243. .popup--content {
  244. flex: 1;
  245. overflow: auto;
  246. background: #1a1a1a;
  247. min-height: 0;
  248. }
  249. .popup--html,
  250. .popup--image {
  251. padding: 20px;
  252. color: #ffffff;
  253. }
  254. .popup--html {
  255. background: #1a1a1a;
  256. }
  257. .popup--html :deep(p),
  258. .popup--html :deep(div),
  259. .popup--html :deep(span) {
  260. color: #ffffff;
  261. }
  262. .popup--html :deep(a) {
  263. color: #4a9eff;
  264. }
  265. .popup--image {
  266. padding: 0;
  267. display: flex;
  268. align-items: center;
  269. justify-content: center;
  270. background: #0a0a0a;
  271. }
  272. .popup--image img {
  273. max-width: 100%;
  274. max-height: 100%;
  275. object-fit: contain;
  276. }
  277. .popup--footer {
  278. padding: 12px 20px;
  279. background: #222222;
  280. border-top: 1px solid #333;
  281. flex-shrink: 0;
  282. }
  283. .popup--checkbox {
  284. display: flex;
  285. align-items: center;
  286. gap: 8px;
  287. font-size: 14px;
  288. cursor: pointer;
  289. user-select: none;
  290. color: #ffffff;
  291. }
  292. .popup--checkbox input[type='checkbox'] {
  293. cursor: pointer;
  294. accent-color: #4a9eff;
  295. }
  296. .popup--checkbox span {
  297. color: #e0e0e0;
  298. }
  299. </style>