"use strict"; const common_vendor = require("../../common/vendor.js"); const utils_api = require("../../utils/api.js"); const _sfc_main = { data() { return { audiobookId: null, chapterId: null, bookInfo: { id: null, title: "", author: "", image: "" }, chapterInfo: { id: null, title: "", audioUrl: "", duration: 0 }, isPlaying: false, currentTime: "00:00", currentSeconds: 0, totalTime: "00:00", totalSeconds: 0, progressPercent: 0, audioContext: null, playTimer: null, progressSaveTimer: null, userInfo: null, isDragging: false }; }, onLoad(options) { if (options.audiobookId) { this.audiobookId = parseInt(options.audiobookId); } else if (options.bookId) { this.audiobookId = 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 (options.chapterId) { this.chapterId = parseInt(options.chapterId); } if (options.audioUrl) { this.chapterInfo.audioUrl = decodeURIComponent(options.audioUrl); } this.loadUserInfo(); if (this.chapterId) { this.initPlayer(); } }, onUnload() { this.saveProgress(); if (this.audioContext) { this.audioContext.destroy(); } if (this.playTimer) { clearInterval(this.playTimer); } if (this.progressSaveTimer) { clearInterval(this.progressSaveTimer); } }, 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; } } catch (e) { common_vendor.index.__f__("error", "at pages/player/player.vue:146", "获取用户信息失败:", e); } }, async initPlayer() { try { if (this.chapterId) { const res = await utils_api.getChapterDetail(this.chapterId); if (res && res.code === 200 && res.data) { this.chapterInfo = { id: res.data.id, title: res.data.title || "", audioUrl: res.data.audioUrl || this.chapterInfo.audioUrl, duration: res.data.duration || 0 }; this.totalSeconds = this.chapterInfo.duration; this.totalTime = this.formatTime(this.totalSeconds); } } if (this.userInfo && this.userInfo.id && this.chapterId) { const progressRes = await utils_api.getListeningProgress(this.userInfo.id, this.chapterId); if (progressRes && progressRes.code === 200 && progressRes.data) { const progress = progressRes.data; this.currentSeconds = progress.currentPosition || 0; this.currentTime = this.formatTime(this.currentSeconds); this.progressPercent = this.totalSeconds > 0 ? this.currentSeconds / this.totalSeconds * 100 : 0; } } this.audioContext = common_vendor.index.createInnerAudioContext(); this.audioContext.src = this.chapterInfo.audioUrl; this.audioContext.startTime = this.currentSeconds; this.audioContext.onPlay(() => { this.isPlaying = true; this.startProgressTimer(); }); this.audioContext.onPause(() => { this.isPlaying = false; this.stopProgressTimer(); }); this.audioContext.onEnded(() => { this.isPlaying = false; this.stopProgressTimer(); }); this.audioContext.onTimeUpdate(() => { if (!this.isDragging) { this.currentSeconds = Math.floor(this.audioContext.currentTime); this.currentTime = this.formatTime(this.currentSeconds); if (this.totalSeconds > 0) { this.progressPercent = this.currentSeconds / this.totalSeconds * 100; } } }); if (this.audiobookId && this.chapterId) { try { await utils_api.playChapter(this.audiobookId, this.chapterId); } catch (e) { common_vendor.index.__f__("error", "at pages/player/player.vue:215", "记录播放失败:", e); } } if (this.userInfo && this.userInfo.id && this.audiobookId && this.chapterId) { try { await utils_api.recordListeningHistory(this.userInfo.id, this.audiobookId, this.chapterId); } catch (e) { common_vendor.index.__f__("error", "at pages/player/player.vue:224", "记录听书历史失败:", e); } } this.progressSaveTimer = setInterval(() => { this.saveProgress(); }, 3e4); } catch (e) { common_vendor.index.__f__("error", "at pages/player/player.vue:234", "初始化播放器失败:", e); common_vendor.index.showToast({ title: "加载失败,请重试", icon: "none" }); } }, goBack() { this.saveProgress(); common_vendor.index.navigateBack(); }, handleShare() { common_vendor.index.showToast({ title: "分享", icon: "none" }); }, handleAddToShelf() { common_vendor.index.showToast({ title: "已加入书架", icon: "success" }); }, togglePlay() { if (!this.audioContext) { common_vendor.index.showToast({ title: "播放器未初始化", icon: "none" }); return; } if (this.isPlaying) { this.audioContext.pause(); } else { this.audioContext.play(); } }, startProgressTimer() { }, stopProgressTimer() { }, playPrevious() { common_vendor.index.showToast({ title: "上一章功能待实现", icon: "none" }); }, playNext() { common_vendor.index.showToast({ title: "下一章功能待实现", icon: "none" }); }, rewind15s() { if (this.audioContext) { const newTime = Math.max(0, this.currentSeconds - 15); this.audioContext.seek(newTime); this.currentSeconds = newTime; this.currentTime = this.formatTime(newTime); if (this.totalSeconds > 0) { this.progressPercent = newTime / this.totalSeconds * 100; } } }, forward15s() { if (this.audioContext) { const newTime = Math.min(this.totalSeconds, this.currentSeconds + 15); this.audioContext.seek(newTime); this.currentSeconds = newTime; this.currentTime = this.formatTime(newTime); if (this.totalSeconds > 0) { this.progressPercent = newTime / this.totalSeconds * 100; } } }, handleProgressTouchStart(e) { this.isDragging = true; }, handleProgressTouchMove(e) { }, async handleProgressTouchEnd(e) { this.isDragging = false; if (this.audioContext && this.totalSeconds > 0) { const newTime = Math.floor(this.progressPercent / 100 * this.totalSeconds); this.audioContext.seek(newTime); this.currentSeconds = newTime; this.currentTime = this.formatTime(newTime); await this.saveProgress(); } }, async saveProgress() { if (this.userInfo && this.userInfo.id && this.audiobookId && this.chapterId) { try { await utils_api.saveListeningProgress( this.userInfo.id, this.audiobookId, this.chapterId, this.currentSeconds, this.totalSeconds ); } catch (e) { common_vendor.index.__f__("error", "at pages/player/player.vue:344", "保存进度失败:", e); } } }, formatTime(seconds) { if (!seconds || seconds < 0) { return "00:00"; } const hours = Math.floor(seconds / 3600); const mins = Math.floor(seconds % 3600 / 60); const secs = seconds % 60; if (hours > 0) { return `${hours.toString().padStart(2, "0")}:${mins.toString().padStart(2, "0")}:${secs.toString().padStart(2, "0")}`; } else { return `${mins.toString().padStart(2, "0")}:${secs.toString().padStart(2, "0")}`; } } } }; 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.o((...args) => $options.handleShare && $options.handleShare(...args)), c: $data.bookInfo.image, d: common_vendor.t($data.bookInfo.title), e: $data.chapterInfo.title }, $data.chapterInfo.title ? { f: common_vendor.t($data.chapterInfo.title) } : {}, { g: common_vendor.t($data.bookInfo.author), h: common_vendor.o((...args) => $options.handleAddToShelf && $options.handleAddToShelf(...args)), i: $data.progressPercent + "%", j: !$data.isDragging }, !$data.isDragging ? { k: $data.progressPercent + "%" } : {}, { l: common_vendor.o((...args) => $options.handleProgressTouchStart && $options.handleProgressTouchStart(...args)), m: common_vendor.o((...args) => $options.handleProgressTouchMove && $options.handleProgressTouchMove(...args)), n: common_vendor.o((...args) => $options.handleProgressTouchEnd && $options.handleProgressTouchEnd(...args)), o: common_vendor.t($data.currentTime), p: common_vendor.t($data.totalTime), q: common_vendor.o((...args) => $options.rewind15s && $options.rewind15s(...args)), r: common_vendor.o((...args) => $options.playPrevious && $options.playPrevious(...args)), s: common_vendor.t($data.isPlaying ? "⏸" : "▶"), t: common_vendor.o((...args) => $options.togglePlay && $options.togglePlay(...args)), v: common_vendor.o((...args) => $options.playNext && $options.playNext(...args)), w: common_vendor.o((...args) => $options.forward15s && $options.forward15s(...args)) }); } const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-0391012f"]]); wx.createPage(MiniProgramPage); //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/player/player.js.map