├── php-api/ # 改造后的PHP接口层 ├── java-ad-service/ # 若依框架微服务(广告+VIP+分账) ├── uniapp-reader/ # UniApp前端项目 │ ├── pages/ # 各端页面 │ └──
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

list.vue 15KB

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