Quellcode durchsuchen

首页列表展示

master
DESKTOP-8FESTBI\Administrator vor 7 Monaten
Ursprung
Commit
cd3aa45dc6
1 geänderte Dateien mit 151 neuen und 65 gelöschten Zeilen
  1. 151
    65
      RuoYi-App/pages/novel/list.vue

+ 151
- 65
RuoYi-App/pages/novel/list.vue Datei anzeigen

@@ -154,26 +154,10 @@ export default {
154 154
   methods: {
155 155
     async initData() {
156 156
       try {
157
-        // 确保所有方法都已绑定
158
-        if (
159
-          typeof this.loadCategories !== 'function' ||
160
-          typeof this.loadHotNovels !== 'function' ||
161
-          typeof this.loadNovels !== 'function' ||
162
-          typeof this.loadAds !== 'function'
163
-        ) {
164
-          console.error('一个或多个方法未绑定!');
165
-          this.error = '系统初始化失败,请刷新页面';
166
-          this.loading = false;
167
-          return;
168
-        }
169
-        
170
-        // 并行加载数据以提高性能
171
-        await Promise.all([
172
-          this.loadCategories(),
173
-          this.loadHotNovels(),
174
-          this.loadNovels(),
175
-          this.loadAds()
176
-        ]);
157
+        // 改为顺序加载,便于调试
158
+        await this.loadCategories();
159
+        await this.loadHotNovels();
160
+        await this.loadNovels();
177 161
       } catch (e) {
178 162
         console.error('初始化失败', e);
179 163
         this.error = '初始化失败,请检查网络连接';
@@ -182,22 +166,70 @@ export default {
182 166
       }
183 167
     },
184 168
 
185
-    // 修复API请求逻辑
169
+    // 修复响应解析方法
170
+    parseResponse(res) {
171
+      console.log('原始响应对象:', res);
172
+      
173
+      // 处理不同的响应格式
174
+      let responseData = {};
175
+      
176
+      // 如果res有arg属性,使用arg作为响应数据
177
+      if (res && res.arg) {
178
+        responseData = res.arg;
179
+        console.log('使用res.arg作为响应数据:', responseData);
180
+      }
181
+      // 如果res有data属性,使用data作为响应数据
182
+      else if (res && res.data) {
183
+        responseData = res.data;
184
+        console.log('使用res.data作为响应数据:', responseData);
185
+      }
186
+      // 如果res本身就是响应数据
187
+      else {
188
+        responseData = res;
189
+        console.log('使用res本身作为响应数据:', responseData);
190
+      }
191
+      
192
+      return responseData;
193
+    },
194
+
195
+    // 修复分类加载逻辑
186 196
     async loadCategories() {
187 197
       try {
188 198
         const res = await this.$http.get('/category/tree', {
189
-          header: { isToken: false }
199
+          header: { isToken: false },
200
+          timeout: 10000
190 201
         });
191 202
         
192
-        console.log('分类API响应:', res);
203
+        console.log('分类API完整响应:', res);
204
+        
205
+        // 使用统一的响应解析方法
206
+        const responseData = this.parseResponse(res);
207
+        console.log('处理后的分类响应数据:', responseData);
193 208
         
194
-        // 确保正确解析API响应
195
-        if (res && res.data && res.data.code === 200 && Array.isArray(res.data.data)) {
196
-          // 提取所有子分类(第二级分类)
209
+        // 修复: 检查不同的响应格式
210
+        let categoriesData = [];
211
+        
212
+        if (responseData.code === 200 && Array.isArray(responseData.data)) {
213
+          categoriesData = responseData.data;
214
+        } else if (Array.isArray(responseData)) {
215
+          categoriesData = responseData;
216
+        } else if (responseData.code === 200 && Array.isArray(responseData.rows)) {
217
+          categoriesData = responseData.rows;
218
+        }
219
+        
220
+        if (categoriesData.length > 0) {
197 221
           let subCategories = [];
198
-          res.data.data.forEach(parent => {
199
-            if (parent.children && Array.isArray(parent.children)) {
200
-              subCategories = subCategories.concat(parent.children);
222
+          categoriesData.forEach(parent => {
223
+            if (parent && parent.children && Array.isArray(parent.children)) {
224
+              // 确保每个子分类都有必要的字段
225
+              const validChildren = parent.children.filter(child => 
226
+                child && child.id && (child.title || child.name)
227
+              ).map(child => ({
228
+                id: child.id,
229
+                name: child.title || child.name // 使用title或name作为分类名称
230
+              }));
231
+              
232
+              subCategories = [...subCategories, ...validChildren];
201 233
             }
202 234
           });
203 235
           
@@ -206,33 +238,45 @@ export default {
206 238
             { id: 0, name: '全部' }, 
207 239
             ...subCategories
208 240
           ];
241
+          
242
+          console.log('处理后的分类:', this.categories);
209 243
         } else {
210
-          console.warn('分类数据结构异常,使用默认分类');
244
+          console.warn('分类数据结构异常,使用默认分类', responseData);
211 245
           this.categories = [{ id: 0, name: '全部' }];
212
-        }
246
+        }   
213 247
       } catch (e) {
214 248
         console.error('加载分类失败', e);
215 249
         this.categories = [{ id: 0, name: '全部' }];
216 250
       }
217 251
     },
218 252
 
253
+    // 修复热门小说加载逻辑
219 254
     async loadHotNovels() {
220 255
       try {
221 256
         const res = await this.$http.get('/novel/hot', {
222
-          header: { isToken: false }
257
+          header: { isToken: false },
258
+          timeout: 10000
223 259
         });
224 260
         
225
-        console.log('热门小说API响应:', res);
261
+        console.log('热门小说API完整响应:', res);
262
+        
263
+        // 使用统一的响应解析方法
264
+        const responseData = this.parseResponse(res);
265
+        console.log('处理后的热门小说响应:', responseData);
226 266
         
227
-        // 处理不同API响应格式
228
-        if (res && res.data) {
229
-          if (res.data.code === 200 && Array.isArray(res.data.rows)) {
230
-            this.hotNovels = res.data.rows;
231
-          } else if (Array.isArray(res.data)) {
232
-            this.hotNovels = res.data;
267
+        // 处理API返回的数据格式
268
+        if (responseData.code === 200) {
269
+          if (Array.isArray(responseData.rows)) {
270
+            this.hotNovels = responseData.rows;
271
+          } else if (Array.isArray(responseData.data)) {
272
+            this.hotNovels = responseData.data;
233 273
           } else {
234
-            console.error('热门小说数据结构异常');
274
+            console.warn('热门小说数据格式异常', responseData);
275
+            this.hotNovels = this.mockHotNovels;
235 276
           }
277
+        } else {
278
+          console.warn('热门小说API返回异常状态码', responseData);
279
+          this.hotNovels = this.mockHotNovels;
236 280
         }
237 281
         
238 282
         // 只在真实数据不可用时使用模拟数据
@@ -246,6 +290,7 @@ export default {
246 290
       }
247 291
     },
248 292
 
293
+    // 修复小说列表加载逻辑
249 294
     async loadNovels(reset = false) {
250 295
       if (reset) {
251 296
         this.page = 1;
@@ -265,47 +310,73 @@ export default {
265 310
             pageSize: this.pageSize,
266 311
             categoryId: this.activeCategory
267 312
           },
268
-          header: { isToken: false }
313
+          header: { isToken: false },
314
+          timeout: 15000
269 315
         });
270 316
         
271
-        console.log('小说列表API响应:', res);
317
+        console.log('小说列表API完整响应:', res);
318
+        
319
+        // 使用统一的响应解析方法
320
+        const responseData = this.parseResponse(res);
321
+        console.log('处理后的小说列表响应:', responseData);
272 322
         
273 323
         let newNovels = [];
274 324
         let total = 0;
275 325
         
276
-        // 处理不同API响应格式
277
-        if (res && res.data) {
278
-          if (res.data.code === 200 && Array.isArray(res.data.rows)) {
279
-            newNovels = res.data.rows;
280
-            total = res.data.total;
281
-          } 
282
-          else if (Array.isArray(res.data)) {
283
-            newNovels = res.data;
284
-            total = res.data.length;
326
+        // 处理API返回的数据格式
327
+        if (responseData.code === 200) {
328
+          if (Array.isArray(responseData.rows)) {
329
+            newNovels = responseData.rows;
330
+            total = responseData.total || 0;
331
+          } else if (Array.isArray(responseData.data)) {
332
+            newNovels = responseData.data;
333
+            total = responseData.total || newNovels.length;
285 334
           }
335
+        } else if (responseData.code === 500) {
336
+          // 处理服务器错误
337
+          console.error('服务器内部错误:', responseData.msg);
338
+          throw new Error('服务器内部错误,请稍后重试');
286 339
         }
287 340
         
288 341
         if (newNovels.length > 0) {
289 342
           this.novels = [...this.novels, ...newNovels];
290 343
           this.hasMore = this.novels.length < total;
291 344
           this.page++;
345
+          
346
+          // 修复: 确保图片URL正确
347
+          this.novels.forEach(novel => {
348
+            if (novel.coverImg) {
349
+              novel.coverImg = this.getCoverUrl(novel.coverImg);
350
+            }
351
+          });
292 352
         } else if (this.page === 1) {
293
-          console.warn('小说列表为空');
294
-          this.novels = [];
353
+          console.warn('小说列表为空,使用模拟数据');
354
+          this.novels = this.mockNovels; // 使用模拟数据
295 355
         }
296 356
       } catch (e) {
297 357
         console.error('加载小说失败', e);
298
-        this.error = '加载失败,请重试';
299
-        if (this.novels.length === 0) {
300
-          this.novels = this.mockNovels;
358
+        if (e.errMsg && e.errMsg.includes('timeout')) {
359
+          this.error = '网络请求超时,请检查网络连接';
360
+        } else if (e.message && e.message.includes('服务器内部错误')) {
361
+          this.error = '服务器暂时不可用,请稍后重试';
362
+        } else {
363
+          this.error = '加载失败,请重试';
301 364
         }
365
+        this.novels = this.mockNovels; // 回退到模拟数据
302 366
       } finally {
303 367
         this.loading = false;
304 368
       }
305 369
     },
306 370
 
371
+    // 暂时禁用广告加载,避免404错误
307 372
     async loadAds() {
308 373
       try {
374
+        console.log('广告功能暂未开放');
375
+        this.topAds = [];
376
+        this.bottomAds = [];
377
+        
378
+        // 以下是原有的广告加载代码,暂时注释掉
379
+        /*
309 380
         // 使用Promise.all并行请求
310 381
         const [topRes, bottomRes] = await Promise.all([
311 382
           this.$http.get('/ad/position?code=TOP_BANNER', {
@@ -319,21 +390,27 @@ export default {
319 390
         // 处理广告响应
320 391
         this.topAds = this.parseAdResponse(topRes);
321 392
         this.bottomAds = this.parseAdResponse(bottomRes);
393
+        */
322 394
       } catch (e) {
323 395
         console.error('加载广告失败', e);
396
+        this.topAds = [];
397
+        this.bottomAds = [];
324 398
       }
325 399
     },
326 400
     
327
-    // 新增:广告响应解析方法
401
+    // 广告响应解析方法 - 简化逻辑
328 402
     parseAdResponse(response) {
329 403
       if (!response || !response.data) return [];
330 404
       
331
-      if (response.data.code === 200 && Array.isArray(response.data.data)) {
332
-        return response.data.data;
333
-      } else if (Array.isArray(response.data)) {
334
-        return response.data;
335
-      } else if (Array.isArray(response.data.rows)) {
336
-        return response.data.rows;
405
+      const responseData = response.data;
406
+      
407
+      // 统一处理可能的响应格式
408
+      if (responseData.code === 200 && Array.isArray(responseData.data)) {
409
+        return responseData.data;
410
+      } else if (Array.isArray(responseData)) {
411
+        return responseData;
412
+      } else if (responseData.code === 200 && Array.isArray(responseData.rows)) {
413
+        return responseData.rows;
337 414
       }
338 415
       
339 416
       return [];
@@ -342,13 +419,22 @@ export default {
342 419
     // 修复封面URL处理
343 420
     getCoverUrl(cover) {
344 421
       if (!cover) return '/static/default-cover.jpg';
422
+      
423
+      // 如果已经是完整URL,直接返回
345 424
       if (cover.startsWith('http')) return cover;
346
-      if (cover.startsWith('/')) return cover;
347
-      return `${process.env.VUE_APP_BASE_URL || ''}${cover}`;
425
+      
426
+      // 如果是相对路径,添加基础URL
427
+      if (cover.startsWith('/')) {
428
+        return `${process.env.VUE_APP_BASE_URL || ''}${cover}`;
429
+      }
430
+      
431
+      // 其他情况,直接返回
432
+      return cover;
348 433
     },
349 434
     
350 435
     // 添加图片错误处理
351 436
     handleImageError(event) {
437
+      console.log('图片加载失败:', event);
352 438
       event.target.src = '/static/default-cover.jpg';
353 439
     },
354 440
     

Laden…
Abbrechen
Speichern