├── php-api/ # 改造后的PHP接口层 ├── java-ad-service/ # 若依框架微服务(广告+VIP+分账) ├── uniapp-reader/ # UniApp前端项目 │ ├── pages/ # 各端页面 │ └──
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

reader.vue 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. <template>
  2. <view class="reader-container">
  3. <!-- 登录提示 -->
  4. <view v-if="requireLogin" class="login-prompt">
  5. <text>免费章节已结束,登录后继续阅读</text>
  6. <button class="btn-login" @click="showLoginPrompt">立即登录</button>
  7. </view>
  8. <!-- 广告提示 -->
  9. <view v-if="showAd" class="ad-prompt">
  10. <text>VIP用户免广告,开通VIP享受更好的阅读体验</text>
  11. </view>
  12. <!-- 加载状态 -->
  13. <view v-if="loading" class="loading-container">
  14. <uni-icons type="spinner-cycle" size="36" color="var(--primary-color)" class="loading-icon" />
  15. <text>加载中...</text>
  16. </view>
  17. <!-- 错误状态 -->
  18. <view v-else-if="error" class="error-container">
  19. <uni-icons type="info" size="48" color="var(--error-color)" />
  20. <text class="error-text">{{ error }}</text>
  21. <button class="btn-retry" @click="retryLoad">重新加载</button>
  22. </view>
  23. <!-- 内容区域 -->
  24. <view v-else class="content-container">
  25. <!-- 小说标题 -->
  26. <view class="novel-header">
  27. <text class="novel-title">{{ novel.title || '加载中...' }}</text>
  28. <text class="novel-author">{{ novel.author || '未知作者' }}</text>
  29. </view>
  30. <!-- 章节标题 -->
  31. <view class="chapter-header">
  32. <text class="chapter-title">{{ currentChapter.title || '第一章' }}</text>
  33. </view>
  34. <!-- 章节内容 -->
  35. <scroll-view scroll-y class="chapter-content">
  36. <text class="content-text">
  37. {{ chapterContent || '正在加载章节内容...' }}
  38. </text>
  39. </scroll-view>
  40. <!-- 底部操作栏 -->
  41. <view class="action-bar">
  42. <button class="btn-action" @click="prevChapter" :disabled="!hasPrevChapter">
  43. 上一章
  44. </button>
  45. <button class="btn-action" @click="showCatalog">
  46. 目录
  47. </button>
  48. <button class="btn-action" @click="nextChapter" :disabled="!hasNextChapter">
  49. 下一章
  50. </button>
  51. </view>
  52. </view>
  53. </view>
  54. </template>
  55. <script>
  56. import request from '@/utils/request'
  57. import { getToken, setToken, removeToken } from '@/utils/auth'
  58. export default {
  59. data() {
  60. return {
  61. novelId: null,
  62. novel: {},
  63. chapters: [],
  64. currentChapter: {},
  65. chapterContent: '',
  66. loading: true,
  67. error: null,
  68. currentChapterIndex: 0,
  69. // 新增字段
  70. freeChapters: 5, // 免费章节数
  71. adStartChapter: 15, // 开始显示广告的章节
  72. isVip: false,
  73. isLoggedIn: false,
  74. showLoginModal: false
  75. }
  76. },
  77. computed: {
  78. // 计算当前章节是否需要登录
  79. requireLogin() {
  80. return this.currentChapterIndex >= this.freeChapters && !this.isLoggedIn;
  81. },
  82. // 计算当前章节是否需要显示广告
  83. showAd() {
  84. return this.currentChapterIndex >= this.adStartChapter && !this.isVip;
  85. },
  86. hasPrevChapter() {
  87. return this.currentChapterIndex > 0;
  88. },
  89. hasNextChapter() {
  90. return this.currentChapterIndex < this.chapters.length - 1;
  91. }
  92. },
  93. // 使用 uni-app 生命周期
  94. onLoad(options) {
  95. console.log('📥 READER onLoad 参数:', options);
  96. console.log('📍 READER 路由信息:', this.$route);
  97. // 简化参数获取
  98. this.novelId = options.novelId || this.$route.query.novelId;
  99. const chapterId = options.chapterId || this.$route.query.chapterId;
  100. console.log('🔍 READER 最终参数 - novelId:', this.novelId, 'chapterId:', chapterId);
  101. if (!this.novelId) {
  102. console.error('❌ READER: 无法获取小说ID');
  103. this.error = '无法获取小说信息';
  104. this.loading = false;
  105. return;
  106. }
  107. this.initReader();
  108. },
  109. // reader.vue - 添加 onShow 方法
  110. onShow() {
  111. console.log('🔍 onShow 生命周期');
  112. // 再次检查参数,确保不会漏掉
  113. if (!this.novelId && this.$route.query.novelId) {
  114. console.log('🔄 onShow 中检测到参数,重新初始化');
  115. this.initReader(this.$route.query);
  116. }
  117. },
  118. // 同时使用 Vue 生命周期作为备选
  119. mounted() {
  120. console.log('🔄 mounted 生命周期');
  121. // 如果 onLoad 没有获取到参数,尝试从路由获取
  122. if (!this.novelId) {
  123. const route = this.$route;
  124. console.log('🔄 从 $route 获取参数:', route);
  125. // 合并所有可能的参数来源
  126. const allParams = {
  127. ...(route.params || {}),
  128. ...(route.query || {}),
  129. novelId: route.query.novelId // 确保novelId被单独提取
  130. };
  131. this.initReader(allParams);
  132. }
  133. },
  134. methods: {
  135. // 检查用户状态
  136. checkUserStatus() {
  137. const token = uni.getStorageSync('token');
  138. this.isLoggedIn = !!token;
  139. // 这里可以调用API检查VIP状态
  140. // const userInfo = uni.getStorageSync('userInfo');
  141. // this.isVip = userInfo && userInfo.isVip;
  142. },
  143. // 修改下一章方法,添加限制
  144. async nextChapter() {
  145. if (!this.hasNextChapter) {
  146. uni.showToast({
  147. title: '已经是最后一章了',
  148. icon: 'none'
  149. });
  150. return;
  151. }
  152. // 检查是否需要登录
  153. if (this.currentChapterIndex + 1 >= this.freeChapters && !this.isLoggedIn) {
  154. this.showLoginPrompt();
  155. return;
  156. }
  157. // 正常跳转到下一章
  158. this.currentChapterIndex++;
  159. this.currentChapter = this.chapters[this.currentChapterIndex];
  160. await this.loadChapterContent(this.currentChapter.id);
  161. // 检查是否需要显示广告
  162. if (this.showAd) {
  163. this.showChapterAd();
  164. }
  165. },
  166. // 显示登录提示
  167. showLoginPrompt() {
  168. uni.showModal({
  169. title: '登录提示',
  170. content: `免费阅读前${this.freeChapters}章,第${this.currentChapterIndex + 2}章需要登录后阅读\n是否立即登录?`,
  171. confirmText: '去登录',
  172. cancelText: '取消',
  173. success: (res) => {
  174. if (res.confirm) {
  175. uni.navigateTo({
  176. url: '/pages/login/index'
  177. });
  178. }
  179. }
  180. });
  181. },
  182. // 显示章节广告
  183. showChapterAd() {
  184. // 这里可以集成广告SDK
  185. console.log('显示章节广告');
  186. uni.showToast({
  187. title: '广告展示中...',
  188. icon: 'none'
  189. });
  190. },
  191. // 在初始化时检查用户状态
  192. async initReader(params) {
  193. console.log('🎬 初始化阅读器,参数:', params);
  194. console.log('📍 当前路由查询参数:', this.$route.query);
  195. // 修复参数获取逻辑 - 从多个位置获取novelId
  196. this.novelId = params.novelId ||
  197. params.id ||
  198. this.$route.query.novelId ||
  199. this.$route.query.id ||
  200. (this.$route.params && this.$route.params.id);
  201. console.log('🔍 最终获取到的小说ID:', this.novelId);
  202. // 检查用户状态
  203. this.checkUserStatus();
  204. // 如果还是没有获取到,从URL中解析
  205. if (!this.novelId) {
  206. console.log('🔄 尝试从URL解析参数...');
  207. const urlParams = new URLSearchParams(window.location.search);
  208. this.novelId = urlParams.get('novelId') || urlParams.get('id');
  209. console.log('🔍 从URL解析到的小说ID:', this.novelId);
  210. }
  211. if (!this.novelId) {
  212. this.error = '无法获取小说ID,请返回重新选择';
  213. this.loading = false;
  214. console.error('❌ 小说ID为空,所有获取方式都失败了');
  215. console.error('📋 可用参数:', {
  216. params: params,
  217. routeQuery: this.$route.query,
  218. routeParams: this.$route.params,
  219. fullPath: this.$route.fullPath
  220. });
  221. return;
  222. }
  223. try {
  224. await this.loadNovelDetail();
  225. await this.loadChapters();
  226. await this.loadFirstChapterContent();
  227. } catch (error) {
  228. console.error('💥 初始化失败:', error);
  229. this.error = '加载失败: ' + (error.message || '未知错误');
  230. } finally {
  231. this.loading = false;
  232. }
  233. },
  234. // 加载小说详情
  235. async loadNovelDetail() {
  236. console.log('📚 加载小说详情,ID:', this.novelId);
  237. try {
  238. // 使用导入的 request,而不是 this.$http
  239. const res = await request.get(`/novel/detail/${this.novelId}`, {}, {
  240. header: { isToken: false }
  241. });
  242. console.log('✅ 小说详情响应:', res);
  243. if (res && res.code === 200 && res.data) {
  244. this.novel = res.data;
  245. console.log('✅ 成功加载小说详情:', this.novel.title);
  246. } else {
  247. console.warn('⚠️ 小说详情接口返回异常:', res);
  248. this.useMockNovelData();
  249. }
  250. } catch (error) {
  251. console.error('❌ 加载小说详情失败:', error);
  252. // 使用模拟数据继续流程
  253. this.useMockNovelData();
  254. }
  255. },
  256. useMockNovelData() {
  257. this.novel = {
  258. title: '加载中的小说',
  259. author: '加载中...',
  260. description: '正在加载小说信息...'
  261. };
  262. },
  263. // 加载章节列表
  264. async loadChapters() {
  265. console.log('📑 加载章节列表,小说ID:', this.novelId);
  266. try {
  267. const token = getToken();
  268. let requestConfig = {
  269. header: { isToken: false }
  270. };
  271. if (token) {
  272. requestConfig.header = {
  273. 'Authorization': 'Bearer ' + token,
  274. isToken: true
  275. };
  276. }
  277. const res = await request.get(`/chapter/list/${this.novelId}`, {}, requestConfig);
  278. console.log('✅ 章节列表响应:', res);
  279. let chapters = [];
  280. if (res && res.code === 200 && res.rows) {
  281. chapters = res.rows;
  282. } else if (res && res.rows) {
  283. chapters = res.rows;
  284. }
  285. if (chapters.length > 0) {
  286. this.chapters = chapters;
  287. console.log(`✅ 成功加载 ${chapters.length} 个章节`);
  288. } else {
  289. this.useMockChapters();
  290. console.log('⚠️ 使用模拟章节数据');
  291. }
  292. } catch (error) {
  293. console.error('❌ 加载章节列表失败:', error);
  294. this.useMockChapters();
  295. }
  296. },
  297. // 加载第一章内容
  298. async loadFirstChapterContent() {
  299. if (this.chapters.length > 0) {
  300. this.currentChapterIndex = 0;
  301. this.currentChapter = this.chapters[0];
  302. await this.loadChapterContent(this.chapters[0].id);
  303. }
  304. },
  305. // 加载章节内容
  306. async loadChapterContent(chapterId) {
  307. console.log('📖 加载章节内容,章节ID:', chapterId);
  308. if (!chapterId) {
  309. console.error('❌ 章节ID为空');
  310. return;
  311. }
  312. this.loading = true;
  313. try {
  314. const token = getToken();
  315. let requestConfig = {
  316. header: { isToken: false }
  317. };
  318. if (token) {
  319. requestConfig.header = {
  320. 'Authorization': 'Bearer ' + token,
  321. isToken: true
  322. };
  323. }
  324. const res = await request.get(`/chapter/content/${chapterId}`, {}, requestConfig);
  325. console.log('✅ 章节内容响应:', res);
  326. if (res && res.code === 200 && res.data) {
  327. // 后端返回的数据在 res.data 中
  328. this.chapterContent = res.data.content || '本章节内容为空';
  329. console.log('✅ 成功加载章节内容');
  330. } else if (res && res.content) {
  331. // 或者直接返回 content
  332. this.chapterContent = res.content;
  333. console.log('✅ 成功加载章节内容(direct)');
  334. } else {
  335. // 模拟章节内容
  336. this.chapterContent = `这是第${this.currentChapterIndex + 1}章的示例内容。\n\n这是一个美丽的春天,主人公开始了他的冒险旅程...\n\n本章内容正在努力加载中,请稍后查看完整内容。`;
  337. console.log('⚠️ 使用模拟章节内容');
  338. }
  339. } catch (error) {
  340. console.error('❌ 加载章节内容失败:', error);
  341. // 模拟错误时的内容
  342. this.chapterContent = `第${this.currentChapterIndex + 1}章内容加载失败,请检查网络连接后重试。\n\n错误信息: ${error.message}`;
  343. } finally {
  344. this.loading = false;
  345. }
  346. },
  347. // 添加模拟章节方法
  348. useMockChapters() {
  349. this.chapters = [
  350. { id: 1, title: '第一章 开始', content: null },
  351. { id: 2, title: '第二章 发展', content: null },
  352. { id: 3, title: '第三章 结束', content: null }
  353. ];
  354. },
  355. // 上一章
  356. async prevChapter() {
  357. if (this.hasPrevChapter) {
  358. this.currentChapterIndex--;
  359. this.currentChapter = this.chapters[this.currentChapterIndex];
  360. await this.loadChapterContent(this.currentChapter.id);
  361. }
  362. },
  363. // 显示目录
  364. showCatalog() {
  365. uni.showToast({
  366. title: '目录功能开发中',
  367. icon: 'none'
  368. });
  369. },
  370. // 重新加载
  371. retryLoad() {
  372. this.loading = true;
  373. this.error = null;
  374. this.initReader({ id: this.novelId });
  375. }
  376. }
  377. }
  378. </script>
  379. <style scoped>
  380. .reader-container {
  381. padding: 20rpx;
  382. min-height: 100vh;
  383. background-color: #f5f5f5;
  384. }
  385. .loading-container, .error-container {
  386. display: flex;
  387. flex-direction: column;
  388. align-items: center;
  389. justify-content: center;
  390. padding: 200rpx 0;
  391. text-align: center;
  392. }
  393. .loading-icon {
  394. animation: rotate 1s linear infinite;
  395. margin-bottom: 20rpx;
  396. }
  397. @keyframes rotate {
  398. from { transform: rotate(0deg); }
  399. to { transform: rotate(360deg); }
  400. }
  401. .error-text {
  402. font-size: 32rpx;
  403. color: var(--error-color);
  404. margin: 20rpx 0;
  405. text-align: center;
  406. }
  407. .btn-retry {
  408. background-color: var(--primary-color);
  409. color: white;
  410. padding: 20rpx 40rpx;
  411. border-radius: 10rpx;
  412. margin-top: 20rpx;
  413. }
  414. .novel-header {
  415. text-align: center;
  416. padding: 30rpx 0;
  417. border-bottom: 1rpx solid #eee;
  418. margin-bottom: 30rpx;
  419. }
  420. .novel-title {
  421. display: block;
  422. font-size: 36rpx;
  423. font-weight: bold;
  424. margin-bottom: 10rpx;
  425. }
  426. .novel-author {
  427. display: block;
  428. font-size: 28rpx;
  429. color: #666;
  430. }
  431. .chapter-header {
  432. padding: 20rpx 0;
  433. border-bottom: 1rpx solid #eee;
  434. margin-bottom: 30rpx;
  435. }
  436. .chapter-title {
  437. font-size: 32rpx;
  438. font-weight: bold;
  439. color: #333;
  440. }
  441. .chapter-content {
  442. height: 70vh;
  443. padding: 30rpx;
  444. background: white;
  445. border-radius: 10rpx;
  446. margin-bottom: 30rpx;
  447. }
  448. .content-text {
  449. font-size: 32rpx;
  450. line-height: 1.8;
  451. color: #333;
  452. }
  453. .action-bar {
  454. display: flex;
  455. justify-content: space-between;
  456. padding: 20rpx 0;
  457. background: white;
  458. border-radius: 10rpx;
  459. }
  460. .btn-action {
  461. flex: 1;
  462. margin: 0 10rpx;
  463. padding: 20rpx;
  464. background-color: var(--primary-color);
  465. color: white;
  466. border-radius: 8rpx;
  467. font-size: 28rpx;
  468. }
  469. .btn-action:disabled {
  470. background-color: #ccc;
  471. color: #666;
  472. }
  473. .login-prompt, .ad-prompt {
  474. background: #fff3cd;
  475. border: 1px solid #ffeaa7;
  476. padding: 20rpx;
  477. margin: 20rpx;
  478. border-radius: 8rpx;
  479. text-align: center;
  480. }
  481. .btn-login {
  482. background: var(--primary-color);
  483. color: white;
  484. padding: 10rpx 20rpx;
  485. border-radius: 6rpx;
  486. margin-top: 10rpx;
  487. font-size: 26rpx;
  488. }
  489. </style>