ranking.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. activeTab: 0,
  8. // 当前选中的标签索引
  9. activeCategory: 0,
  10. // 当前选中的分类索引
  11. hasMore: true,
  12. page: 1,
  13. isLoading: false,
  14. rankingTabs: [],
  15. // 从数据库获取的排行榜类型
  16. rankingGroups: [],
  17. // 排行榜组数据(包含id、name、code)
  18. categories: [],
  19. // 从数据库获取的分类列表
  20. categoryList: [],
  21. // 分类数据(包含id、name)
  22. bookList: []
  23. // 书籍列表(从数据库获取)
  24. };
  25. },
  26. onLoad() {
  27. this.initData();
  28. },
  29. methods: {
  30. // 初始化数据:加载排行榜类型和分类
  31. async initData() {
  32. try {
  33. this.isLoading = true;
  34. await Promise.all([
  35. this.loadRankingGroups(),
  36. this.loadCategories()
  37. ]);
  38. await this.loadBookList();
  39. } catch (err) {
  40. common_vendor.index.__f__("error", "at pages/ranking/ranking.vue:112", "初始化数据失败:", err);
  41. common_vendor.index.showToast({
  42. title: "加载失败,请重试",
  43. icon: "none"
  44. });
  45. } finally {
  46. this.isLoading = false;
  47. }
  48. },
  49. // 加载排行榜类型
  50. async loadRankingGroups() {
  51. try {
  52. const res = await utils_api.getAllRankingGroups();
  53. if (res && res.code === 200 && res.data) {
  54. this.rankingGroups = res.data;
  55. this.rankingTabs = res.data.map((group) => group.name);
  56. common_vendor.index.__f__("log", "at pages/ranking/ranking.vue:128", "排行榜类型加载成功:", this.rankingTabs);
  57. } else {
  58. common_vendor.index.__f__("warn", "at pages/ranking/ranking.vue:130", "排行榜类型数据为空,使用默认数据");
  59. this.rankingTabs = ["畅销榜", "热门榜", "口碑榜", "好评榜", "新书榜"];
  60. this.rankingGroups = [
  61. { id: 1, name: "畅销榜", code: "bestseller" },
  62. { id: 2, name: "热门榜", code: "popular" },
  63. { id: 3, name: "口碑榜", code: "reputation" },
  64. { id: 4, name: "好评榜", code: "good_reviews" },
  65. { id: 5, name: "新书榜", code: "new_books" }
  66. ];
  67. }
  68. } catch (err) {
  69. common_vendor.index.__f__("error", "at pages/ranking/ranking.vue:142", "加载排行榜类型失败:", err);
  70. this.rankingTabs = ["畅销榜", "热门榜", "口碑榜", "好评榜", "新书榜"];
  71. this.rankingGroups = [
  72. { id: 1, name: "畅销榜", code: "bestseller" },
  73. { id: 2, name: "热门榜", code: "popular" },
  74. { id: 3, name: "口碑榜", code: "reputation" },
  75. { id: 4, name: "好评榜", code: "good_reviews" },
  76. { id: 5, name: "新书榜", code: "new_books" }
  77. ];
  78. }
  79. },
  80. // 加载分类列表
  81. async loadCategories() {
  82. try {
  83. const res = await utils_api.getAllCategories();
  84. if (res && res.code === 200 && res.data) {
  85. this.categoryList = res.data;
  86. this.categories = ["全部分类", ...res.data.map((cat) => cat.name)];
  87. common_vendor.index.__f__("log", "at pages/ranking/ranking.vue:162", "分类列表加载成功:", this.categories);
  88. } else {
  89. common_vendor.index.__f__("warn", "at pages/ranking/ranking.vue:164", "分类数据为空,使用默认数据");
  90. this.categories = ["全部分类", "文艺", "历史", "人文", "科学", "教育", "生活", "外语", "商业", "养生", "职场", "少儿"];
  91. this.categoryList = [
  92. { id: 1, name: "文艺" },
  93. { id: 2, name: "历史" },
  94. { id: 3, name: "人文" },
  95. { id: 4, name: "科学" },
  96. { id: 5, name: "教育" },
  97. { id: 6, name: "生活" },
  98. { id: 7, name: "外语" },
  99. { id: 8, name: "商业" },
  100. { id: 9, name: "养生" },
  101. { id: 10, name: "职场" },
  102. { id: 11, name: "少儿" }
  103. ];
  104. }
  105. } catch (err) {
  106. common_vendor.index.__f__("error", "at pages/ranking/ranking.vue:182", "加载分类失败:", err);
  107. this.categories = ["全部分类", "文艺", "历史", "人文", "科学", "教育", "生活", "外语", "商业", "养生", "职场", "少儿"];
  108. this.categoryList = [
  109. { id: 1, name: "文艺" },
  110. { id: 2, name: "历史" },
  111. { id: 3, name: "人文" },
  112. { id: 4, name: "科学" },
  113. { id: 5, name: "教育" },
  114. { id: 6, name: "生活" },
  115. { id: 7, name: "外语" },
  116. { id: 8, name: "商业" },
  117. { id: 9, name: "养生" },
  118. { id: 10, name: "职场" },
  119. { id: 11, name: "少儿" }
  120. ];
  121. }
  122. },
  123. goBack() {
  124. common_vendor.index.navigateBack({
  125. delta: 1
  126. });
  127. },
  128. switchTab(index) {
  129. if (this.activeTab !== index) {
  130. this.activeTab = index;
  131. this.page = 1;
  132. this.hasMore = true;
  133. this.loadBookList();
  134. }
  135. },
  136. switchCategory(index) {
  137. if (this.activeCategory !== index) {
  138. this.activeCategory = index;
  139. this.page = 1;
  140. this.hasMore = true;
  141. this.loadBookList();
  142. }
  143. },
  144. goToBookDetail(book) {
  145. if (!book || !book.id) {
  146. common_vendor.index.showToast({
  147. title: "书籍信息不完整",
  148. icon: "none"
  149. });
  150. return;
  151. }
  152. common_vendor.index.navigateTo({
  153. url: `/pages/book-detail/book-detail?bookId=${book.id}`
  154. });
  155. },
  156. handleImageError(index) {
  157. if (this.bookList[index]) {
  158. this.bookList[index].cover = `https://picsum.photos/seed/fallback${index}/200/300`;
  159. }
  160. },
  161. // 从后端加载书籍列表
  162. async loadBookList() {
  163. try {
  164. this.isLoading = true;
  165. if (!this.rankingGroups || this.rankingGroups.length === 0) {
  166. common_vendor.index.__f__("warn", "at pages/ranking/ranking.vue:246", "排行榜类型未加载");
  167. this.bookList = [];
  168. return;
  169. }
  170. const currentGroup = this.rankingGroups[this.activeTab];
  171. if (!currentGroup || !currentGroup.code) {
  172. common_vendor.index.__f__("warn", "at pages/ranking/ranking.vue:253", "当前排行榜类型无效");
  173. this.bookList = [];
  174. return;
  175. }
  176. let categoryId = null;
  177. if (this.activeCategory > 0 && this.categoryList && this.categoryList.length > 0) {
  178. const categoryIndex = this.activeCategory - 1;
  179. if (categoryIndex >= 0 && categoryIndex < this.categoryList.length) {
  180. categoryId = this.categoryList[categoryIndex].id;
  181. }
  182. }
  183. const res = await utils_api.getRankingByCode(currentGroup.code, categoryId);
  184. if (res && res.code === 200 && res.data) {
  185. this.bookList = res.data.map((book, index) => ({
  186. id: book.id,
  187. title: book.title || "未知书名",
  188. author: book.author || "未知作者",
  189. cover: book.cover || book.image || `https://picsum.photos/seed/book${book.id}/200/300`,
  190. rank: index + 1
  191. }));
  192. common_vendor.index.__f__("log", "at pages/ranking/ranking.vue:280", "排行榜书籍加载成功,共", this.bookList.length, "本");
  193. } else {
  194. common_vendor.index.__f__("warn", "at pages/ranking/ranking.vue:282", "排行榜书籍数据为空");
  195. this.bookList = [];
  196. }
  197. } catch (err) {
  198. common_vendor.index.__f__("error", "at pages/ranking/ranking.vue:286", "加载排行榜书籍失败:", err);
  199. this.bookList = [];
  200. common_vendor.index.showToast({
  201. title: "加载失败,请重试",
  202. icon: "none"
  203. });
  204. } finally {
  205. this.isLoading = false;
  206. this.hasMore = false;
  207. }
  208. },
  209. // 加载更多(排行榜数据一次性加载,此方法保留但不使用)
  210. loadMore() {
  211. if (this.hasMore) {
  212. this.hasMore = false;
  213. }
  214. }
  215. }
  216. };
  217. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  218. return common_vendor.e({
  219. a: common_vendor.o((...args) => $options.goBack && $options.goBack(...args)),
  220. b: common_vendor.f($data.rankingTabs, (tab, index, i0) => {
  221. return {
  222. a: common_vendor.t(tab),
  223. b: $data.activeTab === index ? 1 : "",
  224. c: index,
  225. d: $data.activeTab === index ? 1 : "",
  226. e: common_vendor.o(($event) => $options.switchTab(index), index)
  227. };
  228. }),
  229. c: common_vendor.f($data.categories, (category, index, i0) => {
  230. return common_vendor.e({
  231. a: $data.activeCategory === index
  232. }, $data.activeCategory === index ? {} : {}, {
  233. b: common_vendor.t(category),
  234. c: $data.activeCategory === index ? 1 : "",
  235. d: index,
  236. e: $data.activeCategory === index ? 1 : "",
  237. f: common_vendor.o(($event) => $options.switchCategory(index), index)
  238. });
  239. }),
  240. d: common_vendor.f($data.bookList, (book, index, i0) => {
  241. return {
  242. a: common_vendor.t(index + 1),
  243. b: book.cover,
  244. c: common_vendor.o(($event) => $options.handleImageError(index), book.id || index),
  245. d: common_vendor.t(book.title),
  246. e: common_vendor.t(book.author),
  247. f: book.id || index,
  248. g: common_vendor.o(($event) => $options.goToBookDetail(book), book.id || index)
  249. };
  250. }),
  251. e: $data.isLoading
  252. }, $data.isLoading ? {} : !$data.isLoading && $data.bookList.length === 0 ? {} : {}, {
  253. f: !$data.isLoading && $data.bookList.length === 0,
  254. g: common_vendor.o((...args) => $options.loadMore && $options.loadMore(...args))
  255. });
  256. }
  257. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-d21fa7e5"]]);
  258. wx.createPage(MiniProgramPage);
  259. //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/ranking/ranking.js.map