bookshelf.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. books: [],
  8. audiobooks: [],
  9. allItems: [],
  10. recentItems: [],
  11. userInfo: null,
  12. isLoading: false
  13. };
  14. },
  15. onLoad() {
  16. this.loadUserInfo();
  17. },
  18. onShow() {
  19. this.loadUserInfo();
  20. },
  21. methods: {
  22. loadUserInfo() {
  23. try {
  24. const userInfo = common_vendor.index.getStorageSync("userInfo");
  25. const isLogin = common_vendor.index.getStorageSync("isLogin");
  26. if (userInfo && userInfo.id && isLogin) {
  27. this.userInfo = userInfo;
  28. this.loadBookshelfList();
  29. } else {
  30. this.books = [];
  31. this.audiobooks = [];
  32. this.allItems = [];
  33. this.recentItems = [];
  34. this.userInfo = null;
  35. }
  36. } catch (e) {
  37. common_vendor.index.__f__("error", "at pages/bookshelf/bookshelf.vue:103", "获取用户信息失败", e);
  38. this.books = [];
  39. this.audiobooks = [];
  40. this.allItems = [];
  41. this.recentItems = [];
  42. this.userInfo = null;
  43. }
  44. },
  45. async loadBookshelfList() {
  46. if (!this.userInfo || !this.userInfo.id) {
  47. return;
  48. }
  49. this.isLoading = true;
  50. common_vendor.index.showLoading({
  51. title: "加载中...",
  52. mask: false
  53. });
  54. try {
  55. const [bookRes, audiobookRes] = await Promise.all([
  56. utils_api.getBookshelfList(this.userInfo.id).catch((err) => {
  57. common_vendor.index.__f__("error", "at pages/bookshelf/bookshelf.vue:126", "获取书籍书架失败:", err);
  58. return { code: 200, data: [] };
  59. }),
  60. utils_api.getAudiobookBookshelfList(this.userInfo.id).catch((err) => {
  61. common_vendor.index.__f__("error", "at pages/bookshelf/bookshelf.vue:130", "获取听书书架失败:", err);
  62. return { code: 200, data: [] };
  63. })
  64. ]);
  65. common_vendor.index.hideLoading();
  66. this.isLoading = false;
  67. if (bookRes.code === 200 && bookRes.data) {
  68. this.books = bookRes.data.map((item) => {
  69. const book = item.book || {};
  70. return {
  71. id: book.id || item.bookId,
  72. title: book.title || "未知书籍",
  73. author: book.author || "",
  74. image: book.image || book.cover || "https://picsum.photos/seed/default/200/300",
  75. progress: item.readProgress || 0,
  76. type: "book",
  77. lastReadTime: item.lastReadTime,
  78. addedAt: item.addedAt
  79. };
  80. });
  81. } else {
  82. this.books = [];
  83. }
  84. if (audiobookRes.code === 200 && audiobookRes.data) {
  85. this.audiobooks = audiobookRes.data.map((item) => {
  86. const audiobook = item.audiobook || {};
  87. return {
  88. id: audiobook.id || item.audiobookId,
  89. title: audiobook.title || "未知听书",
  90. author: audiobook.author || "",
  91. narrator: audiobook.narrator || "",
  92. image: audiobook.image || audiobook.cover || "https://picsum.photos/seed/default/200/300",
  93. progress: 0,
  94. progressText: item.lastListenedChapterId ? "继续收听" : "开始收听",
  95. type: "audiobook",
  96. lastListenedChapterId: item.lastListenedChapterId,
  97. lastListenedPosition: item.lastListenedPosition,
  98. addedAt: item.addedAt,
  99. updatedAt: item.updatedAt
  100. };
  101. });
  102. } else {
  103. this.audiobooks = [];
  104. }
  105. this.allItems = [...this.books, ...this.audiobooks].sort((a, b) => {
  106. const timeA = a.addedAt ? new Date(a.addedAt).getTime() : 0;
  107. const timeB = b.addedAt ? new Date(b.addedAt).getTime() : 0;
  108. return timeB - timeA;
  109. });
  110. this.recentItems = [...this.books, ...this.audiobooks].filter((item) => {
  111. if (item.type === "book") {
  112. return item.lastReadTime != null;
  113. } else if (item.type === "audiobook") {
  114. return item.lastListenedChapterId != null || item.updatedAt != null;
  115. }
  116. return false;
  117. }).sort((a, b) => {
  118. let timeA = 0;
  119. let timeB = 0;
  120. if (a.type === "book" && a.lastReadTime) {
  121. timeA = new Date(a.lastReadTime).getTime();
  122. } else if (a.type === "audiobook" && a.updatedAt) {
  123. timeA = new Date(a.updatedAt).getTime();
  124. }
  125. if (b.type === "book" && b.lastReadTime) {
  126. timeB = new Date(b.lastReadTime).getTime();
  127. } else if (b.type === "audiobook" && b.updatedAt) {
  128. timeB = new Date(b.updatedAt).getTime();
  129. }
  130. return timeB - timeA;
  131. }).slice(0, 3);
  132. } catch (err) {
  133. common_vendor.index.hideLoading();
  134. this.isLoading = false;
  135. common_vendor.index.__f__("error", "at pages/bookshelf/bookshelf.vue:221", "获取书架列表失败:", err);
  136. common_vendor.index.showToast({
  137. title: "加载失败",
  138. icon: "none"
  139. });
  140. this.books = [];
  141. this.audiobooks = [];
  142. this.allItems = [];
  143. this.recentItems = [];
  144. }
  145. },
  146. goToDetail(item) {
  147. if (!item || !item.id) {
  148. common_vendor.index.showToast({
  149. title: "信息不完整",
  150. icon: "none"
  151. });
  152. return;
  153. }
  154. if (item.type === "book") {
  155. if (!item.id) {
  156. common_vendor.index.showToast({
  157. title: "书籍信息不完整",
  158. icon: "none"
  159. });
  160. return;
  161. }
  162. common_vendor.index.navigateTo({
  163. url: `/pages/book-detail/book-detail?bookId=${item.id}`
  164. });
  165. } else if (item.type === "audiobook") {
  166. if (!item.id) {
  167. common_vendor.index.showToast({
  168. title: "听书信息不完整",
  169. icon: "none"
  170. });
  171. return;
  172. }
  173. common_vendor.index.navigateTo({
  174. url: `/pages/listen-detail/listen-detail?audiobookId=${item.id}`
  175. });
  176. }
  177. },
  178. goToLogin() {
  179. common_vendor.index.navigateTo({
  180. url: "/pages/login/login"
  181. });
  182. }
  183. }
  184. };
  185. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  186. return common_vendor.e({
  187. a: $data.userInfo
  188. }, $data.userInfo ? common_vendor.e({
  189. b: common_vendor.t($data.allItems.length),
  190. c: $data.allItems.length > 0
  191. }, $data.allItems.length > 0 ? {
  192. d: common_vendor.f($data.allItems, (item, index, i0) => {
  193. return common_vendor.e({
  194. a: item.image,
  195. b: common_vendor.t(item.title),
  196. c: item.type === "book"
  197. }, item.type === "book" ? {
  198. d: common_vendor.t(item.progress)
  199. } : item.type === "audiobook" ? {
  200. f: common_vendor.t(item.progressText || "继续收听")
  201. } : {}, {
  202. e: item.type === "audiobook",
  203. g: item.type === "audiobook"
  204. }, item.type === "audiobook" ? {} : {}, {
  205. h: item.id || index,
  206. i: common_vendor.o(($event) => $options.goToDetail(item), item.id || index)
  207. });
  208. })
  209. } : {}) : {}, {
  210. e: $data.userInfo && $data.recentItems.length > 0
  211. }, $data.userInfo && $data.recentItems.length > 0 ? {
  212. f: common_vendor.f($data.recentItems, (item, index, i0) => {
  213. return common_vendor.e({
  214. a: item.image,
  215. b: common_vendor.t(item.title),
  216. c: item.type === "audiobook"
  217. }, item.type === "audiobook" ? {} : {}, {
  218. d: common_vendor.t(item.author),
  219. e: item.type === "book"
  220. }, item.type === "book" ? {
  221. f: common_vendor.t(item.progress)
  222. } : item.type === "audiobook" ? {
  223. h: common_vendor.t(item.progressText || "继续收听")
  224. } : {}, {
  225. g: item.type === "audiobook",
  226. i: item.id || index,
  227. j: common_vendor.o(($event) => $options.goToDetail(item), item.id || index)
  228. });
  229. })
  230. } : {}, {
  231. g: !$data.userInfo
  232. }, !$data.userInfo ? {
  233. h: common_vendor.o((...args) => $options.goToLogin && $options.goToLogin(...args))
  234. } : {});
  235. }
  236. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-babe43ef"]]);
  237. wx.createPage(MiniProgramPage);
  238. //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/bookshelf/bookshelf.js.map