| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- // pages/index/index.js
- const contentApi = require('../../api/content');
- const categoryApi = require('../../api/category');
- const userUtil = require('../../utils/user');
- Page({
- data: {
- categories: [],
- currentCategory: 0, // 0表示全部
- contentList: [],
- bannerList: [], // 轮播图数据
- recommendedList: [], // 今日推荐数据
- contentType: null, // null全部, 1电子书, 2听书
- keyword: '',
- current: 1,
- size: 10,
- total: 0,
- loading: false,
- hasMore: true,
- showSearch: false,
- swiperMarginTop: 'calc(130rpx + env(safe-area-inset-top))', // 轮播图的上边距
- recommendedColumns: 3 // 推荐区域每行显示数量(2或3)
- },
- onLoad() {
- this.loadCategories();
- this.loadBannerList();
- this.loadRecommended();
- this.loadContentList();
- },
- onShow() {
- // 首页不需要强制登录,可以浏览
- },
- // 加载分类
- async loadCategories() {
- try {
- const categories = await categoryApi.getCategoryList();
- this.setData({
- categories: [{ categoryId: 0, categoryName: '全部' }, ...categories]
- });
- } catch (error) {
- console.error('加载分类失败:', error);
- }
- },
- // 加载轮播图数据(取前5条热门内容)
- async loadBannerList() {
- try {
- const result = await contentApi.getContentList(1, 5, '', null, null);
- this.setData({
- bannerList: result.records || []
- });
- } catch (error) {
- console.error('加载轮播图失败:', error);
- }
- },
- // 加载今日推荐数据
- async loadRecommended() {
- try {
- const size = this.data.recommendedColumns === 2 ? 4 : 6; // 每行2个显示4个,每行3个显示6个
- const recommended = await contentApi.getRecommended(size);
- this.setData({
- recommendedList: recommended || []
- });
- } catch (error) {
- console.error('加载今日推荐失败:', error);
- }
- },
- // 加载内容列表
- async loadContentList(isLoadMore = false) {
- if (this.data.loading) return;
-
- this.setData({ loading: true });
- try {
- const current = isLoadMore ? this.data.current + 1 : 1;
- const params = {
- current,
- size: this.data.size,
- categoryId: this.data.currentCategory === 0 ? null : this.data.currentCategory,
- contentType: this.data.contentType,
- keyword: this.data.keyword || null
- };
- const result = await contentApi.getContentList(
- params.current,
- params.size,
- params.keyword || '',
- params.contentType,
- params.categoryId
- );
- const contentList = isLoadMore
- ? [...this.data.contentList, ...result.records]
- : result.records;
- this.setData({
- contentList,
- current: result.current,
- total: result.total,
- hasMore: contentList.length < result.total,
- loading: false
- });
- } catch (error) {
- wx.showToast({
- title: error || '加载失败',
- icon: 'none'
- });
- this.setData({ loading: false });
- }
- },
- // 切换分类
- onCategoryTap(e) {
- const categoryId = e.currentTarget.dataset.id;
- if (categoryId === this.data.currentCategory) return;
-
- this.setData({
- currentCategory: categoryId,
- current: 1
- });
- this.loadContentList();
- },
- // 切换内容类型
- onContentTypeTap(e) {
- const type = e.currentTarget.dataset.type;
- // 将字符串 "null" 转换为真正的 null
- const contentType = type === 'null' ? null : parseInt(type);
- if (contentType === this.data.contentType) return;
-
- this.setData({
- contentType: contentType,
- current: 1
- });
- this.loadContentList();
- },
- // 搜索
- onSearchInput(e) {
- this.setData({
- keyword: e.detail.value
- });
- },
- onSearchConfirm() {
- this.setData({ current: 1 });
- this.loadContentList();
- },
- // 显示/隐藏搜索框
- toggleSearch() {
- const newShowSearch = !this.data.showSearch;
- this.setData({
- showSearch: newShowSearch,
- keyword: '',
- current: 1
- });
- // 动态调整轮播图的margin-top
- if (newShowSearch) {
- // 搜索框显示时,轮播图需要再向下移动
- this.setData({
- swiperMarginTop: 'calc(200rpx + env(safe-area-inset-top))'
- });
- } else {
- // 搜索框隐藏时,恢复原来的位置
- this.setData({
- swiperMarginTop: 'calc(130rpx + env(safe-area-inset-top))'
- });
- this.loadContentList();
- }
- },
- // 跳转到详情页
- goToDetail(e) {
- const contentId = e.currentTarget.dataset.id;
- wx.navigateTo({
- url: `/pages/detail/detail?id=${contentId}`
- });
- },
- // 上拉加载更多
- onReachBottom() {
- if (this.data.hasMore && !this.data.loading) {
- this.loadContentList(true);
- }
- },
- // 下拉刷新
- onPullDownRefresh() {
- this.setData({ current: 1 });
- this.loadContentList().finally(() => {
- wx.stopPullDownRefresh();
- });
- }
- });
|