├── 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 20KB

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