Files
PC-Light-Forum/js/recommend.js
DESKTOP-RQ919RC\Pc 51179c3842 refactor: 移除调试日志并优化代码结构
移除多个文件中的console.log调试语句
添加search-tag.css和search-tag.less中的set-hint-box样式
新增recommend.js推荐功能模块
优化ajaxGet调用和用户信息处理逻辑
2025-12-17 19:36:53 +08:00

218 lines
7.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const { createApp, ref, onMounted, nextTick, onUnmounted, computed, watch, provide } = Vue;
import { itemForum } from "../component/item-forum/item-forum.js";
import { itemOffer } from "../component/item-offer/item-offer.js";
import { itemSummary } from "../component/item-summary/item-summary.js";
import { itemVote } from "../component/item-vote/item-vote.js";
import { itemMj } from "../component/item-mj/item-mj.js";
import { itemTenement } from "../component/item-tenement/item-tenement.js";
import { headTop } from "../component/head-top/head-top.js";
import { hotTag } from "../component/hot-tag/hot-tag.js";
import { hotSearch } from "../component/hot-search/hot-search.js";
import { slideshowBox } from "../component/slideshow-box/slideshow-box.js";
import { latestList } from "../component/latest-list/latest-list.js";
const appSearch = createApp({
setup() {
let tag = ref("");
onMounted(() => {
const params = getUrlParams();
tag.value = params.tag || "";
// init();
getList();
getUserInfoWin();
setTimeout(() => (permissions.value = window["permissions"] || ["comment.edit", "comment.delete", "offercollege.hide", "offersummary.hide", "mj.hide", "topic:manager", "topic:hide"]), 1000);
});
let isLogin = ref(true);
let realname = ref(1); // 是否已经实名
let userInfoWin = ref({});
let permissions = ref([]);
const getUserInfoWin = () => {
const checkUser = () => {
const user = window.userInfoWin;
if (!user) return;
document.removeEventListener("getUser", checkUser);
realname.value = user.realname;
userInfoWin.value = user;
if (user?.uin > 0 || user?.uid > 0) isLogin.value = true;
permissions.value = user?.authority || [];
};
document.addEventListener("getUser", checkUser);
};
const openAttest = () => {
const handleAttestClose = () => {
document.removeEventListener("closeAttest", handleAttestClose);
realname.value = window.userInfoWin?.realname || 0;
};
// 启动认证流程时添加监听
document.addEventListener("closeAttest", handleAttestClose);
loadAttest(2);
};
// 跳转登录
const goLogin = () => {
if (typeof window === "undefined") return;
if (window["userInfoWin"] && Object.keys(window["userInfoWin"]).length !== 0) {
if (window["userInfoWin"]["uid"]) isLogin.value = true;
else ajax_login();
} else ajax_login();
};
provide("isLogin", isLogin);
provide("userInfoWin", userInfoWin);
provide("realname", realname);
provide("openAttest", openAttest);
provide("goLogin", goLogin);
const cutTab = (type) => {
if (tabValue.value == type) return;
page.value = 1;
list.value = [];
count.value = 0;
tabValue.value = type;
pagination.value = [];
getList();
};
let tabList = ref({
all: "全部",
thread: "论坛",
offer: "Offer",
offer_summary: "总结",
interviewexperience: "面经",
vote: "投票",
});
let tabValue = ref("all");
let uniqid = "";
const init = () => {
ajaxGet(`/v2/api/forum/topicLists?type=thread&page=1&limit=20&recommend=1`).then((res) => {
if (res.code != 200) {
creationAlertBox("error", res.message);
page.value = 0;
return;
}
const data = res.data;
uniqid = data.uniqid;
page.value = 1;
getList();
});
};
let loading = false;
let page = ref(0);
let maxPage = ref(0);
let count = ref(0);
let list = ref([]);
let pagination = ref([]);
const getList = () => {
if (loading || page.value == null) return;
loading = true;
// page.value += 1;
// wx.showLoading();
const limit = 20;
ajaxGet(`/v2/api/forum/topicLists?type=${tabValue.value == "all" ? "" : tabValue.value}&page=${page.value}&limit=${limit}`)
.then((res) => {
// wx.hideLoading();
if (res.code != 200) {
creationAlertBox("error", res.message);
return;
}
let data = res.data;
list.value = data.data;
// page.value = data.count > limit * data.page ? page.value : null;
count.value = data.count;
loading = false;
maxPage.value = Math.ceil(count.value / limit);
pagination.value = calculatePagination(page.value, maxPage.value);
})
.catch((err) => {
// wx.hideLoading();
err = err.data;
if (err.code == 401) goLogin();
loading = false;
});
};
const calculatePagination = (currentPage, totalPages, visibleCount = 3) => {
// 处理特殊情况总页数小于等于1时无需显示分页
if (totalPages <= 1) {
return [];
}
const pages = [];
// 始终显示第一页
pages.push(1);
// 计算中间需要显示的页码范围
let startPage = Math.max(2, currentPage - Math.floor(visibleCount / 2));
let endPage = Math.min(totalPages - 1, startPage + visibleCount - 1);
// 调整起始页码,确保显示足够数量的页码
startPage = Math.max(2, endPage - visibleCount + 1);
// 前面的省略号:如果第一页和起始页之间有间隔
if (startPage > 2) {
pages.push("...");
}
// 添加中间的页码
for (let i = startPage; i <= endPage; i++) {
pages.push(i);
}
// 后面的省略号:如果最后一页和结束页之间有间隔
if (endPage < totalPages - 1) {
pages.push("...");
}
// 始终显示最后一页如果总页数大于1
if (totalPages > 1) {
pages.push(totalPages);
}
return pages;
};
const cutPage = (value) => {
if (value == "...") return;
if (value == page.value) return;
page.value = value;
getList();
};
const prevPage = () => {
page.value -= 1;
pagination.value = [];
getList();
};
const nextPage = () => {
page.value += 1;
pagination.value = [];
getList();
};
return { maxPage, prevPage, nextPage, tag, tabValue, cutTab, tabList, count, list, page, pagination, cutPage };
},
});
appSearch.component("item-forum", itemForum);
appSearch.component("itemOffer", itemOffer);
appSearch.component("itemSummary", itemSummary);
appSearch.component("itemVote", itemVote);
appSearch.component("itemMj", itemMj);
appSearch.component("itemTenement", itemTenement);
appSearch.component("head-top", headTop);
appSearch.component("hot-tag", hotTag);
appSearch.component("hot-search", hotSearch);
appSearch.component("slideshow-box", slideshowBox);
appSearch.component("latest-list", latestList);
appSearch.mount("#search-tag");