123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- // 載入共用 template
- $('#navbar').load('../../template/navbar.html');
- $('#footer').load('../../template/footer.html');
- $('#btn-box').load('../../template/button.html');
- $('#topCarousel').load('../../template/top_carousel.html');
- // window.onload = function () {
- // if (screen.width < 991) {
- // window.location.href = `https://m.hhh.com.tw/videos/lists/`;
- // }
- // }
- // window.onload = function () {
- // //hhh_user_api();
- // if (screen.width >= 991) {
- // window.location.href = `https://hhh.com.tw/videos/lists/`;
- // }
- // }
- let assignOrder = ""; // 當前排序
- // new, hot, recommend 排序 (預設推薦)
- $(".search-btn-filter button").click(function () {
- // 切換選取狀態
- $('.search-btn-filter').find('.active').removeClass('active');
- $(this).addClass('active');
- assignOrder = $(this).attr('id');
- dataSearch("order_by");
- });
- let page = 1; // 當前頁數
- let pageSize = 18; // 每頁數量
- let isFirstLoad = true; // 初始載入
- // 列表篩選
- async function dataSearch(type = "") {
- let url;
- let input = $(".keywords").val();
- $('#videoSpinner').show();
- $('#dataList').hide();
- if (isFirstLoad && input === "") {
- // 第一次載入使用本地 JSON 文件
- url = './json/videos_lists_data.json';
- } else {
- // 後續使用 API
- url = `https://m3.hhh.com.tw:18673/video_search?page=${page}&page_size=${pageSize}`;
- // if (type === "order_by") {
- // url += `&${type}=${assignOrder}`;
- // }
- if (assignOrder === "") {
- url += "&order_by=new"; // 預設排序為 new
- } else {
- url += `&order_by=${assignOrder}`;
- }
- if (input !== "") {
- let isExist = filterList.some((item) => item.id === "keyword"); // 判斷是否已存在關鍵字
- const newItem = {
- id: "keyword",
- text: "關鍵字",
- value: input
- };
- if (!isExist) {
- filterList.push(newItem);
- createFilterHtml(newItem);
- } else {
- // 移除原本關鍵字
- $('.budget p.me-1').each(function () {
- if ($(this).text().includes('關鍵字')) {
- $(this).closest('.me-3').remove();
- }
- });
- if (filterList.length === 0) {
- $('#removeResultBtn').hide();
- }
- filterList = filterList.filter(item => item.text !== "關鍵字");
- filterList.push(newItem);
- createFilterHtml(newItem);
- }
- // $(".keywords").val("");
- // url += `&keyword=${input}`;
- }
- if (filterList.length) {
- filterList.map(item => {
- url += `&${item.id}=${item.value}`;
- });
- }
- }
- try {
- const response = await axios.get(url);
- console.log('response.data.videos', response.data.videos);
- let totalCount = response.data.total_count;
- let totalPages = Math.ceil(totalCount / pageSize);
- $("#totalCount").html(totalCount.toLocaleString());
- if (totalPages) {
- $('.filter-list .pagination').show();
- setPagination(totalPages); // 分頁處理
- } else {
- $('.filter-list .pagination').hide();
- }
- let resultHtml = '';
- if (response.data.videos.length) {
- response.data.videos.forEach((item) => {
- let tagsHtml = '';
- item.VideoTag.forEach((tag) => {
- if (tag !== "") {
- tagsHtml += `<span class="tag-item" data-tag="${tag}">${tag}</span>`;
- }
- });
- resultHtml += `
- <div class="col-md-4 mb-4">
- <div class="card lists-card">
- <a href="${item.VideoLink}">
- <img src="${item.VideoCoverImg}" class="video-cover-img" alt="${item.DesignerName} ${item.DesignerTitle}">
- </a>
- <div class="card-body d-flex flex-column align-items-center">
- <a href="${item.DesignerLink}" class="d-flex align-items-center w-100">
- <div class="person-img me-3 me-md-2 me-lg-3" style="background-image: url('${item.DesignerCoverImg}');"></div>
- <h5 class="text-muted mb-2">${item.DesignerTitle}</h5>
- </a>
- <a href="${item.VideoLink}">
- <h3 class="my-3 video-title">${item.VideoTitle}</h3>
- </a>
- <div class="tags-container mt-2 me-auto">${tagsHtml}</div>
- </div>
- </div>
- </div>`;
- });
- } else {
- resultHtml += "<p class='text-center mt-5'>找不到符合的資料,請重新搜尋。</p>"
- }
- $('#dataList').html(resultHtml);
- setTimeout(() => {
- $('#dataList').show();
- $('#videoSpinner').hide();
- }, 100)
- // 更新初始載入狀態
- if (isFirstLoad) {
- isFirstLoad = false;
- }
- } catch (error) {
- console.log("error", error);
- }
- }
- dataSearch();
|