├── php-api/ # 改造后的PHP接口层 ├── java-ad-service/ # 若依框架微服务(广告+VIP+分账) ├── uniapp-reader/ # UniApp前端项目 │ ├── pages/ # 各端页面 │ └──
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. <template>
  2. <view class="novel-list-page">
  3. <!-- 加载状态 -->
  4. <view v-if="loading" class="loading-container">
  5. <uni-icons type="spinner-cycle" size="36" color="var(--primary-color)" class="loading-icon" />
  6. <text>加载中...</text>
  7. </view>
  8. <!-- 内容区域 -->
  9. <template v-else>
  10. <!-- 顶部广告 -->
  11. <ad-banner v-if="topAds.length" :ads="topAds" />
  12. <!-- 顶部分类导航 -->
  13. <scroll-view scroll-x class="category-nav">
  14. <view
  15. v-for="category in categories"
  16. :key="category.id"
  17. class="category-item"
  18. :class="{ active: activeCategory === category.id }"
  19. @click="changeCategory(category.id)"
  20. >
  21. {{ category.name }}
  22. </view>
  23. </scroll-view>
  24. <!-- 空状态 -->
  25. <view v-if="showEmptyState" class="empty-container">
  26. <image src="/static/empty-book.png" class="empty-img" />
  27. <text class="empty-text">暂无小说数据</text>
  28. <button class="btn-refresh" @click="reloadData">重新加载</button>
  29. </view>
  30. <!-- 错误状态 -->
  31. <view v-if="error && !showNovels" class="error-container">
  32. <uni-icons type="info" size="48" color="var(--error-color)" />
  33. <text class="error-text">{{ error }}</text>
  34. <button class="btn-refresh" @click="loadNovels">重新加载</button>
  35. </view>
  36. <!-- 热门推荐 -->
  37. <view v-if="showHotNovels" class="section">
  38. <view class="section-header">
  39. <text class="section-title">热门推荐</text>
  40. <text class="section-more" @click="goMore">更多 ></text>
  41. </view>
  42. <scroll-view scroll-x class="hot-list">
  43. <view
  44. v-for="novel in displayedHotNovels"
  45. :key="novel.id"
  46. class="hot-item"
  47. @click="openNovel(novel)"
  48. >
  49. <image
  50. :src="getCoverUrl(novel.coverImg)"
  51. class="hot-cover"
  52. @error="handleImageError"
  53. />
  54. <text class="hot-title">{{ novel.title }}</text>
  55. </view>
  56. </scroll-view>
  57. </view>
  58. <!-- 小说列表 -->
  59. <view v-if="showNovels" class="section">
  60. <view class="section-header">
  61. <text class="section-title">小说列表</text>
  62. </view>
  63. <view class="novel-grid">
  64. <view
  65. v-for="novel in displayedNovels"
  66. :key="novel.id"
  67. class="novel-item"
  68. @click="openNovel(novel)"
  69. >
  70. <image
  71. :src="getCoverUrl(novel.coverImg)"
  72. class="novel-cover"
  73. @error="handleImageError"
  74. />
  75. <text class="novel-title">{{ novel.title }}</text>
  76. <text class="novel-author">{{ novel.author || '未知作者' }}</text>
  77. </view>
  78. </view>
  79. </view>
  80. <!-- 成为签约作家按钮 -->
  81. <view class="become-author" @click="applyAuthor">
  82. <text>成为签约作家,发布你的作品</text>
  83. </view>
  84. <!-- 底部广告 -->
  85. <ad-banner v-if="bottomAds.length" :ads="bottomAds" />
  86. </template>
  87. </view>
  88. </template>
  89. <script>
  90. import AdBanner from '@/components/AdBanner';
  91. export default {
  92. components: { AdBanner },
  93. data() {
  94. return {
  95. topAds: [],
  96. bottomAds: [],
  97. categories: [],
  98. activeCategory: 0,
  99. hotNovels: [],
  100. novels: [],
  101. loading: true,
  102. error: null,
  103. currentCategoryName: '全部',
  104. // 临时解决方案 - 模拟数据
  105. mockHotNovels: [
  106. { id: 1, title: '总裁的替身前妻', coverImg: '/static/demo-cover1.jpg', author: '言情作家' },
  107. { id: 2, title: '神医毒妃', coverImg: '/static/demo-cover2.jpg', author: '古风大神' },
  108. { id: 3, title: '穿越之王妃逆袭', coverImg: '/static/demo-cover3.jpg', author: '穿越达人' }
  109. ],
  110. mockNovels: [
  111. { id: 4, title: '都市最强赘婿', author: '网络作家', coverImg: '/static/demo-cover4.jpg' },
  112. { id: 5, title: '修仙归来', author: '仙侠迷', coverImg: '/static/demo-cover5.jpg' },
  113. { id: 6, title: '校园纯爱物语', author: '青春校园', coverImg: '/static/demo-cover6.jpg' }
  114. ],
  115. // 添加分页参数
  116. page: 1,
  117. pageSize: 10,
  118. total: 0,
  119. hasMore: true
  120. }
  121. },
  122. computed: {
  123. // 添加计算属性控制显示逻辑
  124. showEmptyState() {
  125. return this.novels.length === 0 && !this.error && !this.loading;
  126. },
  127. showHotNovels() {
  128. return this.hotNovels.length > 0 && !this.error;
  129. },
  130. showNovels() {
  131. return this.novels.length > 0 && !this.error;
  132. },
  133. displayedHotNovels() {
  134. return this.hotNovels.length > 0 ? this.hotNovels : this.mockHotNovels;
  135. },
  136. displayedNovels() {
  137. return this.novels.length > 0 ? this.novels : this.mockNovels;
  138. }
  139. },
  140. mounted() {
  141. this.initData();
  142. },
  143. methods: {
  144. async initData() {
  145. try {
  146. // 改为顺序加载,便于调试
  147. await this.loadCategories();
  148. await this.loadHotNovels();
  149. await this.loadNovels();
  150. } catch (e) {
  151. console.error('初始化失败', e);
  152. this.error = '初始化失败,请检查网络连接';
  153. } finally {
  154. this.loading = false;
  155. }
  156. },
  157. openNovel(novel) {
  158. // 使用uni.navigateTo替代this.$tab.navigateTo
  159. uni.navigateTo({
  160. url: `/pages/novel/reader?novelId=${novel.id}`
  161. });
  162. },
  163. // 修复响应解析方法
  164. parseResponse(res) {
  165. console.log('原始响应对象:', res);
  166. // 处理不同的响应格式
  167. let responseData = {};
  168. // 如果res有arg属性,使用arg作为响应数据
  169. if (res && res.arg) {
  170. responseData = res.arg;
  171. console.log('使用res.arg作为响应数据:', responseData);
  172. }
  173. // 如果res有data属性,使用data作为响应数据
  174. else if (res && res.data) {
  175. responseData = res.data;
  176. console.log('使用res.data作为响应数据:', responseData);
  177. }
  178. // 如果res本身就是响应数据
  179. else {
  180. responseData = res;
  181. console.log('使用res本身作为响应数据:', responseData);
  182. }
  183. return responseData;
  184. },
  185. // 修复分类加载逻辑
  186. async loadCategories() {
  187. try {
  188. const res = await this.$http.get('/category/tree', {
  189. header: { isToken: false },
  190. timeout: 10000
  191. });
  192. console.log('分类API完整响应:', res);
  193. // 使用统一的响应解析方法
  194. const responseData = this.parseResponse(res);
  195. console.log('处理后的分类响应数据:', responseData);
  196. // 修复: 检查不同的响应格式
  197. let categoriesData = [];
  198. if (responseData.code === 200 && Array.isArray(responseData.data)) {
  199. categoriesData = responseData.data;
  200. } else if (Array.isArray(responseData)) {
  201. categoriesData = responseData;
  202. } else if (responseData.code === 200 && Array.isArray(responseData.rows)) {
  203. categoriesData = responseData.rows;
  204. }
  205. if (categoriesData.length > 0) {
  206. let subCategories = [];
  207. categoriesData.forEach(parent => {
  208. if (parent && parent.children && Array.isArray(parent.children)) {
  209. // 确保每个子分类都有必要的字段
  210. const validChildren = parent.children.filter(child =>
  211. child && child.id && (child.title || child.name)
  212. ).map(child => ({
  213. id: child.id,
  214. name: child.title || child.name // 使用title或name作为分类名称
  215. }));
  216. subCategories = [...subCategories, ...validChildren];
  217. }
  218. });
  219. // 添加"全部"分类
  220. this.categories = [
  221. { id: 0, name: '全部' },
  222. ...subCategories
  223. ];
  224. console.log('处理后的分类:', this.categories);
  225. } else {
  226. console.warn('分类数据结构异常,使用默认分类', responseData);
  227. this.categories = [{ id: 0, name: '全部' }];
  228. }
  229. } catch (e) {
  230. console.error('加载分类失败', e);
  231. this.categories = [{ id: 0, name: '全部' }];
  232. }
  233. },
  234. // 修复热门小说加载逻辑
  235. async loadHotNovels() {
  236. try {
  237. const res = await this.$http.get('/novel/hot', {
  238. header: { isToken: false },
  239. timeout: 10000
  240. });
  241. console.log('热门小说API完整响应:', res);
  242. // 使用统一的响应解析方法
  243. const responseData = this.parseResponse(res);
  244. console.log('处理后的热门小说响应:', responseData);
  245. // 处理API返回的数据格式
  246. if (responseData.code === 200) {
  247. if (Array.isArray(responseData.rows)) {
  248. this.hotNovels = responseData.rows;
  249. } else if (Array.isArray(responseData.data)) {
  250. this.hotNovels = responseData.data;
  251. } else {
  252. console.warn('热门小说数据格式异常', responseData);
  253. this.hotNovels = this.mockHotNovels;
  254. }
  255. } else {
  256. console.warn('热门小说API返回异常状态码', responseData);
  257. this.hotNovels = this.mockHotNovels;
  258. }
  259. // 只在真实数据不可用时使用模拟数据
  260. if (!this.hotNovels || this.hotNovels.length === 0) {
  261. console.warn('使用模拟热门小说数据');
  262. this.hotNovels = this.mockHotNovels;
  263. }
  264. } catch (e) {
  265. console.error('加载热门小说失败', e);
  266. this.hotNovels = this.mockHotNovels;
  267. }
  268. },
  269. // 修复小说列表加载逻辑
  270. async loadNovels(reset = false) {
  271. if (reset) {
  272. this.page = 1;
  273. this.hasMore = true;
  274. this.novels = [];
  275. }
  276. if (!this.hasMore) return;
  277. this.loading = true;
  278. this.error = null;
  279. try {
  280. const res = await this.$http.get('/novel/list', {
  281. params: {
  282. page: this.page,
  283. pageSize: this.pageSize,
  284. categoryId: this.activeCategory
  285. },
  286. header: { isToken: false },
  287. timeout: 15000
  288. });
  289. console.log('小说列表API完整响应:', res);
  290. // 使用统一的响应解析方法
  291. const responseData = this.parseResponse(res);
  292. console.log('处理后的小说列表响应:', responseData);
  293. let newNovels = [];
  294. let total = 0;
  295. // 处理API返回的数据格式
  296. if (responseData.code === 200) {
  297. if (Array.isArray(responseData.rows)) {
  298. newNovels = responseData.rows;
  299. total = responseData.total || 0;
  300. } else if (Array.isArray(responseData.data)) {
  301. newNovels = responseData.data;
  302. total = responseData.total || newNovels.length;
  303. }
  304. } else if (responseData.code === 500) {
  305. // 处理服务器错误
  306. console.error('服务器内部错误:', responseData.msg);
  307. throw new Error('服务器内部错误,请稍后重试');
  308. }
  309. if (newNovels.length > 0) {
  310. this.novels = [...this.novels, ...newNovels];
  311. this.hasMore = this.novels.length < total;
  312. this.page++;
  313. // 修复: 确保图片URL正确
  314. this.novels.forEach(novel => {
  315. if (novel.coverImg) {
  316. novel.coverImg = this.getCoverUrl(novel.coverImg);
  317. }
  318. });
  319. } else if (this.page === 1) {
  320. console.warn('小说列表为空,使用模拟数据');
  321. this.novels = this.mockNovels; // 使用模拟数据
  322. }
  323. } catch (e) {
  324. console.error('加载小说失败', e);
  325. if (e.errMsg && e.errMsg.includes('timeout')) {
  326. this.error = '网络请求超时,请检查网络连接';
  327. } else if (e.message && e.message.includes('服务器内部错误')) {
  328. this.error = '服务器暂时不可用,请稍后重试';
  329. } else {
  330. this.error = '加载失败,请重试';
  331. }
  332. this.novels = this.mockNovels; // 回退到模拟数据
  333. } finally {
  334. this.loading = false;
  335. }
  336. },
  337. // 暂时禁用广告加载,避免404错误
  338. async loadAds() {
  339. try {
  340. console.log('广告功能暂未开放');
  341. this.topAds = [];
  342. this.bottomAds = [];
  343. // 以下是原有的广告加载代码,暂时注释掉
  344. /*
  345. // 使用Promise.all并行请求
  346. const [topRes, bottomRes] = await Promise.all([
  347. this.$http.get('/ad/position?code=TOP_BANNER', {
  348. header: { isToken: false }
  349. }),
  350. this.$http.get('/ad/position?code=BOTTOM_BANNER', {
  351. header: { isToken: false }
  352. })
  353. ]);
  354. // 处理广告响应
  355. this.topAds = this.parseAdResponse(topRes);
  356. this.bottomAds = this.parseAdResponse(bottomRes);
  357. */
  358. } catch (e) {
  359. console.error('加载广告失败', e);
  360. this.topAds = [];
  361. this.bottomAds = [];
  362. }
  363. },
  364. // 广告响应解析方法 - 简化逻辑
  365. parseAdResponse(response) {
  366. if (!response || !response.data) return [];
  367. const responseData = response.data;
  368. // 统一处理可能的响应格式
  369. if (responseData.code === 200 && Array.isArray(responseData.data)) {
  370. return responseData.data;
  371. } else if (Array.isArray(responseData)) {
  372. return responseData;
  373. } else if (responseData.code === 200 && Array.isArray(responseData.rows)) {
  374. return responseData.rows;
  375. }
  376. return [];
  377. },
  378. // 修复封面URL处理
  379. getCoverUrl(cover) {
  380. if (!cover) return '/static/default-cover.jpg';
  381. // 如果已经是完整URL,直接返回
  382. if (cover.startsWith('http')) return cover;
  383. // 如果是相对路径,添加基础URL
  384. if (cover.startsWith('/')) {
  385. return `${process.env.VUE_APP_BASE_URL || ''}${cover}`;
  386. }
  387. // 其他情况,直接返回
  388. return cover;
  389. },
  390. // 添加图片错误处理
  391. handleImageError(event) {
  392. console.log('图片加载失败:', event);
  393. event.target.src = '/static/default-cover.jpg';
  394. },
  395. // 其他方法...
  396. changeCategory(categoryId) {
  397. this.activeCategory = categoryId;
  398. const category = this.categories.find(c => c.id === categoryId);
  399. this.currentCategoryName = category ? category.name : '全部';
  400. this.loadNovels(true); // 重置并加载新分类
  401. },
  402. applyAuthor() {
  403. if (!this.$store.getters.token) {
  404. uni.showToast({ title: '请先登录', icon: 'none' });
  405. uni.navigateTo({ url: '/pages/login' });
  406. return;
  407. }
  408. uni.navigateTo({ url: '/pages/author/apply' });
  409. },
  410. goMore() {
  411. uni.navigateTo({
  412. url: '/pages/novel/more'
  413. });
  414. },
  415. // 确保重新加载按钮绑定到正确的方法
  416. reloadData() {
  417. this.initData();
  418. },
  419. // 添加上拉加载事件
  420. onReachBottom() {
  421. this.loadNovels();
  422. }
  423. }
  424. }
  425. </script>
  426. <style scoped>
  427. /* 添加加载状态样式 */
  428. .loading-container {
  429. display: flex;
  430. flex-direction: column;
  431. align-items: center;
  432. justify-content: center;
  433. padding: 100rpx 0;
  434. }
  435. .loading-icon {
  436. animation: rotate 1s linear infinite;
  437. margin-bottom: 20rpx;
  438. }
  439. @keyframes rotate {
  440. from { transform: rotate(0deg); }
  441. to { transform: rotate(360deg); }
  442. }
  443. /* 添加空状态样式 */
  444. .empty-container, .error-container {
  445. display: flex;
  446. flex-direction: column;
  447. align-items: center;
  448. justify-content: center;
  449. padding: 100rpx 0;
  450. text-align: center;
  451. }
  452. .empty-img {
  453. width: 200rpx;
  454. height: 200rpx;
  455. opacity: 0.6;
  456. margin-bottom: 40rpx;
  457. }
  458. .empty-text, .error-text {
  459. font-size: 32rpx;
  460. color: var(--text-color);
  461. margin-bottom: 40rpx;
  462. }
  463. .error-text {
  464. color: var(--error-color);
  465. }
  466. .btn-refresh {
  467. background-color: var(--primary-color);
  468. color: white;
  469. width: 60%;
  470. border-radius: 50rpx;
  471. }
  472. /* 确保网格布局正确 */
  473. .novel-grid {
  474. display: grid;
  475. grid-template-columns: repeat(3, 1fr);
  476. gap: 20rpx;
  477. padding: 20rpx;
  478. }
  479. .novel-item {
  480. background-color: white;
  481. border-radius: 12rpx;
  482. overflow: hidden;
  483. box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
  484. }
  485. /* 确保封面有默认背景和固定比例 */
  486. .novel-cover, .hot-cover {
  487. width: 100%;
  488. height: 300rpx;
  489. background-color: #f5f5f5;
  490. object-fit: cover;
  491. }
  492. .novel-title {
  493. display: block;
  494. font-size: 28rpx;
  495. padding: 10rpx 15rpx;
  496. white-space: nowrap;
  497. overflow: hidden;
  498. text-overflow: ellipsis;
  499. }
  500. .novel-author {
  501. display: block;
  502. font-size: 24rpx;
  503. color: #888;
  504. padding: 0 15rpx 15rpx;
  505. }
  506. /* 热门列表样式 */
  507. .hot-list {
  508. display: flex;
  509. padding: 20rpx;
  510. overflow-x: auto;
  511. white-space: nowrap;
  512. }
  513. .hot-item {
  514. display: inline-block;
  515. width: 200rpx;
  516. margin-right: 30rpx;
  517. }
  518. .hot-cover {
  519. width: 200rpx;
  520. height: 280rpx;
  521. border-radius: 8rpx;
  522. }
  523. .hot-title {
  524. display: block;
  525. margin-top: 10rpx;
  526. font-size: 26rpx;
  527. white-space: nowrap;
  528. overflow: hidden;
  529. text-overflow: ellipsis;
  530. }
  531. /* 分类导航样式 */
  532. .category-nav {
  533. white-space: nowrap;
  534. padding: 20rpx 0;
  535. background-color: #fff;
  536. border-bottom: 1rpx solid #eee;
  537. }
  538. .category-item {
  539. display: inline-block;
  540. padding: 10rpx 30rpx;
  541. margin: 0 10rpx;
  542. border-radius: 30rpx;
  543. font-size: 28rpx;
  544. }
  545. .category-item.active {
  546. background-color: var(--primary-color);
  547. color: white;
  548. }
  549. /* 章节标题样式 */
  550. .section {
  551. margin-top: 30rpx;
  552. }
  553. .section-header {
  554. display: flex;
  555. justify-content: space-between;
  556. align-items: center;
  557. padding: 0 20rpx 20rpx;
  558. }
  559. .section-title {
  560. font-size: 32rpx;
  561. font-weight: bold;
  562. }
  563. .section-more {
  564. font-size: 26rpx;
  565. color: var(--text-secondary-color);
  566. }
  567. /* 成为作家按钮样式 */
  568. .become-author {
  569. margin: 40rpx 20rpx;
  570. padding: 20rpx;
  571. text-align: center;
  572. background-color: var(--primary-color);
  573. color: white;
  574. border-radius: 10rpx;
  575. font-size: 30rpx;
  576. }
  577. </style>