book-min/miniprogram/pages/index/index.ts

153 lines
4.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// index.ts
// 获取应用实例
const app = getApp<IAppOption>()
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
Component({
data: {
userInfo: {
avatarUrl: '/images/default-avatar.png',
nickName: '体验卡已过期'
},
recentBooks: [
{
id: 1,
title: 'Java编程思想',
author: 'Bruce Eckel',
coverUrl: 'https://img3.doubanio.com/view/subject/l/public/s27243455.jpg',
bookUrl: '',
description: '本书赢得了全球程序员的广泛赞誉'
},
{
id: 2,
title: 'C Primer Plus',
author: 'Stephen Prata',
coverUrl: 'https://img2.doubanio.com/view/subject/l/public/s29196249.jpg',
bookUrl: '',
description: 'C语言程序设计经典教程'
},
{
id: 3,
title: '狂帆',
author: '血红',
coverUrl: 'https://img1.doubanio.com/view/subject/l/public/s29934566.jpg',
bookUrl: '',
description: '一本精彩的玄幻小说'
}
] as Book[],
recommendBooks: [
{
id: 4,
title: '龙族IV奥丁之渊',
author: '江南',
coverUrl: 'https://img2.doubanio.com/view/subject/l/public/s29850307.jpg',
bookUrl: '',
description: '青春如同奔流的江水,一去不回来不及道别',
rating: 89.8,
status: '大家都在读'
},
{
id: 5,
title: '白鹿原',
author: '陈忠实',
coverUrl: 'https://img1.doubanio.com/view/subject/l/public/s28111905.jpg',
bookUrl: '',
description: '一部渭河平原五十年变迁的雄奇史诗',
rating: 91.9,
status: '神作'
},
{
id: 6,
title: 'Linux是怎样工作的',
author: '[日]武内觉',
coverUrl: 'https://img3.doubanio.com/view/subject/l/public/s33654311.jpg',
bookUrl: '',
description: '从命令行、Shell脚本到内核原理',
rating: 80.0,
status: '技术经典'
}
] as Book[],
currentTab: 'recommend',
loading: false
},
lifetimes: {
attached() {
// 使用模拟数据暂时注释掉实际的API调用
// this.getUserInfo();
// this.getRecentBooks();
// this.getRecommendBooks();
}
},
methods: {
// 获取用户信息
async getUserInfo() {
const userInfo = wx.getStorageSync('userInfo');
if (userInfo) {
this.setData({ userInfo });
}
},
// 获取最近阅读的书籍
async getRecentBooks() {
try {
const res = await request.get<BookListResponse>('/books/recent');
if (res.code === 200) {
this.setData({
recentBooks: res.data.records
});
}
} catch (error) {
console.error('获取最近阅读失败:', error);
}
},
// 获取推荐书籍
async getRecommendBooks() {
try {
this.setData({ loading: true });
const res = await request.get<BookListResponse>('/books/recommend');
if (res.code === 200) {
this.setData({
recommendBooks: res.data.records
});
}
} catch (error) {
console.error('获取推荐书籍失败:', error);
} finally {
this.setData({ loading: false });
}
},
// 切换标签
switchTab(e: any) {
const tab = e.currentTarget.dataset.tab;
this.setData({ currentTab: tab });
// TODO: 根据不同标签加载不同内容
},
// 打开书籍
openBook(e: any) {
const { id, url } = e.currentTarget.dataset;
wx.navigateTo({
url: `/pages/bookshelf/reader?id=${id}&bookUrl=${encodeURIComponent(url)}`
});
},
// 跳转到书架
goToBookshelf() {
wx.switchTab({
url: '/pages/bookshelf/list'
});
},
// 跳转到设置
goToSettings() {
wx.navigateTo({
url: '/pages/profile/settings'
});
}
}
})