| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752 |
- <template>
- <view class="novel-list-page">
- <!-- 加载状态 -->
- <view v-if="loading" class="loading-container">
- <uni-icons type="spinner-cycle" size="36" color="var(--primary-color)" class="loading-icon" />
- <text>加载中...</text>
- </view>
-
- <!-- 内容区域 -->
- <template v-else>
- <!-- 顶部广告 -->
- <ad-banner v-if="topAds.length" :ads="topAds" />
-
- <!-- 顶部分类导航 -->
- <scroll-view scroll-x class="category-nav">
- <view
- v-for="category in categories"
- :key="category.id"
- class="category-item"
- :class="{ active: activeCategory === category.id }"
- @click="changeCategory(category.id)"
- >
- {{ category.name }}
- </view>
- </scroll-view>
-
- <!-- 空状态 -->
- <view v-if="showEmptyState" class="empty-container">
- <image src="/static/empty-book.png" class="empty-img" />
- <text class="empty-text">暂无小说数据</text>
- <button class="btn-refresh" @click="reloadData">重新加载</button>
- </view>
-
- <!-- 错误状态 -->
- <view v-if="error && !showNovels" class="error-container">
- <uni-icons type="info" size="48" color="var(--error-color)" />
- <text class="error-text">{{ error }}</text>
- <button class="btn-refresh" @click="loadNovels">重新加载</button>
- </view>
-
- <!-- 热门推荐 -->
- <view v-if="showHotNovels" class="section">
- <view class="section-header">
- <text class="section-title">热门推荐</text>
- <text class="section-more" @click="goMore">更多 ></text>
- </view>
- <scroll-view scroll-x class="hot-list">
- <view
- v-for="novel in displayedHotNovels"
- :key="novel.id"
- class="hot-item"
- @click="openNovel(novel)"
- >
- <image
- :src="getCoverUrl(novel.coverImg)"
- class="hot-cover"
- @error="handleImageError"
- />
- <text class="hot-title">{{ novel.title }}</text>
- </view>
- </scroll-view>
- </view>
-
- <!-- 小说列表 -->
- <view v-if="showNovels" class="section">
- <view class="section-header">
- <text class="section-title">小说列表</text>
- </view>
- <view class="novel-grid">
- <view
- v-for="novel in displayedNovels"
- :key="novel.id"
- class="novel-item"
- @click="openNovel(novel)"
- >
- <image
- :src="getCoverUrl(novel.coverImg)"
- class="novel-cover"
- @error="handleImageError"
- />
- <text class="novel-title">{{ novel.title }}</text>
- <text class="novel-author">{{ novel.author || '未知作者' }}</text>
- </view>
- </view>
- </view>
-
- <!-- 成为签约作家按钮 -->
- <view class="become-author" @click="applyAuthor">
- <text>成为签约作家,发布你的作品</text>
- </view>
-
- <!-- 底部广告 -->
- <ad-banner v-if="bottomAds.length" :ads="bottomAds" />
- </template>
- </view>
- </template>
-
- <script>
- import AdBanner from '@/components/AdBanner';
-
- export default {
- components: { AdBanner },
- data() {
- return {
- topAds: [],
- bottomAds: [],
- categories: [],
- activeCategory: 0,
- hotNovels: [],
- novels: [],
- loading: true,
- error: null,
- currentCategoryName: '全部',
-
- // 临时解决方案 - 模拟数据
- mockHotNovels: [
- { id: 1, title: '总裁的替身前妻', coverImg: '/static/demo-cover1.jpg', author: '言情作家' },
- { id: 2, title: '神医毒妃', coverImg: '/static/demo-cover2.jpg', author: '古风大神' },
- { id: 3, title: '穿越之王妃逆袭', coverImg: '/static/demo-cover3.jpg', author: '穿越达人' }
- ],
- mockNovels: [
- { id: 4, title: '都市最强赘婿', author: '网络作家', coverImg: '/static/demo-cover4.jpg' },
- { id: 5, title: '修仙归来', author: '仙侠迷', coverImg: '/static/demo-cover5.jpg' },
- { id: 6, title: '校园纯爱物语', author: '青春校园', coverImg: '/static/demo-cover6.jpg' }
- ],
- // 添加分页参数
- page: 1,
- pageSize: 10,
- total: 0,
- hasMore: true
- }
- },
- computed: {
- // 添加计算属性控制显示逻辑
- showEmptyState() {
- return this.novels.length === 0 && !this.error && !this.loading;
- },
- showHotNovels() {
- return this.hotNovels.length > 0 && !this.error;
- },
- showNovels() {
- return this.novels.length > 0 && !this.error;
- },
- displayedHotNovels() {
- return this.hotNovels.length > 0 ? this.hotNovels : this.mockHotNovels;
- },
- displayedNovels() {
- return this.novels.length > 0 ? this.novels : this.mockNovels;
- }
- },
- mounted() {
- this.initData();
- },
- methods: {
- async initData() {
- try {
- // 改为顺序加载,便于调试
- await this.loadCategories();
- await this.loadHotNovels();
- await this.loadNovels();
- } catch (e) {
- console.error('初始化失败', e);
- this.error = '初始化失败,请检查网络连接';
- } finally {
- this.loading = false;
- }
- },
- // 在 list.vue 的 methods 中修改 openNovel 方法
- openNovel(novel) {
- try {
- console.log('准备打开小说:', novel);
-
- // 检查novel对象是否存在
- if (!novel || typeof novel !== 'object') {
- console.error('novel对象无效:', novel);
- uni.showToast({
- title: '小说信息错误',
- icon: 'none'
- });
- return;
- }
-
- // 获取小说ID
- const novelId = novel.id || novel.novelId || novel.bookId;
- if (!novelId) {
- console.error('无法获取小说ID:', novel);
- uni.showToast({
- title: '小说ID不存在',
- icon: 'none'
- });
- return;
- }
-
- // 确保ID是数字
- const id = Number(novelId);
- if (isNaN(id)) {
- console.error('小说ID不是有效数字:', novelId);
- uni.showToast({
- title: '小说ID格式错误',
- icon: 'none'
- });
- return;
- }
-
- console.log('跳转到阅读页面,小说ID:', id);
-
- // 直接使用uni.navigateTo进行跳转
- uni.navigateTo({
- url: `/pages/novel/reader?novelId=${id}`,
- success: (res) => {
- console.log('跳转成功', res);
- },
- fail: (err) => {
- console.error('跳转失败', err);
- uni.showToast({
- title: '跳转失败,请重试',
- icon: 'none'
- });
- }
- });
- } catch (error) {
- console.error('openNovel方法执行异常:', error);
- uni.showToast({
- title: '发生未知错误',
- icon: 'none'
- });
- }
- },
-
- // 备用导航方法
- fallbackNavigate(novelId) {
- try {
- console.log('尝试备用导航方法,小说ID:', novelId);
-
- // 方法1: 使用window.location (仅H5环境)
- if (typeof window !== 'undefined' && window.location) {
- window.location.href = `/pages/novel/reader?novelId=${novelId}`;
- return;
- }
-
- // 方法2: 使用Vue Router (如果可用)
- if (this.$router && typeof this.$router.push === 'function') {
- this.$router.push({
- path: '/pages/novel/reader',
- query: { novelId }
- });
- return;
- }
-
- // 方法3: 显示错误提示
- uni.showToast({
- title: '无法跳转,请检查页面配置',
- icon: 'none'
- });
-
- } catch (error) {
- console.error('备用导航方法也失败:', error);
- }
- },
- // 修复响应解析方法
- parseResponse(res) {
- console.log('原始响应对象:', res);
-
- // 处理不同的响应格式
- let responseData = {};
-
- // 如果res有arg属性,使用arg作为响应数据
- if (res && res.arg) {
- responseData = res.arg;
- console.log('使用res.arg作为响应数据:', responseData);
- }
- // 如果res有data属性,使用data作为响应数据
- else if (res && res.data) {
- responseData = res.data;
- console.log('使用res.data作为响应数据:', responseData);
- }
- // 如果res本身就是响应数据
- else {
- responseData = res;
- console.log('使用res本身作为响应数据:', responseData);
- }
-
- return responseData;
- },
-
- // 修复分类加载逻辑
- async loadCategories() {
- try {
- const res = await this.$http.get('/category/tree', {
- header: { isToken: false },
- timeout: 10000
- });
-
- console.log('分类API完整响应:', res);
-
- // 使用统一的响应解析方法
- const responseData = this.parseResponse(res);
- console.log('处理后的分类响应数据:', responseData);
-
- // 修复: 检查不同的响应格式
- let categoriesData = [];
-
- if (responseData.code === 200 && Array.isArray(responseData.data)) {
- categoriesData = responseData.data;
- } else if (Array.isArray(responseData)) {
- categoriesData = responseData;
- } else if (responseData.code === 200 && Array.isArray(responseData.rows)) {
- categoriesData = responseData.rows;
- }
-
- if (categoriesData.length > 0) {
- let subCategories = [];
- categoriesData.forEach(parent => {
- if (parent && parent.children && Array.isArray(parent.children)) {
- // 确保每个子分类都有必要的字段
- const validChildren = parent.children.filter(child =>
- child && child.id && (child.title || child.name)
- ).map(child => ({
- id: child.id,
- name: child.title || child.name // 使用title或name作为分类名称
- }));
-
- subCategories = [...subCategories, ...validChildren];
- }
- });
-
- // 添加"全部"分类
- this.categories = [
- { id: 0, name: '全部' },
- ...subCategories
- ];
-
- console.log('处理后的分类:', this.categories);
- } else {
- console.warn('分类数据结构异常,使用默认分类', responseData);
- this.categories = [{ id: 0, name: '全部' }];
- }
- } catch (e) {
- console.error('加载分类失败', e);
- this.categories = [{ id: 0, name: '全部' }];
- }
- },
-
- // 修复热门小说加载逻辑
- async loadHotNovels() {
- try {
- const res = await this.$http.get('/novel/hot', {
- header: { isToken: false },
- timeout: 10000
- });
-
- console.log('热门小说API完整响应:', res);
-
- // 使用统一的响应解析方法
- const responseData = this.parseResponse(res);
- console.log('处理后的热门小说响应:', responseData);
-
- // 处理API返回的数据格式
- if (responseData.code === 200) {
- if (Array.isArray(responseData.rows)) {
- this.hotNovels = responseData.rows;
- } else if (Array.isArray(responseData.data)) {
- this.hotNovels = responseData.data;
- } else {
- console.warn('热门小说数据格式异常', responseData);
- this.hotNovels = this.mockHotNovels;
- }
- } else {
- console.warn('热门小说API返回异常状态码', responseData);
- this.hotNovels = this.mockHotNovels;
- }
-
- // 只在真实数据不可用时使用模拟数据
- if (!this.hotNovels || this.hotNovels.length === 0) {
- console.warn('使用模拟热门小说数据');
- this.hotNovels = this.mockHotNovels;
- }
- } catch (e) {
- console.error('加载热门小说失败', e);
- this.hotNovels = this.mockHotNovels;
- }
- },
-
- // 修复小说列表加载逻辑
- async loadNovels(reset = false) {
- if (reset) {
- this.page = 1;
- this.hasMore = true;
- this.novels = [];
- }
-
- if (!this.hasMore) return;
-
- this.loading = true;
- this.error = null;
-
- // 提前声明变量,使其在整个函数中可用
- let newNovels = [];
- let total = 0;
-
- try {
- const res = await this.$http.get('/novel/list', {
- params: {
- page: this.page,
- pageSize: this.pageSize,
- categoryId: this.activeCategory
- },
- header: { isToken: false },
- timeout: 15000
- });
-
- console.log('小说列表API完整响应:', res);
-
- // 使用统一的响应解析方法
- const responseData = this.parseResponse(res);
- console.log('处理后的小说列表响应:', responseData);
-
- // 处理API返回的数据格式
- if (responseData.code === 200) {
- if (Array.isArray(responseData.rows)) {
- newNovels = responseData.rows;
- total = responseData.total || 0;
- } else if (Array.isArray(responseData.data)) {
- newNovels = responseData.data;
- total = responseData.total || newNovels.length;
- }
- } else if (responseData.code === 500) {
- // 处理服务器错误
- console.error('服务器内部错误:', responseData.msg);
- throw new Error('服务器内部错误,请稍后重试');
- }
-
- if (newNovels.length > 0) {
- this.novels = [...this.novels, ...newNovels];
- this.hasMore = this.novels.length < total;
- this.page++;
-
- // 修复: 确保图片URL正确
- this.novels.forEach(novel => {
- if (novel.coverImg) {
- novel.coverImg = this.getCoverUrl(novel.coverImg);
- }
- });
-
- // 确保每个novel对象都有有效的id
- this.novels.forEach((novel, index) => {
- if (!novel.id) {
- console.warn(`小说 ${index} 缺少ID,使用索引作为临时ID`);
- novel.id = index + 1; // 使用索引作为临时ID
- }
- });
- } else if (this.page === 1) {
- console.warn('小说列表为空,使用模拟数据');
- this.novels = this.mockNovels; // 使用模拟数据
- }
- } catch (e) {
- console.error('加载小说失败', e);
- if (e.errMsg && e.errMsg.includes('timeout')) {
- this.error = '网络请求超时,请检查网络连接';
- } else if (e.message && e.message.includes('服务器内部错误')) {
- this.error = '服务器暂时不可用,请稍后重试';
- } else {
- this.error = '加载失败,请重试';
- }
- this.novels = this.mockNovels; // 回退到模拟数据
- } finally {
- this.loading = false;
- }
- },
-
- // 暂时禁用广告加载,避免404错误
- async loadAds() {
- try {
- console.log('广告功能暂未开放');
- this.topAds = [];
- this.bottomAds = [];
-
- // 以下是原有的广告加载代码,暂时注释掉
- /*
- // 使用Promise.all并行请求
- const [topRes, bottomRes] = await Promise.all([
- this.$http.get('/ad/position?code=TOP_BANNER', {
- header: { isToken: false }
- }),
- this.$http.get('/ad/position?code=BOTTOM_BANNER', {
- header: { isToken: false }
- })
- ]);
-
- // 处理广告响应
- this.topAds = this.parseAdResponse(topRes);
- this.bottomAds = this.parseAdResponse(bottomRes);
- */
- } catch (e) {
- console.error('加载广告失败', e);
- this.topAds = [];
- this.bottomAds = [];
- }
- },
-
- // 广告响应解析方法 - 简化逻辑
- parseAdResponse(response) {
- if (!response || !response.data) return [];
-
- const responseData = response.data;
-
- // 统一处理可能的响应格式
- if (responseData.code === 200 && Array.isArray(responseData.data)) {
- return responseData.data;
- } else if (Array.isArray(responseData)) {
- return responseData;
- } else if (responseData.code === 200 && Array.isArray(responseData.rows)) {
- return responseData.rows;
- }
-
- return [];
- },
-
- // 修复封面URL处理
- getCoverUrl(cover) {
- if (!cover) return '/static/default-cover.jpg';
-
- // 如果已经是完整URL,直接返回
- if (cover.startsWith('http')) return cover;
-
- // 如果是相对路径,添加基础URL
- if (cover.startsWith('/')) {
- return `${process.env.VUE_APP_BASE_URL || ''}${cover}`;
- }
-
- // 其他情况,直接返回
- return cover;
- },
-
- // 添加图片错误处理
- handleImageError(event) {
- console.log('图片加载失败:', event);
- event.target.src = '/static/default-cover.jpg';
- },
-
- // 其他方法...
- changeCategory(categoryId) {
- this.activeCategory = categoryId;
- const category = this.categories.find(c => c.id === categoryId);
- this.currentCategoryName = category ? category.name : '全部';
- this.loadNovels(true); // 重置并加载新分类
- },
-
- applyAuthor() {
- if (!this.$store.getters.token) {
- uni.showToast({ title: '请先登录', icon: 'none' });
- uni.navigateTo({ url: '/pages/login' });
- return;
- }
- uni.navigateTo({ url: '/pages/author/apply' });
- },
-
- goMore() {
- uni.navigateTo({
- url: '/pages/novel/more'
- });
- },
-
- // 确保重新加载按钮绑定到正确的方法
- reloadData() {
- this.initData();
- },
-
- // 添加上拉加载事件
- onReachBottom() {
- this.loadNovels();
- }
- }
- }
- </script>
-
- <style scoped>
- /* 添加加载状态样式 */
- .loading-container {
- display: flex;
- flex-direction: column;
- align-items: center;
- justify-content: center;
- padding: 100rpx 0;
- }
-
- .loading-icon {
- animation: rotate 1s linear infinite;
- margin-bottom: 20rpx;
- }
-
- @keyframes rotate {
- from { transform: rotate(0deg); }
- to { transform: rotate(360deg); }
- }
-
- /* 添加空状态样式 */
- .empty-container, .error-container {
- display: flex;
- flex-direction: column;
- align-items: center;
- justify-content: center;
- padding: 100rpx 0;
- text-align: center;
- }
-
- .empty-img {
- width: 200rpx;
- height: 200rpx;
- opacity: 0.6;
- margin-bottom: 40rpx;
- }
-
- .empty-text, .error-text {
- font-size: 32rpx;
- color: var(--text-color);
- margin-bottom: 40rpx;
- }
-
- .error-text {
- color: var(--error-color);
- }
-
- .btn-refresh {
- background-color: var(--primary-color);
- color: white;
- width: 60%;
- border-radius: 50rpx;
- }
-
- /* 确保网格布局正确 */
- .novel-grid {
- display: grid;
- grid-template-columns: repeat(3, 1fr);
- gap: 20rpx;
- padding: 20rpx;
- }
-
- .novel-item {
- background-color: white;
- border-radius: 12rpx;
- overflow: hidden;
- box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
- }
-
- /* 确保封面有默认背景和固定比例 */
- .novel-cover, .hot-cover {
- width: 100%;
- height: 300rpx;
- background-color: #f5f5f5;
- object-fit: cover;
- }
-
- .novel-title {
- display: block;
- font-size: 28rpx;
- padding: 10rpx 15rpx;
- white-space: nowrap;
- overflow: hidden;
- text-overflow: ellipsis;
- }
-
- .novel-author {
- display: block;
- font-size: 24rpx;
- color: #888;
- padding: 0 15rpx 15rpx;
- }
-
- /* 热门列表样式 */
- .hot-list {
- display: flex;
- padding: 20rpx;
- overflow-x: auto;
- white-space: nowrap;
- }
-
- .hot-item {
- display: inline-block;
- width: 200rpx;
- margin-right: 30rpx;
- }
-
- .hot-cover {
- width: 200rpx;
- height: 280rpx;
- border-radius: 8rpx;
- }
-
- .hot-title {
- display: block;
- margin-top: 10rpx;
- font-size: 26rpx;
- white-space: nowrap;
- overflow: hidden;
- text-overflow: ellipsis;
- }
-
- /* 分类导航样式 */
- .category-nav {
- white-space: nowrap;
- padding: 20rpx 0;
- background-color: #fff;
- border-bottom: 1rpx solid #eee;
- }
-
- .category-item {
- display: inline-block;
- padding: 10rpx 30rpx;
- margin: 0 10rpx;
- border-radius: 30rpx;
- font-size: 28rpx;
- }
-
- .category-item.active {
- background-color: var(--primary-color);
- color: white;
- }
-
- /* 章节标题样式 */
- .section {
- margin-top: 30rpx;
- }
-
- .section-header {
- display: flex;
- justify-content: space-between;
- align-items: center;
- padding: 0 20rpx 20rpx;
- }
-
- .section-title {
- font-size: 32rpx;
- font-weight: bold;
- }
-
- .section-more {
- font-size: 26rpx;
- color: var(--text-secondary-color);
- }
-
- /* 成为作家按钮样式 */
- .become-author {
- margin: 40rpx 20rpx;
- padding: 20rpx;
- text-align: center;
- background-color: var(--primary-color);
- color: white;
- border-radius: 10rpx;
- font-size: 30rpx;
- }
- </style>
|