├── php-api/ # 改造后的PHP接口层 ├── java-ad-service/ # 若依框架微服务(广告+VIP+分账) ├── uniapp-reader/ # UniApp前端项目 │ ├── pages/ # 各端页面 │ └──
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

reader.vue 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. <template>
  2. <view class="reader-container" :class="{ 'night-mode': nightMode }">
  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>{{content}}</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. <view class="font-size-controls">
  53. <text class="iconfont icon-font-small" @click="decreaseFontSize"></text>
  54. <text>字体大小: {{fontSize}}px</text>
  55. <text class="iconfont icon-font-large" @click="increaseFontSize"></text>
  56. </view>
  57. <view class="background-options">
  58. <view
  59. v-for="bg in backgroundOptions"
  60. :key="bg.value"
  61. class="bg-option"
  62. :class="{ active: backgroundColor === bg.value }"
  63. :style="{ backgroundColor: bg.value }"
  64. @click="changeBackground(bg.value)"
  65. ></view>
  66. </view>
  67. </view>
  68. <!-- 目录面板 -->
  69. <view class="catalog-panel" v-if="showCatalogPanel">
  70. <view class="catalog-header">
  71. <text class="catalog-title">目录</text>
  72. <text class="iconfont icon-close" @click="showCatalogPanel = false"></text>
  73. </view>
  74. <scroll-view class="chapter-list" scroll-y>
  75. <view
  76. v-for="chapter in chapterList"
  77. :key="chapter.id"
  78. class="chapter-item"
  79. :class="{ active: chapter.id === chapterId }"
  80. @click="selectChapter(chapter.id)"
  81. >
  82. <text>{{chapter.title}}</text>
  83. </view>
  84. </scroll-view>
  85. </view>
  86. </view>
  87. </template>
  88. <script>
  89. export default {
  90. data() {
  91. return {
  92. novelId: '',
  93. chapterId: '',
  94. novelTitle: '加载中...',
  95. chapterTitle: '加载中...',
  96. content: '正在加载内容...', // 直接存储解码后的内容
  97. currentChapter: 1,
  98. totalChapters: 0,
  99. progress: 0,
  100. scrollTop: 0,
  101. showHeader: false,
  102. showFooter: false,
  103. showSettings: false,
  104. showCatalogPanel: false,
  105. showAd: false,
  106. nightMode: false,
  107. touchStartX: 0,
  108. touchStartY: 0,
  109. lastScrollPosition: 0,
  110. loading: false,
  111. error: null,
  112. fontSize: 18,
  113. backgroundColor: '#f5f0e1',
  114. backgroundOptions: [
  115. { value: '#f5f0e1', name: '羊皮纸' },
  116. { value: '#e8f5e9', name: '护眼绿' },
  117. { value: '#e3f2fd', name: '天空蓝' },
  118. { value: '#fce4ec', name: '粉红' },
  119. { value: '#1a1a1a', name: '夜间' }
  120. ],
  121. chapterList: []
  122. }
  123. },
  124. // 添加所有生命周期钩子进行调试
  125. beforeCreate() {
  126. console.log('reader beforeCreate');
  127. },
  128. created() {
  129. console.log('reader created');
  130. },
  131. beforeMount() {
  132. console.log('reader beforeMount');
  133. },
  134. mounted() {
  135. console.log('reader mounted');
  136. },
  137. onReady() {
  138. console.log('reader onReady');
  139. },
  140. onHide() {
  141. console.log('reader onHide');
  142. },
  143. onUnload() {
  144. console.log('reader onUnload');
  145. },
  146. onLoad(options) {
  147. console.log('阅读器页面加载,参数:', options);
  148. // 确保参数获取方式正确
  149. this.novelId = options.novelId || '';
  150. this.chapterId = options.chapterId || '1';
  151. if (!this.novelId) {
  152. console.error('小说ID参数缺失');
  153. this.error = '小说ID参数缺失';
  154. uni.showToast({
  155. title: '小说ID参数缺失',
  156. icon: 'none'
  157. });
  158. return;
  159. }
  160. // 显示加载提示
  161. uni.showLoading({
  162. title: '加载中...',
  163. mask: true
  164. });
  165. this.loadChapterContent();
  166. },
  167. onShow() {
  168. // 确保页面显示时也能处理参数
  169. const pages = getCurrentPages();
  170. const currentPage = pages[pages.length - 1];
  171. if (currentPage && currentPage.options) {
  172. const options = currentPage.options;
  173. if (options.novelId && !this.novelId) {
  174. this.novelId = options.novelId;
  175. this.chapterId = options.chapterId || '1';
  176. this.loadChapterContent();
  177. }
  178. }
  179. },
  180. methods: {
  181. async loadChapterContent() {
  182. this.loading = true;
  183. this.error = null;
  184. try {
  185. console.log('开始请求章节内容,章节ID:', this.chapterId);
  186. // 使用若依框架的API调用方式
  187. const res = await this.$http.get(`/novel/chapter/${this.chapterId}`);
  188. console.log('章节内容响应:', res);
  189. if (res.code === 200) {
  190. this.novelTitle = res.data.novelTitle || '未知小说';
  191. this.chapterTitle = res.data.chapterTitle || '未知章节';
  192. // 后台已经解码Base64,直接使用内容
  193. this.content = res.data.content || '';
  194. // 设置章节信息
  195. this.currentChapter = res.data.chapterIndex || 1;
  196. this.totalChapters = res.data.totalChapters || 0;
  197. uni.showToast({
  198. title: '加载成功',
  199. icon: 'success',
  200. duration: 1500
  201. });
  202. } else {
  203. console.error('API返回错误状态码:', res.code);
  204. this.error = '加载失败: ' + (res.msg || '未知错误');
  205. uni.showToast({
  206. title: res.msg || '加载失败',
  207. icon: 'none'
  208. });
  209. }
  210. } catch (error) {
  211. console.error('加载章节内容失败:', error);
  212. this.error = '加载失败,请检查网络连接';
  213. uni.showToast({
  214. title: '加载失败,请检查网络连接',
  215. icon: 'none'
  216. });
  217. } finally {
  218. this.loading = false;
  219. uni.hideLoading();
  220. }
  221. },
  222. calculateProgress() {
  223. // 简化进度计算
  224. this.progress = Math.min(100, Math.round((this.lastScrollPosition / 1000) * 100));
  225. },
  226. goBack() {
  227. uni.navigateBack();
  228. },
  229. prevChapter() {
  230. if (this.currentChapter > 1) {
  231. this.chapterId = parseInt(this.chapterId) - 1;
  232. this.loadChapterContent();
  233. } else {
  234. uni.showToast({ title: '已经是第一章了', icon: 'none' });
  235. }
  236. },
  237. nextChapter() {
  238. if (this.currentChapter < this.totalChapters) {
  239. this.chapterId = parseInt(this.chapterId) + 1;
  240. this.loadChapterContent();
  241. } else {
  242. uni.showToast({ title: '已经是最后一章了', icon: 'none' });
  243. }
  244. },
  245. selectChapter(chapterId) {
  246. this.chapterId = chapterId;
  247. this.showCatalogPanel = false;
  248. this.loadChapterContent();
  249. },
  250. showMenu() {
  251. this.showHeader = !this.showHeader;
  252. this.showFooter = !this.showFooter;
  253. },
  254. showCatalog() {
  255. this.showCatalogPanel = true;
  256. },
  257. showFontSettings() {
  258. this.showSettings = !this.showSettings;
  259. },
  260. toggleNightMode() {
  261. this.nightMode = !this.nightMode;
  262. this.backgroundColor = this.nightMode ? '#1a1a1a' : '#f5f0e1';
  263. },
  264. increaseFontSize() {
  265. this.fontSize = Math.min(24, this.fontSize + 2);
  266. },
  267. decreaseFontSize() {
  268. this.fontSize = Math.max(14, this.fontSize - 2);
  269. },
  270. changeBackground(color) {
  271. this.backgroundColor = color;
  272. this.nightMode = color === '#1a1a1a';
  273. },
  274. setAdStrategy() {
  275. const readChapters = uni.getStorageSync('readChapters') || 0;
  276. this.showAd = readChapters > 0 && readChapters % 3 === 0;
  277. },
  278. downloadChapter() {
  279. // 下载本章内容供离线阅读
  280. }
  281. }
  282. }
  283. </script>
  284. <style>
  285. .reader-container {
  286. position: relative;
  287. height: 100vh;
  288. display: flex;
  289. flex-direction: column;
  290. background-color: v-bind(backgroundColor);
  291. transition: background-color 0.3s ease;
  292. }
  293. .reader-header {
  294. height: 44px;
  295. padding: 0 15px;
  296. display: flex;
  297. justify-content: space-between;
  298. align-items: center;
  299. background: rgba(0, 0, 0, 0.8);
  300. color: white;
  301. position: fixed;
  302. top: 0;
  303. left: 0;
  304. right: 0;
  305. z-index: 1000;
  306. }
  307. .reader-content {
  308. flex: 1;
  309. padding: 20px;
  310. box-sizing: border-box;
  311. line-height: 1.8;
  312. font-size: v-bind(fontSize + 'px');
  313. color: #333;
  314. margin-top: 44px;
  315. margin-bottom: 50px;
  316. }
  317. .chapter-title {
  318. text-align: center;
  319. font-size: 20px;
  320. font-weight: bold;
  321. margin-bottom: 20px;
  322. }
  323. .content-text {
  324. text-indent: 2em;
  325. line-height: 1.8;
  326. }
  327. .reader-footer {
  328. height: 50px;
  329. padding: 0 15px;
  330. display: flex;
  331. justify-content: space-between;
  332. align-items: center;
  333. background: rgba(0, 0, 0, 0.8);
  334. color: white;
  335. position: fixed;
  336. bottom: 0;
  337. left: 0;
  338. right: 0;
  339. z-index: 1000;
  340. }
  341. .reader-ad {
  342. height: 90px;
  343. margin-bottom: 10px;
  344. }
  345. .loading-container, .error-container {
  346. display: flex;
  347. flex-direction: column;
  348. align-items: center;
  349. justify-content: center;
  350. padding: 50px 0;
  351. }
  352. .error-container button {
  353. margin-top: 20px;
  354. background-color: var(--primary-color);
  355. color: white;
  356. border: none;
  357. padding: 10px 20px;
  358. border-radius: 5px;
  359. }
  360. .settings-panel {
  361. position: fixed;
  362. bottom: 50px;
  363. left: 0;
  364. right: 0;
  365. background: rgba(255, 255, 255, 0.95);
  366. padding: 20px;
  367. z-index: 999;
  368. box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
  369. }
  370. .font-size-controls {
  371. display: flex;
  372. justify-content: space-between;
  373. align-items: center;
  374. margin-bottom: 20px;
  375. }
  376. .background-options {
  377. display: flex;
  378. justify-content: space-around;
  379. }
  380. .bg-option {
  381. width: 40px;
  382. height: 40px;
  383. border-radius: 50%;
  384. border: 2px solid transparent;
  385. cursor: pointer;
  386. }
  387. .bg-option.active {
  388. border-color: var(--primary-color);
  389. }
  390. .catalog-panel {
  391. position: fixed;
  392. top: 0;
  393. left: 0;
  394. right: 0;
  395. bottom: 0;
  396. background: white;
  397. z-index: 1001;
  398. }
  399. .catalog-header {
  400. display: flex;
  401. justify-content: space-between;
  402. align-items: center;
  403. padding: 15px;
  404. border-bottom: 1px solid #eee;
  405. }
  406. .chapter-list {
  407. height: calc(100vh - 60px);
  408. }
  409. .chapter-item {
  410. padding: 15px;
  411. border-bottom: 1px solid #eee;
  412. }
  413. .chapter-item.active {
  414. color: var(--primary-color);
  415. font-weight: bold;
  416. }
  417. /* 夜间模式样式 */
  418. .night-mode .reader-content {
  419. color: #888;
  420. }
  421. .night-mode .content-text {
  422. color: #888;
  423. }
  424. </style>