more-books.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const utils_api = require("../../utils/api.js");
  4. const _sfc_main = {
  5. data() {
  6. return {
  7. type: "",
  8. // 类型:today, bestseller, featured, category等
  9. categoryId: null,
  10. // 分类ID
  11. categoryName: "",
  12. // 分类名称
  13. pageTitle: "更多书籍",
  14. bookList: [],
  15. isLoading: false,
  16. hasMore: true,
  17. page: 1,
  18. pageSize: 20
  19. };
  20. },
  21. onLoad(options) {
  22. common_vendor.index.__f__("log", "at pages/more-books/more-books.vue:79", "more-books页面加载,参数:", options);
  23. if (options && options.type) {
  24. this.type = options.type;
  25. }
  26. if (options && options.categoryId) {
  27. this.categoryId = parseInt(options.categoryId);
  28. }
  29. if (options && options.categoryName) {
  30. try {
  31. this.categoryName = decodeURIComponent(options.categoryName);
  32. } catch (e) {
  33. this.categoryName = options.categoryName;
  34. }
  35. }
  36. this.setPageTitle();
  37. this.loadBooks();
  38. },
  39. onShow() {
  40. if (!this.bookList || this.bookList.length === 0) {
  41. this.page = 1;
  42. this.hasMore = true;
  43. this.loadBooks();
  44. }
  45. },
  46. methods: {
  47. goBack() {
  48. common_vendor.index.navigateBack({
  49. delta: 1
  50. });
  51. },
  52. setPageTitle() {
  53. if (this.categoryName) {
  54. this.pageTitle = this.categoryName;
  55. } else {
  56. const titleMap = {
  57. "today": "今日推荐",
  58. "bestseller": "畅销书籍",
  59. "featured": "精品书单",
  60. "ranking": "排行榜",
  61. "popular": "热门书籍",
  62. "new": "新书榜",
  63. "vip": "VIP书籍",
  64. "category": "分类书籍",
  65. "all": "全部分类"
  66. };
  67. this.pageTitle = titleMap[this.type] || "更多书籍";
  68. }
  69. },
  70. loadBooks() {
  71. if (this.isLoading)
  72. return;
  73. common_vendor.index.__f__("log", "at pages/more-books/more-books.vue:137", "开始加载书籍,类型:", this.type, "分类ID:", this.categoryId, "页码:", this.page);
  74. this.isLoading = true;
  75. let promise;
  76. if (this.type === "all") {
  77. promise = utils_api.getAllBooks(this.page, this.pageSize);
  78. } else if (this.type === "category" && this.categoryId) {
  79. promise = utils_api.getBooks({
  80. page: this.page,
  81. size: this.pageSize,
  82. categoryId: this.categoryId,
  83. status: 1
  84. });
  85. } else if (this.type === "today") {
  86. promise = utils_api.getTodayRecommend(this.pageSize * this.page);
  87. } else if (this.type === "bestseller") {
  88. promise = utils_api.getBestsellers(this.pageSize * this.page);
  89. } else if (this.type === "featured") {
  90. promise = utils_api.getFeaturedList(this.pageSize * this.page);
  91. } else if (this.type === "ranking") {
  92. promise = utils_api.getRankingBooks(this.pageSize * this.page);
  93. } else if (this.type === "popular") {
  94. promise = utils_api.getPopularBooks(this.pageSize * this.page);
  95. } else if (this.type === "new") {
  96. promise = utils_api.getNewBooks(this.pageSize * this.page);
  97. } else if (this.type === "vip") {
  98. promise = utils_api.getVipBooks(this.page, this.pageSize);
  99. } else {
  100. promise = utils_api.getAllBooks(this.page, this.pageSize);
  101. }
  102. promise.then((res) => {
  103. common_vendor.index.__f__("log", "at pages/more-books/more-books.vue:181", "书籍列表API响应:", res);
  104. this.isLoading = false;
  105. if (res && (res.code === 200 || res.success === true)) {
  106. let newBooks = [];
  107. if (this.type === "vip" || this.type === "category" || this.type === "all" || this.type === "" || !this.type) {
  108. const pageData = res && res.data ? res.data : null;
  109. if (pageData && Array.isArray(pageData.list)) {
  110. newBooks = pageData.list;
  111. this.hasMore = pageData.list.length >= this.pageSize && this.page * this.pageSize < (pageData.total || 0);
  112. } else if (Array.isArray(pageData)) {
  113. newBooks = pageData;
  114. this.hasMore = newBooks.length >= this.pageSize;
  115. }
  116. } else {
  117. if (res && Array.isArray(res.data)) {
  118. const start = (this.page - 1) * this.pageSize;
  119. const end = start + this.pageSize;
  120. newBooks = res.data.slice(start, end);
  121. this.hasMore = end < res.data.length;
  122. }
  123. }
  124. if (newBooks.length > 0) {
  125. const formattedBooks = newBooks.map((book) => {
  126. return {
  127. id: book.id,
  128. title: book.title || "未知书名",
  129. author: book.author || "",
  130. image: book.image || book.cover || "https://picsum.photos/seed/book" + book.id + "/200/300",
  131. cover: book.cover || book.image,
  132. desc: book.desc,
  133. brief: book.brief,
  134. introduction: book.introduction
  135. };
  136. });
  137. if (this.page === 1) {
  138. this.bookList = formattedBooks;
  139. } else {
  140. this.bookList = [...this.bookList, ...formattedBooks];
  141. }
  142. common_vendor.index.__f__("log", "at pages/more-books/more-books.vue:229", "书籍列表加载成功,当前共", this.bookList.length, "本");
  143. } else {
  144. if (this.page === 1) {
  145. this.bookList = [];
  146. }
  147. this.hasMore = false;
  148. }
  149. } else {
  150. common_vendor.index.__f__("warn", "at pages/more-books/more-books.vue:237", "书籍列表API返回错误:", res);
  151. if (this.page === 1) {
  152. this.bookList = [];
  153. }
  154. common_vendor.index.showToast({
  155. title: res && (res.message || res.msg) ? res.message || res.msg : "获取书籍列表失败",
  156. icon: "none",
  157. duration: 2e3
  158. });
  159. }
  160. }).catch((err) => {
  161. this.isLoading = false;
  162. common_vendor.index.__f__("error", "at pages/more-books/more-books.vue:250", "获取书籍列表失败:", err);
  163. if (this.page === 1) {
  164. this.bookList = [];
  165. }
  166. common_vendor.index.showToast({
  167. title: err && err.message ? err.message : "网络请求失败,请检查后端服务",
  168. icon: "none",
  169. duration: 3e3
  170. });
  171. });
  172. },
  173. loadMore() {
  174. if (this.hasMore && !this.isLoading) {
  175. this.page++;
  176. this.loadBooks();
  177. }
  178. },
  179. goToBookDetail(book) {
  180. if (!book || !book.id) {
  181. common_vendor.index.showToast({
  182. title: "书籍信息不完整",
  183. icon: "none"
  184. });
  185. return;
  186. }
  187. common_vendor.index.navigateTo({
  188. url: `/pages/book-detail/book-detail?bookId=${book.id}`
  189. });
  190. },
  191. handleImageError(index) {
  192. if (this.bookList[index]) {
  193. this.bookList[index].image = "https://picsum.photos/seed/fallback" + index + "/200/300";
  194. }
  195. }
  196. }
  197. };
  198. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  199. return common_vendor.e({
  200. a: common_vendor.o((...args) => $options.goBack && $options.goBack(...args)),
  201. b: common_vendor.t($data.pageTitle),
  202. c: $data.isLoading && $data.bookList.length === 0
  203. }, $data.isLoading && $data.bookList.length === 0 ? {} : $data.bookList.length > 0 ? {
  204. e: common_vendor.f($data.bookList, (book, index, i0) => {
  205. return common_vendor.e({
  206. a: book.image || book.cover,
  207. b: common_vendor.o(($event) => $options.handleImageError(index), book.id || index),
  208. c: common_vendor.t(book.title),
  209. d: common_vendor.t(book.author || "未知作者"),
  210. e: book.desc || book.brief
  211. }, book.desc || book.brief ? {
  212. f: common_vendor.t((book.desc || book.brief).substring(0, 50))
  213. } : {}, {
  214. g: book.id || index,
  215. h: common_vendor.o(($event) => $options.goToBookDetail(book), book.id || index)
  216. });
  217. })
  218. } : {}, {
  219. d: $data.bookList.length > 0,
  220. f: $data.hasMore && $data.bookList.length > 0
  221. }, $data.hasMore && $data.bookList.length > 0 ? {} : !$data.hasMore && $data.bookList.length > 0 ? {} : {}, {
  222. g: !$data.hasMore && $data.bookList.length > 0,
  223. h: common_vendor.o((...args) => $options.loadMore && $options.loadMore(...args))
  224. });
  225. }
  226. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-a14ffe9d"]]);
  227. wx.createPage(MiniProgramPage);
  228. //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/more-books/more-books.js.map