| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- "use strict";
- const common_vendor = require("../../common/vendor.js");
- const utils_api = require("../../utils/api.js");
- const _sfc_main = {
- data() {
- return {
- type: "",
- // 类型:today, bestseller, featured, category等
- categoryId: null,
- // 分类ID
- categoryName: "",
- // 分类名称
- pageTitle: "更多书籍",
- bookList: [],
- isLoading: false,
- hasMore: true,
- page: 1,
- pageSize: 20
- };
- },
- onLoad(options) {
- common_vendor.index.__f__("log", "at pages/more-books/more-books.vue:79", "more-books页面加载,参数:", options);
- if (options && options.type) {
- this.type = options.type;
- }
- if (options && options.categoryId) {
- this.categoryId = parseInt(options.categoryId);
- }
- if (options && options.categoryName) {
- try {
- this.categoryName = decodeURIComponent(options.categoryName);
- } catch (e) {
- this.categoryName = options.categoryName;
- }
- }
- this.setPageTitle();
- this.loadBooks();
- },
- onShow() {
- if (!this.bookList || this.bookList.length === 0) {
- this.page = 1;
- this.hasMore = true;
- this.loadBooks();
- }
- },
- methods: {
- goBack() {
- common_vendor.index.navigateBack({
- delta: 1
- });
- },
- setPageTitle() {
- if (this.categoryName) {
- this.pageTitle = this.categoryName;
- } else {
- const titleMap = {
- "today": "今日推荐",
- "bestseller": "畅销书籍",
- "featured": "精品书单",
- "ranking": "排行榜",
- "popular": "热门书籍",
- "new": "新书榜",
- "vip": "VIP书籍",
- "category": "分类书籍",
- "all": "全部分类"
- };
- this.pageTitle = titleMap[this.type] || "更多书籍";
- }
- },
- loadBooks() {
- if (this.isLoading)
- return;
- common_vendor.index.__f__("log", "at pages/more-books/more-books.vue:137", "开始加载书籍,类型:", this.type, "分类ID:", this.categoryId, "页码:", this.page);
- this.isLoading = true;
- let promise;
- if (this.type === "all") {
- promise = utils_api.getAllBooks(this.page, this.pageSize);
- } else if (this.type === "category" && this.categoryId) {
- promise = utils_api.getBooks({
- page: this.page,
- size: this.pageSize,
- categoryId: this.categoryId,
- status: 1
- });
- } else if (this.type === "today") {
- promise = utils_api.getTodayRecommend(this.pageSize * this.page);
- } else if (this.type === "bestseller") {
- promise = utils_api.getBestsellers(this.pageSize * this.page);
- } else if (this.type === "featured") {
- promise = utils_api.getFeaturedList(this.pageSize * this.page);
- } else if (this.type === "ranking") {
- promise = utils_api.getRankingBooks(this.pageSize * this.page);
- } else if (this.type === "popular") {
- promise = utils_api.getPopularBooks(this.pageSize * this.page);
- } else if (this.type === "new") {
- promise = utils_api.getNewBooks(this.pageSize * this.page);
- } else if (this.type === "vip") {
- promise = utils_api.getVipBooks(this.page, this.pageSize);
- } else {
- promise = utils_api.getAllBooks(this.page, this.pageSize);
- }
- promise.then((res) => {
- common_vendor.index.__f__("log", "at pages/more-books/more-books.vue:181", "书籍列表API响应:", res);
- this.isLoading = false;
- if (res && (res.code === 200 || res.success === true)) {
- let newBooks = [];
- if (this.type === "vip" || this.type === "category" || this.type === "all" || this.type === "" || !this.type) {
- const pageData = res && res.data ? res.data : null;
- if (pageData && Array.isArray(pageData.list)) {
- newBooks = pageData.list;
- this.hasMore = pageData.list.length >= this.pageSize && this.page * this.pageSize < (pageData.total || 0);
- } else if (Array.isArray(pageData)) {
- newBooks = pageData;
- this.hasMore = newBooks.length >= this.pageSize;
- }
- } else {
- if (res && Array.isArray(res.data)) {
- const start = (this.page - 1) * this.pageSize;
- const end = start + this.pageSize;
- newBooks = res.data.slice(start, end);
- this.hasMore = end < res.data.length;
- }
- }
- if (newBooks.length > 0) {
- const formattedBooks = newBooks.map((book) => {
- return {
- id: book.id,
- title: book.title || "未知书名",
- author: book.author || "",
- image: book.image || book.cover || "https://picsum.photos/seed/book" + book.id + "/200/300",
- cover: book.cover || book.image,
- desc: book.desc,
- brief: book.brief,
- introduction: book.introduction
- };
- });
- if (this.page === 1) {
- this.bookList = formattedBooks;
- } else {
- this.bookList = [...this.bookList, ...formattedBooks];
- }
- common_vendor.index.__f__("log", "at pages/more-books/more-books.vue:229", "书籍列表加载成功,当前共", this.bookList.length, "本");
- } else {
- if (this.page === 1) {
- this.bookList = [];
- }
- this.hasMore = false;
- }
- } else {
- common_vendor.index.__f__("warn", "at pages/more-books/more-books.vue:237", "书籍列表API返回错误:", res);
- if (this.page === 1) {
- this.bookList = [];
- }
- common_vendor.index.showToast({
- title: res && (res.message || res.msg) ? res.message || res.msg : "获取书籍列表失败",
- icon: "none",
- duration: 2e3
- });
- }
- }).catch((err) => {
- this.isLoading = false;
- common_vendor.index.__f__("error", "at pages/more-books/more-books.vue:250", "获取书籍列表失败:", err);
- if (this.page === 1) {
- this.bookList = [];
- }
- common_vendor.index.showToast({
- title: err && err.message ? err.message : "网络请求失败,请检查后端服务",
- icon: "none",
- duration: 3e3
- });
- });
- },
- loadMore() {
- if (this.hasMore && !this.isLoading) {
- this.page++;
- this.loadBooks();
- }
- },
- 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].image = "https://picsum.photos/seed/fallback" + index + "/200/300";
- }
- }
- }
- };
- 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.t($data.pageTitle),
- c: $data.isLoading && $data.bookList.length === 0
- }, $data.isLoading && $data.bookList.length === 0 ? {} : $data.bookList.length > 0 ? {
- e: common_vendor.f($data.bookList, (book, index, i0) => {
- return common_vendor.e({
- a: book.image || book.cover,
- b: common_vendor.o(($event) => $options.handleImageError(index), book.id || index),
- c: common_vendor.t(book.title),
- d: common_vendor.t(book.author || "未知作者"),
- e: book.desc || book.brief
- }, book.desc || book.brief ? {
- f: common_vendor.t((book.desc || book.brief).substring(0, 50))
- } : {}, {
- g: book.id || index,
- h: common_vendor.o(($event) => $options.goToBookDetail(book), book.id || index)
- });
- })
- } : {}, {
- d: $data.bookList.length > 0,
- f: $data.hasMore && $data.bookList.length > 0
- }, $data.hasMore && $data.bookList.length > 0 ? {} : !$data.hasMore && $data.bookList.length > 0 ? {} : {}, {
- g: !$data.hasMore && $data.bookList.length > 0,
- h: common_vendor.o((...args) => $options.loadMore && $options.loadMore(...args))
- });
- }
- const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-a14ffe9d"]]);
- wx.createPage(MiniProgramPage);
- //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/more-books/more-books.js.map
|