main.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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/columns/lists/`;
  9. // }
  10. // }
  11. // window.onload = function () {
  12. // // hhh_user_api();
  13. // if (screen.width >= 991) {
  14. // window.location.href = `https://hhh.com.tw/columns/lists/`;
  15. // }
  16. // }
  17. // $(".spinners").hide(); // 隱藏讀取動畫
  18. let assignOrder = ""; // 當前排序
  19. // new, hot, recommend 排序 (預設推薦)
  20. $(".search-btn-filter button").click(function () {
  21. // 切換選取狀態
  22. $('.search-btn-filter').find('.active').removeClass('active');
  23. $(this).addClass('active');
  24. assignOrder = $(this).attr('id');
  25. dataSearch("order_by");
  26. });
  27. let columnType = ""; // 主要類別
  28. let columnSubType = ""; // 次要類別
  29. // 次要類別選單
  30. let subCategory = [
  31. {
  32. title: "編輯精選",
  33. list: ["居家趨勢", "人氣排行", "建築設計", "風格選店", "公益活動", "展演資訊"]
  34. },
  35. {
  36. title: "居家設計",
  37. list: ["小宅規劃", "老屋翻新", "風格營造", "配色佈置", "好宅特輯", "設計提案"]
  38. },
  39. {
  40. title: "裝修前線",
  41. list: ["新聞最前線", "預算分配", "施工流程", "建材知識", "裝潢撇步"]
  42. },
  43. {
  44. title: "生活PLUS",
  45. list: ["居家風水", "家事清潔", "收納技巧", "改造修繕", "退休好幸福"]
  46. },
  47. {
  48. title: "品牌好物",
  49. list: ["家具家飾", "美型家電", "推薦廚衛", "居家好物", "優質建材", "品牌新訊"]
  50. },
  51. {
  52. title: "房市焦點",
  53. list: ["房市新聞", "建案特搜", "買屋賣屋", "房貸稅務", "租房須知"]
  54. }
  55. ];
  56. // 主要篩選按鈕
  57. $('.category-btn .main button').on('click', function () {
  58. let btnText = $(this)[0].innerText;
  59. push_columns_GA(btnText)
  60. if (btnText === "所有文章") {
  61. columnType = "";
  62. } else if (btnText === "主題企劃") {
  63. window.open("https://hhh.com.tw/topic/lists/1-page/new-sort/", "_blank"); // 另開頁面
  64. return;
  65. } else {
  66. columnType = btnText;
  67. }
  68. page = 1;
  69. columnSubType = "";
  70. $('.category-btn .main button').removeClass('active');
  71. $(this).toggleClass('active');
  72. $('#dataList').html("");
  73. dataSearch();
  74. let subBtnHtml = [];
  75. subCategory.map(item => {
  76. if (btnText !== "所有文章" && btnText !== "主題企劃") {
  77. if (item.title === btnText) {
  78. $('.category-btn .sub').show();
  79. item.list.forEach(item => {
  80. let dom = `<button onclick="clickSubBtn(event, '${item}')">${item}</button>`;
  81. subBtnHtml.push(dom);
  82. })
  83. $('.category-btn .sub').html(subBtnHtml);
  84. }
  85. } else {
  86. $('.category-btn .sub').hide();
  87. }
  88. })
  89. });
  90. // 次要篩選按鈕
  91. function clickSubBtn(event, val) {
  92. if (val === "小宅規劃" || val === "老屋翻新") {
  93. push_columns_GA(val);
  94. }
  95. page = 1;
  96. columnSubType = val;
  97. $('.category-btn .sub button').removeClass('active');
  98. $(event.currentTarget).toggleClass('active');
  99. $('#dataList').html("");
  100. dataSearch();
  101. }
  102. let page = 1; // 當前頁數
  103. let pageSize = 18; // 每頁數量
  104. let isFirstLoad = true; // 初始載入
  105. let noData = false; // 無資料
  106. // 列表篩選
  107. async function dataSearch(type = "") {
  108. let url;
  109. let input = $(".keywords").val();
  110. if (isFirstLoad && input === "") {
  111. $('#dataSpinner').show();
  112. // 第一次載入使用本地 JSON 文件
  113. url = './json/columns_lists_data.json';
  114. } else {
  115. // 後續使用 API
  116. url = `https://m3.hhh.com.tw:18673/column_search?page=${page}&page_size=${pageSize}`;
  117. if (assignOrder === "") {
  118. url += "&order_by=new"; // 預設排序為 new
  119. } else {
  120. url += `&order_by=${assignOrder}`;
  121. }
  122. if (input !== "") {
  123. url += `&keyword=${input}`;
  124. }
  125. if (type === "keyword") {
  126. $('#dataList').html("");
  127. }
  128. // 篩選按鈕
  129. if (columnType !== "") {
  130. url += `&ctype=${columnType}`;
  131. }
  132. if (columnSubType !== "") {
  133. url += `&ctype_sub=${columnSubType}`;
  134. }
  135. }
  136. try {
  137. const response = await axios.get(url);
  138. console.log('response.data.columns', response.data.columns);
  139. let totalCount = response.data.total_count;
  140. $("#totalCount").html(totalCount.toLocaleString());
  141. let resultHtml = '';
  142. if (response.data.columns.length) {
  143. if (response.data.columns.length === 18) {
  144. noData = false;
  145. } else {
  146. noData = true;
  147. }
  148. // 取得當前日期並計算三天前的日期
  149. const currentDate = new Date();
  150. const threeDaysAgo = new Date();
  151. threeDaysAgo.setDate(currentDate.getDate() - 3);
  152. response.data.columns.forEach((item, index) => {
  153. const columnDate = new Date(item.ColumnDate); // 將 ColumnDate 轉換成日期物件
  154. const isNew = columnDate >= threeDaysAgo && columnDate <= currentDate; // 判斷 ColumnDate 是否在三天內
  155. const newItemClass = isNew ? '' : 'd-none'; // 非三天內則隱藏 New 標籤
  156. let tagList = item.ColumnTag.split(',');
  157. let tagsHtml = '';
  158. tagList.forEach((tag) => {
  159. if (tag !== "") {
  160. tagsHtml += `<span class="tag-item" data-tag="${tag}">${tag}</span>`;
  161. }
  162. });
  163. resultHtml += `
  164. <div class="col-md-4 mb-4 ${index === 1 ? 'mt-md-4' : ''} ${index > 2 && index % 3 !== 1 ? 'mt-masonry' : ''}">
  165. <a href="${item.ColumnLink}">
  166. <div class="card lists-card h-auto">
  167. <img src="${item.ColumnCoverImg}" class="cover-img" alt="${item.ColumnTitle}">
  168. <div class="card-body d-flex flex-column">
  169. <h5 class="text-dark title">${item.ColumnTitle}</h5>
  170. <div class="tags-container columns-tag mt-3 me-auto">${tagsHtml}</div>
  171. </div>
  172. <span class="new-item ${newItemClass}">NEW</span>
  173. </div>
  174. </a>
  175. </div>`;
  176. });
  177. $('#dataList').append(resultHtml);
  178. } else {
  179. resultHtml += "<p class='text-center mt-5'>找不到符合的資料,請重新搜尋。</p>"
  180. $('#dataList').append(resultHtml);
  181. $(".spinners").hide();
  182. noData = true;
  183. }
  184. setTimeout(() => {
  185. // $('#dataList').show();
  186. $('#dataSpinner').hide();
  187. $(".spinners").hide();
  188. }, 100)
  189. setTimeout(() => {
  190. isLoading = false;
  191. if (response.data.columns.length === 18) {
  192. $(".spinners").show(); // 顯示讀取動畫
  193. } else {
  194. $(".spinners").hide(); // 隱藏讀取動畫
  195. }
  196. }, 2000)
  197. // 更新初始載入狀態
  198. if (isFirstLoad) {
  199. isFirstLoad = false;
  200. }
  201. } catch (error) {
  202. console.log("error", error);
  203. }
  204. }
  205. dataSearch();
  206. let isLoading = false;
  207. // 判斷是否捲動到底部
  208. $(window).on('scroll', function () {
  209. // 資料 < 18 筆取消判斷
  210. if (noData) {
  211. return;
  212. }
  213. let scrollTop = $(window).scrollTop(); // 捲軸頂部位置
  214. let windowHeight = $(window).height(); // 視窗高度
  215. let footerOffset = $('#footer').offset().top; // 取得 footer 位置
  216. if (!isLoading && scrollTop + windowHeight >= footerOffset - 150) {
  217. isLoading = true;
  218. // page++;
  219. setTimeout(() => {
  220. page++;
  221. dataSearch();
  222. }, 300)
  223. }
  224. });
  225. // 發送 GA
  226. function push_columns_GA(key) {
  227. let eventLabel = '';
  228. switch (key) {
  229. case '編輯精選':
  230. eventLabel = 'editorchoose_article';
  231. break;
  232. case '居家設計':
  233. eventLabel = 'housedesign_article';
  234. break;
  235. case '裝修前線':
  236. eventLabel = 'decoration_article';
  237. break;
  238. case '生活PLUS':
  239. eventLabel = 'plus_article';
  240. break;
  241. case '品牌好物':
  242. eventLabel = 'gooditem_article';
  243. break;
  244. case '房市焦點':
  245. eventLabel = 'focus_article';
  246. break;
  247. case '小宅規劃':
  248. eventLabel = 'small_article';
  249. break;
  250. case '老屋翻新':
  251. eventLabel = 'renovation_condition_article';
  252. break;
  253. default:
  254. eventLabel = 'unknown';
  255. }
  256. dataLayer.push({ 'event': eventLabel });
  257. // 發送 Facebook Pixel 事件
  258. fbq('trackCustom', eventLabel);
  259. }
  260. // ga 設計師搜尋 (熱搜關鍵字)
  261. $(".search-bar-keyword li a").on("click", (e) => {
  262. dataLayer.push({
  263. 'event': 'Click_Promo_Keyword',
  264. 'promo_keyword': `${e.target.innerText}`,
  265. 'promo_category': 'Designer_Search'
  266. });
  267. });
  268. // 新的搜尋欄位處理
  269. function ga4_keywords() {
  270. let elements = document.querySelectorAll(".me-1");
  271. // 創建一個數組來存儲所有元素的文本內容
  272. let keywords = Array.from(elements).map(element => {
  273. let text = element.innerText;
  274. // 查找 "關鍵字:" 的起始位置
  275. let keywordIndex = text.indexOf("關鍵字:");
  276. if (keywordIndex !== -1) {
  277. // 提取 "關鍵字:" 後面的部分
  278. return text.substring(keywordIndex + "關鍵字:".length).trim();
  279. }
  280. return ""; // 如果沒有找到 "關鍵字:",返回空字符串
  281. });
  282. // 過濾掉空字符串,並將剩餘文本合併為一個字符串
  283. let keyword = keywords.filter(text => text !== "").join(", ");
  284. dataLayer.push({
  285. 'event': 'View_Search_Results',
  286. 'search_term': `${keyword}`,
  287. 'search_category': 'Designer_Search'
  288. });
  289. }
  290. document.getElementById("keywordInput").addEventListener("keydown", function (event) {
  291. if (event.key === "Enter") {
  292. ga4_keywords();
  293. }
  294. });