main.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // 載入共用 template
  2. $('#navbar').load('../../template/navbar.html');
  3. $('#footer').load('../../template/footer.html');
  4. $('#btn-box').load('../../template/button.html');
  5. $('#topCarousel').load('../../template/top_carousel.html');
  6. // window.onload = function () {
  7. // if (screen.width < 991) {
  8. // window.location.href = `https://m.hhh.com.tw/videos/lists/`;
  9. // }
  10. // }
  11. // window.onload = function () {
  12. // //hhh_user_api();
  13. // if (screen.width >= 991) {
  14. // window.location.href = `https://hhh.com.tw/videos/lists/`;
  15. // }
  16. // }
  17. let assignOrder = ""; // 當前排序
  18. // new, hot, recommend 排序 (預設推薦)
  19. $(".search-btn-filter button").click(function () {
  20. // 切換選取狀態
  21. $('.search-btn-filter').find('.active').removeClass('active');
  22. $(this).addClass('active');
  23. assignOrder = $(this).attr('id');
  24. dataSearch("order_by");
  25. });
  26. let page = 1; // 當前頁數
  27. let pageSize = 18; // 每頁數量
  28. let isFirstLoad = true; // 初始載入
  29. // 列表篩選
  30. async function dataSearch(type = "") {
  31. let url;
  32. let input = $(".keywords").val();
  33. $('#videoSpinner').show();
  34. $('#dataList').hide();
  35. if (isFirstLoad && input === "") {
  36. // 第一次載入使用本地 JSON 文件
  37. url = './json/videos_lists_data.json';
  38. } else {
  39. // 後續使用 API
  40. url = `https://m3.hhh.com.tw:18673/video_search?page=${page}&page_size=${pageSize}`;
  41. // if (type === "order_by") {
  42. // url += `&${type}=${assignOrder}`;
  43. // }
  44. if (assignOrder === "") {
  45. url += "&order_by=new"; // 預設排序為 new
  46. } else {
  47. url += `&order_by=${assignOrder}`;
  48. }
  49. if (input !== "") {
  50. let isExist = filterList.some((item) => item.id === "keyword"); // 判斷是否已存在關鍵字
  51. const newItem = {
  52. id: "keyword",
  53. text: "關鍵字",
  54. value: input
  55. };
  56. if (!isExist) {
  57. filterList.push(newItem);
  58. createFilterHtml(newItem);
  59. } else {
  60. // 移除原本關鍵字
  61. $('.budget p.me-1').each(function () {
  62. if ($(this).text().includes('關鍵字')) {
  63. $(this).closest('.me-3').remove();
  64. }
  65. });
  66. if (filterList.length === 0) {
  67. $('#removeResultBtn').hide();
  68. }
  69. filterList = filterList.filter(item => item.text !== "關鍵字");
  70. filterList.push(newItem);
  71. createFilterHtml(newItem);
  72. }
  73. // $(".keywords").val("");
  74. // url += `&keyword=${input}`;
  75. }
  76. if (filterList.length) {
  77. filterList.map(item => {
  78. url += `&${item.id}=${item.value}`;
  79. });
  80. }
  81. }
  82. try {
  83. const response = await axios.get(url);
  84. console.log('response.data.videos', response.data.videos);
  85. let totalCount = response.data.total_count;
  86. let totalPages = Math.ceil(totalCount / pageSize);
  87. $("#totalCount").html(totalCount.toLocaleString());
  88. if (totalPages) {
  89. $('.filter-list .pagination').show();
  90. setPagination(totalPages); // 分頁處理
  91. } else {
  92. $('.filter-list .pagination').hide();
  93. }
  94. let resultHtml = '';
  95. if (response.data.videos.length) {
  96. response.data.videos.forEach((item) => {
  97. let tagsHtml = '';
  98. item.VideoTag.forEach((tag) => {
  99. if (tag !== "") {
  100. tagsHtml += `<span class="tag-item" data-tag="${tag}">${tag}</span>`;
  101. }
  102. });
  103. resultHtml += `
  104. <div class="col-md-4 mb-4">
  105. <div class="card lists-card">
  106. <a href="${item.VideoLink}">
  107. <img src="${item.VideoCoverImg}" class="video-cover-img" alt="${item.DesignerName} ${item.DesignerTitle}">
  108. </a>
  109. <div class="card-body d-flex flex-column align-items-center">
  110. <a href="${item.DesignerLink}" class="d-flex align-items-center w-100">
  111. <div class="person-img me-3 me-md-2 me-lg-3" style="background-image: url('${item.DesignerCoverImg}');"></div>
  112. <h5 class="text-muted mb-2">${item.DesignerTitle}</h5>
  113. </a>
  114. <a href="${item.VideoLink}">
  115. <h3 class="my-3 video-title">${item.VideoTitle}</h3>
  116. </a>
  117. <div class="tags-container mt-2 me-auto">${tagsHtml}</div>
  118. </div>
  119. </div>
  120. </div>`;
  121. });
  122. } else {
  123. resultHtml += "<p class='text-center mt-5'>找不到符合的資料,請重新搜尋。</p>"
  124. }
  125. $('#dataList').html(resultHtml);
  126. setTimeout(() => {
  127. $('#dataList').show();
  128. $('#videoSpinner').hide();
  129. }, 100)
  130. // 更新初始載入狀態
  131. if (isFirstLoad) {
  132. isFirstLoad = false;
  133. }
  134. } catch (error) {
  135. console.log("error", error);
  136. }
  137. }
  138. dataSearch();