// pages/exhibition/index.js const { museumApi } = require('../../utils/api.js'); const { navigateToWebview } = require('../../utils/util.js'); Page({ /** * 页面的初始数据 */ data: { selectedType: 0, // 当前选中的类型:0-全部,1-室内,2-室外 carouselList: [], // 轮播图数据 exhibitionList: [], // 展览列表数据 loading: false, // 加载状态 hasMore: true, // 是否还有更多数据 currentPage: 1, // 当前页码 pageSize: 10 // 每页数量 }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { this.initData(); }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { this.initData(); }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { this.loadMore(); }, // 初始化数据 async initData() { await this.getCarouselData(); await this.getExhibitionList(false); wx.stopPullDownRefresh(); }, // 获取轮播图数据(type为0的前5条) async getCarouselData() { try { const response = await museumApi.getExhibitionList({ pageNum: 1, pageSize: 5, type: 0 }); if (response && response.records) { this.setData({ carouselList: response.records }); } } catch (error) { console.error('获取轮播图数据失败:', error); } }, // 获取展览列表数据 async getExhibitionList(isLoadMore = false) { if (this.data.loading) return; this.setData({ loading: true }); try { const response = await museumApi.getExhibitionList({ pageNum: isLoadMore ? this.data.currentPage : 1, pageSize: this.data.pageSize, type: this.data.selectedType }); if (response && response.records) { let exhibitionList; if (isLoadMore) { // 加载更多,追加数据 exhibitionList = [...this.data.exhibitionList, ...response.records]; } else { // 重新加载,替换数据 exhibitionList = response.records; this.setData({ currentPage: 1 }); } this.setData({ exhibitionList: exhibitionList, hasMore: response.records.length === this.data.pageSize }); if (isLoadMore) { this.setData({ currentPage: this.data.currentPage + 1 }); } } } catch (error) { console.error('获取展览列表失败:', error); wx.showToast({ title: '加载失败', icon: 'none' }); } finally { this.setData({ loading: false }); } }, // 选择分类 async selectCategory(e) { const type = e.currentTarget.dataset.type; if (this.data.selectedType === type) return; this.setData({ selectedType: type, currentPage: 1, hasMore: true }); // 重新获取列表数据 await this.getExhibitionList(false); }, // 加载更多 async loadMore() { if (!this.data.hasMore || this.data.loading) return; await this.getExhibitionList(true); }, // 展览项点击事件 onExhibitionTap(e) { const item = e.currentTarget.dataset.item; this.goToDetail(item.exhibitId); }, // 线上观展点击事件 onOnlineExhibitionTap(e) { console.log('线上观展点击事件触发', e.currentTarget.dataset.item); // e.stopPropagation(); // 阻止事件冒泡 const item = e.currentTarget.dataset.item; if (item.webSite) { console.log('跳转到webSite:', item.webSite); navigateToWebview(item.webSite,'open'); } else { this.goToDetail(item.exhibitId); console.log('webSite为空'); } }, // 跳转到详情页面 goToDetail(id) { navigateToWebview(`/allDetailsShow?isFrom=weixin&id=${id}&type=exhibition`); }, /** * 用户点击右上角分享 */ onShareAppMessage() { return { title: '克拉玛依博物馆 - 展览', path: '/pages/exhibition/index' }; }, /** * 用户点击右上角分享到朋友圈 */ onShareTimeline() { return { title: '克拉玛依博物馆 - 精彩展览等你来看', query: '', imageUrl: '' // 可以设置自定义分享图片 }; } });