main.js 6.3 KB

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