| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479 |
- <template>
- <view class="reader-container" :class="{ 'night-mode': nightMode }">
- <!-- 顶部导航栏 -->
- <view class="reader-header" v-if="showHeader">
- <view class="header-left">
- <text class="iconfont icon-back" @click="goBack"></text>
- <text class="novel-title">{{novelTitle}}</text>
- </view>
- <view class="header-right">
- <text class="iconfont icon-menu" @click="showMenu"></text>
- </view>
- </view>
-
- <!-- 阅读内容区域 -->
- <scroll-view
- class="reader-content"
- scroll-y
- :scroll-top="scrollTop"
- @scroll="onScroll"
- @touchstart="onTouchStart"
- @touchend="onTouchEnd"
- >
- <view class="chapter-title">{{chapterTitle}}</view>
- <view class="content-text">
- <text>{{content}}</text>
- </view>
-
- <!-- 加载状态 -->
- <view v-if="loading" class="loading-container">
- <text>加载中...</text>
- </view>
-
- <!-- 错误状态 -->
- <view v-if="error" class="error-container">
- <text>{{error}}</text>
- <button @click="loadChapterContent">重试</button>
- </view>
- </scroll-view>
-
- <!-- 底部控制栏 -->
- <view class="reader-footer" v-if="showFooter">
- <view class="progress-text">
- 第{{currentChapter}}章 {{progress}}%
- </view>
- <view class="control-buttons">
- <text class="iconfont icon-catalog" @click="showCatalog"></text>
- <text class="iconfont icon-font" @click="showFontSettings"></text>
- <text class="iconfont icon-night" @click="toggleNightMode"></text>
- <text class="iconfont icon-download" @click="downloadChapter"></text>
- </view>
- </view>
-
- <!-- 广告位 -->
- <ad v-if="showAd" unit-id="您的广告单元ID" class="reader-ad"></ad>
-
- <!-- 设置面板 -->
- <view class="settings-panel" v-if="showSettings">
- <view class="font-size-controls">
- <text class="iconfont icon-font-small" @click="decreaseFontSize"></text>
- <text>字体大小: {{fontSize}}px</text>
- <text class="iconfont icon-font-large" @click="increaseFontSize"></text>
- </view>
- <view class="background-options">
- <view
- v-for="bg in backgroundOptions"
- :key="bg.value"
- class="bg-option"
- :class="{ active: backgroundColor === bg.value }"
- :style="{ backgroundColor: bg.value }"
- @click="changeBackground(bg.value)"
- ></view>
- </view>
- </view>
-
- <!-- 目录面板 -->
- <view class="catalog-panel" v-if="showCatalogPanel">
- <view class="catalog-header">
- <text class="catalog-title">目录</text>
- <text class="iconfont icon-close" @click="showCatalogPanel = false"></text>
- </view>
- <scroll-view class="chapter-list" scroll-y>
- <view
- v-for="chapter in chapterList"
- :key="chapter.id"
- class="chapter-item"
- :class="{ active: chapter.id === chapterId }"
- @click="selectChapter(chapter.id)"
- >
- <text>{{chapter.title}}</text>
- </view>
- </scroll-view>
- </view>
- </view>
- </template>
-
-
- <script>
- export default {
- data() {
- return {
- novelId: '',
- chapterId: '',
- novelTitle: '加载中...',
- chapterTitle: '加载中...',
- content: '正在加载内容...', // 直接存储解码后的内容
- currentChapter: 1,
- totalChapters: 0,
- progress: 0,
- scrollTop: 0,
- showHeader: false,
- showFooter: false,
- showSettings: false,
- showCatalogPanel: false,
- showAd: false,
- nightMode: false,
- touchStartX: 0,
- touchStartY: 0,
- lastScrollPosition: 0,
- loading: false,
- error: null,
- fontSize: 18,
- backgroundColor: '#f5f0e1',
- backgroundOptions: [
- { value: '#f5f0e1', name: '羊皮纸' },
- { value: '#e8f5e9', name: '护眼绿' },
- { value: '#e3f2fd', name: '天空蓝' },
- { value: '#fce4ec', name: '粉红' },
- { value: '#1a1a1a', name: '夜间' }
- ],
- chapterList: []
- }
- },
- // 添加所有生命周期钩子进行调试
- beforeCreate() {
- console.log('reader beforeCreate');
- },
- created() {
- console.log('reader created');
- },
- beforeMount() {
- console.log('reader beforeMount');
- },
- mounted() {
- console.log('reader mounted');
- },
- onReady() {
- console.log('reader onReady');
- },
- onHide() {
- console.log('reader onHide');
- },
- onUnload() {
- console.log('reader onUnload');
- },
- onLoad(options) {
- console.log('阅读器页面加载,参数:', options);
-
- // 确保参数获取方式正确
- this.novelId = options.novelId || '';
- this.chapterId = options.chapterId || '1';
-
- if (!this.novelId) {
- console.error('小说ID参数缺失');
- this.error = '小说ID参数缺失';
- uni.showToast({
- title: '小说ID参数缺失',
- icon: 'none'
- });
- return;
- }
-
- // 显示加载提示
- uni.showLoading({
- title: '加载中...',
- mask: true
- });
-
- this.loadChapterContent();
- },
- onShow() {
- // 确保页面显示时也能处理参数
- const pages = getCurrentPages();
- const currentPage = pages[pages.length - 1];
- if (currentPage && currentPage.options) {
- const options = currentPage.options;
- if (options.novelId && !this.novelId) {
- this.novelId = options.novelId;
- this.chapterId = options.chapterId || '1';
- this.loadChapterContent();
- }
- }
- },
- methods: {
- async loadChapterContent() {
- this.loading = true;
- this.error = null;
-
- try {
- console.log('开始请求章节内容,章节ID:', this.chapterId);
-
- // 使用若依框架的API调用方式
- const res = await this.$http.get(`/novel/chapter/${this.chapterId}`);
- console.log('章节内容响应:', res);
-
- if (res.code === 200) {
- this.novelTitle = res.data.novelTitle || '未知小说';
- this.chapterTitle = res.data.chapterTitle || '未知章节';
-
- // 后台已经解码Base64,直接使用内容
- this.content = res.data.content || '';
-
- // 设置章节信息
- this.currentChapter = res.data.chapterIndex || 1;
- this.totalChapters = res.data.totalChapters || 0;
-
- uni.showToast({
- title: '加载成功',
- icon: 'success',
- duration: 1500
- });
- } else {
- console.error('API返回错误状态码:', res.code);
- this.error = '加载失败: ' + (res.msg || '未知错误');
- uni.showToast({
- title: res.msg || '加载失败',
- icon: 'none'
- });
- }
- } catch (error) {
- console.error('加载章节内容失败:', error);
- this.error = '加载失败,请检查网络连接';
- uni.showToast({
- title: '加载失败,请检查网络连接',
- icon: 'none'
- });
- } finally {
- this.loading = false;
- uni.hideLoading();
- }
- },
-
- calculateProgress() {
- // 简化进度计算
- this.progress = Math.min(100, Math.round((this.lastScrollPosition / 1000) * 100));
- },
-
- goBack() {
- uni.navigateBack();
- },
-
- prevChapter() {
- if (this.currentChapter > 1) {
- this.chapterId = parseInt(this.chapterId) - 1;
- this.loadChapterContent();
- } else {
- uni.showToast({ title: '已经是第一章了', icon: 'none' });
- }
- },
-
- nextChapter() {
- if (this.currentChapter < this.totalChapters) {
- this.chapterId = parseInt(this.chapterId) + 1;
- this.loadChapterContent();
- } else {
- uni.showToast({ title: '已经是最后一章了', icon: 'none' });
- }
- },
-
- selectChapter(chapterId) {
- this.chapterId = chapterId;
- this.showCatalogPanel = false;
- this.loadChapterContent();
- },
-
- showMenu() {
- this.showHeader = !this.showHeader;
- this.showFooter = !this.showFooter;
- },
-
- showCatalog() {
- this.showCatalogPanel = true;
- },
-
- showFontSettings() {
- this.showSettings = !this.showSettings;
- },
-
- toggleNightMode() {
- this.nightMode = !this.nightMode;
- this.backgroundColor = this.nightMode ? '#1a1a1a' : '#f5f0e1';
- },
-
- increaseFontSize() {
- this.fontSize = Math.min(24, this.fontSize + 2);
- },
-
- decreaseFontSize() {
- this.fontSize = Math.max(14, this.fontSize - 2);
- },
-
- changeBackground(color) {
- this.backgroundColor = color;
- this.nightMode = color === '#1a1a1a';
- },
-
- setAdStrategy() {
- const readChapters = uni.getStorageSync('readChapters') || 0;
- this.showAd = readChapters > 0 && readChapters % 3 === 0;
- },
-
- downloadChapter() {
- // 下载本章内容供离线阅读
- }
- }
- }
- </script>
-
-
- <style>
- .reader-container {
- position: relative;
- height: 100vh;
- display: flex;
- flex-direction: column;
- background-color: v-bind(backgroundColor);
- transition: background-color 0.3s ease;
- }
-
- .reader-header {
- height: 44px;
- padding: 0 15px;
- display: flex;
- justify-content: space-between;
- align-items: center;
- background: rgba(0, 0, 0, 0.8);
- color: white;
- position: fixed;
- top: 0;
- left: 0;
- right: 0;
- z-index: 1000;
- }
-
- .reader-content {
- flex: 1;
- padding: 20px;
- box-sizing: border-box;
- line-height: 1.8;
- font-size: v-bind(fontSize + 'px');
- color: #333;
- margin-top: 44px;
- margin-bottom: 50px;
- }
-
- .chapter-title {
- text-align: center;
- font-size: 20px;
- font-weight: bold;
- margin-bottom: 20px;
- }
-
- .content-text {
- text-indent: 2em;
- line-height: 1.8;
- }
-
- .reader-footer {
- height: 50px;
- padding: 0 15px;
- display: flex;
- justify-content: space-between;
- align-items: center;
- background: rgba(0, 0, 0, 0.8);
- color: white;
- position: fixed;
- bottom: 0;
- left: 0;
- right: 0;
- z-index: 1000;
- }
-
- .reader-ad {
- height: 90px;
- margin-bottom: 10px;
- }
-
- .loading-container, .error-container {
- display: flex;
- flex-direction: column;
- align-items: center;
- justify-content: center;
- padding: 50px 0;
- }
-
- .error-container button {
- margin-top: 20px;
- background-color: var(--primary-color);
- color: white;
- border: none;
- padding: 10px 20px;
- border-radius: 5px;
- }
-
- .settings-panel {
- position: fixed;
- bottom: 50px;
- left: 0;
- right: 0;
- background: rgba(255, 255, 255, 0.95);
- padding: 20px;
- z-index: 999;
- box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
- }
-
- .font-size-controls {
- display: flex;
- justify-content: space-between;
- align-items: center;
- margin-bottom: 20px;
- }
-
- .background-options {
- display: flex;
- justify-content: space-around;
- }
-
- .bg-option {
- width: 40px;
- height: 40px;
- border-radius: 50%;
- border: 2px solid transparent;
- cursor: pointer;
- }
-
- .bg-option.active {
- border-color: var(--primary-color);
- }
-
- .catalog-panel {
- position: fixed;
- top: 0;
- left: 0;
- right: 0;
- bottom: 0;
- background: white;
- z-index: 1001;
- }
-
- .catalog-header {
- display: flex;
- justify-content: space-between;
- align-items: center;
- padding: 15px;
- border-bottom: 1px solid #eee;
- }
-
- .chapter-list {
- height: calc(100vh - 60px);
- }
-
- .chapter-item {
- padding: 15px;
- border-bottom: 1px solid #eee;
- }
-
- .chapter-item.active {
- color: var(--primary-color);
- font-weight: bold;
- }
-
- /* 夜间模式样式 */
- .night-mode .reader-content {
- color: #888;
- }
-
- .night-mode .content-text {
- color: #888;
- }
- </style>
|