| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515 |
- "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
|