├── php-api/ # 改造后的PHP接口层 ├── java-ad-service/ # 若依框架微服务(广告+VIP+分账) ├── uniapp-reader/ # UniApp前端项目 │ ├── pages/ # 各端页面 │ └──
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

NovelReader.vue 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <template>
  2. <view class="reader-container">
  3. <!-- 顶部操作栏 -->
  4. <view class="reader-header">
  5. <view class="left-actions">
  6. <button class="back-btn" @click="goBack">
  7. <uni-icons type="arrowleft" size="24" color="#333"></uni-icons>
  8. </button>
  9. <text class="chapter-title">{{ chapterTitle }}</text>
  10. </view>
  11. <view class="right-actions">
  12. <button class="action-btn" @click="toggleTheme">
  13. <uni-icons type="color" size="20"></uni-icons>
  14. </button>
  15. <button class="action-btn" @click="toggleFontSize">
  16. <uni-icons type="font" size="20"></uni-icons>
  17. </button>
  18. <button class="action-btn" @click="toggleMode">
  19. <uni-icons :type="readingMode === 'scroll' ? 'list' : 'grid'" size="20"></uni-icons>
  20. </button>
  21. </view>
  22. </view>
  23. <!-- 阅读模式选择器 -->
  24. <view v-if="showModeSelector" class="mode-selector">
  25. <button
  26. v-for="mode in readingModes"
  27. :key="mode.value"
  28. :class="['mode-btn', { active: readingMode === mode.value }]"
  29. @click="changeMode(mode.value)"
  30. >
  31. {{ mode.label }}
  32. </button>
  33. </view>
  34. <!-- 字体大小选择器 -->
  35. <view v-if="showFontSizeSelector" class="font-size-selector">
  36. <button class="size-btn" @click="decreaseFontSize">A-</button>
  37. <slider
  38. :value="fontSize"
  39. min="14"
  40. max="24"
  41. step="2"
  42. @change="changeFontSize"
  43. class="font-slider"
  44. />
  45. <button class="size-btn" @click="increaseFontSize">A+</button>
  46. </view>
  47. <!-- 滚动阅读模式 -->
  48. <scroll-view
  49. v-if="readingMode === 'scroll'"
  50. scroll-y
  51. class="scroll-reader"
  52. :style="{ fontSize: fontSize + 'px' }"
  53. :scroll-top="scrollTop"
  54. @scroll="onScroll"
  55. @scrolltolower="loadNextChapter"
  56. >
  57. <rich-text :nodes="formatContent(chapterContent)" />
  58. </scroll-view>
  59. <!-- 翻页阅读模式 -->
  60. <swiper
  61. v-else
  62. :current="currentPage"
  63. circular
  64. class="page-reader"
  65. @change="onPageChange"
  66. >
  67. <swiper-item v-for="(page, index) in paginatedContent" :key="index">
  68. <view :style="{ fontSize: fontSize + 'px', padding: '20px' }">
  69. <rich-text :nodes="page" />
  70. </view>
  71. </swiper-item>
  72. </swiper>
  73. <!-- 底部操作栏 -->
  74. <view class="reader-footer">
  75. <button class="footer-btn" @click="prevChapter">上一章</button>
  76. <button class="footer-btn" @click="showChapterList">目录</button>
  77. <button class="footer-btn" @click="nextChapter">下一章</button>
  78. </view>
  79. <!-- 底部广告 -->
  80. <view v-if="showBottomAd && !isVIP" class="bottom-ad">
  81. <ad :unit-id="bottomAdUnitId" ad-type="feed" />
  82. </view>
  83. </view>
  84. </template>
  85. <script setup>
  86. import { ref, computed, onMounted, watch } from 'vue'
  87. import { useAdManager } from '@/utils/adManager'
  88. import { useUserStore } from '@/stores/user'
  89. import { useThemeStore } from '@/stores/theme'
  90. import { useReadingProgress } from '@/composables/useReadingProgress'
  91. import { cleanNovelContent, paginateContent } from '@/utils/contentUtils'
  92. // 导入清洗工具
  93. import { contentCleaner } from '@/utils/contentCleaner'
  94. //import { useAdManager } from '@/utils/adManager'
  95. import { useVipStore } from '@/stores/vip'
  96. const props = defineProps({
  97. chapterId: {
  98. type: Number,
  99. required: true
  100. },
  101. novelId: {
  102. type: Number,
  103. required: true
  104. },
  105. chapterTitle: {
  106. type: String,
  107. default: '加载中...'
  108. }
  109. })
  110. const emit = defineEmits(['back', 'prev', 'next', 'show-chapters'])
  111. const vipStore = useVipStore()
  112. const { showRewardAd, showFeedAd } = useAdManager()
  113. const userStore = useUserStore()
  114. const themeStore = useThemeStore()
  115. //const { showRewardAd } = useAdManager()
  116. const { saveProgress, loadProgress } = useReadingProgress()
  117. // 阅读状态
  118. const readingMode = ref('scroll') // 'scroll' | 'page'
  119. const readingModes = ref([
  120. { label: '滚动模式', value: 'scroll' },
  121. { label: '翻页模式', value: 'page' }
  122. ])
  123. const showModeSelector = ref(false)
  124. const showFontSizeSelector = ref(false)
  125. const fontSize = ref(18)
  126. const chapterContent = ref('')
  127. const paginatedContent = ref([])
  128. const currentPage = ref(0)
  129. const scrollTop = ref(0)
  130. const lastScrollPosition = ref(0)
  131. const showBottomAd = ref(true)
  132. const bottomAdUnitId = ref('')
  133. const isVIP = computed(() => userStore.isVIP)
  134. // 获取章节内容
  135. const fetchChapterContent = async () => {
  136. try {
  137. const res = await uni.request({
  138. url: `https://php-backend.aiyadianzi.ltd/chapter/${props.chapterId}`,
  139. method: 'GET'
  140. })
  141. if (res.statusCode === 200) {
  142. // 清洗内容并移除原始网站信息
  143. chapterContent.value = cleanNovelContent(res.data.content)
  144. // 分页处理
  145. paginatedContent.value = paginateContent(chapterContent.value, 800)
  146. // 恢复阅读位置
  147. restoreReadingPosition()
  148. } else {
  149. throw new Error('章节加载失败')
  150. }
  151. } catch (error) {
  152. uni.showToast({
  153. title: error.message || '加载章节失败',
  154. icon: 'none'
  155. })
  156. }
  157. }
  158. // 恢复阅读位置
  159. const restoreReadingPosition = async () => {
  160. const progress = await loadProgress(props.novelId, props.chapterId)
  161. if (progress) {
  162. if (readingMode.value === 'scroll') {
  163. scrollTop.value = progress.scrollTop
  164. } else {
  165. currentPage.value = progress.page
  166. }
  167. }
  168. }
  169. // 翻页事件处理
  170. const onPageChange = (e) => {
  171. currentPage.value = e.detail.current
  172. saveReadingPosition()
  173. // VIP用户跳过广告
  174. if (vipStore.hasPrivilege('免广告')) return
  175. // 每5页触发广告
  176. if (currentPage.value % 5 === 0) {
  177. showRewardAd(props.chapterId)
  178. }
  179. }
  180. // 滚动事件处理
  181. const onScroll = (e) => {
  182. lastScrollPosition.value = e.detail.scrollTop
  183. saveReadingPosition()
  184. }
  185. // 保存阅读位置
  186. const saveReadingPosition = () => {
  187. saveProgress(props.novelId, props.chapterId, {
  188. page: currentPage.value,
  189. scrollTop: lastScrollPosition.value
  190. })
  191. }
  192. // 切换阅读模式
  193. const toggleMode = () => {
  194. showModeSelector.value = !showModeSelector.value
  195. }
  196. // 改变阅读模式
  197. const changeMode = (mode) => {
  198. readingMode.value = mode
  199. showModeSelector.value = false
  200. saveReadingPosition()
  201. }
  202. // 切换字体大小选择器
  203. const toggleFontSize = () => {
  204. showFontSizeSelector.value = !showFontSizeSelector.value
  205. }
  206. // 改变字体大小
  207. const changeFontSize = (e) => {
  208. fontSize.value = e.detail.value
  209. }
  210. // 增大字体
  211. const increaseFontSize = () => {
  212. if (fontSize.value < 24) fontSize.value += 2
  213. }
  214. // 减小字体
  215. const decreaseFontSize = () => {
  216. if (fontSize.value > 14) fontSize.value -= 2
  217. }
  218. // 切换主题
  219. const toggleTheme = () => {
  220. const themes = Object.keys(themeStore.themes)
  221. const currentIndex = themes.indexOf(themeStore.currentTheme)
  222. const nextIndex = (currentIndex + 1) % themes.length
  223. themeStore.setTheme(themes[nextIndex])
  224. }
  225. // 加载下一章
  226. const loadNextChapter = () => {
  227. emit('next')
  228. }
  229. // 返回
  230. const goBack = () => {
  231. emit('back')
  232. }
  233. // 上一章
  234. const prevChapter = () => {
  235. emit('prev')
  236. }
  237. // 下一章
  238. const nextChapter = () => {
  239. emit('next')
  240. }
  241. // 显示章节列表
  242. const showChapterList = () => {
  243. emit('show-chapters')
  244. }
  245. // 初始化
  246. onMounted(() => {
  247. // VIP用户不显示广告
  248. if (vipStore.hasPrivilege('免广告')) return
  249. // 显示底部广告
  250. feedAd.value = showFeedAd()
  251. fetchChapterContent()
  252. // 设置广告单元ID
  253. // #ifdef MP-WEIXIN
  254. bottomAdUnitId.value = 'wechat_feed_ad_123456'
  255. // #endif
  256. // #ifdef MP-TOUTIAO
  257. bottomAdUnitId.value = 'douyin_feed_ad_654321'
  258. // #endif
  259. // #ifdef H5
  260. bottomAdUnitId.value = 'h5_feed_ad_789012'
  261. // #endif
  262. // 监听VIP状态变化
  263. watch(() => userStore.isVIP, (isVip) => {
  264. showBottomAd.value = !isVip
  265. })
  266. })
  267. // 在获取章节内容后清洗
  268. const fetchChapterContent = async () => {
  269. const res = await uni.request({
  270. url: `https://php-backend.aiyadianzi.ltd/chapter/${props.chapterId}`,
  271. method: 'GET'
  272. })
  273. // 清洗内容
  274. chapterContent.value = contentCleaner.clean(res.data.content)
  275. // 分页处理
  276. paginatedContent.value = paginateContent(chapterContent.value)
  277. }
  278. </script>
  279. <style scoped>
  280. .reader-container {
  281. display: flex;
  282. flex-direction: column;
  283. height: 100vh;
  284. background-color: var(--reader-bg-color);
  285. color: var(--reader-text-color);
  286. }
  287. .reader-header {
  288. display: flex;
  289. justify-content: space-between;
  290. align-items: center;
  291. padding: 10px 15px;
  292. background-color: var(--header-bg-color);
  293. border-bottom: 1px solid var(--border-color);
  294. height: 50px;
  295. box-sizing: border-box;
  296. }
  297. .left-actions {
  298. display: flex;
  299. align-items: center;
  300. flex: 1;
  301. }
  302. .back-btn {
  303. margin-right: 15px;
  304. background: none;
  305. border: none;
  306. padding: 0;
  307. }
  308. .chapter-title {
  309. font-size: 16px;
  310. font-weight: bold;
  311. white-space: nowrap;
  312. overflow: hidden;
  313. text-overflow: ellipsis;
  314. max-width: 60vw;
  315. }
  316. .right-actions {
  317. display: flex;
  318. }
  319. .action-btn {
  320. background: none;
  321. border: none;
  322. padding: 5px 10px;
  323. margin-left: 10px;
  324. }
  325. .mode-selector {
  326. display: flex;
  327. padding: 10px;
  328. background-color: var(--header-bg-color);
  329. justify-content: center;
  330. border-bottom: 1px solid var(--border-color);
  331. }
  332. .mode-btn {
  333. margin: 0 10px;
  334. padding: 5px 15px;
  335. border-radius: 15px;
  336. background-color: var(--button-bg);
  337. color: var(--button-color);
  338. border: none;
  339. font-size: 14px;
  340. }
  341. .mode-btn.active {
  342. background-color: var(--primary-color);
  343. color: white;
  344. }
  345. .font-size-selector {
  346. display: flex;
  347. align-items: center;
  348. padding: 10px 15px;
  349. background-color: var(--header-bg-color);
  350. border-bottom: 1px solid var(--border-color);
  351. }
  352. .size-btn {
  353. background: none;
  354. border: 1px solid var(--border-color);
  355. border-radius: 4px;
  356. width: 40px;
  357. height: 40px;
  358. display: flex;
  359. align-items: center;
  360. justify-content: center;
  361. font-weight: bold;
  362. }
  363. .font-slider {
  364. flex: 1;
  365. margin: 0 15px;
  366. }
  367. .scroll-reader {
  368. flex: 1;
  369. padding: 20px;
  370. overflow-y: auto;
  371. line-height: 1.8;
  372. }
  373. .page-reader {
  374. flex: 1;
  375. }
  376. .reader-footer {
  377. display: flex;
  378. justify-content: space-between;
  379. padding: 10px 15px;
  380. background-color: var(--header-bg-color);
  381. border-top: 1px solid var(--border-color);
  382. }
  383. .footer-btn {
  384. flex: 1;
  385. margin: 0 5px;
  386. padding: 8px 0;
  387. border-radius: 4px;
  388. background-color: var(--button-bg);
  389. color: var(--button-color);
  390. border: none;
  391. font-size: 14px;
  392. }
  393. .bottom-ad {
  394. height: 100px;
  395. background-color: #f5f5f5;
  396. }
  397. </style>