| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- "use strict";
- const common_vendor = require("../../common/vendor.js");
- const utils_api = require("../../utils/api.js");
- const _sfc_main = {
- data() {
- return {
- books: [],
- audiobooks: [],
- allItems: [],
- recentItems: [],
- userInfo: null,
- isLoading: false
- };
- },
- onLoad() {
- this.loadUserInfo();
- },
- onShow() {
- this.loadUserInfo();
- },
- methods: {
- loadUserInfo() {
- try {
- const userInfo = common_vendor.index.getStorageSync("userInfo");
- const isLogin = common_vendor.index.getStorageSync("isLogin");
- if (userInfo && userInfo.id && isLogin) {
- this.userInfo = userInfo;
- this.loadBookshelfList();
- } else {
- this.books = [];
- this.audiobooks = [];
- this.allItems = [];
- this.recentItems = [];
- this.userInfo = null;
- }
- } catch (e) {
- common_vendor.index.__f__("error", "at pages/bookshelf/bookshelf.vue:103", "获取用户信息失败", e);
- this.books = [];
- this.audiobooks = [];
- this.allItems = [];
- this.recentItems = [];
- this.userInfo = null;
- }
- },
- async loadBookshelfList() {
- if (!this.userInfo || !this.userInfo.id) {
- return;
- }
- this.isLoading = true;
- common_vendor.index.showLoading({
- title: "加载中...",
- mask: false
- });
- try {
- const [bookRes, audiobookRes] = await Promise.all([
- utils_api.getBookshelfList(this.userInfo.id).catch((err) => {
- common_vendor.index.__f__("error", "at pages/bookshelf/bookshelf.vue:126", "获取书籍书架失败:", err);
- return { code: 200, data: [] };
- }),
- utils_api.getAudiobookBookshelfList(this.userInfo.id).catch((err) => {
- common_vendor.index.__f__("error", "at pages/bookshelf/bookshelf.vue:130", "获取听书书架失败:", err);
- return { code: 200, data: [] };
- })
- ]);
- common_vendor.index.hideLoading();
- this.isLoading = false;
- if (bookRes.code === 200 && bookRes.data) {
- this.books = bookRes.data.map((item) => {
- const book = item.book || {};
- return {
- id: book.id || item.bookId,
- title: book.title || "未知书籍",
- author: book.author || "",
- image: book.image || book.cover || "https://picsum.photos/seed/default/200/300",
- progress: item.readProgress || 0,
- type: "book",
- lastReadTime: item.lastReadTime,
- addedAt: item.addedAt
- };
- });
- } else {
- this.books = [];
- }
- if (audiobookRes.code === 200 && audiobookRes.data) {
- this.audiobooks = audiobookRes.data.map((item) => {
- const audiobook = item.audiobook || {};
- return {
- id: audiobook.id || item.audiobookId,
- title: audiobook.title || "未知听书",
- author: audiobook.author || "",
- narrator: audiobook.narrator || "",
- image: audiobook.image || audiobook.cover || "https://picsum.photos/seed/default/200/300",
- progress: 0,
- progressText: item.lastListenedChapterId ? "继续收听" : "开始收听",
- type: "audiobook",
- lastListenedChapterId: item.lastListenedChapterId,
- lastListenedPosition: item.lastListenedPosition,
- addedAt: item.addedAt,
- updatedAt: item.updatedAt
- };
- });
- } else {
- this.audiobooks = [];
- }
- this.allItems = [...this.books, ...this.audiobooks].sort((a, b) => {
- const timeA = a.addedAt ? new Date(a.addedAt).getTime() : 0;
- const timeB = b.addedAt ? new Date(b.addedAt).getTime() : 0;
- return timeB - timeA;
- });
- this.recentItems = [...this.books, ...this.audiobooks].filter((item) => {
- if (item.type === "book") {
- return item.lastReadTime != null;
- } else if (item.type === "audiobook") {
- return item.lastListenedChapterId != null || item.updatedAt != null;
- }
- return false;
- }).sort((a, b) => {
- let timeA = 0;
- let timeB = 0;
- if (a.type === "book" && a.lastReadTime) {
- timeA = new Date(a.lastReadTime).getTime();
- } else if (a.type === "audiobook" && a.updatedAt) {
- timeA = new Date(a.updatedAt).getTime();
- }
- if (b.type === "book" && b.lastReadTime) {
- timeB = new Date(b.lastReadTime).getTime();
- } else if (b.type === "audiobook" && b.updatedAt) {
- timeB = new Date(b.updatedAt).getTime();
- }
- return timeB - timeA;
- }).slice(0, 3);
- } catch (err) {
- common_vendor.index.hideLoading();
- this.isLoading = false;
- common_vendor.index.__f__("error", "at pages/bookshelf/bookshelf.vue:221", "获取书架列表失败:", err);
- common_vendor.index.showToast({
- title: "加载失败",
- icon: "none"
- });
- this.books = [];
- this.audiobooks = [];
- this.allItems = [];
- this.recentItems = [];
- }
- },
- goToDetail(item) {
- if (!item || !item.id) {
- common_vendor.index.showToast({
- title: "信息不完整",
- icon: "none"
- });
- return;
- }
- if (item.type === "book") {
- if (!item.id) {
- common_vendor.index.showToast({
- title: "书籍信息不完整",
- icon: "none"
- });
- return;
- }
- common_vendor.index.navigateTo({
- url: `/pages/book-detail/book-detail?bookId=${item.id}`
- });
- } else if (item.type === "audiobook") {
- if (!item.id) {
- common_vendor.index.showToast({
- title: "听书信息不完整",
- icon: "none"
- });
- return;
- }
- common_vendor.index.navigateTo({
- url: `/pages/listen-detail/listen-detail?audiobookId=${item.id}`
- });
- }
- },
- goToLogin() {
- common_vendor.index.navigateTo({
- url: "/pages/login/login"
- });
- }
- }
- };
- function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
- return common_vendor.e({
- a: $data.userInfo
- }, $data.userInfo ? common_vendor.e({
- b: common_vendor.t($data.allItems.length),
- c: $data.allItems.length > 0
- }, $data.allItems.length > 0 ? {
- d: common_vendor.f($data.allItems, (item, index, i0) => {
- return common_vendor.e({
- a: item.image,
- b: common_vendor.t(item.title),
- c: item.type === "book"
- }, item.type === "book" ? {
- d: common_vendor.t(item.progress)
- } : item.type === "audiobook" ? {
- f: common_vendor.t(item.progressText || "继续收听")
- } : {}, {
- e: item.type === "audiobook",
- g: item.type === "audiobook"
- }, item.type === "audiobook" ? {} : {}, {
- h: item.id || index,
- i: common_vendor.o(($event) => $options.goToDetail(item), item.id || index)
- });
- })
- } : {}) : {}, {
- e: $data.userInfo && $data.recentItems.length > 0
- }, $data.userInfo && $data.recentItems.length > 0 ? {
- f: common_vendor.f($data.recentItems, (item, index, i0) => {
- return common_vendor.e({
- a: item.image,
- b: common_vendor.t(item.title),
- c: item.type === "audiobook"
- }, item.type === "audiobook" ? {} : {}, {
- d: common_vendor.t(item.author),
- e: item.type === "book"
- }, item.type === "book" ? {
- f: common_vendor.t(item.progress)
- } : item.type === "audiobook" ? {
- h: common_vendor.t(item.progressText || "继续收听")
- } : {}, {
- g: item.type === "audiobook",
- i: item.id || index,
- j: common_vendor.o(($event) => $options.goToDetail(item), item.id || index)
- });
- })
- } : {}, {
- g: !$data.userInfo
- }, !$data.userInfo ? {
- h: common_vendor.o((...args) => $options.goToLogin && $options.goToLogin(...args))
- } : {});
- }
- const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-babe43ef"]]);
- wx.createPage(MiniProgramPage);
- //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/bookshelf/bookshelf.js.map
|