main.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. let page = 1; // 當前頁數
  7. let pageSize = 18; // 每頁數量
  8. let isFirstLoad = true; // 初始載入
  9. // 列表篩選
  10. async function dataSearch(type = "") {
  11. let url;
  12. $('#builderSpinner').show();
  13. $('#builderList').hide();
  14. if (isFirstLoad) {
  15. // 第一次載入使用本地 JSON 文件
  16. // url = './json/designers_lists_data.json';
  17. url = `https://m3.hhh.com.tw:18673/builder_search?page=${page}&page_size=${pageSize}`;
  18. } else {
  19. // 後續使用 API (預設排序為 recommend)
  20. url = `https://m3.hhh.com.tw:18673/builder_search?page=${page}&page_size=${pageSize}`;
  21. let input = $(".keywords").val();
  22. if (input !== "") {
  23. let isExist = filterList.some((item) => item.id === "keyword"); // 判斷是否已存在關鍵字
  24. const newItem = {
  25. id: "keyword",
  26. text: "關鍵字",
  27. value: input
  28. };
  29. if (!isExist) {
  30. filterList.push(newItem);
  31. createFilterHtml(newItem);
  32. } else {
  33. // 移除原本關鍵字
  34. $('.budget p.me-1').each(function () {
  35. if ($(this).text().includes('關鍵字')) {
  36. $(this).closest('.me-3').remove();
  37. }
  38. });
  39. if (filterList.length === 0) {
  40. $('#removeResultBtn').hide();
  41. }
  42. filterList = filterList.filter(item => item.text !== "關鍵字");
  43. filterList.push(newItem);
  44. createFilterHtml(newItem);
  45. }
  46. $(".keywords").val("")
  47. // url += `&keyword=${input}`;
  48. }
  49. if (filterList.length) {
  50. filterList.map(item => {
  51. url += `&${item.id}=${item.value}`;
  52. });
  53. }
  54. }
  55. try {
  56. const response = await axios.get(url);
  57. console.log('response.data', response.data);
  58. // console.log('response.data.videos', response.data.videos);
  59. let totalCount = response.data.total_count;
  60. let totalPages = Math.ceil(totalCount / pageSize);
  61. $("#totalCount").html(totalCount);
  62. if (totalPages) {
  63. $('.filter-list .pagination').show();
  64. setPagination(totalPages); // 分頁處理
  65. } else {
  66. $('.filter-list .pagination').hide();
  67. }
  68. let resultHtml = '';
  69. if (response.data.videos.length) {
  70. response.data.videos.forEach((item) => {
  71. resultHtml += `
  72. <div class="col-md-4 mb-4">
  73. <a href="${item.DesignerLink}">
  74. <div class="card lists-card">
  75. <div class="position-relative">
  76. <img src="../../img/icon/play.svg" class="play-img${item.Is_video === '0' ? ' d-none' : ''}" alt="play-img">
  77. <img src="${item.BuilderCoverImg}" class="cover-img" alt="${item.BuilderTitle}">
  78. </div>
  79. <div class="card-body p-4">
  80. <section>
  81. <h5 class="text-dark">${item.BuilderTitle}</h5>
  82. <p class="text-dark my-3">${item.BuilderDescr}</p>
  83. <h6 class="mb-0 text-muted fw-normal">
  84. ${item.BuilderAddress}
  85. </h6>
  86. </section>
  87. </div>
  88. </div>
  89. </a>
  90. </div>`;
  91. });
  92. } else {
  93. resultHtml += "<p class='text-center mt-5'>找不到符合的資料,請重新搜尋。</p>"
  94. }
  95. $('#builderList').html(resultHtml);
  96. setTimeout(() => {
  97. $('#builderList').show();
  98. $('#builderSpinner').hide();
  99. }, 100)
  100. // 更新初始載入狀態
  101. if (isFirstLoad) {
  102. isFirstLoad = false;
  103. }
  104. } catch (error) {
  105. console.log("error", error);
  106. }
  107. }
  108. dataSearch();
  109. async function getData() {
  110. // 取得當前網址的查詢字串部分
  111. let urlParams = new URLSearchParams(window.location.search);
  112. // 從查詢字串中取得 'id' 的值
  113. let id = urlParams.get('id');
  114. let url = `https://m3.hhh.com.tw:18674/genbuilder?id=${id}&page=1`;
  115. console.log(id); // 顯示 "2058"
  116. try {
  117. const response = await axios.get(url);
  118. let data = response.data[0];
  119. $("#name").text(data.name);
  120. $("#city").text(data.city);
  121. $("#builder_name").text(data.name);
  122. $("#service_phone").text(data.service_phone);
  123. $("#service_phone").attr("href", "tel:" + data.service_phone);
  124. $("#detail").text(data.detail);
  125. if (data.detail_data.unit_price !== "") {
  126. $("#unit_price span").text(data.detail_data.unit_price);
  127. } else {
  128. $("#unit_price").hide();
  129. }
  130. // 判斷是否有值,若無則隱藏
  131. if (data.detail_data.construction_company !== "") {
  132. $("#construction_company span").text(data.detail_data.construction_company);
  133. } else {
  134. $("#construction_company").hide();
  135. }
  136. if (data.detail_data.builder_type !== "") {
  137. $("#builder_type span").text(data.detail_data.builder_type);
  138. } else {
  139. $("#builder_type").hide();
  140. }
  141. if (data.detail_data.configuration !== "") {
  142. $("#configuration span").text(data.detail_data.configuration);
  143. } else {
  144. $("#configuration").hide();
  145. }
  146. if (data.detail_data.house_type !== "") {
  147. $("#house_type span").text(data.detail_data.house_type);
  148. } else {
  149. $("#house_type").hide();
  150. }
  151. if (data.detail_data.address !== "") {
  152. $("#address a").text(data.detail_data.address);
  153. $("#address a").attr("href", "https://maps.google.com.tw/maps?q=" + data.detail_data.address);
  154. } else {
  155. $("#address").hide();
  156. }
  157. if (data.detail_data.phone !== "") {
  158. $("#phone span").text(data.detail_data.phone);
  159. } else {
  160. $("#phone").hide();
  161. }
  162. if (data.detail_data.website !== "") {
  163. $("#website a").text(data.detail_data.website);
  164. $("#website a").attr("href", data.detail_data.website);
  165. } else {
  166. $("#website").hide();
  167. }
  168. let sliderDom = "";
  169. // 建案影片
  170. if (data.yt.length) {
  171. // let dom = "";
  172. data.yt.map((item) => {
  173. sliderDom += `
  174. <div class="swiper-slide">
  175. <button class="video-item" data-yt-id="${item.yt_id}">
  176. <img src="${item.imgUrl}" alt="">
  177. <img src="../../img/icon/play.svg" class="play-img" alt="play-img">
  178. </button>
  179. </div>`;
  180. });
  181. $(document).on('click', '.video-item', function () {
  182. const videoId = $(this).data('yt-id'); // 取得 data-yt-id
  183. const videoUrl = `https://www.youtube.com/embed/${videoId}`; // 影片 url
  184. $('#videoFrame').attr('src', videoUrl); // 設置 iframe src
  185. $('#videoModal').modal('show'); // 顯示影片彈跳視窗
  186. });
  187. // 當 modal 關閉時清空 iframe src
  188. $('#videoModal').on('hidden.bs.modal', function () {
  189. $('#videoFrame').attr('src', '');
  190. });
  191. }
  192. // 建案圖片
  193. if (data.img.length) {
  194. data.img.map((item) => {
  195. sliderDom += `
  196. <div class="swiper-slide">
  197. <a href="${item.name}" data-src="${item.name}">
  198. <img src="${item.name}" alt="">
  199. </a>
  200. </div>`;
  201. // sliderDom += `
  202. // <a href="${item.name}" data-src="${item.name}">
  203. // <img src="${item.name}" alt="">
  204. // </a>`;
  205. })
  206. }
  207. $(".slider-list").append(sliderDom);
  208. // 標籤
  209. if (data.tags.length) {
  210. let dom = "";
  211. data.tags.map((item) => {
  212. dom += ` <a href="/builder/lists/${item}-keyword/" target="_blank">${item}</a>`;
  213. })
  214. $(".tag-list").html(dom);
  215. }
  216. // 輪播
  217. // $(".slider-list").slick({
  218. // dots: false,
  219. // infinite: false,
  220. // speed: 500,
  221. // autoplay: true,
  222. // autoplaySpeed: 3000,
  223. // slidesToScroll: 1,
  224. // arrows: true,
  225. // draggable: false, // 禁用鼠標拖曳功能
  226. // prevArrow:
  227. // '<button type="button" class="slick-prev"><i class="fas fa-chevron-left" style="font-size: 36px;color: #D4D4D4;transform: translateY(-10px);"></i></button>',
  228. // nextArrow:
  229. // '<button type="button" class="slick-next"><i class="fas fa-chevron-right" style="font-size: 36px;color: #D4D4D4;transform: translateY(-10px);"></i></button>',
  230. // });
  231. var swiper = new Swiper(".mySwiper", {
  232. navigation: {
  233. nextEl: ".swiper-button-next",
  234. prevEl: ".swiper-button-prev",
  235. },
  236. });
  237. console.log('$("#lightgallery")', $("#lightgallery"));
  238. console.log('$("#lightgallery length")', $("#lightgallery").length);
  239. const container = document.querySelector('.slider-list');
  240. console.log('container', container);
  241. window.lightGallery(container, {
  242. selector: 'a', // 對應圖片的 <a> 標籤
  243. plugins: [lgZoom, lgThumbnail],
  244. preload: 4
  245. });
  246. // 雷達圖繪製 Chart.js
  247. let chartRadarDOM;
  248. let chartRadarData;
  249. let chartRadarOptions;
  250. //載入雷達圖
  251. Chart.defaults.global.legend.display = false;
  252. Chart.defaults.global.defaultFontColor = 'rgba(0,0,74, 1)';
  253. chartRadarDOM = document.getElementById("radar-pc");
  254. chartRadarData;
  255. chartRadarOptions =
  256. {
  257. scale:
  258. {
  259. ticks:
  260. {
  261. fontSize: 12,
  262. beginAtZero: true,
  263. maxTicksLimit: 5,
  264. min: 0,
  265. max: 5,
  266. backdropPaddingX: 5,
  267. backdropPaddingY: 5,
  268. },
  269. pointLabels:
  270. {
  271. fontSize: 16,
  272. fontColor: '#AAA',
  273. fontStyle: 'bold',
  274. lineHeight: 'normal',
  275. },
  276. gridLines:
  277. {
  278. color: '#AAA'
  279. },
  280. }
  281. };
  282. let graphData = new Array();
  283. graphData.push(data.traffic_level);
  284. graphData.push(data.life_level);
  285. graphData.push(data.building_level);
  286. graphData.push(data.price_level);
  287. graphData.push(data.space_level);
  288. console.log(graphData);
  289. // CreateData
  290. chartRadarData = {
  291. labels: ['交通', '生活機能', '建材公設', '總價', '空間坪數'],
  292. datasets: [{
  293. label: data.name,
  294. backgroundColor: "rgba(238, 120, 0, 0.7)",
  295. borderColor: "rgba(238, 120, 0, 1)",
  296. pointBackgroundColor: "rgba(238, 120, 0, 1)",
  297. pointBorderWidth: 5,
  298. borderWidth: 1,
  299. layout: {
  300. padding: {
  301. left: 0,
  302. right: 0,
  303. top: 0,
  304. bottom: 0
  305. }
  306. },
  307. data: graphData
  308. }]
  309. };
  310. // Draw
  311. var chartRadar = new Chart(chartRadarDOM, {
  312. type: 'radar',
  313. data: chartRadarData,
  314. options: chartRadarOptions
  315. });
  316. // 推薦建案
  317. let otherBuilder = '<div class="row" data-masonry="{" percentPosition": true }">';
  318. data.other_builder.map(item => {
  319. let dom = `
  320. <div class="col-6 col-md-4 mb-3">
  321. <div class="card">
  322. <a href="${item.link}">
  323. <img src="../../img/icon/play.svg" class="play-img${item.is_video === '0' ? ' d-none' : ''}" alt="play-img">
  324. <img src="${item.img}" alt="" class="img-fluid card-img-top">
  325. <div class="card-body">
  326. <h5 class="card-title">${item.title}</h5>
  327. </div>
  328. </a>
  329. </div>
  330. </div>
  331. `;
  332. otherBuilder += dom;
  333. });
  334. otherBuilder += "</div>";
  335. $(".other-list .container").html(otherBuilder);
  336. // 確保圖片都載入完成,避免造成剛進入頁面時圖片被覆蓋
  337. $('.other-list .row').imagesLoaded().progress(function () {
  338. $('.other-list .row').masonry(); // 渲染整體畫面
  339. });
  340. console.log('getData', data);
  341. } catch (error) {
  342. console.log("error", error);
  343. }
  344. }
  345. getData();
  346. // 閱讀更多
  347. $(".article-read-more").on("click", () => {
  348. $(".article-content").css("height", "100%");
  349. $(".article-read-more").hide();
  350. });