// 小程序首页逻辑 const { museumApi } = require('../../utils/api.js'); const { navigateToWebview } = require('../../utils/util.js'); const app = getApp(); Page({ /** * 页面的初始数据 */ data: { showLoading: false, // 控制是否显示loading组件 isFirstOpen: false, // 是否首次打开 bannerData: [], // 轮播图数据 newsList: [], // 资讯列表 exhibitionList: [], // 展览列表 activeList: [], // 活动列表 loading: false, // 加载状态 isLoggedIn: false // 登录状态 }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { // 检查登录状态 this.checkLoginStatus(); // 检查是否首次访问 const hasVisited = wx.getStorageSync('hasVisited'); if (!hasVisited) { // 首次访问,显示loading组件并隐藏导航栏 // wx.hideNavigationBarLoading(); // wx.setNavigationBarTitle({ // title: '' // }); wx.hideTabBar(); this.setData({ showLoading: true, isFirstOpen: true }); this.initAllData(); } else { // 非首次访问,立即显示页面内容 this.setData({ showLoading: false, isFirstOpen: false }); // 设置已访问标记 wx.setStorageSync('hasVisited', true); // 立即加载数据,但不显示loading状态 this.initAllData(); } }, // loading组件"开始探索"事件处理 onStartExplore() { // 设置已访问标记 wx.setStorageSync('hasVisited', true); wx.showTabBar(); this.setData({ showLoading: false, isFirstOpen: false }); // 恢复导航栏 // wx.setNavigationBarTitle({ // title: '克拉玛依博物馆' // }); // 初始化数据 // this.initAllData(); }, // 轮播图点击事件 onBannerClick(e) { const url = e.currentTarget.dataset.url; if (url) { console.log('轮播图点击,跳转URL:', url); navigateToWebview(url); } else { console.log('轮播图URL为空,无法跳转'); } }, // 查看更多 viewMore(e) { const section = e.currentTarget.dataset.section; console.log(`查看更多${section}`); switch (section) { case 'exhibition': wx.switchTab({ url: '/pages/exhibition/index' }); break; case 'recommended': wx.navigateTo({ url: '/pages/index/news/news' }); break; case 'activity': wx.navigateTo({ url: '/pages/index/activity/activity' }); break; } }, /** * 初始化所有数据 */ async initAllData() { try { // 只有首次访问时才显示loading状态 if (this.data.isFirstOpen) { this.setData({ loading: true }); } // 并行请求所有数据 const [bannerRes, newsRes, exhibitionRes, activeRes] = await Promise.all([ this.getBannerData({ pageNum: 1, pageSize: 5, status: 1 }), this.getNewsList({ pageNum: 1, pageSize: 3, status: 1 }), this.getExhibitionList({ pageNum: 1, pageSize: 5, status: 1 , recommend: 1 }), this.getActiveList({ pageNum: 1, pageSize: 5, status: 1 }) ]); // console.log('所有数据加载完成'); } catch (error) { console.error('数据加载失败:', error); wx.showToast({ title: '数据加载失败', icon: 'none' }); } finally { // 只有首次访问时才需要关闭loading状态 if (this.data.isFirstOpen) { this.setData({ loading: false }); } } }, /** * 获取轮播图数据 */ async getBannerData(params = {}) { try { const response = await museumApi.getCarouselList(params); console.log('轮播图数据:', response); const bannerData = response.records || response.list || response.data || response || []; this.setData({ bannerData }); return response; } catch (error) { console.error('获取轮播图数据失败:', error); // 使用默认数据 this.setData({ bannerData: [{ carouselId: 1, title: '轮播图1', img: '' }] }); throw error; } }, /** * 获取资讯列表 */ async getNewsList(params = {}) { try { const response = await museumApi.getNewsList(params); console.log('资讯数据:', response); const newsList = response.records || response.list || response.data || response || []; this.setData({ newsList }); return response; } catch (error) { console.error('获取资讯数据失败:', error); this.setData({ newsList: [] }); throw error; } }, /** * 获取展览列表 */ async getExhibitionList(params = {}) { try { const response = await museumApi.getExhibitionList(params); console.log('展览数据:', response); const exhibitionList = response.records || response.list || response.data || response || []; this.setData({ exhibitionList }); return response; } catch (error) { console.error('获取展览数据失败:', error); this.setData({ exhibitionList: [] }); throw error; } }, /** * 获取活动列表 */ async getActiveList(params = {}) { try { const response = await museumApi.getSocialActivityList(params); console.log('活动数据:', response); const activeList = response.records || response.list || response.data || response || []; this.setData({ activeList }); return response; } catch (error) { console.error('获取活动数据失败:', error); this.setData({ activeList: [] }); throw error; } }, /** * 功能点击处理 */ handleFunctionClick(e) { const { type } = e.currentTarget.dataset; console.log(`点击了${type}功能`); // 检查是否需要登录的功能 if ((type === 'visit' || type === 'activity') && !this.data.isLoggedIn) { // 未登录,显示登录提示 this.showLoginPrompt(); return; } // 根据不同功能跳转到webview页面 switch (type) { case 'visit': wx.navigateTo({ url: '/pages/index/visit-preview/visit-preview' }); break; case 'activity': wx.navigateTo({ url: '/pages/index/active-preview/active-preview' }); break; case 'map': this.handleMapClick(); break; case 'introduce': this.navigateToWebview('/allDetailsShow?id=1&type=museum'); break; } }, /** * 查看展览详情 */ viewExhibition(e) { const { item } = e.currentTarget.dataset; console.log(`查看展览${item.exhibitId}详情`); this.navigateToWebview(`/allDetailsShow?isFrom=weixin&id=${item.exhibitId}&type=exhibition`); }, /** * 查看活动详情 */ viewActivity(e) { const { item } = e.currentTarget.dataset; console.log(`查看活动${item.activityId}详情`); // this.navigateToWebview(`/allDetailsShow?isFrom=weixin&id=${item.activityId}&type=activity`); wx.navigateTo({ url: `/pages/exhibition/activeDetails/index?isFrom=weixin&id=${item.activityId}&type=activity` }); }, /** * 查看资讯详情 */ viewNews(e) { const { item } = e.currentTarget.dataset; console.log(`查看资讯${item.informationId}详情`); this.navigateToWebview(`/allDetailsShow?isFrom=weixin&id=${item.informationId}&type=information`); }, /** * 处理地图点击事件 */ async handleMapClick() { // this.navigateToWebview('/indexPage/map?isFrom=weixin'); wx.navigateTo({ url: '/pages/user/map/index' }); }, /** * 导航到webview页面 */ navigateToWebview(path) { navigateToWebview(path); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { // 每次显示页面时检查登录状态 this.checkLoginStatus(); }, /** * 检查登录状态 */ checkLoginStatus() { const app = getApp(); const token = wx.getStorageSync('token'); const isLoggedIn = !!(token && !app.globalData.isGuest); this.setData({ isLoggedIn: isLoggedIn }); }, /** * 显示登录提示 */ showLoginPrompt() { const app = getApp(); wx.showModal({ title: '登录提示', content: '为了给您提供更好的服务,需要获取您的微信登录信息,是否同意?', confirmText: '立即登录', cancelText: '取消', success: (res) => { if (res.confirm) { // 用户确认登录,调用app的登录方法 app.wxLogin(); // 登录弹窗关闭后,页面会重新显示,onShow会被触发,从而更新登录状态 } } }); }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉刷新 */ onPullDownRefresh() { this.initAllData().finally(() => { wx.stopPullDownRefresh(); }); }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { return { title: '克拉玛依博物馆', path: '/pages/index/index?isFrom=weixin' }; }, /** * 用户点击右上角分享到朋友圈 */ onShareTimeline() { return { title: '克拉玛依博物馆 - 探索历史文化之美', query: 'isFrom=weixin', imageUrl: '' // 可以设置自定义分享图片 }; } })