"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: "", author: "", image: "" }, currentChapter: null, contentLines: [], chapterList: [], isLoading: false, showToolbar: false, showChapterModal: false, showNotesModal: false, showFontSizeModal: false, isDarkMode: false, fontSize: 32, fontSizeOptions: [ { label: "小", value: 28 }, { label: "中", value: 32 }, { label: "大", value: 36 }, { label: "特大", value: 40 } ], notes: [], newNote: "", showSelectionMenu: false, selectionMenuTop: 0, selectionMenuLeft: 0, selectedText: "", selectedLine: "", selectedLineIndex: -1 }; }, onLoad(options) { if (options.bookId) { this.bookInfo.id = parseInt(options.bookId); } 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 (this.bookInfo.id) { this.loadBookInfo(); this.loadChapterList(); } this.loadReadingProgress(); this.loadSettings(); this.loadNotes(); }, methods: { goBack() { common_vendor.index.navigateBack(); }, handleShare() { common_vendor.index.showToast({ title: "分享功能", icon: "none" }); }, handleContentTap(e) { if (!this.showSelectionMenu) { this.toggleToolbar(); } this.hideSelectionMenu(); }, toggleToolbar() { this.showToolbar = !this.showToolbar; }, handleScroll(e) { if (this.showSelectionMenu) { this.hideSelectionMenu(); } }, // 文本选择功能 handleLongPress(e, line, index) { if (line && line.trim()) { const touch = e.touches && e.touches[0] || e.changedTouches && e.changedTouches[0]; if (touch) { this.selectedText = line; this.selectedLine = line; this.selectedLineIndex = index; setTimeout(() => { const query = common_vendor.index.createSelectorQuery().in(this); query.select(".content-area").boundingClientRect((rect) => { if (rect) { const menuWidth = 200; const menuHeight = 100; let left = touch.clientX - menuWidth / 2; if (left < 20) left = 20; if (left + menuWidth > rect.width - 20) { left = rect.width - menuWidth - 20; } let top = touch.clientY - menuHeight - 20; if (top < rect.top + 20) { top = touch.clientY + 20; } this.selectionMenuTop = top; this.selectionMenuLeft = left; this.showSelectionMenu = true; } }).exec(); }, 50); } } }, handleTouchStart(e) { }, handleTouchEnd(e) { }, hideSelectionMenu() { this.showSelectionMenu = false; this.selectedText = ""; this.selectedLine = ""; this.selectedLineIndex = -1; }, copySelectedText() { if (this.selectedText) { common_vendor.index.setClipboardData({ data: this.selectedText, success: () => { common_vendor.index.showToast({ title: "已复制到剪贴板", icon: "success" }); } }); this.hideSelectionMenu(); } }, fallbackCopy(text) { const textarea = document.createElement("textarea"); textarea.value = text; textarea.style.position = "fixed"; textarea.style.opacity = "0"; document.body.appendChild(textarea); textarea.select(); try { document.execCommand("copy"); common_vendor.index.showToast({ title: "已复制到剪贴板", icon: "success" }); } catch (err) { common_vendor.index.showToast({ title: "复制失败", icon: "none" }); } document.body.removeChild(textarea); }, writeNoteFromSelection() { if (this.selectedText) { this.newNote = `"${this.selectedText}"`; this.hideSelectionMenu(); this.showNotesModal = true; } }, // 目录功能 showChapterList() { this.showChapterModal = true; }, closeChapterModal() { this.showChapterModal = false; }, selectChapter(chapter) { this.currentChapter = chapter; this.loadChapterContent(chapter.id); this.closeChapterModal(); this.showToolbar = false; }, async loadBookInfo() { if (!this.bookInfo.id) return; try { const res = await utils_api.getBookById(this.bookInfo.id); if (res && res.code === 200 && res.data) { const book = res.data; this.bookInfo.title = book.title || this.bookInfo.title; this.bookInfo.author = book.author || this.bookInfo.author; this.bookInfo.image = book.image || book.cover || this.bookInfo.image; } } catch (e) { common_vendor.index.__f__("error", "at pages/reader/reader.vue:410", "加载书籍信息失败:", e); } }, async loadChapterList() { if (!this.bookInfo.id) return; try { this.isLoading = true; const res = await utils_api.getBookChapters(this.bookInfo.id); if (res && res.code === 200 && res.data) { this.chapterList = res.data.map((ch) => ({ id: ch.id, title: ch.title, chapterNumber: ch.chapterNumber })); if (this.chapterList.length > 0 && !this.currentChapter) { this.currentChapter = this.chapterList[0]; this.loadChapterContent(this.currentChapter.id); } } } catch (e) { common_vendor.index.__f__("error", "at pages/reader/reader.vue:433", "加载章节列表失败:", e); common_vendor.index.showToast({ title: "加载章节列表失败", icon: "none" }); } finally { this.isLoading = false; } }, async loadChapterContent(chapterId) { if (!chapterId) return; try { this.isLoading = true; const res = await utils_api.getBookChapterDetail(chapterId); if (res && res.code === 200 && res.data) { const chapter = res.data; this.currentChapter = { id: chapter.id, title: chapter.title, chapterNumber: chapter.chapterNumber }; if (chapter.content && chapter.content.trim()) { const lines = chapter.content.split(/\r?\n/); this.contentLines = lines && lines.length > 0 ? lines : [chapter.content]; } else { const fallback = (this.bookInfo.introduction || this.bookInfo.desc || this.bookInfo.brief || "").trim(); this.contentLines = fallback ? fallback.split(/\r?\n/) : ["暂无内容"]; } this.saveReadingProgress(); } else { common_vendor.index.showToast({ title: res && res.message ? res.message : "加载章节内容失败", icon: "none" }); } } catch (e) { common_vendor.index.__f__("error", "at pages/reader/reader.vue:476", "加载章节内容失败:", e); common_vendor.index.showToast({ title: "加载章节内容失败,请重试", icon: "none" }); } finally { this.isLoading = false; } }, // 笔记功能 showNotes() { this.showNotesModal = true; }, closeNotesModal() { this.showNotesModal = false; this.newNote = ""; }, addNote() { if (!this.newNote.trim()) { common_vendor.index.showToast({ title: "请输入笔记内容", icon: "none" }); return; } const note = { content: this.newNote, time: this.getCurrentTime(), chapter: this.currentChapter.title }; this.notes.unshift(note); this.newNote = ""; this.saveNotes(); common_vendor.index.showToast({ title: "笔记已添加", icon: "success" }); }, deleteNote(index) { common_vendor.index.showModal({ title: "提示", content: "确定要删除这条笔记吗?", success: (res) => { if (res.confirm) { this.notes.splice(index, 1); this.saveNotes(); common_vendor.index.showToast({ title: "已删除", icon: "success" }); } } }); }, getCurrentTime() { const now = /* @__PURE__ */ new Date(); const month = now.getMonth() + 1; const day = now.getDate(); const hour = now.getHours(); const minute = now.getMinutes(); return `${month}-${day} ${hour}:${minute < 10 ? "0" + minute : minute}`; }, // 夜间模式 toggleTheme() { this.isDarkMode = !this.isDarkMode; this.saveSettings(); common_vendor.index.showToast({ title: this.isDarkMode ? "已切换夜间模式" : "已切换日间模式", icon: "none", duration: 1500 }); }, // 字号功能 showFontSize() { this.showFontSizeModal = true; }, closeFontSizeModal() { this.showFontSizeModal = false; }, selectFontSize(size) { this.fontSize = size; this.saveSettings(); this.closeFontSizeModal(); common_vendor.index.showToast({ title: "字号已调整", icon: "success", duration: 1e3 }); }, // 保存和加载设置 loadSettings() { try { const settings = common_vendor.index.getStorageSync(`reading_settings_${this.bookInfo.id}`); if (settings) { if (settings.fontSize) this.fontSize = settings.fontSize; if (settings.isDarkMode !== void 0) this.isDarkMode = settings.isDarkMode; } } catch (e) { common_vendor.index.__f__("error", "at pages/reader/reader.vue:574", "加载设置失败", e); } }, saveSettings() { try { common_vendor.index.setStorageSync(`reading_settings_${this.bookInfo.id}`, { fontSize: this.fontSize, isDarkMode: this.isDarkMode }); } catch (e) { common_vendor.index.__f__("error", "at pages/reader/reader.vue:584", "保存设置失败", e); } }, loadNotes() { try { const notes = common_vendor.index.getStorageSync(`reading_notes_${this.bookInfo.id}`); if (notes && Array.isArray(notes)) { this.notes = notes; } } catch (e) { common_vendor.index.__f__("error", "at pages/reader/reader.vue:594", "加载笔记失败", e); } }, saveNotes() { try { common_vendor.index.setStorageSync(`reading_notes_${this.bookInfo.id}`, this.notes); } catch (e) { common_vendor.index.__f__("error", "at pages/reader/reader.vue:601", "保存笔记失败", e); } }, loadReadingProgress() { try { const progress = common_vendor.index.getStorageSync(`reading_progress_${this.bookInfo.id}`); if (progress) { if (progress.chapter) this.currentChapter = progress.chapter; } } catch (e) { common_vendor.index.__f__("error", "at pages/reader/reader.vue:612", "加载阅读进度失败", e); } }, saveReadingProgress() { try { common_vendor.index.setStorageSync(`reading_progress_${this.bookInfo.id}`, { chapter: this.currentChapter, bookId: this.bookInfo.id }); } catch (e) { common_vendor.index.__f__("error", "at pages/reader/reader.vue:623", "保存阅读进度失败", e); } } }, onUnload() { this.saveReadingProgress(); } }; function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) { return common_vendor.e({ a: $data.showToolbar }, $data.showToolbar ? { b: common_vendor.o((...args) => $options.goBack && $options.goBack(...args)), c: common_vendor.t($data.bookInfo.title), d: common_vendor.o((...args) => $options.handleShare && $options.handleShare(...args)) } : {}, { e: $data.isLoading && $data.contentLines.length === 0 }, $data.isLoading && $data.contentLines.length === 0 ? {} : {}, { f: $data.currentChapter && !$data.isLoading }, $data.currentChapter && !$data.isLoading ? { g: common_vendor.t($data.currentChapter.title) } : {}, { h: !$data.isLoading || $data.contentLines.length > 0 }, !$data.isLoading || $data.contentLines.length > 0 ? { i: common_vendor.f($data.contentLines, (line, index, i0) => { return { a: common_vendor.t(line), b: index, c: common_vendor.o(($event) => $options.handleLongPress($event, line, index), index), d: common_vendor.o((...args) => $options.handleTouchStart && $options.handleTouchStart(...args), index), e: common_vendor.o((...args) => $options.handleTouchEnd && $options.handleTouchEnd(...args), index) }; }), j: $data.fontSize + "rpx" } : {}, { k: !$data.isLoading && $data.contentLines.length === 0 && $data.chapterList.length === 0 }, !$data.isLoading && $data.contentLines.length === 0 && $data.chapterList.length === 0 ? {} : {}, { l: common_vendor.o((...args) => $options.handleScroll && $options.handleScroll(...args)), m: common_vendor.o((...args) => $options.handleContentTap && $options.handleContentTap(...args)), n: $data.showSelectionMenu }, $data.showSelectionMenu ? { o: common_vendor.o((...args) => $options.copySelectedText && $options.copySelectedText(...args)), p: common_vendor.o((...args) => $options.writeNoteFromSelection && $options.writeNoteFromSelection(...args)), q: $data.selectionMenuTop + "px", r: $data.selectionMenuLeft + "px" } : {}, { s: $data.showSelectionMenu }, $data.showSelectionMenu ? { t: common_vendor.o((...args) => $options.hideSelectionMenu && $options.hideSelectionMenu(...args)) } : {}, { v: $data.showToolbar }, $data.showToolbar ? { w: common_vendor.o((...args) => $options.showChapterList && $options.showChapterList(...args)), x: common_vendor.o((...args) => $options.showNotes && $options.showNotes(...args)), y: common_vendor.o((...args) => $options.toggleTheme && $options.toggleTheme(...args)), z: common_vendor.o((...args) => $options.showFontSize && $options.showFontSize(...args)), A: $data.isDarkMode ? 1 : "" } : {}, { B: $data.showChapterModal }, $data.showChapterModal ? { C: common_vendor.o((...args) => $options.closeChapterModal && $options.closeChapterModal(...args)), D: common_vendor.f($data.chapterList, (chapter, index, i0) => { return { a: common_vendor.t(chapter.title), b: index, c: $data.currentChapter.id === chapter.id ? 1 : "", d: common_vendor.o(($event) => $options.selectChapter(chapter), index) }; }), E: $data.isDarkMode ? 1 : "", F: common_vendor.o(() => { }), G: common_vendor.o((...args) => $options.closeChapterModal && $options.closeChapterModal(...args)) } : {}, { H: $data.showNotesModal }, $data.showNotesModal ? common_vendor.e({ I: common_vendor.o((...args) => $options.closeNotesModal && $options.closeNotesModal(...args)), J: $data.newNote, K: common_vendor.o(($event) => $data.newNote = $event.detail.value), L: common_vendor.o((...args) => $options.addNote && $options.addNote(...args)), M: common_vendor.f($data.notes, (note, index, i0) => { return { a: common_vendor.t(note.time), b: common_vendor.o(($event) => $options.deleteNote(index), index), c: common_vendor.t(note.content), d: index }; }), N: $data.notes.length === 0 }, $data.notes.length === 0 ? {} : {}, { O: $data.isDarkMode ? 1 : "", P: common_vendor.o(() => { }), Q: common_vendor.o((...args) => $options.closeNotesModal && $options.closeNotesModal(...args)) }) : {}, { R: $data.showFontSizeModal }, $data.showFontSizeModal ? { S: common_vendor.f($data.fontSizeOptions, (size, index, i0) => { return { a: common_vendor.t(size.label), b: index, c: $data.fontSize === size.value ? 1 : "", d: common_vendor.o(($event) => $options.selectFontSize(size.value), index) }; }), T: $data.isDarkMode ? 1 : "", U: common_vendor.o(() => { }), V: common_vendor.o((...args) => $options.closeFontSizeModal && $options.closeFontSizeModal(...args)) } : {}, { W: $data.isDarkMode ? 1 : "" }); } const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-7912e6ab"]]); wx.createPage(MiniProgramPage); //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/reader/reader.js.map