├── php-api/ # 改造后的PHP接口层 ├── java-ad-service/ # 若依框架微服务(广告+VIP+分账) ├── uniapp-reader/ # UniApp前端项目 │ ├── pages/ # 各端页面 │ └──
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

NovelReader.vue 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. const props = defineProps({
  93. chapterId: {
  94. type: Number,
  95. required: true
  96. },
  97. novelId: {
  98. type: Number,
  99. required: true
  100. },
  101. chapterTitle: {
  102. type: String,
  103. default: '加载中...'
  104. }
  105. })
  106. const emit = defineEmits(['back', 'prev', 'next', 'show-chapters'])
  107. const userStore = useUserStore()
  108. const themeStore = useThemeStore()
  109. const { showRewardAd } = useAdManager()
  110. const { saveProgress, loadProgress } = useReadingProgress()
  111. // 阅读状态
  112. const readingMode = ref('scroll') // 'scroll' | 'page'
  113. const readingModes = ref([
  114. { label: '滚动模式', value: 'scroll' },
  115. { label: '翻页模式', value: 'page' }
  116. ])
  117. const showModeSelector = ref(false)
  118. const showFontSizeSelector = ref(false)
  119. const fontSize = ref(18)
  120. const chapterContent = ref('')
  121. const paginatedContent = ref([])
  122. const currentPage = ref(0)
  123. const scrollTop = ref(0)
  124. const lastScrollPosition = ref(0)
  125. const showBottomAd = ref(true)
  126. const bottomAdUnitId = ref('')
  127. const isVIP = computed(() => userStore.isVIP)
  128. // 获取章节内容
  129. const fetchChapterContent = async () => {
  130. try {
  131. const res = await uni.request({
  132. url: `https://php-backend.aiyadianzi.ltd/chapter/${props.chapterId}`,
  133. method: 'GET'
  134. })
  135. if (res.statusCode === 200) {
  136. // 清洗内容并移除原始网站信息
  137. chapterContent.value = cleanNovelContent(res.data.content)
  138. // 分页处理
  139. paginatedContent.value = paginateContent(chapterContent.value, 800)
  140. // 恢复阅读位置
  141. restoreReadingPosition()
  142. } else {
  143. throw new Error('章节加载失败')
  144. }
  145. } catch (error) {
  146. uni.showToast({
  147. title: error.message || '加载章节失败',
  148. icon: 'none'
  149. })
  150. }
  151. }
  152. // 恢复阅读位置
  153. const restoreReadingPosition = async () => {
  154. const progress = await loadProgress(props.novelId, props.chapterId)
  155. if (progress) {
  156. if (readingMode.value === 'scroll') {
  157. scrollTop.value = progress.scrollTop
  158. } else {
  159. currentPage.value = progress.page
  160. }
  161. }
  162. }
  163. // 翻页事件处理
  164. const onPageChange = (e) => {
  165. currentPage.value = e.detail.current
  166. saveReadingPosition()
  167. // 每5页触发广告
  168. if (currentPage.value % 5 === 0) {
  169. showRewardAd(props.chapterId)
  170. }
  171. }
  172. // 滚动事件处理
  173. const onScroll = (e) => {
  174. lastScrollPosition.value = e.detail.scrollTop
  175. saveReadingPosition()
  176. }
  177. // 保存阅读位置
  178. const saveReadingPosition = () => {
  179. saveProgress(props.novelId, props.chapterId, {
  180. page: currentPage.value,
  181. scrollTop: lastScrollPosition.value
  182. })
  183. }
  184. // 切换阅读模式
  185. const toggleMode = () => {
  186. showModeSelector.value = !showModeSelector.value
  187. }
  188. // 改变阅读模式
  189. const changeMode = (mode) => {
  190. readingMode.value = mode
  191. showModeSelector.value = false
  192. saveReadingPosition()
  193. }
  194. // 切换字体大小选择器
  195. const toggleFontSize = () => {
  196. showFontSizeSelector.value = !showFontSizeSelector.value
  197. }
  198. // 改变字体大小
  199. const changeFontSize = (e) => {
  200. fontSize.value = e.detail.value
  201. }
  202. // 增大字体
  203. const increaseFontSize = () => {
  204. if (fontSize.value < 24) fontSize.value += 2
  205. }
  206. // 减小字体
  207. const decreaseFontSize = () => {
  208. if (fontSize.value > 14) fontSize.value -= 2
  209. }
  210. // 切换主题
  211. const toggleTheme = () => {
  212. const themes = Object.keys(themeStore.themes)
  213. const currentIndex = themes.indexOf(themeStore.currentTheme)
  214. const nextIndex = (currentIndex + 1) % themes.length
  215. themeStore.setTheme(themes[nextIndex])
  216. }
  217. // 加载下一章
  218. const loadNextChapter = () => {
  219. emit('next')
  220. }
  221. // 返回
  222. const goBack = () => {
  223. emit('back')
  224. }
  225. // 上一章
  226. const prevChapter = () => {
  227. emit('prev')
  228. }
  229. // 下一章
  230. const nextChapter = () => {
  231. emit('next')
  232. }
  233. // 显示章节列表
  234. const showChapterList = () => {
  235. emit('show-chapters')
  236. }
  237. // 初始化
  238. onMounted(() => {
  239. fetchChapterContent()
  240. // 设置广告单元ID
  241. // #ifdef MP-WEIXIN
  242. bottomAdUnitId.value = 'wechat_feed_ad_123456'
  243. // #endif
  244. // #ifdef MP-TOUTIAO
  245. bottomAdUnitId.value = 'douyin_feed_ad_654321'
  246. // #endif
  247. // #ifdef H5
  248. bottomAdUnitId.value = 'h5_feed_ad_789012'
  249. // #endif
  250. // 监听VIP状态变化
  251. watch(() => userStore.isVIP, (isVip) => {
  252. showBottomAd.value = !isVip
  253. })
  254. })
  255. </script>
  256. <style scoped>
  257. .reader-container {
  258. display: flex;
  259. flex-direction: column;
  260. height: 100vh;
  261. background-color: var(--reader-bg-color);
  262. color: var(--reader-text-color);
  263. }
  264. .reader-header {
  265. display: flex;
  266. justify-content: space-between;
  267. align-items: center;
  268. padding: 10px 15px;
  269. background-color: var(--header-bg-color);
  270. border-bottom: 1px solid var(--border-color);
  271. height: 50px;
  272. box-sizing: border-box;
  273. }
  274. .left-actions {
  275. display: flex;
  276. align-items: center;
  277. flex: 1;
  278. }
  279. .back-btn {
  280. margin-right: 15px;
  281. background: none;
  282. border: none;
  283. padding: 0;
  284. }
  285. .chapter-title {
  286. font-size: 16px;
  287. font-weight: bold;
  288. white-space: nowrap;
  289. overflow: hidden;
  290. text-overflow: ellipsis;
  291. max-width: 60vw;
  292. }
  293. .right-actions {
  294. display: flex;
  295. }
  296. .action-btn {
  297. background: none;
  298. border: none;
  299. padding: 5px 10px;
  300. margin-left: 10px;
  301. }
  302. .mode-selector {
  303. display: flex;
  304. padding: 10px;
  305. background-color: var(--header-bg-color);
  306. justify-content: center;
  307. border-bottom: 1px solid var(--border-color);
  308. }
  309. .mode-btn {
  310. margin: 0 10px;
  311. padding: 5px 15px;
  312. border-radius: 15px;
  313. background-color: var(--button-bg);
  314. color: var(--button-color);
  315. border: none;
  316. font-size: 14px;
  317. }
  318. .mode-btn.active {
  319. background-color: var(--primary-color);
  320. color: white;
  321. }
  322. .font-size-selector {
  323. display: flex;
  324. align-items: center;
  325. padding: 10px 15px;
  326. background-color: var(--header-bg-color);
  327. border-bottom: 1px solid var(--border-color);
  328. }
  329. .size-btn {
  330. background: none;
  331. border: 1px solid var(--border-color);
  332. border-radius: 4px;
  333. width: 40px;
  334. height: 40px;
  335. display: flex;
  336. align-items: center;
  337. justify-content: center;
  338. font-weight: bold;
  339. }
  340. .font-slider {
  341. flex: 1;
  342. margin: 0 15px;
  343. }
  344. .scroll-reader {
  345. flex: 1;
  346. padding: 20px;
  347. overflow-y: auto;
  348. line-height: 1.8;
  349. }
  350. .page-reader {
  351. flex: 1;
  352. }
  353. .reader-footer {
  354. display: flex;
  355. justify-content: space-between;
  356. padding: 10px 15px;
  357. background-color: var(--header-bg-color);
  358. border-top: 1px solid var(--border-color);
  359. }
  360. .footer-btn {
  361. flex: 1;
  362. margin: 0 5px;
  363. padding: 8px 0;
  364. border-radius: 4px;
  365. background-color: var(--button-bg);
  366. color: var(--button-color);
  367. border: none;
  368. font-size: 14px;
  369. }
  370. .bottom-ad {
  371. height: 100px;
  372. background-color: #f5f5f5;
  373. }
  374. </style>