notice.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <template>
  2. <main>
  3. <TopVisual :className="className" :title="title" :navigation="navigation" />
  4. <section class="notice--section">
  5. <div class="sub--container type2">
  6. <div class="title--wrap">
  7. <h2 class="title">공지사항</h2>
  8. </div>
  9. <div class="search--wrap">
  10. <USelect v-model="searchValue" :items="searchItems" />
  11. <UInput
  12. v-model="searchKeyword"
  13. placeholder="검색어를 입력해주세요."
  14. @keyup.enter="performSearch"
  15. />
  16. <UButton @click="performSearch" class="search--btn"></UButton>
  17. </div>
  18. <div class="notice--wrap">
  19. <div class="notice--list">
  20. <NuxtLink
  21. v-for="news in paginatedNews"
  22. :key="news.id"
  23. :to="news.link"
  24. class="notice"
  25. >
  26. <span class="news--index">{{ news.id }}</span>
  27. <h4>{{ news.title }}</h4>
  28. <span class="news--date">{{ news.date }}</span>
  29. </NuxtLink>
  30. </div>
  31. <div class="pagination--wrap">
  32. <UButton
  33. @click="prevPage"
  34. class="prev--btn"
  35. :disabled="currentPage === 1"
  36. ></UButton>
  37. <div class="numbs">
  38. <UButton
  39. v-for="page in totalPages"
  40. :key="page"
  41. @click="goToPage(page)"
  42. :class="{ active: currentPage === page }"
  43. >
  44. {{ page }}
  45. </UButton>
  46. </div>
  47. <UButton
  48. @click="nextPage"
  49. class="next--btn"
  50. :disabled="currentPage === totalPages"
  51. ></UButton>
  52. </div>
  53. </div>
  54. </div>
  55. </section>
  56. </main>
  57. </template>
  58. <script setup>
  59. import { ref, computed, onMounted } from "vue";
  60. import TopVisual from "~/components/topVisual.vue";
  61. const searchItems = ref(["제목", "내용", "제목+내용"]);
  62. const searchValue = ref("제목");
  63. const searchKeyword = ref("");
  64. const totalCount = ref(0);
  65. const loading = ref(true);
  66. const className = ref("contact");
  67. const title = ref("Contact");
  68. const navigation = ref([
  69. {
  70. name: "Contact",
  71. link: "/contact/notice",
  72. gnbList: [
  73. { name: "Company", link: "/company/intro" },
  74. { name: "Product", link: "/products/materials" },
  75. { name: "Technology", link: "/technology/facilities" },
  76. { name: "Media", link: "/media/news" },
  77. { name: "Contact", link: "/contact/notice" },
  78. ],
  79. },
  80. {
  81. name: "공지사항",
  82. link: "/contact/notice",
  83. gnbList: [
  84. { name: "공지사항", link: "/contact/notice" },
  85. { name: "FAQ", link: "/contact/faq" },
  86. { name: "고객센터", link: "/contact/support" },
  87. { name: "오시는길", link: "/contact/location" },
  88. ],
  89. },
  90. ]);
  91. // 뉴스 데이터 배열 - API에서 받아올 것
  92. const newsData = ref([]);
  93. // 검색 실행 함수
  94. const performSearch = async () => {
  95. currentPage.value = 1; // 검색시 첫 페이지로 이동
  96. await fetchNoticeList(1);
  97. };
  98. // 검색 초기화 함수
  99. const resetSearch = async () => {
  100. searchKeyword.value = "";
  101. searchValue.value = "제목";
  102. currentPage.value = 1;
  103. await fetchNoticeList(1);
  104. };
  105. // API에서 공지사항 데이터 가져오기
  106. const fetchNoticeList = async (page = 1) => {
  107. try {
  108. loading.value = true;
  109. // 검색 종류 매핑
  110. const getSearchKind = (searchType) => {
  111. switch (searchType) {
  112. case "제목": return "title";
  113. case "내용": return "contents";
  114. case "제목+내용": return "title_contents";
  115. default: return "title";
  116. }
  117. };
  118. // CodeIgniter 방식으로 호출
  119. const response = await $postForm(`/board_list/notice`, {
  120. page: page,
  121. searchKind: getSearchKind(searchValue.value),
  122. searchKeyword: searchKeyword.value || "",
  123. });
  124. // 백엔드가 JSON으로 응답하는지 확인
  125. if (response && typeof response === "object") {
  126. // JSON 응답인 경우
  127. if (response.success && response.list) {
  128. // 전체 개수와 현재 페이지를 기준으로 번호 계산
  129. totalCount.value = response.totalCount || 0;
  130. const currentPageNum = page || 1;
  131. const pageSize = 20; // 백엔드의 페이지 사이즈와 동일
  132. newsData.value = response.list.map((item, index) => {
  133. // 번호 = 전체개수 - ((현재페이지-1) * 페이지크기 + 인덱스)
  134. const displayNumber = totalCount.value - ((currentPageNum - 1) * pageSize + index);
  135. return {
  136. id: displayNumber, // 순차적인 번호로 표시
  137. title: item.title,
  138. date: item.regdate,
  139. link: `/contact/noticeView?idx=${item.board_idx}`, // 실제 링크는 board_idx 사용
  140. };
  141. });
  142. } else {
  143. console.error("JSON 응답 형식이 올바르지 않습니다:", response);
  144. newsData.value = [];
  145. }
  146. } else if (typeof response === "string") {
  147. // HTML 응답인 경우 (백엔드에서 AJAX 감지 실패시)
  148. console.warn("HTML 응답을 받았습니다. AJAX 감지가 실패했을 수 있습니다.");
  149. console.log("HTML 내용:", response.substring(0, 200) + "...");
  150. newsData.value = [];
  151. } else {
  152. console.error("예상하지 못한 응답 형식:", typeof response, response);
  153. newsData.value = [];
  154. }
  155. } catch (error) {
  156. console.error("공지사항 데이터 로드 실패:", error);
  157. // 에러시 기본 더미 데이터 사용
  158. } finally {
  159. loading.value = false;
  160. }
  161. };
  162. // 페이지네이션 로직
  163. const currentPage = ref(1);
  164. const itemsPerPage = 10;
  165. const backendPageSize = 20;
  166. const totalPages = computed(() => Math.ceil(totalCount.value / itemsPerPage));
  167. const paginatedNews = computed(() => {
  168. const start = (currentPage.value - 1) * itemsPerPage;
  169. const end = start + itemsPerPage;
  170. return newsData.value.slice(start, end);
  171. });
  172. // 백엔드에서 필요한 데이터가 있는지 확인하고 필요시 API 호출
  173. const needToFetchData = (targetPage) => {
  174. const startIndex = (targetPage - 1) * itemsPerPage;
  175. const endIndex = startIndex + itemsPerPage - 1;
  176. return endIndex >= newsData.value.length && newsData.value.length < totalCount.value;
  177. };
  178. const goToPage = async (page) => {
  179. if (page >= 1 && page <= totalPages.value) {
  180. currentPage.value = page;
  181. if (needToFetchData(page)) {
  182. // 백엔드 페이지 계산: 프론트 페이지를 백엔드 페이지로 변환
  183. const backendPage = Math.ceil((page * itemsPerPage) / backendPageSize);
  184. await fetchNoticeList(backendPage);
  185. }
  186. }
  187. };
  188. const nextPage = async () => {
  189. if (currentPage.value < totalPages.value) {
  190. await goToPage(currentPage.value + 1);
  191. }
  192. };
  193. const prevPage = async () => {
  194. if (currentPage.value > 1) {
  195. await goToPage(currentPage.value - 1);
  196. }
  197. };
  198. // 컴포넌트 마운트 시 데이터 로드
  199. onMounted(() => {
  200. fetchNoticeList(1);
  201. });
  202. </script>