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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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="novels.length === 0 && !error" 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="loadNovels">重新加载</button>
  29. </view>
  30. <!-- 错误状态 -->
  31. <view v-if="error" 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="hotNovels.length > 0" class="section">
  38. <!-- ... -->
  39. </view>
  40. <view v-else class="section">
  41. <view class="section-header">
  42. <text class="section-title">热门推荐(演示数据)</text>
  43. <text class="section-more" @click="goMore">更多 ></text>
  44. </view>
  45. <scroll-view scroll-x class="hot-list">
  46. <view
  47. v-for="novel in demoHotNovels"
  48. :key="novel.id"
  49. class="hot-item"
  50. @click="openNovel(novel)"
  51. >
  52. <image :src="novel.cover" class="hot-cover" />
  53. <text class="hot-title">{{ novel.title }}</text>
  54. </view>
  55. </scroll-view>
  56. </view>
  57. <!-- 分类小说列表 -->
  58. <view v-if="novels.length > 0" class="section">
  59. <!-- ... -->
  60. </view>
  61. <view v-else class="section">
  62. <view class="section-header">
  63. <text class="section-title">小说列表(演示数据)</text>
  64. </view>
  65. <view class="novel-grid">
  66. <view
  67. v-for="novel in demoNovels"
  68. :key="novel.id"
  69. class="novel-item"
  70. @click="openNovel(novel)"
  71. >
  72. <image :src="novel.cover" class="novel-cover" />
  73. <text class="novel-title">{{ novel.title }}</text>
  74. <text class="novel-author">{{ novel.author }}</text>
  75. </view>
  76. </view>
  77. </view>
  78. <!-- 成为签约作家按钮 -->
  79. <view class="become-author" @click="applyAuthor">
  80. <text>成为签约作家,发布你的作品</text>
  81. </view>
  82. <!-- 底部广告 -->
  83. <ad-banner v-if="bottomAds.length" :ads="bottomAds" />
  84. </template>
  85. </view>
  86. </template>
  87. <script>
  88. // 确保路径正确
  89. import AdBanner from '@/components/AdBanner';
  90. export default {
  91. components: { AdBanner },
  92. data() {
  93. return {
  94. topAds: [],
  95. bottomAds: [],
  96. categories: [],
  97. activeCategory: 0,
  98. hotNovels: [],
  99. novels: [],
  100. loading: true,
  101. error: null,
  102. currentCategoryName: '全部',
  103. // 临时解决方案 - 模拟数据
  104. mockHotNovels: [
  105. { id: 1, title: '总裁的替身前妻', cover: '/static/demo-cover1.jpg' },
  106. { id: 2, title: '神医毒妃', cover: '/static/demo-cover2.jpg' },
  107. { id: 3, title: '穿越之王妃逆袭', cover: '/static/demo-cover3.jpg' }
  108. ],
  109. mockNovels: [
  110. { id: 4, title: '都市最强赘婿', author: '网络作家', cover: '/static/demo-cover4.jpg' },
  111. { id: 5, title: '修仙归来', author: '仙侠迷', cover: '/static/demo-cover5.jpg' },
  112. { id: 6, title: '校园纯爱物语', author: '青春校园', cover: '/static/demo-cover6.jpg' }
  113. ]
  114. }
  115. },
  116. mounted() {
  117. console.log('$http available in component:', typeof this.$http.get === 'function');
  118. // 确保所有方法都已绑定
  119. console.log('loadCategories:', typeof this.loadCategories);
  120. console.log('loadHotNovels:', typeof this.loadHotNovels);
  121. console.log('loadNovels:', typeof this.loadNovels);
  122. console.log('loadAds:', typeof this.loadAds);
  123. this.initData();
  124. },
  125. methods: {
  126. async initData() {
  127. // 确保所有方法都已绑定
  128. if (
  129. typeof this.loadCategories !== 'function' ||
  130. typeof this.loadHotNovels !== 'function' ||
  131. typeof this.loadNovels !== 'function' ||
  132. typeof this.loadAds !== 'function'
  133. ) {
  134. console.error('一个或多个方法未绑定!');
  135. this.error = '系统初始化失败,请刷新页面';
  136. this.loading = false;
  137. return;
  138. }
  139. try {
  140. // 顺序加载数据
  141. await this.loadCategories();
  142. await this.loadHotNovels();
  143. await this.loadNovels();
  144. await this.loadAds();
  145. } catch (e) {
  146. console.error('初始化失败', e);
  147. this.error = '初始化失败,请检查网络连接';
  148. } finally {
  149. this.loading = false;
  150. }
  151. },
  152. // 加载分类
  153. async loadCategories() {
  154. try {
  155. const res = await this.$http.get('/java-api/category/tree');
  156. this.categories = res?.data ? [{ id: 0, name: '全部' }, ...res.data] : [];
  157. } catch (e) {
  158. console.error('加载分类失败', e);
  159. this.categories = [{ id: 0, name: '全部' }];
  160. }
  161. },
  162. // 加载热门小说
  163. async loadHotNovels() {
  164. try {
  165. const res = await this.$http.get('/novel/hot');
  166. // 处理响应
  167. if (res?.data?.rows) {
  168. this.hotNovels = res.data.rows;
  169. } else if (Array.isArray(res?.data)) {
  170. this.hotNovels = res.data;
  171. } else {
  172. console.warn('热门小说API返回格式未知', res);
  173. this.hotNovels = this.mockHotNovels; // 使用模拟数据
  174. }
  175. } catch (e) {
  176. console.error('加载热门小说失败', e);
  177. this.hotNovels = this.mockHotNovels; // 使用模拟数据
  178. }
  179. },
  180. // 加载小说列表
  181. async loadNovels() {
  182. this.loading = true;
  183. this.error = null;
  184. try {
  185. const res = await this.$http.get('/novel/list');
  186. // 处理响应
  187. if (res?.data?.rows) {
  188. this.novels = res.data.rows;
  189. } else if (Array.isArray(res?.data)) {
  190. this.novels = res.data;
  191. } else {
  192. console.warn('小说列表API返回格式未知', res);
  193. this.novels = this.mockNovels; // 使用模拟数据
  194. }
  195. } catch (e) {
  196. console.error('加载小说失败', e);
  197. this.error = '加载失败,请重试';
  198. this.novels = this.mockNovels; // 使用模拟数据
  199. } finally {
  200. this.loading = false;
  201. }
  202. },
  203. // 加载广告
  204. async loadAds() {
  205. try {
  206. const [topRes, bottomRes] = await Promise.all([
  207. this.$http.get('/java-api/ad/position?code=TOP_BANNER'),
  208. this.$http.get('/java-api/ad/position?code=BOTTOM_BANNER')
  209. ]);
  210. this.topAds = topRes?.data || topRes || [];
  211. this.bottomAds = bottomRes?.data || bottomRes || [];
  212. } catch (e) {
  213. console.error('加载广告失败', e);
  214. }
  215. },
  216. // 封面URL处理
  217. getCoverUrl(cover) {
  218. if (!cover) return '/static/default-cover.jpg';
  219. if (cover.startsWith('http')) return cover;
  220. if (cover.startsWith('/')) return cover;
  221. return `${process.env.VUE_APP_BASE_URL || ''}/uploads/${cover}`;
  222. },
  223. // 其他方法...
  224. changeCategory(categoryId) {
  225. this.activeCategory = categoryId;
  226. this.currentCategoryName = this.categories.find(c => c.id === categoryId)?.name || '全部';
  227. this.loadNovels();
  228. },
  229. openNovel(novel) {
  230. if (novel.id) {
  231. uni.navigateTo({
  232. url: `/pages/novel/detail?id=${novel.id}`
  233. });
  234. } else {
  235. uni.showToast({
  236. title: '小说ID不存在',
  237. icon: 'none'
  238. });
  239. }
  240. },
  241. applyAuthor() {
  242. if (!this.$store.getters.token) {
  243. uni.showToast({ title: '请先登录', icon: 'none' });
  244. uni.navigateTo({ url: '/pages/login' });
  245. return;
  246. }
  247. uni.navigateTo({ url: '/pages/author/apply' });
  248. },
  249. goMore() {
  250. uni.navigateTo({
  251. url: '/pages/novel/more'
  252. });
  253. }
  254. }
  255. }
  256. </script>
  257. <style scoped>
  258. /* 添加加载状态样式 */
  259. .loading-container {
  260. display: flex;
  261. flex-direction: column;
  262. align-items: center;
  263. justify-content: center;
  264. padding: 100rpx 0;
  265. }
  266. .loading-icon {
  267. animation: rotate 1s linear infinite;
  268. margin-bottom: 20rpx;
  269. }
  270. @keyframes rotate {
  271. from { transform: rotate(0deg); }
  272. to { transform: rotate(360deg); }
  273. }
  274. /* 添加空状态样式 */
  275. .empty-container, .error-container {
  276. display: flex;
  277. flex-direction: column;
  278. align-items: center;
  279. justify-content: center;
  280. padding: 100rpx 0;
  281. text-align: center;
  282. }
  283. .empty-img {
  284. width: 200rpx;
  285. height: 200rpx;
  286. opacity: 0.6;
  287. margin-bottom: 40rpx;
  288. }
  289. .empty-text, .error-text {
  290. font-size: 32rpx;
  291. color: var(--text-color);
  292. margin-bottom: 40rpx;
  293. }
  294. .error-text {
  295. color: var(--error-color);
  296. }
  297. .btn-refresh {
  298. background-color: var(--primary-color);
  299. color: white;
  300. width: 60%;
  301. border-radius: 50rpx;
  302. }
  303. /* 确保网格布局正确 */
  304. .novel-grid {
  305. display: grid;
  306. grid-template-columns: repeat(3, 1fr);
  307. gap: 20rpx;
  308. padding: 20rpx;
  309. }
  310. .novel-item {
  311. background-color: var(--card-bg);
  312. border-radius: 12rpx;
  313. overflow: hidden;
  314. box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
  315. }
  316. .novel-cover {
  317. width: 100%;
  318. height: 300rpx;
  319. background-color: #f5f5f5; /* 添加默认背景 */
  320. }
  321. .novel-title {
  322. display: block;
  323. font-size: 28rpx;
  324. padding: 10rpx 15rpx;
  325. white-space: nowrap;
  326. overflow: hidden;
  327. text-overflow: ellipsis;
  328. }
  329. .novel-author {
  330. display: block;
  331. font-size: 24rpx;
  332. color: #888;
  333. padding: 0 15rpx 15rpx;
  334. }
  335. </style>