92 lines
2.2 KiB
TypeScript
92 lines
2.2 KiB
TypeScript
import request from '../../utils/request';
|
|
|
|
Component({
|
|
data: {
|
|
books: [] as Book[],
|
|
categories: ['全部', '在读', '已读', '想读'],
|
|
currentCategory: '全部',
|
|
loading: false,
|
|
current: 1,
|
|
size: 10,
|
|
total: 0
|
|
},
|
|
|
|
lifetimes: {
|
|
attached() {
|
|
this.getBookList();
|
|
}
|
|
},
|
|
|
|
methods: {
|
|
// 获取书籍列表
|
|
async getBookList() {
|
|
if(this.data.loading) return;
|
|
|
|
this.setData({ loading: true });
|
|
try {
|
|
const res = await request.get<BookListResponse>('/books', {
|
|
current: this.data.current,
|
|
size: this.data.size,
|
|
category: this.data.currentCategory === '全部' ? '' : this.data.currentCategory
|
|
});
|
|
|
|
if(res.code === 200) {
|
|
|
|
console.log(res.data);
|
|
|
|
|
|
this.setData({
|
|
books: res.data.records,
|
|
total: res.data.total
|
|
});
|
|
} else {
|
|
wx.showToast({
|
|
title: '获取书籍列表失败',
|
|
icon: 'none'
|
|
});
|
|
}
|
|
} catch (error) {
|
|
console.error('获取书籍列表失败:', error);
|
|
wx.showToast({
|
|
title: '获取书籍列表失败',
|
|
icon: 'none'
|
|
});
|
|
} finally {
|
|
this.setData({ loading: false });
|
|
}
|
|
},
|
|
|
|
// 切换分类
|
|
async switchCategory(e: any) {
|
|
const category = e.currentTarget.dataset.category;
|
|
this.setData({
|
|
currentCategory: category,
|
|
current: 1 // 切换分类时重置页码
|
|
});
|
|
await this.getBookList();
|
|
},
|
|
|
|
// 打开阅读器
|
|
openReader(e: any) {
|
|
const { id, bookUrl } = e.currentTarget.dataset;
|
|
console.log('准备跳转,参数:', e.currentTarget.dataset);
|
|
|
|
wx.navigateTo({
|
|
url: `/pages/bookshelf/reader/reader?id=${id}&bookUrl=${encodeURIComponent(bookUrl)}&title=${encodeURIComponent(this.data.books.find(b => b.id === id)?.title || '')}`,
|
|
success: () => {
|
|
console.log('跳转成功');
|
|
},
|
|
fail: (err) => {
|
|
console.error('跳转失败:', err);
|
|
}
|
|
});
|
|
},
|
|
|
|
// 跳转到添加书籍页
|
|
goToAddBook() {
|
|
wx.navigateTo({
|
|
url: '/pages/bookshelf/add'
|
|
});
|
|
}
|
|
}
|
|
});
|