|
|
@@ -1,5 +1,5 @@
|
|
1
|
1
|
<template>
|
|
2
|
|
- <view class="reader-container">
|
|
|
2
|
+ <view class="reader-container" :class="{ 'night-mode': nightMode }">
|
|
3
|
3
|
<!-- 顶部导航栏 -->
|
|
4
|
4
|
<view class="reader-header" v-if="showHeader">
|
|
5
|
5
|
<view class="header-left">
|
|
|
@@ -22,7 +22,7 @@
|
|
22
|
22
|
>
|
|
23
|
23
|
<view class="chapter-title">{{chapterTitle}}</view>
|
|
24
|
24
|
<view class="content-text">
|
|
25
|
|
- <text>{{decodedContent}}</text>
|
|
|
25
|
+ <text>{{content}}</text>
|
|
26
|
26
|
</view>
|
|
27
|
27
|
|
|
28
|
28
|
<!-- 加载状态 -->
|
|
|
@@ -55,16 +55,45 @@
|
|
55
|
55
|
|
|
56
|
56
|
<!-- 设置面板 -->
|
|
57
|
57
|
<view class="settings-panel" v-if="showSettings">
|
|
58
|
|
- <!-- 字体大小、背景颜色等设置 -->
|
|
|
58
|
+ <view class="font-size-controls">
|
|
|
59
|
+ <text class="iconfont icon-font-small" @click="decreaseFontSize"></text>
|
|
|
60
|
+ <text>字体大小: {{fontSize}}px</text>
|
|
|
61
|
+ <text class="iconfont icon-font-large" @click="increaseFontSize"></text>
|
|
|
62
|
+ </view>
|
|
|
63
|
+ <view class="background-options">
|
|
|
64
|
+ <view
|
|
|
65
|
+ v-for="bg in backgroundOptions"
|
|
|
66
|
+ :key="bg.value"
|
|
|
67
|
+ class="bg-option"
|
|
|
68
|
+ :class="{ active: backgroundColor === bg.value }"
|
|
|
69
|
+ :style="{ backgroundColor: bg.value }"
|
|
|
70
|
+ @click="changeBackground(bg.value)"
|
|
|
71
|
+ ></view>
|
|
|
72
|
+ </view>
|
|
59
|
73
|
</view>
|
|
60
|
74
|
|
|
61
|
75
|
<!-- 目录面板 -->
|
|
62
|
76
|
<view class="catalog-panel" v-if="showCatalogPanel">
|
|
63
|
|
- <!-- 章节列表 -->
|
|
|
77
|
+ <view class="catalog-header">
|
|
|
78
|
+ <text class="catalog-title">目录</text>
|
|
|
79
|
+ <text class="iconfont icon-close" @click="showCatalogPanel = false"></text>
|
|
|
80
|
+ </view>
|
|
|
81
|
+ <scroll-view class="chapter-list" scroll-y>
|
|
|
82
|
+ <view
|
|
|
83
|
+ v-for="chapter in chapterList"
|
|
|
84
|
+ :key="chapter.id"
|
|
|
85
|
+ class="chapter-item"
|
|
|
86
|
+ :class="{ active: chapter.id === chapterId }"
|
|
|
87
|
+ @click="selectChapter(chapter.id)"
|
|
|
88
|
+ >
|
|
|
89
|
+ <text>{{chapter.title}}</text>
|
|
|
90
|
+ </view>
|
|
|
91
|
+ </scroll-view>
|
|
64
|
92
|
</view>
|
|
65
|
93
|
</view>
|
|
66
|
94
|
</template>
|
|
67
|
95
|
|
|
|
96
|
+
|
|
68
|
97
|
<script>
|
|
69
|
98
|
export default {
|
|
70
|
99
|
data() {
|
|
|
@@ -73,8 +102,7 @@ export default {
|
|
73
|
102
|
chapterId: '',
|
|
74
|
103
|
novelTitle: '加载中...',
|
|
75
|
104
|
chapterTitle: '加载中...',
|
|
76
|
|
- content: '', // Base64编码的内容
|
|
77
|
|
- decodedContent: '正在加载内容...', // 解码后的内容
|
|
|
105
|
+ content: '正在加载内容...', // 直接存储解码后的内容
|
|
78
|
106
|
currentChapter: 1,
|
|
79
|
107
|
totalChapters: 0,
|
|
80
|
108
|
progress: 0,
|
|
|
@@ -83,122 +111,136 @@ export default {
|
|
83
|
111
|
showFooter: false,
|
|
84
|
112
|
showSettings: false,
|
|
85
|
113
|
showCatalogPanel: false,
|
|
86
|
|
- showAd: false, // 默认不显示广告
|
|
|
114
|
+ showAd: false,
|
|
87
|
115
|
nightMode: false,
|
|
88
|
116
|
touchStartX: 0,
|
|
89
|
117
|
touchStartY: 0,
|
|
90
|
118
|
lastScrollPosition: 0,
|
|
91
|
119
|
loading: false,
|
|
92
|
|
- error: null
|
|
|
120
|
+ error: null,
|
|
|
121
|
+ fontSize: 18,
|
|
|
122
|
+ backgroundColor: '#f5f0e1',
|
|
|
123
|
+ backgroundOptions: [
|
|
|
124
|
+ { value: '#f5f0e1', name: '羊皮纸' },
|
|
|
125
|
+ { value: '#e8f5e9', name: '护眼绿' },
|
|
|
126
|
+ { value: '#e3f2fd', name: '天空蓝' },
|
|
|
127
|
+ { value: '#fce4ec', name: '粉红' },
|
|
|
128
|
+ { value: '#1a1a1a', name: '夜间' }
|
|
|
129
|
+ ],
|
|
|
130
|
+ chapterList: []
|
|
93
|
131
|
}
|
|
94
|
132
|
},
|
|
|
133
|
+ // 添加所有生命周期钩子进行调试
|
|
|
134
|
+ beforeCreate() {
|
|
|
135
|
+ console.log('reader beforeCreate');
|
|
|
136
|
+ },
|
|
|
137
|
+ created() {
|
|
|
138
|
+ console.log('reader created');
|
|
|
139
|
+ },
|
|
|
140
|
+ beforeMount() {
|
|
|
141
|
+ console.log('reader beforeMount');
|
|
|
142
|
+ },
|
|
|
143
|
+ mounted() {
|
|
|
144
|
+ console.log('reader mounted');
|
|
|
145
|
+ },
|
|
|
146
|
+ onReady() {
|
|
|
147
|
+ console.log('reader onReady');
|
|
|
148
|
+ },
|
|
|
149
|
+ onHide() {
|
|
|
150
|
+ console.log('reader onHide');
|
|
|
151
|
+ },
|
|
|
152
|
+ onUnload() {
|
|
|
153
|
+ console.log('reader onUnload');
|
|
|
154
|
+ },
|
|
95
|
155
|
onLoad(options) {
|
|
96
|
156
|
console.log('阅读器页面加载,参数:', options);
|
|
97
|
157
|
|
|
98
|
|
- // 确保参数正确获取
|
|
|
158
|
+ // 确保参数获取方式正确
|
|
99
|
159
|
this.novelId = options.novelId || '';
|
|
100
|
160
|
this.chapterId = options.chapterId || '1';
|
|
101
|
161
|
|
|
102
|
162
|
if (!this.novelId) {
|
|
|
163
|
+ console.error('小说ID参数缺失');
|
|
103
|
164
|
this.error = '小说ID参数缺失';
|
|
|
165
|
+ uni.showToast({
|
|
|
166
|
+ title: '小说ID参数缺失',
|
|
|
167
|
+ icon: 'none'
|
|
|
168
|
+ });
|
|
104
|
169
|
return;
|
|
105
|
170
|
}
|
|
106
|
171
|
|
|
|
172
|
+ // 显示加载提示
|
|
|
173
|
+ uni.showLoading({
|
|
|
174
|
+ title: '加载中...',
|
|
|
175
|
+ mask: true
|
|
|
176
|
+ });
|
|
|
177
|
+
|
|
107
|
178
|
this.loadChapterContent();
|
|
108
|
179
|
},
|
|
109
|
|
- methods: {
|
|
110
|
|
- // 添加onScroll方法
|
|
111
|
|
- onScroll(e) {
|
|
112
|
|
- this.lastScrollPosition = e.detail.scrollTop;
|
|
113
|
|
- // 计算阅读进度
|
|
114
|
|
- this.calculateProgress();
|
|
115
|
|
- },
|
|
116
|
|
-
|
|
117
|
|
- onTouchStart(e) {
|
|
118
|
|
- this.touchStartX = e.touches[0].clientX;
|
|
119
|
|
- this.touchStartY = e.touches[0].clientY;
|
|
120
|
|
- },
|
|
121
|
|
-
|
|
122
|
|
- onTouchEnd(e) {
|
|
123
|
|
- const endX = e.changedTouches[0].clientX;
|
|
124
|
|
- const endY = e.changedTouches[0].clientY;
|
|
125
|
|
- const diffX = endX - this.touchStartX;
|
|
126
|
|
- const diffY = endY - this.touchStartY;
|
|
127
|
|
-
|
|
128
|
|
- // 横向滑动大于纵向滑动,且滑动距离大于50px
|
|
129
|
|
- if (Math.abs(diffX) > Math.abs(diffY) && Math.abs(diffX) > 50) {
|
|
130
|
|
- if (diffX > 0) {
|
|
131
|
|
- this.prevChapter(); // 右滑,上一章
|
|
132
|
|
- } else {
|
|
133
|
|
- this.nextChapter(); // 左滑,下一章
|
|
134
|
|
- }
|
|
135
|
|
- }
|
|
136
|
|
- },
|
|
137
|
|
-
|
|
138
|
|
- // Base64解码
|
|
139
|
|
- decodeBase64(content) {
|
|
140
|
|
- if (typeof content !== 'string') {
|
|
141
|
|
- console.error('Base64解码失败:内容不是字符串');
|
|
142
|
|
- return '内容格式错误';
|
|
143
|
|
- }
|
|
144
|
|
-
|
|
145
|
|
- try {
|
|
146
|
|
- // 处理可能的URL安全Base64编码
|
|
147
|
|
- let base64 = content.replace(/-/g, '+').replace(/_/g, '/');
|
|
148
|
|
-
|
|
149
|
|
- // 添加必要的填充字符
|
|
150
|
|
- const pad = base64.length % 4;
|
|
151
|
|
- if (pad) {
|
|
152
|
|
- if (pad === 1) {
|
|
153
|
|
- throw new Error('InvalidLengthError: Input base64url string is the wrong length to determine padding');
|
|
154
|
|
- }
|
|
155
|
|
- base64 += new Array(5-pad).join('=');
|
|
156
|
|
- }
|
|
157
|
|
-
|
|
158
|
|
- // 解码
|
|
159
|
|
- const decoded = uni.base64ToArrayBuffer(base64);
|
|
160
|
|
- const textDecoder = new TextDecoder('utf-8');
|
|
161
|
|
- return textDecoder.decode(decoded);
|
|
162
|
|
- } catch (error) {
|
|
163
|
|
- console.error('Base64解码失败:', error);
|
|
164
|
|
- return '内容解码失败,请稍后再试。';
|
|
|
180
|
+ onShow() {
|
|
|
181
|
+ // 确保页面显示时也能处理参数
|
|
|
182
|
+ const pages = getCurrentPages();
|
|
|
183
|
+ const currentPage = pages[pages.length - 1];
|
|
|
184
|
+ if (currentPage && currentPage.options) {
|
|
|
185
|
+ const options = currentPage.options;
|
|
|
186
|
+ if (options.novelId && !this.novelId) {
|
|
|
187
|
+ this.novelId = options.novelId;
|
|
|
188
|
+ this.chapterId = options.chapterId || '1';
|
|
|
189
|
+ this.loadChapterContent();
|
|
165
|
190
|
}
|
|
166
|
|
- },
|
|
167
|
|
-
|
|
|
191
|
+ }
|
|
|
192
|
+ },
|
|
|
193
|
+ methods: {
|
|
168
|
194
|
async loadChapterContent() {
|
|
169
|
195
|
this.loading = true;
|
|
170
|
196
|
this.error = null;
|
|
171
|
197
|
|
|
172
|
198
|
try {
|
|
173
|
199
|
console.log('开始请求章节内容,章节ID:', this.chapterId);
|
|
|
200
|
+
|
|
|
201
|
+ // 使用若依框架的API调用方式
|
|
174
|
202
|
const res = await this.$http.get(`/novel/chapter/${this.chapterId}`);
|
|
175
|
203
|
console.log('章节内容响应:', res);
|
|
176
|
204
|
|
|
177
|
205
|
if (res.code === 200) {
|
|
178
|
206
|
this.novelTitle = res.data.novelTitle || '未知小说';
|
|
179
|
207
|
this.chapterTitle = res.data.chapterTitle || '未知章节';
|
|
|
208
|
+
|
|
|
209
|
+ // 后台已经解码Base64,直接使用内容
|
|
180
|
210
|
this.content = res.data.content || '';
|
|
181
|
|
- this.decodedContent = this.decodeBase64(this.content);
|
|
182
|
211
|
|
|
183
|
212
|
// 设置章节信息
|
|
184
|
213
|
this.currentChapter = res.data.chapterIndex || 1;
|
|
185
|
214
|
this.totalChapters = res.data.totalChapters || 0;
|
|
|
215
|
+
|
|
|
216
|
+ uni.showToast({
|
|
|
217
|
+ title: '加载成功',
|
|
|
218
|
+ icon: 'success',
|
|
|
219
|
+ duration: 1500
|
|
|
220
|
+ });
|
|
186
|
221
|
} else {
|
|
187
|
222
|
console.error('API返回错误状态码:', res.code);
|
|
188
|
|
- this.error = '加载失败,请重试';
|
|
|
223
|
+ this.error = '加载失败: ' + (res.msg || '未知错误');
|
|
|
224
|
+ uni.showToast({
|
|
|
225
|
+ title: res.msg || '加载失败',
|
|
|
226
|
+ icon: 'none'
|
|
|
227
|
+ });
|
|
189
|
228
|
}
|
|
190
|
229
|
} catch (error) {
|
|
191
|
230
|
console.error('加载章节内容失败:', error);
|
|
192
|
231
|
this.error = '加载失败,请检查网络连接';
|
|
|
232
|
+ uni.showToast({
|
|
|
233
|
+ title: '加载失败,请检查网络连接',
|
|
|
234
|
+ icon: 'none'
|
|
|
235
|
+ });
|
|
193
|
236
|
} finally {
|
|
194
|
237
|
this.loading = false;
|
|
|
238
|
+ uni.hideLoading();
|
|
195
|
239
|
}
|
|
196
|
240
|
},
|
|
197
|
241
|
|
|
198
|
242
|
calculateProgress() {
|
|
199
|
|
- // 根据滚动位置计算阅读进度
|
|
200
|
|
- // 这里需要获取内容区域的实际高度,由于uni-app中获取节点高度较为复杂,暂时用模拟数据
|
|
201
|
|
- // 实际项目中应该通过uni.createSelectorQuery()获取
|
|
|
243
|
+ // 简化进度计算
|
|
202
|
244
|
this.progress = Math.min(100, Math.round((this.lastScrollPosition / 1000) * 100));
|
|
203
|
245
|
},
|
|
204
|
246
|
|
|
|
@@ -224,8 +266,13 @@ export default {
|
|
224
|
266
|
}
|
|
225
|
267
|
},
|
|
226
|
268
|
|
|
|
269
|
+ selectChapter(chapterId) {
|
|
|
270
|
+ this.chapterId = chapterId;
|
|
|
271
|
+ this.showCatalogPanel = false;
|
|
|
272
|
+ this.loadChapterContent();
|
|
|
273
|
+ },
|
|
|
274
|
+
|
|
227
|
275
|
showMenu() {
|
|
228
|
|
- // 显示/隐藏顶部和底部栏
|
|
229
|
276
|
this.showHeader = !this.showHeader;
|
|
230
|
277
|
this.showFooter = !this.showFooter;
|
|
231
|
278
|
},
|
|
|
@@ -235,16 +282,28 @@ export default {
|
|
235
|
282
|
},
|
|
236
|
283
|
|
|
237
|
284
|
showFontSettings() {
|
|
238
|
|
- this.showSettings = true;
|
|
|
285
|
+ this.showSettings = !this.showSettings;
|
|
239
|
286
|
},
|
|
240
|
287
|
|
|
241
|
288
|
toggleNightMode() {
|
|
242
|
289
|
this.nightMode = !this.nightMode;
|
|
243
|
|
- // 切换夜间模式样式
|
|
|
290
|
+ this.backgroundColor = this.nightMode ? '#1a1a1a' : '#f5f0e1';
|
|
|
291
|
+ },
|
|
|
292
|
+
|
|
|
293
|
+ increaseFontSize() {
|
|
|
294
|
+ this.fontSize = Math.min(24, this.fontSize + 2);
|
|
|
295
|
+ },
|
|
|
296
|
+
|
|
|
297
|
+ decreaseFontSize() {
|
|
|
298
|
+ this.fontSize = Math.max(14, this.fontSize - 2);
|
|
|
299
|
+ },
|
|
|
300
|
+
|
|
|
301
|
+ changeBackground(color) {
|
|
|
302
|
+ this.backgroundColor = color;
|
|
|
303
|
+ this.nightMode = color === '#1a1a1a';
|
|
244
|
304
|
},
|
|
245
|
305
|
|
|
246
|
306
|
setAdStrategy() {
|
|
247
|
|
- // 设置广告显示策略,如每阅读三章显示一次广告
|
|
248
|
307
|
const readChapters = uni.getStorageSync('readChapters') || 0;
|
|
249
|
308
|
this.showAd = readChapters > 0 && readChapters % 3 === 0;
|
|
250
|
309
|
},
|
|
|
@@ -256,13 +315,15 @@ export default {
|
|
256
|
315
|
}
|
|
257
|
316
|
</script>
|
|
258
|
317
|
|
|
|
318
|
+
|
|
259
|
319
|
<style>
|
|
260
|
320
|
.reader-container {
|
|
261
|
321
|
position: relative;
|
|
262
|
322
|
height: 100vh;
|
|
263
|
323
|
display: flex;
|
|
264
|
324
|
flex-direction: column;
|
|
265
|
|
- background-color: #f5f0e1; /* 羊皮纸色背景 */
|
|
|
325
|
+ background-color: v-bind(backgroundColor);
|
|
|
326
|
+ transition: background-color 0.3s ease;
|
|
266
|
327
|
}
|
|
267
|
328
|
|
|
268
|
329
|
.reader-header {
|
|
|
@@ -273,6 +334,11 @@ export default {
|
|
273
|
334
|
align-items: center;
|
|
274
|
335
|
background: rgba(0, 0, 0, 0.8);
|
|
275
|
336
|
color: white;
|
|
|
337
|
+ position: fixed;
|
|
|
338
|
+ top: 0;
|
|
|
339
|
+ left: 0;
|
|
|
340
|
+ right: 0;
|
|
|
341
|
+ z-index: 1000;
|
|
276
|
342
|
}
|
|
277
|
343
|
|
|
278
|
344
|
.reader-content {
|
|
|
@@ -280,8 +346,10 @@ export default {
|
|
280
|
346
|
padding: 20px;
|
|
281
|
347
|
box-sizing: border-box;
|
|
282
|
348
|
line-height: 1.8;
|
|
283
|
|
- font-size: 18px;
|
|
|
349
|
+ font-size: v-bind(fontSize + 'px');
|
|
284
|
350
|
color: #333;
|
|
|
351
|
+ margin-top: 44px;
|
|
|
352
|
+ margin-bottom: 50px;
|
|
285
|
353
|
}
|
|
286
|
354
|
|
|
287
|
355
|
.chapter-title {
|
|
|
@@ -304,6 +372,11 @@ export default {
|
|
304
|
372
|
align-items: center;
|
|
305
|
373
|
background: rgba(0, 0, 0, 0.8);
|
|
306
|
374
|
color: white;
|
|
|
375
|
+ position: fixed;
|
|
|
376
|
+ bottom: 0;
|
|
|
377
|
+ left: 0;
|
|
|
378
|
+ right: 0;
|
|
|
379
|
+ z-index: 1000;
|
|
307
|
380
|
}
|
|
308
|
381
|
|
|
309
|
382
|
.reader-ad {
|
|
|
@@ -321,16 +394,86 @@ export default {
|
|
321
|
394
|
|
|
322
|
395
|
.error-container button {
|
|
323
|
396
|
margin-top: 20px;
|
|
|
397
|
+ background-color: var(--primary-color);
|
|
|
398
|
+ color: white;
|
|
|
399
|
+ border: none;
|
|
|
400
|
+ padding: 10px 20px;
|
|
|
401
|
+ border-radius: 5px;
|
|
|
402
|
+}
|
|
|
403
|
+
|
|
|
404
|
+.settings-panel {
|
|
|
405
|
+ position: fixed;
|
|
|
406
|
+ bottom: 50px;
|
|
|
407
|
+ left: 0;
|
|
|
408
|
+ right: 0;
|
|
|
409
|
+ background: rgba(255, 255, 255, 0.95);
|
|
|
410
|
+ padding: 20px;
|
|
|
411
|
+ z-index: 999;
|
|
|
412
|
+ box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
|
|
|
413
|
+}
|
|
|
414
|
+
|
|
|
415
|
+.font-size-controls {
|
|
|
416
|
+ display: flex;
|
|
|
417
|
+ justify-content: space-between;
|
|
|
418
|
+ align-items: center;
|
|
|
419
|
+ margin-bottom: 20px;
|
|
|
420
|
+}
|
|
|
421
|
+
|
|
|
422
|
+.background-options {
|
|
|
423
|
+ display: flex;
|
|
|
424
|
+ justify-content: space-around;
|
|
|
425
|
+}
|
|
|
426
|
+
|
|
|
427
|
+.bg-option {
|
|
|
428
|
+ width: 40px;
|
|
|
429
|
+ height: 40px;
|
|
|
430
|
+ border-radius: 50%;
|
|
|
431
|
+ border: 2px solid transparent;
|
|
|
432
|
+ cursor: pointer;
|
|
|
433
|
+}
|
|
|
434
|
+
|
|
|
435
|
+.bg-option.active {
|
|
|
436
|
+ border-color: var(--primary-color);
|
|
|
437
|
+}
|
|
|
438
|
+
|
|
|
439
|
+.catalog-panel {
|
|
|
440
|
+ position: fixed;
|
|
|
441
|
+ top: 0;
|
|
|
442
|
+ left: 0;
|
|
|
443
|
+ right: 0;
|
|
|
444
|
+ bottom: 0;
|
|
|
445
|
+ background: white;
|
|
|
446
|
+ z-index: 1001;
|
|
|
447
|
+}
|
|
|
448
|
+
|
|
|
449
|
+.catalog-header {
|
|
|
450
|
+ display: flex;
|
|
|
451
|
+ justify-content: space-between;
|
|
|
452
|
+ align-items: center;
|
|
|
453
|
+ padding: 15px;
|
|
|
454
|
+ border-bottom: 1px solid #eee;
|
|
|
455
|
+}
|
|
|
456
|
+
|
|
|
457
|
+.chapter-list {
|
|
|
458
|
+ height: calc(100vh - 60px);
|
|
|
459
|
+}
|
|
|
460
|
+
|
|
|
461
|
+.chapter-item {
|
|
|
462
|
+ padding: 15px;
|
|
|
463
|
+ border-bottom: 1px solid #eee;
|
|
|
464
|
+}
|
|
|
465
|
+
|
|
|
466
|
+.chapter-item.active {
|
|
|
467
|
+ color: var(--primary-color);
|
|
|
468
|
+ font-weight: bold;
|
|
324
|
469
|
}
|
|
325
|
470
|
|
|
326
|
471
|
/* 夜间模式样式 */
|
|
327
|
|
-.night-mode {
|
|
328
|
|
- background-color: #1a1a1a;
|
|
|
472
|
+.night-mode .reader-content {
|
|
329
|
473
|
color: #888;
|
|
330
|
474
|
}
|
|
331
|
475
|
|
|
332
|
|
-.night-mode .reader-content {
|
|
333
|
|
- background-color: #1a1a1a;
|
|
|
476
|
+.night-mode .content-text {
|
|
334
|
477
|
color: #888;
|
|
335
|
478
|
}
|
|
336
|
479
|
</style>
|