main.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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/cases/lists/`;
  9. // }
  10. // }
  11. // window.onload = function () {
  12. // // hhh_user_api();
  13. // if (screen.width >= 991) {
  14. // window.location.href = `https://hhh.com.tw/cases/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. $('#dataSpinner').show();
  34. $('#dataList').hide();
  35. if (isFirstLoad && input === "") {
  36. // 第一次載入使用本地 JSON 文件
  37. url = './json/cases_lists_data.json';
  38. } else {
  39. // 後續使用 API
  40. url = `https://m3.hhh.com.tw:18673/case_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. if (item.id === "fee") {
  79. // 裝修預算
  80. switch (item.value) {
  81. case "100萬以下":
  82. url += `&${item.id}=1`;
  83. break;
  84. case "101~200萬":
  85. url += `&${item.id}=2`;
  86. break;
  87. case "201~300萬":
  88. url += `&${item.id}=3`;
  89. break;
  90. case "301萬以上":
  91. url += `&${item.id}=4`;
  92. break;
  93. default:
  94. break;
  95. }
  96. } else if (item.id === "area") {
  97. // 房屋坪數
  98. switch (item.value) {
  99. case "20坪以下":
  100. url += `&${item.id}=1`;
  101. break;
  102. case "21-30坪":
  103. url += `&${item.id}=2`;
  104. break;
  105. case "31-50坪":
  106. url += `&${item.id}=3`;
  107. break;
  108. case "51坪以上":
  109. url += `&${item.id}=4`;
  110. break;
  111. default:
  112. break;
  113. }
  114. } else {
  115. url += `&${item.id}=${item.value}`;
  116. }
  117. });
  118. }
  119. }
  120. try {
  121. const response = await axios.get(url);
  122. console.log('response.data.cases', response.data.cases);
  123. let totalCount = response.data.total_count;
  124. let totalPages = Math.ceil(totalCount / pageSize);
  125. $("#totalCount").html(totalCount.toLocaleString());
  126. if (totalPages) {
  127. $('.filter-list .pagination').show();
  128. setPagination(totalPages); // 分頁處理
  129. } else {
  130. $('.filter-list .pagination').hide();
  131. }
  132. let resultHtml = '';
  133. if (response.data.cases.length) {
  134. response.data.cases.forEach((item) => {
  135. let tagList = item.CaseTag.split(',');
  136. let tagsHtml = '';
  137. tagList.forEach((tag) => {
  138. if (tag !== "") {
  139. tagsHtml += `<span class="tag-item" data-tag="${tag}">${tag}</span>`;
  140. }
  141. });
  142. resultHtml += `
  143. <div class="col-md-4 mb-4">
  144. <div class="card lists-card">
  145. <a href="${item.CaseLink}">
  146. <img src="${item.CaseCoverImg}" class="cover-img" alt="${item.DesignerName} ${item.DesignerTitle}">
  147. </a>
  148. <div class="card-body d-flex flex-column">
  149. <div class="d-flex align-items-center">
  150. <div class="person-img me-3 me-md-2 me-lg-3" style="background-image: url('${item.DesignerCoverImg}');"></div>
  151. <a href="${item.DesignerLink}" class="link">
  152. <h5 class="text-muted mb-2">${item.DesignerTitle}</h5>
  153. <h6 class="mb-0 text-dark">
  154. <span class="font-weight-bold">
  155. ${item.DesignerName}
  156. </span>
  157. 設計師
  158. </h6>
  159. </section>
  160. </a>
  161. </div>
  162. <a href="${item.CaseLink}" class="link">
  163. <p class="mt-3 text-dark title">${item.CaseTitle}</p>
  164. </a>
  165. <div class="tags-container my-3 me-auto">${tagsHtml}</div>
  166. <small class="d-block ms-auto mt-auto date-item">上架日期 ${item.CaseSdate}</small>
  167. </div>
  168. </div>
  169. </div >`;
  170. });
  171. } else {
  172. resultHtml += "<p class='text-center mt-5'>找不到符合的資料,請重新搜尋。</p>"
  173. }
  174. $('#dataList').html(resultHtml);
  175. setTimeout(() => {
  176. $('#dataList').show();
  177. $('#dataSpinner').hide();
  178. }, 100)
  179. // 更新初始載入狀態
  180. if (isFirstLoad) {
  181. isFirstLoad = false;
  182. }
  183. } catch (error) {
  184. console.log("error", error);
  185. }
  186. }
  187. dataSearch();
  188. // ga 設計師搜尋 (熱搜關鍵字)
  189. $(".search-bar-keyword li a").on("click", (e) => {
  190. dataLayer.push({
  191. 'event': 'Click_Promo_Keyword',
  192. 'promo_keyword': `${e.target.innerText}`,
  193. 'promo_category': 'Designer_Search'
  194. });
  195. });
  196. // 新的搜尋欄位處理
  197. function ga4_keywords() {
  198. let elements = document.querySelectorAll(".me-1");
  199. // 創建一個數組來存儲所有元素的文本內容
  200. let keywords = Array.from(elements).map(element => {
  201. let text = element.innerText;
  202. // 查找 "關鍵字:" 的起始位置
  203. let keywordIndex = text.indexOf("關鍵字:");
  204. if (keywordIndex !== -1) {
  205. // 提取 "關鍵字:" 後面的部分
  206. return text.substring(keywordIndex + "關鍵字:".length).trim();
  207. }
  208. return ""; // 如果沒有找到 "關鍵字:",返回空字符串
  209. });
  210. // 過濾掉空字符串,並將剩餘文本合併為一個字符串
  211. let keyword = keywords.filter(text => text !== "").join(", ");
  212. dataLayer.push({
  213. 'event': 'View_Search_Results',
  214. 'search_term': `${keyword}`,
  215. 'search_category': 'Designer_Search'
  216. });
  217. }
  218. document.getElementById("keywordInput").addEventListener("keydown", function (event) {
  219. if (event.key === "Enter") {
  220. ga4_keywords();
  221. }
  222. });