├── php-api/ # 改造后的PHP接口层 ├── java-ad-service/ # 若依框架微服务(广告+VIP+分账) ├── uniapp-reader/ # UniApp前端项目 │ ├── pages/ # 各端页面 │ └──
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

reader.vue 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <template>
  2. <view class="reader-container">
  3. <!-- 顶部导航栏 -->
  4. <view class="reader-header" v-if="showHeader">
  5. <view class="header-left">
  6. <text class="iconfont icon-back" @click="goBack"></text>
  7. <text class="novel-title">{{novelTitle}}</text>
  8. </view>
  9. <view class="header-right">
  10. <text class="iconfont icon-menu" @click="showMenu"></text>
  11. </view>
  12. </view>
  13. <!-- 阅读内容区域 -->
  14. <scroll-view
  15. class="reader-content"
  16. scroll-y
  17. :scroll-top="scrollTop"
  18. @scroll="onScroll"
  19. @touchstart="onTouchStart"
  20. @touchend="onTouchEnd"
  21. >
  22. <view class="chapter-title">{{chapterTitle}}</view>
  23. <view class="content-text">
  24. <text>{{decodedContent}}</text>
  25. </view>
  26. <!-- 加载状态 -->
  27. <view v-if="loading" class="loading-container">
  28. <text>加载中...</text>
  29. </view>
  30. <!-- 错误状态 -->
  31. <view v-if="error" class="error-container">
  32. <text>{{error}}</text>
  33. <button @click="loadChapterContent">重试</button>
  34. </view>
  35. </scroll-view>
  36. <!-- 底部控制栏 -->
  37. <view class="reader-footer" v-if="showFooter">
  38. <view class="progress-text">
  39. 第{{currentChapter}}章 {{progress}}%
  40. </view>
  41. <view class="control-buttons">
  42. <text class="iconfont icon-catalog" @click="showCatalog"></text>
  43. <text class="iconfont icon-font" @click="showFontSettings"></text>
  44. <text class="iconfont icon-night" @click="toggleNightMode"></text>
  45. <text class="iconfont icon-download" @click="downloadChapter"></text>
  46. </view>
  47. </view>
  48. <!-- 广告位 -->
  49. <ad v-if="showAd" unit-id="您的广告单元ID" class="reader-ad"></ad>
  50. <!-- 设置面板 -->
  51. <view class="settings-panel" v-if="showSettings">
  52. <!-- 字体大小、背景颜色等设置 -->
  53. </view>
  54. <!-- 目录面板 -->
  55. <view class="catalog-panel" v-if="showCatalogPanel">
  56. <!-- 章节列表 -->
  57. </view>
  58. </view>
  59. </template>
  60. <script>
  61. export default {
  62. data() {
  63. return {
  64. novelId: '',
  65. chapterId: '',
  66. novelTitle: '加载中...',
  67. chapterTitle: '加载中...',
  68. content: '', // Base64编码的内容
  69. decodedContent: '正在加载内容...', // 解码后的内容
  70. currentChapter: 1,
  71. totalChapters: 0,
  72. progress: 0,
  73. scrollTop: 0,
  74. showHeader: false,
  75. showFooter: false,
  76. showSettings: false,
  77. showCatalogPanel: false,
  78. showAd: false, // 默认不显示广告
  79. nightMode: false,
  80. touchStartX: 0,
  81. touchStartY: 0,
  82. lastScrollPosition: 0,
  83. loading: false,
  84. error: null
  85. }
  86. },
  87. onLoad(options) {
  88. console.log('阅读器页面加载,参数:', options);
  89. // 确保参数正确获取
  90. this.novelId = options.novelId || '';
  91. this.chapterId = options.chapterId || '1';
  92. if (!this.novelId) {
  93. this.error = '小说ID参数缺失';
  94. return;
  95. }
  96. this.loadChapterContent();
  97. },
  98. methods: {
  99. // 添加onScroll方法
  100. onScroll(e) {
  101. this.lastScrollPosition = e.detail.scrollTop;
  102. // 计算阅读进度
  103. this.calculateProgress();
  104. },
  105. onTouchStart(e) {
  106. this.touchStartX = e.touches[0].clientX;
  107. this.touchStartY = e.touches[0].clientY;
  108. },
  109. onTouchEnd(e) {
  110. const endX = e.changedTouches[0].clientX;
  111. const endY = e.changedTouches[0].clientY;
  112. const diffX = endX - this.touchStartX;
  113. const diffY = endY - this.touchStartY;
  114. // 横向滑动大于纵向滑动,且滑动距离大于50px
  115. if (Math.abs(diffX) > Math.abs(diffY) && Math.abs(diffX) > 50) {
  116. if (diffX > 0) {
  117. this.prevChapter(); // 右滑,上一章
  118. } else {
  119. this.nextChapter(); // 左滑,下一章
  120. }
  121. }
  122. },
  123. // Base64解码
  124. decodeBase64(content) {
  125. if (typeof content !== 'string') {
  126. console.error('Base64解码失败:内容不是字符串');
  127. return '内容格式错误';
  128. }
  129. try {
  130. // 处理可能的URL安全Base64编码
  131. let base64 = content.replace(/-/g, '+').replace(/_/g, '/');
  132. // 添加必要的填充字符
  133. const pad = base64.length % 4;
  134. if (pad) {
  135. if (pad === 1) {
  136. throw new Error('InvalidLengthError: Input base64url string is the wrong length to determine padding');
  137. }
  138. base64 += new Array(5-pad).join('=');
  139. }
  140. // 解码
  141. const decoded = uni.base64ToArrayBuffer(base64);
  142. const textDecoder = new TextDecoder('utf-8');
  143. return textDecoder.decode(decoded);
  144. } catch (error) {
  145. console.error('Base64解码失败:', error);
  146. return '内容解码失败,请稍后再试。';
  147. }
  148. },
  149. async loadChapterContent() {
  150. this.loading = true;
  151. this.error = null;
  152. try {
  153. console.log('开始请求章节内容,章节ID:', this.chapterId);
  154. const res = await this.$http.get(`/novel/chapter/${this.chapterId}`);
  155. console.log('章节内容响应:', res);
  156. if (res.code === 200) {
  157. this.novelTitle = res.data.novelTitle || '未知小说';
  158. this.chapterTitle = res.data.chapterTitle || '未知章节';
  159. this.content = res.data.content || '';
  160. this.decodedContent = this.decodeBase64(this.content);
  161. // 设置章节信息
  162. this.currentChapter = res.data.chapterIndex || 1;
  163. this.totalChapters = res.data.totalChapters || 0;
  164. } else {
  165. console.error('API返回错误状态码:', res.code);
  166. this.error = '加载失败,请重试';
  167. }
  168. } catch (error) {
  169. console.error('加载章节内容失败:', error);
  170. this.error = '加载失败,请检查网络连接';
  171. } finally {
  172. this.loading = false;
  173. }
  174. },
  175. calculateProgress() {
  176. // 根据滚动位置计算阅读进度
  177. // 这里需要获取内容区域的实际高度,由于uni-app中获取节点高度较为复杂,暂时用模拟数据
  178. // 实际项目中应该通过uni.createSelectorQuery()获取
  179. this.progress = Math.min(100, Math.round((this.lastScrollPosition / 1000) * 100));
  180. },
  181. goBack() {
  182. uni.navigateBack();
  183. },
  184. prevChapter() {
  185. if (this.currentChapter > 1) {
  186. this.chapterId = parseInt(this.chapterId) - 1;
  187. this.loadChapterContent();
  188. } else {
  189. uni.showToast({ title: '已经是第一章了', icon: 'none' });
  190. }
  191. },
  192. nextChapter() {
  193. if (this.currentChapter < this.totalChapters) {
  194. this.chapterId = parseInt(this.chapterId) + 1;
  195. this.loadChapterContent();
  196. } else {
  197. uni.showToast({ title: '已经是最后一章了', icon: 'none' });
  198. }
  199. },
  200. showMenu() {
  201. // 显示/隐藏顶部和底部栏
  202. this.showHeader = !this.showHeader;
  203. this.showFooter = !this.showFooter;
  204. },
  205. showCatalog() {
  206. this.showCatalogPanel = true;
  207. },
  208. showFontSettings() {
  209. this.showSettings = true;
  210. },
  211. toggleNightMode() {
  212. this.nightMode = !this.nightMode;
  213. // 切换夜间模式样式
  214. },
  215. setAdStrategy() {
  216. // 设置广告显示策略,如每阅读三章显示一次广告
  217. const readChapters = uni.getStorageSync('readChapters') || 0;
  218. this.showAd = readChapters > 0 && readChapters % 3 === 0;
  219. },
  220. downloadChapter() {
  221. // 下载本章内容供离线阅读
  222. }
  223. }
  224. }
  225. </script>
  226. <style>
  227. .reader-container {
  228. position: relative;
  229. height: 100vh;
  230. display: flex;
  231. flex-direction: column;
  232. background-color: #f5f0e1; /* 羊皮纸色背景 */
  233. }
  234. .reader-header {
  235. height: 44px;
  236. padding: 0 15px;
  237. display: flex;
  238. justify-content: space-between;
  239. align-items: center;
  240. background: rgba(0, 0, 0, 0.8);
  241. color: white;
  242. }
  243. .reader-content {
  244. flex: 1;
  245. padding: 20px;
  246. box-sizing: border-box;
  247. line-height: 1.8;
  248. font-size: 18px;
  249. color: #333;
  250. }
  251. .chapter-title {
  252. text-align: center;
  253. font-size: 20px;
  254. font-weight: bold;
  255. margin-bottom: 20px;
  256. }
  257. .content-text {
  258. text-indent: 2em;
  259. line-height: 1.8;
  260. }
  261. .reader-footer {
  262. height: 50px;
  263. padding: 0 15px;
  264. display: flex;
  265. justify-content: space-between;
  266. align-items: center;
  267. background: rgba(0, 0, 0, 0.8);
  268. color: white;
  269. }
  270. .reader-ad {
  271. height: 90px;
  272. margin-bottom: 10px;
  273. }
  274. .loading-container, .error-container {
  275. display: flex;
  276. flex-direction: column;
  277. align-items: center;
  278. justify-content: center;
  279. padding: 50px 0;
  280. }
  281. .error-container button {
  282. margin-top: 20px;
  283. }
  284. /* 夜间模式样式 */
  285. .night-mode {
  286. background-color: #1a1a1a;
  287. color: #888;
  288. }
  289. .night-mode .reader-content {
  290. background-color: #1a1a1a;
  291. color: #888;
  292. }
  293. </style>