main.js 4.1 KB

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