| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- "use strict";
- const common_vendor = require("../../common/vendor.js");
- const utils_api = require("../../utils/api.js");
- const _sfc_main = {
- data() {
- return {
- bookInfo: {
- id: null,
- title: "",
- image: "",
- cover: "",
- brief: "",
- desc: "",
- author: "",
- isFree: false,
- price: 0,
- introduction: ""
- },
- isLoading: false,
- userInfo: null,
- isInShelf: false,
- reviews: [],
- recommendBooks: []
- };
- },
- onLoad(options) {
- try {
- const userInfo = common_vendor.index.getStorageSync("userInfo");
- if (userInfo && userInfo.id) {
- this.userInfo = userInfo;
- }
- } catch (e) {
- common_vendor.index.__f__("error", "at pages/book-detail/book-detail.vue:131", "获取用户信息失败", e);
- }
- if (options.bookId) {
- this.bookInfo.id = parseInt(options.bookId);
- this.loadBookDetail(this.bookInfo.id);
- } else if (options.title) {
- this.bookInfo.title = decodeURIComponent(options.title);
- if (options.image) {
- this.bookInfo.image = decodeURIComponent(options.image);
- }
- if (options.author) {
- this.bookInfo.author = decodeURIComponent(options.author);
- }
- if (options.bookId) {
- this.bookInfo.id = parseInt(options.bookId);
- this.loadBookDetail(this.bookInfo.id);
- }
- }
- },
- onShow() {
- try {
- const userInfo = common_vendor.index.getStorageSync("userInfo");
- if (userInfo && userInfo.id) {
- this.userInfo = userInfo;
- if (this.bookInfo.id) {
- this.checkBookshelfStatus(userInfo.id, this.bookInfo.id);
- this.loadComments(this.bookInfo.id);
- }
- }
- } catch (e) {
- common_vendor.index.__f__("error", "at pages/book-detail/book-detail.vue:169", "获取用户信息失败", e);
- }
- },
- methods: {
- goBack() {
- common_vendor.index.navigateBack();
- },
- handleShare() {
- common_vendor.index.showToast({
- title: "分享功能",
- icon: "none"
- });
- },
- async loadBookDetail(bookId) {
- if (!bookId) {
- common_vendor.index.showToast({
- title: "书籍ID不能为空",
- icon: "none"
- });
- return;
- }
- try {
- this.isLoading = true;
- common_vendor.index.showLoading({
- title: "加载中...",
- mask: false
- });
- const userId = this.userInfo && this.userInfo.id ? this.userInfo.id : null;
- const res = await utils_api.getBookById(bookId, userId);
- if (res && res.code === 200 && res.data) {
- const book = res.data;
- this.bookInfo = {
- id: book.id,
- title: book.title || "",
- image: book.image || book.cover || "",
- cover: book.cover || book.image || "",
- brief: book.brief || "",
- desc: book.desc || book.brief || "",
- author: book.author || "",
- isFree: book.isFree || false,
- price: book.price || 0,
- introduction: book.introduction || book.desc || book.brief || ""
- };
- if (!this.bookInfo.brief && this.bookInfo.desc) {
- this.bookInfo.brief = this.bookInfo.desc;
- }
- if (!this.bookInfo.introduction) {
- this.bookInfo.introduction = this.bookInfo.desc || this.bookInfo.brief || "";
- }
- if (this.userInfo && this.userInfo.id) {
- this.checkBookshelfStatus(this.userInfo.id, bookId);
- }
- this.loadRecommendBooks();
- if (this.userInfo && this.userInfo.id) {
- this.recordBrowsingHistory(this.userInfo.id, bookId);
- }
- this.loadComments(bookId);
- } else {
- common_vendor.index.showToast({
- title: res.message || "加载失败",
- icon: "none"
- });
- }
- } catch (e) {
- common_vendor.index.__f__("error", "at pages/book-detail/book-detail.vue:250", "加载书籍详情失败:", e);
- common_vendor.index.showToast({
- title: e.message || "加载失败,请重试",
- icon: "none"
- });
- } finally {
- this.isLoading = false;
- common_vendor.index.hideLoading();
- }
- },
- async loadRecommendBooks() {
- try {
- const res = await utils_api.getMoreRecommend(6);
- if (res && res.code === 200 && res.data && Array.isArray(res.data)) {
- this.recommendBooks = res.data.filter((book) => book.id !== this.bookInfo.id).slice(0, 6).map((book) => ({
- id: book.id,
- title: book.title || "",
- image: book.image || book.cover || ""
- }));
- }
- } catch (e) {
- common_vendor.index.__f__("error", "at pages/book-detail/book-detail.vue:275", "加载推荐书籍失败:", e);
- }
- },
- handleWriteReview() {
- common_vendor.index.navigateTo({
- url: `/pages/write-review/write-review?bookId=${this.bookInfo.id}&title=${encodeURIComponent(this.bookInfo.title)}`
- });
- },
- async toggleLike(review, index) {
- if (!this.userInfo || !this.userInfo.id) {
- common_vendor.index.showToast({
- title: "请先登录",
- icon: "none"
- });
- return;
- }
- try {
- const res = await utils_api.toggleCommentLike(this.userInfo.id, review.id);
- if (res && res.code === 200) {
- this.reviews[index].isLiked = res.data;
- if (res.data) {
- this.reviews[index].likes++;
- } else {
- this.reviews[index].likes--;
- }
- } else {
- common_vendor.index.showToast({
- title: res.message || "操作失败",
- icon: "none"
- });
- }
- } catch (e) {
- common_vendor.index.__f__("error", "at pages/book-detail/book-detail.vue:311", "点赞失败", e);
- common_vendor.index.showToast({
- title: "操作失败,请重试",
- icon: "none"
- });
- }
- },
- async loadComments(bookId) {
- if (!bookId) {
- return;
- }
- try {
- const userId = this.userInfo && this.userInfo.id ? this.userInfo.id : null;
- const res = await utils_api.getComments(bookId, userId);
- if (res && res.code === 200 && res.data) {
- this.reviews = res.data.map((item) => ({
- id: item.id,
- name: item.name || "匿名用户",
- avatar: item.avatar || "https://via.placeholder.com/100x100?text=User",
- isRecommended: item.isRecommended || false,
- date: item.date || "",
- likes: item.likes || 0,
- isLiked: item.isLiked || false,
- content: item.content || ""
- }));
- }
- } catch (e) {
- common_vendor.index.__f__("error", "at pages/book-detail/book-detail.vue:340", "加载评论失败", e);
- }
- },
- async recordBrowsingHistory(userId, bookId) {
- if (!userId || !bookId) {
- return;
- }
- try {
- await utils_api.recordBrowsingHistory(userId, bookId);
- } catch (e) {
- common_vendor.index.__f__("error", "at pages/book-detail/book-detail.vue:352", "记录浏览历史失败", e);
- }
- },
- viewAllReviews() {
- common_vendor.index.showToast({
- title: "查看全部书评",
- icon: "none"
- });
- },
- viewMoreRecommend() {
- common_vendor.index.showToast({
- title: "查看更多推荐",
- icon: "none"
- });
- },
- 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}`
- });
- },
- // 检查书架状态
- checkBookshelfStatus(userId, bookId) {
- if (!userId || !bookId) {
- return;
- }
- utils_api.checkInBookshelf(userId, bookId).then((res) => {
- if (res.code === 200 && res.data !== void 0) {
- this.isInShelf = res.data;
- }
- }).catch((err) => {
- common_vendor.index.__f__("error", "at pages/book-detail/book-detail.vue:393", "检查书架状态失败", err);
- });
- },
- handleAddToShelf() {
- if (!this.userInfo || !this.userInfo.id) {
- common_vendor.index.showModal({
- title: "提示",
- content: "请先登录",
- showCancel: true,
- cancelText: "取消",
- confirmText: "去登录",
- success: (res) => {
- if (res.confirm) {
- common_vendor.index.navigateTo({
- url: "/pages/login/login"
- });
- }
- }
- });
- return;
- }
- if (this.isInShelf) {
- common_vendor.index.showToast({
- title: "已在书架中",
- icon: "none"
- });
- return;
- }
- if (!this.bookInfo.id) {
- common_vendor.index.showToast({
- title: "书籍信息不完整",
- icon: "none"
- });
- return;
- }
- if (this.isLoading) {
- return;
- }
- this.isLoading = true;
- common_vendor.index.showLoading({
- title: "加入中...",
- mask: true
- });
- utils_api.addToBookshelf(this.userInfo.id, this.bookInfo.id).then((res) => {
- common_vendor.index.hideLoading();
- this.isLoading = false;
- if (res.code === 200) {
- this.isInShelf = true;
- common_vendor.index.showToast({
- title: "已加入书架",
- icon: "success"
- });
- } else {
- common_vendor.index.showToast({
- title: res.message || "加入失败",
- icon: "none"
- });
- }
- }).catch((err) => {
- common_vendor.index.hideLoading();
- this.isLoading = false;
- common_vendor.index.__f__("error", "at pages/book-detail/book-detail.vue:468", "加入书架失败:", err);
- common_vendor.index.showToast({
- title: err.message || "加入失败,请检查网络连接",
- icon: "none",
- duration: 2e3
- });
- });
- },
- handleRead() {
- common_vendor.index.navigateTo({
- url: `/pages/book-cover/book-cover?bookId=${this.bookInfo.id}&title=${encodeURIComponent(this.bookInfo.title)}&image=${encodeURIComponent(this.bookInfo.image)}&author=${encodeURIComponent(this.bookInfo.author)}`
- });
- }
- }
- };
- 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.bookInfo.title),
- c: common_vendor.o((...args) => $options.handleShare && $options.handleShare(...args)),
- d: !$data.isLoading
- }, !$data.isLoading ? common_vendor.e({
- e: $data.bookInfo.image || $data.bookInfo.cover,
- f: common_vendor.t($data.bookInfo.title),
- g: $data.bookInfo.brief || $data.bookInfo.desc
- }, $data.bookInfo.brief || $data.bookInfo.desc ? {
- h: common_vendor.t($data.bookInfo.brief || $data.bookInfo.desc)
- } : {}, {
- i: $data.bookInfo.author
- }, $data.bookInfo.author ? {
- j: common_vendor.t($data.bookInfo.author)
- } : {}, {
- k: $data.bookInfo.isFree
- }, $data.bookInfo.isFree ? {} : $data.bookInfo.price ? {
- m: common_vendor.t($data.bookInfo.price)
- } : {}, {
- l: $data.bookInfo.price
- }) : {}, {
- n: $data.isLoading
- }, $data.isLoading ? {} : {}, {
- o: $data.bookInfo.introduction || $data.bookInfo.desc || $data.bookInfo.brief
- }, $data.bookInfo.introduction || $data.bookInfo.desc || $data.bookInfo.brief ? {
- p: common_vendor.t($data.bookInfo.introduction || $data.bookInfo.desc || $data.bookInfo.brief)
- } : {}, {
- q: common_vendor.o((...args) => $options.handleWriteReview && $options.handleWriteReview(...args)),
- r: common_vendor.f($data.reviews, (review, index, i0) => {
- return common_vendor.e({
- a: review.avatar,
- b: common_vendor.t(review.name),
- c: review.isRecommended
- }, review.isRecommended ? {} : {}, {
- d: common_vendor.t(review.date),
- e: common_vendor.t(review.isLiked ? "❤️" : "🤍"),
- f: common_vendor.t(review.likes),
- g: common_vendor.o(($event) => $options.toggleLike(review, index), index),
- h: common_vendor.t(review.content),
- i: index
- });
- }),
- s: common_vendor.o((...args) => $options.viewAllReviews && $options.viewAllReviews(...args)),
- t: common_vendor.o((...args) => $options.viewMoreRecommend && $options.viewMoreRecommend(...args)),
- v: common_vendor.f($data.recommendBooks, (book, index, i0) => {
- return {
- a: book.image,
- b: common_vendor.t(book.title),
- c: index,
- d: common_vendor.o(($event) => $options.goToBookDetail(book), index)
- };
- }),
- w: common_vendor.t($data.isInShelf ? "已在书架" : "加入书架"),
- x: $data.isInShelf ? 1 : "",
- y: common_vendor.o((...args) => $options.handleAddToShelf && $options.handleAddToShelf(...args)),
- z: $data.isLoading,
- A: common_vendor.o((...args) => $options.handleRead && $options.handleRead(...args))
- });
- }
- const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-ac1886ed"]]);
- wx.createPage(MiniProgramPage);
- //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/book-detail/book-detail.js.map
|