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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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="reloadData">重新加载</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. import urls from '@/api/urls';
  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: '总裁的替身前妻', cover: '/static/demo-cover1.jpg' },
  107. { id: 2, title: '神医毒妃', cover: '/static/demo-cover2.jpg' },
  108. { id: 3, title: '穿越之王妃逆袭', cover: '/static/demo-cover3.jpg' }
  109. ],
  110. mockNovels: [
  111. { id: 4, title: '都市最强赘婿', author: '网络作家', cover: '/static/demo-cover4.jpg' },
  112. { id: 5, title: '修仙归来', author: '仙侠迷', cover: '/static/demo-cover5.jpg' },
  113. { id: 6, title: '校园纯爱物语', author: '青春校园', cover: '/static/demo-cover6.jpg' }
  114. ]
  115. }
  116. },
  117. mounted() {
  118. console.log('$http available in component:', typeof this.$http.get === 'function');
  119. // 确保所有方法都已绑定
  120. console.log('loadCategories:', typeof this.loadCategories);
  121. console.log('loadHotNovels:', typeof this.loadHotNovels);
  122. console.log('loadNovels:', typeof this.loadNovels);
  123. console.log('loadAds:', typeof this.loadAds);
  124. this.initData();
  125. },
  126. methods: {
  127. async initData() {
  128. // 确保所有方法都已绑定
  129. if (
  130. typeof this.loadCategories !== 'function' ||
  131. typeof this.loadHotNovels !== 'function' ||
  132. typeof this.loadNovels !== 'function' ||
  133. typeof this.loadAds !== 'function'
  134. ) {
  135. console.error('一个或多个方法未绑定!');
  136. this.error = '系统初始化失败,请刷新页面';
  137. this.loading = false;
  138. return;
  139. }
  140. try {
  141. // 顺序加载数据
  142. await this.loadCategories();
  143. await this.loadHotNovels();
  144. await this.loadNovels();
  145. await this.loadAds();
  146. } catch (e) {
  147. console.error('初始化失败', e);
  148. this.error = '初始化失败,请检查网络连接';
  149. } finally {
  150. this.loading = false;
  151. }
  152. },
  153. // 加载分类
  154. async loadCategories() {
  155. try {
  156. // 使用正确的参数格式
  157. const res = await this.$http.get('/category/tree', {
  158. header: { isToken: false }
  159. });
  160. this.categories = res && res.data ? [{ id: 0, name: '全部' }, ...res.data] : [];
  161. } catch (e) {
  162. console.error('加载分类失败', e);
  163. this.categories = [{ id: 0, name: '全部' }];
  164. }
  165. },
  166. // 加载热门小说
  167. async loadHotNovels() {
  168. try {
  169. // 公开API,不需要认证
  170. const res = await this.$http.get('/novel/hot', {
  171. header: { isToken: false }
  172. });
  173. // 处理响应
  174. if (res && res.data && res.data.rows) {
  175. this.hotNovels = res.data.rows;
  176. } else if (res && res.data && Array.isArray(res.data)) {
  177. this.hotNovels = res.data;
  178. } else {
  179. console.warn('热门小说API返回格式未知', res);
  180. this.hotNovels = this.mockHotNovels;
  181. }
  182. } catch (e) {
  183. console.error('加载热门小说失败', e);
  184. this.hotNovels = this.mockHotNovels;
  185. }
  186. },
  187. // 加载小说列表
  188. async loadNovels() {
  189. this.loading = true;
  190. this.error = null;
  191. try {
  192. // 使用正确的请求方式
  193. //const res = await this.$http.get('/novel/list');
  194. const res = await this.$http.get('/novel/list', {
  195. header: { isToken: false }
  196. });
  197. // 修复可选链操作符问题
  198. if (res && res.data && res.data.rows) {
  199. this.novels = res.data.rows;
  200. } else if (res && res.data && Array.isArray(res.data)) {
  201. this.novels = res.data;
  202. } else {
  203. console.warn('小说列表API返回格式未知', res);
  204. this.novels = this.mockNovels;
  205. }
  206. } catch (e) {
  207. console.error('加载小说失败', e);
  208. this.error = '加载失败,请重试';
  209. this.novels = this.mockNovels;
  210. } finally {
  211. this.loading = false;
  212. }
  213. },
  214. // 加载广告
  215. async loadAds() {
  216. try {
  217. const [topRes, bottomRes] = await Promise.all([
  218. this.$http.get('/ad/position?code=TOP_BANNER', {
  219. header: { isToken: false }
  220. }),
  221. this.$http.get('/ad/position?code=BOTTOM_BANNER', {
  222. header: { isToken: false }
  223. })
  224. ]);
  225. this.topAds = topRes && topRes.data ? topRes.data : [];
  226. this.bottomAds = bottomRes && bottomRes.data ? bottomRes.data : [];
  227. } catch (e) {
  228. console.error('加载广告失败', e);
  229. }
  230. },
  231. // 封面URL处理
  232. getCoverUrl(cover) {
  233. if (!cover) return '/static/default-cover.jpg';
  234. if (cover.startsWith('http')) return cover;
  235. if (cover.startsWith('/')) return cover;
  236. return `${process.env.VUE_APP_BASE_URL || ''}/uploads/${cover}`;
  237. },
  238. // 其他方法...
  239. changeCategory(categoryId) {
  240. this.activeCategory = categoryId;
  241. this.currentCategoryName = this.categories.find(c => c.id === categoryId)?.name || '全部';
  242. this.loadNovels();
  243. },
  244. openNovel(novel) {
  245. if (novel.id) {
  246. uni.navigateTo({
  247. url: `/pages/novel/detail?id=${novel.id}`
  248. });
  249. } else {
  250. uni.showToast({
  251. title: '小说ID不存在',
  252. icon: 'none'
  253. });
  254. }
  255. },
  256. applyAuthor() {
  257. if (!this.$store.getters.token) {
  258. uni.showToast({ title: '请先登录', icon: 'none' });
  259. uni.navigateTo({ url: '/pages/login' });
  260. return;
  261. }
  262. uni.navigateTo({ url: '/pages/author/apply' });
  263. },
  264. goMore() {
  265. uni.navigateTo({
  266. url: '/pages/novel/more'
  267. });
  268. },
  269. // 确保重新加载按钮绑定到正确的方法
  270. reloadData() {
  271. this.initData();
  272. }
  273. }
  274. }
  275. </script>
  276. <style scoped>
  277. /* 添加加载状态样式 */
  278. .loading-container {
  279. display: flex;
  280. flex-direction: column;
  281. align-items: center;
  282. justify-content: center;
  283. padding: 100rpx 0;
  284. }
  285. .loading-icon {
  286. animation: rotate 1s linear infinite;
  287. margin-bottom: 20rpx;
  288. }
  289. @keyframes rotate {
  290. from { transform: rotate(0deg); }
  291. to { transform: rotate(360deg); }
  292. }
  293. /* 添加空状态样式 */
  294. .empty-container, .error-container {
  295. display: flex;
  296. flex-direction: column;
  297. align-items: center;
  298. justify-content: center;
  299. padding: 100rpx 0;
  300. text-align: center;
  301. }
  302. .empty-img {
  303. width: 200rpx;
  304. height: 200rpx;
  305. opacity: 0.6;
  306. margin-bottom: 40rpx;
  307. }
  308. .empty-text, .error-text {
  309. font-size: 32rpx;
  310. color: var(--text-color);
  311. margin-bottom: 40rpx;
  312. }
  313. .error-text {
  314. color: var(--error-color);
  315. }
  316. .btn-refresh {
  317. background-color: var(--primary-color);
  318. color: white;
  319. width: 60%;
  320. border-radius: 50rpx;
  321. }
  322. /* 确保网格布局正确 */
  323. .novel-grid {
  324. display: grid;
  325. grid-template-columns: repeat(3, 1fr);
  326. gap: 20rpx;
  327. padding: 20rpx;
  328. }
  329. .novel-item {
  330. background-color: var(--card-bg);
  331. border-radius: 12rpx;
  332. overflow: hidden;
  333. box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
  334. }
  335. .novel-cover {
  336. width: 100%;
  337. height: 300rpx;
  338. background-color: #f5f5f5; /* 添加默认背景 */
  339. }
  340. .novel-title {
  341. display: block;
  342. font-size: 28rpx;
  343. padding: 10rpx 15rpx;
  344. white-space: nowrap;
  345. overflow: hidden;
  346. text-overflow: ellipsis;
  347. }
  348. .novel-author {
  349. display: block;
  350. font-size: 24rpx;
  351. color: #888;
  352. padding: 0 15rpx 15rpx;
  353. }
  354. </style>