"use strict"; const common_vendor = require("../../common/vendor.js"); const utils_api = require("../../utils/api.js"); const _sfc_main = { data() { return { activeTab: 0, // 当前选中的标签索引 activeCategory: 0, // 当前选中的分类索引 hasMore: true, page: 1, isLoading: false, rankingTabs: [], // 从数据库获取的排行榜类型 rankingGroups: [], // 排行榜组数据(包含id、name、code) categories: [], // 从数据库获取的分类列表 categoryList: [], // 分类数据(包含id、name) bookList: [] // 书籍列表(从数据库获取) }; }, onLoad() { this.initData(); }, methods: { // 初始化数据:加载排行榜类型和分类 async initData() { try { this.isLoading = true; await Promise.all([ this.loadRankingGroups(), this.loadCategories() ]); await this.loadBookList(); } catch (err) { common_vendor.index.__f__("error", "at pages/ranking/ranking.vue:112", "初始化数据失败:", err); common_vendor.index.showToast({ title: "加载失败,请重试", icon: "none" }); } finally { this.isLoading = false; } }, // 加载排行榜类型 async loadRankingGroups() { try { const res = await utils_api.getAllRankingGroups(); if (res && res.code === 200 && res.data) { this.rankingGroups = res.data; this.rankingTabs = res.data.map((group) => group.name); common_vendor.index.__f__("log", "at pages/ranking/ranking.vue:128", "排行榜类型加载成功:", this.rankingTabs); } else { common_vendor.index.__f__("warn", "at pages/ranking/ranking.vue:130", "排行榜类型数据为空,使用默认数据"); this.rankingTabs = ["畅销榜", "热门榜", "口碑榜", "好评榜", "新书榜"]; this.rankingGroups = [ { id: 1, name: "畅销榜", code: "bestseller" }, { id: 2, name: "热门榜", code: "popular" }, { id: 3, name: "口碑榜", code: "reputation" }, { id: 4, name: "好评榜", code: "good_reviews" }, { id: 5, name: "新书榜", code: "new_books" } ]; } } catch (err) { common_vendor.index.__f__("error", "at pages/ranking/ranking.vue:142", "加载排行榜类型失败:", err); this.rankingTabs = ["畅销榜", "热门榜", "口碑榜", "好评榜", "新书榜"]; this.rankingGroups = [ { id: 1, name: "畅销榜", code: "bestseller" }, { id: 2, name: "热门榜", code: "popular" }, { id: 3, name: "口碑榜", code: "reputation" }, { id: 4, name: "好评榜", code: "good_reviews" }, { id: 5, name: "新书榜", code: "new_books" } ]; } }, // 加载分类列表 async loadCategories() { try { const res = await utils_api.getAllCategories(); if (res && res.code === 200 && res.data) { this.categoryList = res.data; this.categories = ["全部分类", ...res.data.map((cat) => cat.name)]; common_vendor.index.__f__("log", "at pages/ranking/ranking.vue:162", "分类列表加载成功:", this.categories); } else { common_vendor.index.__f__("warn", "at pages/ranking/ranking.vue:164", "分类数据为空,使用默认数据"); this.categories = ["全部分类", "文艺", "历史", "人文", "科学", "教育", "生活", "外语", "商业", "养生", "职场", "少儿"]; this.categoryList = [ { id: 1, name: "文艺" }, { id: 2, name: "历史" }, { id: 3, name: "人文" }, { id: 4, name: "科学" }, { id: 5, name: "教育" }, { id: 6, name: "生活" }, { id: 7, name: "外语" }, { id: 8, name: "商业" }, { id: 9, name: "养生" }, { id: 10, name: "职场" }, { id: 11, name: "少儿" } ]; } } catch (err) { common_vendor.index.__f__("error", "at pages/ranking/ranking.vue:182", "加载分类失败:", err); this.categories = ["全部分类", "文艺", "历史", "人文", "科学", "教育", "生活", "外语", "商业", "养生", "职场", "少儿"]; this.categoryList = [ { id: 1, name: "文艺" }, { id: 2, name: "历史" }, { id: 3, name: "人文" }, { id: 4, name: "科学" }, { id: 5, name: "教育" }, { id: 6, name: "生活" }, { id: 7, name: "外语" }, { id: 8, name: "商业" }, { id: 9, name: "养生" }, { id: 10, name: "职场" }, { id: 11, name: "少儿" } ]; } }, goBack() { common_vendor.index.navigateBack({ delta: 1 }); }, switchTab(index) { if (this.activeTab !== index) { this.activeTab = index; this.page = 1; this.hasMore = true; this.loadBookList(); } }, switchCategory(index) { if (this.activeCategory !== index) { this.activeCategory = index; this.page = 1; this.hasMore = true; this.loadBookList(); } }, goToBookDetail(book) { if (!book || !book.id) { common_vendor.index.showToast({ title: "书籍信息不完整", icon: "none" }); return; } common_vendor.index.navigateTo({ url: `/pages/book-detail/book-detail?bookId=${book.id}` }); }, handleImageError(index) { if (this.bookList[index]) { this.bookList[index].cover = `https://picsum.photos/seed/fallback${index}/200/300`; } }, // 从后端加载书籍列表 async loadBookList() { try { this.isLoading = true; if (!this.rankingGroups || this.rankingGroups.length === 0) { common_vendor.index.__f__("warn", "at pages/ranking/ranking.vue:246", "排行榜类型未加载"); this.bookList = []; return; } const currentGroup = this.rankingGroups[this.activeTab]; if (!currentGroup || !currentGroup.code) { common_vendor.index.__f__("warn", "at pages/ranking/ranking.vue:253", "当前排行榜类型无效"); this.bookList = []; return; } let categoryId = null; if (this.activeCategory > 0 && this.categoryList && this.categoryList.length > 0) { const categoryIndex = this.activeCategory - 1; if (categoryIndex >= 0 && categoryIndex < this.categoryList.length) { categoryId = this.categoryList[categoryIndex].id; } } const res = await utils_api.getRankingByCode(currentGroup.code, categoryId); if (res && res.code === 200 && res.data) { this.bookList = res.data.map((book, index) => ({ id: book.id, title: book.title || "未知书名", author: book.author || "未知作者", cover: book.cover || book.image || `https://picsum.photos/seed/book${book.id}/200/300`, rank: index + 1 })); common_vendor.index.__f__("log", "at pages/ranking/ranking.vue:280", "排行榜书籍加载成功,共", this.bookList.length, "本"); } else { common_vendor.index.__f__("warn", "at pages/ranking/ranking.vue:282", "排行榜书籍数据为空"); this.bookList = []; } } catch (err) { common_vendor.index.__f__("error", "at pages/ranking/ranking.vue:286", "加载排行榜书籍失败:", err); this.bookList = []; common_vendor.index.showToast({ title: "加载失败,请重试", icon: "none" }); } finally { this.isLoading = false; this.hasMore = false; } }, // 加载更多(排行榜数据一次性加载,此方法保留但不使用) loadMore() { if (this.hasMore) { this.hasMore = false; } } } }; function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) { return common_vendor.e({ a: common_vendor.o((...args) => $options.goBack && $options.goBack(...args)), b: common_vendor.f($data.rankingTabs, (tab, index, i0) => { return { a: common_vendor.t(tab), b: $data.activeTab === index ? 1 : "", c: index, d: $data.activeTab === index ? 1 : "", e: common_vendor.o(($event) => $options.switchTab(index), index) }; }), c: common_vendor.f($data.categories, (category, index, i0) => { return common_vendor.e({ a: $data.activeCategory === index }, $data.activeCategory === index ? {} : {}, { b: common_vendor.t(category), c: $data.activeCategory === index ? 1 : "", d: index, e: $data.activeCategory === index ? 1 : "", f: common_vendor.o(($event) => $options.switchCategory(index), index) }); }), d: common_vendor.f($data.bookList, (book, index, i0) => { return { a: common_vendor.t(index + 1), b: book.cover, c: common_vendor.o(($event) => $options.handleImageError(index), book.id || index), d: common_vendor.t(book.title), e: common_vendor.t(book.author), f: book.id || index, g: common_vendor.o(($event) => $options.goToBookDetail(book), book.id || index) }; }), e: $data.isLoading }, $data.isLoading ? {} : !$data.isLoading && $data.bookList.length === 0 ? {} : {}, { f: !$data.isLoading && $data.bookList.length === 0, g: common_vendor.o((...args) => $options.loadMore && $options.loadMore(...args)) }); } const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-d21fa7e5"]]); wx.createPage(MiniProgramPage); //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/ranking/ranking.js.map