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

reader.vue 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <template>
  2. <view class="reader-container" :style="readerStyles">
  3. <!-- 顶部导航 -->
  4. <view class="reader-header">
  5. <uni-icons type="arrowleft" size="28" color="#fff" @click="goBack"></uni-icons>
  6. <text class="novel-title">{{ novelTitle }}</text>
  7. <view class="header-actions">
  8. <uni-icons type="more" size="28" color="#fff"></uni-icons>
  9. </view>
  10. </view>
  11. <!-- 阅读区域 -->
  12. <scroll-view scroll-y class="reader-content" :scroll-top="scrollTop" @scroll="onScroll">
  13. <view class="chapter-title">{{ chapterDetail.title }}</view>
  14. <rich-text :nodes="chapterContent" class="content-text"></rich-text>
  15. </scroll-view>
  16. <!-- 底部操作栏 -->
  17. <view class="reader-footer">
  18. <view class="progress">
  19. <text>{{ chapterDetail.chapterNumber }}/{{ totalChapters }}</text>
  20. <text>{{ progress }}%</text>
  21. </view>
  22. <view class="actions">
  23. <button class="action-btn" @click="prevChapter">
  24. <uni-icons type="arrow-up" size="24"></uni-icons>
  25. <text>上一章</text>
  26. </button>
  27. <button class="action-btn" @click="toggleMenu">
  28. <uni-icons type="list" size="24"></uni-icons>
  29. <text>目录</text>
  30. </button>
  31. <button class="action-btn" @click="toggleSettings">
  32. <uni-icons type="gear" size="24"></uni-icons>
  33. <text>设置</text>
  34. </button>
  35. <button class="action-btn" @click="nextChapter">
  36. <uni-icons type="arrow-down" size="24"></uni-icons>
  37. <text>下一章</text>
  38. </button>
  39. </view>
  40. </view>
  41. <!-- 目录抽屉 -->
  42. <uni-drawer ref="drawer" mode="right" :width="300">
  43. <view class="drawer-content">
  44. <text class="drawer-title">目录</text>
  45. <scroll-view scroll-y class="chapter-list">
  46. <view
  47. v-for="chapter in chapters"
  48. :key="chapter.id"
  49. class="chapter-item"
  50. :class="{ active: chapter.id === chapterDetail.id }"
  51. @click="selectChapter(chapter)"
  52. >
  53. <text>{{ chapter.chapterNumber }}. {{ chapter.title }}</text>
  54. </view>
  55. </scroll-view>
  56. </view>
  57. </uni-drawer>
  58. </view>
  59. </template>
  60. <script>
  61. import novelService from '@/services/novelService'
  62. export default {
  63. data() {
  64. return {
  65. novelId: null,
  66. chapterId: null,
  67. novelTitle: '',
  68. chapterDetail: {},
  69. chapterContent: '',
  70. chapters: [],
  71. // 确保所有字段初始化
  72. totalChapters: 0,
  73. progress: 0,
  74. scrollTop: 0,
  75. readerStyles: {
  76. fontSize: '32rpx',
  77. lineHeight: '1.8',
  78. backgroundColor: '#f8f2e0',
  79. color: '#333'
  80. }
  81. }
  82. },
  83. async onLoad(options) {
  84. this.novelId = options.novelId;
  85. this.chapterId = options.chapterId || 1;
  86. await this.loadNovelData();
  87. await this.loadChapter();
  88. },
  89. methods: {
  90. async loadNovelData() {
  91. const res = await this.$http.get(`/novel/detail/${this.novelId}`);
  92. if (res.data) {
  93. this.novelTitle = res.data.title;
  94. }
  95. // 获取章节列表
  96. const chapterRes = await this.$http.get(`/chapter/list/${this.novelId}`);
  97. if (chapterRes.rows && Array.isArray(chapterRes.rows)) {
  98. this.chapters = chapterRes.rows;
  99. this.totalChapters = this.chapters.length;
  100. }
  101. },
  102. async loadChapter() {
  103. uni.showLoading({ title: '加载中...' });
  104. try {
  105. const res = await this.$http.get(`/chapter/content/${this.chapterId}`);
  106. if (res.data) {
  107. this.chapterDetail = res.data;
  108. this.chapterContent = this.formatContent(res.data.content);
  109. this.progress = Math.round((this.chapterDetail.chapterOrder / this.totalChapters) * 100);
  110. }
  111. } finally {
  112. uni.hideLoading();
  113. }
  114. },
  115. formatContent(content) {
  116. // 处理特殊格式
  117. return content
  118. .replace(/<br>/g, '\n')
  119. .replace(/&nbsp;/g, ' ')
  120. .replace(/<p>/g, '\n\n')
  121. .replace(/<\/p>/g, '')
  122. .replace(/<[^>]+>/g, '');
  123. }
  124. saveReadingProgress() {
  125. // 保存到本地
  126. uni.setStorageSync('readingProgress', {
  127. novelId: this.novelId,
  128. chapterId: this.chapterId
  129. })
  130. // 如果已登录,同步到服务器
  131. if (uni.getStorageSync('token')) {
  132. this.$http.post('/reading/progress', {
  133. novelId: this.novelId,
  134. chapterId: this.chapterId
  135. })
  136. }
  137. },
  138. prevChapter() {
  139. if (this.chapterId > 1) {
  140. this.chapterId--
  141. this.loadChapter()
  142. } else {
  143. uni.showToast({
  144. title: '已经是第一章',
  145. icon: 'none'
  146. })
  147. }
  148. },
  149. nextChapter() {
  150. if (this.chapterId < this.totalChapters) {
  151. this.chapterId++
  152. this.loadChapter()
  153. } else {
  154. uni.showToast({
  155. title: '已是最新章节',
  156. icon: 'none'
  157. })
  158. }
  159. },
  160. selectChapter(chapter) {
  161. this.chapterId = chapter.id
  162. this.$refs.drawer.close()
  163. this.loadChapter()
  164. },
  165. toggleMenu() {
  166. this.$refs.drawer.open()
  167. },
  168. toggleSettings() {
  169. uni.navigateTo({
  170. url: '/pages/reader/settings'
  171. })
  172. },
  173. goBack() {
  174. uni.navigateBack()
  175. },
  176. onScroll(e) {
  177. // 记录滚动位置
  178. this.scrollTop = e.detail.scrollTop
  179. }
  180. }
  181. }
  182. </script>
  183. <style scoped>
  184. .reader-container {
  185. position: relative;
  186. height: 100vh;
  187. padding: 20rpx;
  188. box-sizing: border-box;
  189. }
  190. .reader-header {
  191. position: absolute;
  192. top: 0;
  193. left: 0;
  194. right: 0;
  195. display: flex;
  196. justify-content: space-between;
  197. align-items: center;
  198. padding: 20rpx 30rpx;
  199. background: rgba(0, 0, 0, 0.7);
  200. color: white;
  201. z-index: 100;
  202. }
  203. .novel-title {
  204. font-size: 32rpx;
  205. max-width: 60%;
  206. overflow: hidden;
  207. text-overflow: ellipsis;
  208. white-space: nowrap;
  209. }
  210. .reader-content {
  211. height: calc(100vh - 200rpx);
  212. padding-top: 80rpx;
  213. padding-bottom: 120rpx;
  214. }
  215. .chapter-title {
  216. font-size: 40rpx;
  217. font-weight: bold;
  218. text-align: center;
  219. margin-bottom: 40rpx;
  220. color: #2a5caa;
  221. }
  222. .content-text {
  223. font-size: 32rpx;
  224. line-height: 1.8;
  225. }
  226. .reader-footer {
  227. position: absolute;
  228. bottom: 0;
  229. left: 0;
  230. right: 0;
  231. background: rgba(255, 255, 255, 0.9);
  232. padding: 20rpx;
  233. border-top: 1rpx solid #eee;
  234. }
  235. .progress {
  236. display: flex;
  237. justify-content: space-between;
  238. font-size: 28rpx;
  239. color: #666;
  240. margin-bottom: 20rpx;
  241. }
  242. .actions {
  243. display: flex;
  244. justify-content: space-between;
  245. }
  246. .action-btn {
  247. display: flex;
  248. flex-direction: column;
  249. align-items: center;
  250. background: none;
  251. border: none;
  252. font-size: 24rpx;
  253. padding: 10rpx;
  254. }
  255. .drawer-content {
  256. padding: 30rpx;
  257. }
  258. .drawer-title {
  259. font-size: 36rpx;
  260. font-weight: bold;
  261. display: block;
  262. margin-bottom: 30rpx;
  263. border-bottom: 1rpx solid #eee;
  264. padding-bottom: 20rpx;
  265. }
  266. .chapter-list {
  267. height: calc(100vh - 150rpx);
  268. }
  269. .chapter-item {
  270. padding: 20rpx 10rpx;
  271. border-bottom: 1rpx solid #f0f0f0;
  272. font-size: 28rpx;
  273. }
  274. .chapter-item.active {
  275. color: #2a5caa;
  276. font-weight: bold;
  277. }
  278. </style>