├── 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.

list.vue 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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. // 修复响应解析方法
  158. parseResponse(res) {
  159. console.log('原始响应对象:', res);
  160. // 处理不同的响应格式
  161. let responseData = {};
  162. // 如果res有arg属性,使用arg作为响应数据
  163. if (res && res.arg) {
  164. responseData = res.arg;
  165. console.log('使用res.arg作为响应数据:', responseData);
  166. }
  167. // 如果res有data属性,使用data作为响应数据
  168. else if (res && res.data) {
  169. responseData = res.data;
  170. console.log('使用res.data作为响应数据:', responseData);
  171. }
  172. // 如果res本身就是响应数据
  173. else {
  174. responseData = res;
  175. console.log('使用res本身作为响应数据:', responseData);
  176. }
  177. return responseData;
  178. },
  179. // 修复分类加载逻辑
  180. async loadCategories() {
  181. try {
  182. const res = await this.$http.get('/category/tree', {
  183. header: { isToken: false },
  184. timeout: 10000
  185. });
  186. console.log('分类API完整响应:', res);
  187. // 使用统一的响应解析方法
  188. const responseData = this.parseResponse(res);
  189. console.log('处理后的分类响应数据:', responseData);
  190. // 修复: 检查不同的响应格式
  191. let categoriesData = [];
  192. if (responseData.code === 200 && Array.isArray(responseData.data)) {
  193. categoriesData = responseData.data;
  194. } else if (Array.isArray(responseData)) {
  195. categoriesData = responseData;
  196. } else if (responseData.code === 200 && Array.isArray(responseData.rows)) {
  197. categoriesData = responseData.rows;
  198. }
  199. if (categoriesData.length > 0) {
  200. let subCategories = [];
  201. categoriesData.forEach(parent => {
  202. if (parent && parent.children && Array.isArray(parent.children)) {
  203. // 确保每个子分类都有必要的字段
  204. const validChildren = parent.children.filter(child =>
  205. child && child.id && (child.title || child.name)
  206. ).map(child => ({
  207. id: child.id,
  208. name: child.title || child.name // 使用title或name作为分类名称
  209. }));
  210. subCategories = [...subCategories, ...validChildren];
  211. }
  212. });
  213. // 添加"全部"分类
  214. this.categories = [
  215. { id: 0, name: '全部' },
  216. ...subCategories
  217. ];
  218. console.log('处理后的分类:', this.categories);
  219. } else {
  220. console.warn('分类数据结构异常,使用默认分类', responseData);
  221. this.categories = [{ id: 0, name: '全部' }];
  222. }
  223. } catch (e) {
  224. console.error('加载分类失败', e);
  225. this.categories = [{ id: 0, name: '全部' }];
  226. }
  227. },
  228. // 修复热门小说加载逻辑
  229. async loadHotNovels() {
  230. try {
  231. const res = await this.$http.get('/novel/hot', {
  232. header: { isToken: false },
  233. timeout: 10000
  234. });
  235. console.log('热门小说API完整响应:', res);
  236. // 使用统一的响应解析方法
  237. const responseData = this.parseResponse(res);
  238. console.log('处理后的热门小说响应:', responseData);
  239. // 处理API返回的数据格式
  240. if (responseData.code === 200) {
  241. if (Array.isArray(responseData.rows)) {
  242. this.hotNovels = responseData.rows;
  243. } else if (Array.isArray(responseData.data)) {
  244. this.hotNovels = responseData.data;
  245. } else {
  246. console.warn('热门小说数据格式异常', responseData);
  247. this.hotNovels = this.mockHotNovels;
  248. }
  249. } else {
  250. console.warn('热门小说API返回异常状态码', responseData);
  251. this.hotNovels = this.mockHotNovels;
  252. }
  253. // 只在真实数据不可用时使用模拟数据
  254. if (!this.hotNovels || this.hotNovels.length === 0) {
  255. console.warn('使用模拟热门小说数据');
  256. this.hotNovels = this.mockHotNovels;
  257. }
  258. } catch (e) {
  259. console.error('加载热门小说失败', e);
  260. this.hotNovels = this.mockHotNovels;
  261. }
  262. },
  263. // 修复小说列表加载逻辑
  264. async loadNovels(reset = false) {
  265. if (reset) {
  266. this.page = 1;
  267. this.hasMore = true;
  268. this.novels = [];
  269. }
  270. if (!this.hasMore) return;
  271. this.loading = true;
  272. this.error = null;
  273. try {
  274. const res = await this.$http.get('/novel/list', {
  275. params: {
  276. page: this.page,
  277. pageSize: this.pageSize,
  278. categoryId: this.activeCategory
  279. },
  280. header: { isToken: false },
  281. timeout: 15000
  282. });
  283. console.log('小说列表API完整响应:', res);
  284. // 使用统一的响应解析方法
  285. const responseData = this.parseResponse(res);
  286. console.log('处理后的小说列表响应:', responseData);
  287. let newNovels = [];
  288. let total = 0;
  289. // 处理API返回的数据格式
  290. if (responseData.code === 200) {
  291. if (Array.isArray(responseData.rows)) {
  292. newNovels = responseData.rows;
  293. total = responseData.total || 0;
  294. } else if (Array.isArray(responseData.data)) {
  295. newNovels = responseData.data;
  296. total = responseData.total || newNovels.length;
  297. }
  298. } else if (responseData.code === 500) {
  299. // 处理服务器错误
  300. console.error('服务器内部错误:', responseData.msg);
  301. throw new Error('服务器内部错误,请稍后重试');
  302. }
  303. if (newNovels.length > 0) {
  304. this.novels = [...this.novels, ...newNovels];
  305. this.hasMore = this.novels.length < total;
  306. this.page++;
  307. // 修复: 确保图片URL正确
  308. this.novels.forEach(novel => {
  309. if (novel.coverImg) {
  310. novel.coverImg = this.getCoverUrl(novel.coverImg);
  311. }
  312. });
  313. } else if (this.page === 1) {
  314. console.warn('小说列表为空,使用模拟数据');
  315. this.novels = this.mockNovels; // 使用模拟数据
  316. }
  317. } catch (e) {
  318. console.error('加载小说失败', e);
  319. if (e.errMsg && e.errMsg.includes('timeout')) {
  320. this.error = '网络请求超时,请检查网络连接';
  321. } else if (e.message && e.message.includes('服务器内部错误')) {
  322. this.error = '服务器暂时不可用,请稍后重试';
  323. } else {
  324. this.error = '加载失败,请重试';
  325. }
  326. this.novels = this.mockNovels; // 回退到模拟数据
  327. } finally {
  328. this.loading = false;
  329. }
  330. },
  331. // 暂时禁用广告加载,避免404错误
  332. async loadAds() {
  333. try {
  334. console.log('广告功能暂未开放');
  335. this.topAds = [];
  336. this.bottomAds = [];
  337. // 以下是原有的广告加载代码,暂时注释掉
  338. /*
  339. // 使用Promise.all并行请求
  340. const [topRes, bottomRes] = await Promise.all([
  341. this.$http.get('/ad/position?code=TOP_BANNER', {
  342. header: { isToken: false }
  343. }),
  344. this.$http.get('/ad/position?code=BOTTOM_BANNER', {
  345. header: { isToken: false }
  346. })
  347. ]);
  348. // 处理广告响应
  349. this.topAds = this.parseAdResponse(topRes);
  350. this.bottomAds = this.parseAdResponse(bottomRes);
  351. */
  352. } catch (e) {
  353. console.error('加载广告失败', e);
  354. this.topAds = [];
  355. this.bottomAds = [];
  356. }
  357. },
  358. // 广告响应解析方法 - 简化逻辑
  359. parseAdResponse(response) {
  360. if (!response || !response.data) return [];
  361. const responseData = response.data;
  362. // 统一处理可能的响应格式
  363. if (responseData.code === 200 && Array.isArray(responseData.data)) {
  364. return responseData.data;
  365. } else if (Array.isArray(responseData)) {
  366. return responseData;
  367. } else if (responseData.code === 200 && Array.isArray(responseData.rows)) {
  368. return responseData.rows;
  369. }
  370. return [];
  371. },
  372. // 修复封面URL处理
  373. getCoverUrl(cover) {
  374. if (!cover) return '/static/default-cover.jpg';
  375. // 如果已经是完整URL,直接返回
  376. if (cover.startsWith('http')) return cover;
  377. // 如果是相对路径,添加基础URL
  378. if (cover.startsWith('/')) {
  379. return `${process.env.VUE_APP_BASE_URL || ''}${cover}`;
  380. }
  381. // 其他情况,直接返回
  382. return cover;
  383. },
  384. // 添加图片错误处理
  385. handleImageError(event) {
  386. console.log('图片加载失败:', event);
  387. event.target.src = '/static/default-cover.jpg';
  388. },
  389. // 其他方法...
  390. changeCategory(categoryId) {
  391. this.activeCategory = categoryId;
  392. const category = this.categories.find(c => c.id === categoryId);
  393. this.currentCategoryName = category ? category.name : '全部';
  394. this.loadNovels(true); // 重置并加载新分类
  395. },
  396. openNovel(novel) {
  397. if (novel.id) {
  398. uni.navigateTo({
  399. url: `/pages/novel/detail?id=${novel.id}`
  400. });
  401. } else {
  402. uni.showToast({
  403. title: '小说ID不存在',
  404. icon: 'none'
  405. });
  406. }
  407. },
  408. applyAuthor() {
  409. if (!this.$store.getters.token) {
  410. uni.showToast({ title: '请先登录', icon: 'none' });
  411. uni.navigateTo({ url: '/pages/login' });
  412. return;
  413. }
  414. uni.navigateTo({ url: '/pages/author/apply' });
  415. },
  416. goMore() {
  417. uni.navigateTo({
  418. url: '/pages/novel/more'
  419. });
  420. },
  421. // 确保重新加载按钮绑定到正确的方法
  422. reloadData() {
  423. this.initData();
  424. },
  425. // 添加上拉加载事件
  426. onReachBottom() {
  427. this.loadNovels();
  428. }
  429. }
  430. }
  431. </script>
  432. <style scoped>
  433. /* 添加加载状态样式 */
  434. .loading-container {
  435. display: flex;
  436. flex-direction: column;
  437. align-items: center;
  438. justify-content: center;
  439. padding: 100rpx 0;
  440. }
  441. .loading-icon {
  442. animation: rotate 1s linear infinite;
  443. margin-bottom: 20rpx;
  444. }
  445. @keyframes rotate {
  446. from { transform: rotate(0deg); }
  447. to { transform: rotate(360deg); }
  448. }
  449. /* 添加空状态样式 */
  450. .empty-container, .error-container {
  451. display: flex;
  452. flex-direction: column;
  453. align-items: center;
  454. justify-content: center;
  455. padding: 100rpx 0;
  456. text-align: center;
  457. }
  458. .empty-img {
  459. width: 200rpx;
  460. height: 200rpx;
  461. opacity: 0.6;
  462. margin-bottom: 40rpx;
  463. }
  464. .empty-text, .error-text {
  465. font-size: 32rpx;
  466. color: var(--text-color);
  467. margin-bottom: 40rpx;
  468. }
  469. .error-text {
  470. color: var(--error-color);
  471. }
  472. .btn-refresh {
  473. background-color: var(--primary-color);
  474. color: white;
  475. width: 60%;
  476. border-radius: 50rpx;
  477. }
  478. /* 确保网格布局正确 */
  479. .novel-grid {
  480. display: grid;
  481. grid-template-columns: repeat(3, 1fr);
  482. gap: 20rpx;
  483. padding: 20rpx;
  484. }
  485. .novel-item {
  486. background-color: white;
  487. border-radius: 12rpx;
  488. overflow: hidden;
  489. box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
  490. }
  491. /* 确保封面有默认背景和固定比例 */
  492. .novel-cover, .hot-cover {
  493. width: 100%;
  494. height: 300rpx;
  495. background-color: #f5f5f5;
  496. object-fit: cover;
  497. }
  498. .novel-title {
  499. display: block;
  500. font-size: 28rpx;
  501. padding: 10rpx 15rpx;
  502. white-space: nowrap;
  503. overflow: hidden;
  504. text-overflow: ellipsis;
  505. }
  506. .novel-author {
  507. display: block;
  508. font-size: 24rpx;
  509. color: #888;
  510. padding: 0 15rpx 15rpx;
  511. }
  512. /* 热门列表样式 */
  513. .hot-list {
  514. display: flex;
  515. padding: 20rpx;
  516. overflow-x: auto;
  517. white-space: nowrap;
  518. }
  519. .hot-item {
  520. display: inline-block;
  521. width: 200rpx;
  522. margin-right: 30rpx;
  523. }
  524. .hot-cover {
  525. width: 200rpx;
  526. height: 280rpx;
  527. border-radius: 8rpx;
  528. }
  529. .hot-title {
  530. display: block;
  531. margin-top: 10rpx;
  532. font-size: 26rpx;
  533. white-space: nowrap;
  534. overflow: hidden;
  535. text-overflow: ellipsis;
  536. }
  537. /* 分类导航样式 */
  538. .category-nav {
  539. white-space: nowrap;
  540. padding: 20rpx 0;
  541. background-color: #fff;
  542. border-bottom: 1rpx solid #eee;
  543. }
  544. .category-item {
  545. display: inline-block;
  546. padding: 10rpx 30rpx;
  547. margin: 0 10rpx;
  548. border-radius: 30rpx;
  549. font-size: 28rpx;
  550. }
  551. .category-item.active {
  552. background-color: var(--primary-color);
  553. color: white;
  554. }
  555. /* 章节标题样式 */
  556. .section {
  557. margin-top: 30rpx;
  558. }
  559. .section-header {
  560. display: flex;
  561. justify-content: space-between;
  562. align-items: center;
  563. padding: 0 20rpx 20rpx;
  564. }
  565. .section-title {
  566. font-size: 32rpx;
  567. font-weight: bold;
  568. }
  569. .section-more {
  570. font-size: 26rpx;
  571. color: var(--text-secondary-color);
  572. }
  573. /* 成为作家按钮样式 */
  574. .become-author {
  575. margin: 40rpx 20rpx;
  576. padding: 20rpx;
  577. text-align: center;
  578. background-color: var(--primary-color);
  579. color: white;
  580. border-radius: 10rpx;
  581. font-size: 30rpx;
  582. }
  583. </style>