main.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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/builder/lists/`;
  9. // }
  10. // }
  11. // window.onload = function () {
  12. // // hhh_user_api();
  13. // if (screen.width >= 991) {
  14. // window.location.href = `https://hhh.com.tw/builder/lists/`;
  15. // }
  16. // }
  17. let page = 1; // 當前頁數
  18. let pageSize = 18; // 每頁數量
  19. let isFirstLoad = true; // 初始載入
  20. // 列表篩選
  21. async function dataSearch(type = "") {
  22. let url;
  23. let input = $(".keywords").val();
  24. $('#builderSpinner').show();
  25. $('#builderList').hide();
  26. if (isFirstLoad && input === "") {
  27. // 第一次載入使用本地 JSON 文件
  28. url = './json/builders_lists_data.json';
  29. } else {
  30. // 後續使用 API (預設排序為 recommend)
  31. url = `https://m3.hhh.com.tw:18673/builder_search?page=${page}&page_size=${pageSize}`;
  32. if (input !== "") {
  33. let isExist = filterList.some((item) => item.id === "keyword"); // 判斷是否已存在關鍵字
  34. const newItem = {
  35. id: "keyword",
  36. text: "關鍵字",
  37. value: input
  38. };
  39. if (!isExist) {
  40. filterList.push(newItem);
  41. createFilterHtml(newItem);
  42. } else {
  43. // 移除原本關鍵字
  44. $('.budget p.me-1').each(function () {
  45. if ($(this).text().includes('關鍵字')) {
  46. $(this).closest('.me-3').remove();
  47. }
  48. });
  49. if (filterList.length === 0) {
  50. $('#removeResultBtn').hide();
  51. }
  52. filterList = filterList.filter(item => item.text !== "關鍵字");
  53. filterList.push(newItem);
  54. createFilterHtml(newItem);
  55. }
  56. // $(".keywords").val("");
  57. // url += `&keyword=${input}`;
  58. }
  59. if (filterList.length) {
  60. filterList.map(item => {
  61. url += `&${item.id}=${item.value}`;
  62. });
  63. }
  64. }
  65. try {
  66. const response = await axios.get(url);
  67. console.log('response.data', response.data);
  68. let totalCount = response.data.total_count;
  69. let totalPages = Math.ceil(totalCount / pageSize);
  70. $("#totalCount").html(totalCount.toLocaleString());
  71. if (totalPages) {
  72. $('.filter-list .pagination').show();
  73. setPagination(totalPages); // 分頁處理
  74. } else {
  75. $('.filter-list .pagination').hide();
  76. }
  77. let resultHtml = '';
  78. if (response.data.videos.length) {
  79. response.data.videos.forEach((item) => {
  80. resultHtml += `
  81. <div class="col-md-4 mb-4">
  82. <a href="${item.DesignerLink}">
  83. <div class="card lists-card">
  84. <div class="position-relative">
  85. <img src="../../img/icon/play.svg" class="play-img${item.Is_video === '0' ? ' d-none' : ''}" alt="play-img">
  86. <img src="${item.BuilderCoverImg}" class="cover-img" alt="${item.BuilderTitle}">
  87. </div>
  88. <div class="card-body p-4">
  89. <section>
  90. <h5 class="text-dark">${item.BuilderTitle}</h5>
  91. <p class="text-dark my-3">${item.BuilderDescr}</p>
  92. <h6 class="mb-0 text-muted fw-normal">
  93. ${item.BuilderAddress}
  94. </h6>
  95. </section>
  96. </div>
  97. </div>
  98. </a>
  99. </div>`;
  100. });
  101. } else {
  102. resultHtml += "<p class='text-center mt-5'>找不到符合的資料,請重新搜尋。</p>"
  103. }
  104. $('#builderList').html(resultHtml);
  105. setTimeout(() => {
  106. $('#builderList').show();
  107. $('#builderSpinner').hide();
  108. }, 100)
  109. // 更新初始載入狀態
  110. if (isFirstLoad) {
  111. isFirstLoad = false;
  112. }
  113. } catch (error) {
  114. console.log("error", error);
  115. }
  116. }
  117. dataSearch();