123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- // 小程序首页逻辑
- 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 // 加载状态
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad(options) {
- // 检查是否首次访问
- const hasVisited = wx.getStorageSync('hasVisited');
- if (!hasVisited) {
- // 首次访问,显示loading组件并隐藏导航栏
- // wx.hideNavigationBarLoading();
- // wx.setNavigationBarTitle({
- // title: ''
- // });
- wx.hideTabBar();
- this.setData({
- showLoading: true,
- isFirstOpen: true
- });
-
- } else {
- // 非首次访问,直接显示内容并初始化数据
- this.setData({
- showLoading: false,
- isFirstOpen: false
- });
- // 设置已访问标记
- wx.setStorageSync('hasVisited', true);
- this.initAllData();
- }
- },
-
- // loading组件"开始探索"事件处理
- onStartExplore() {
- // 设置已访问标记
- wx.setStorageSync('hasVisited', true);
- wx.showTabBar();
- this.setData({
- showLoading: false,
- isFirstOpen: false
- });
- // 恢复导航栏
- // wx.setNavigationBarTitle({
- // title: '克拉玛依博物馆'
- // });
- // 初始化数据
- this.initAllData();
- },
-
- // 查看更多
- 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 {
- this.setData({ loading: true });
-
- // 并行请求所有数据
- const [bannerRes, newsRes, exhibitionRes, activeRes] = await Promise.all([
- this.getBannerData({ pageNum: 1, pageSize: 10, status: 1 }),
- this.getNewsList({ pageNum: 1, pageSize: 3, status: 1 }),
- this.getExhibitionList({ pageNum: 1, pageSize: 5, status: 1 }),
- this.getActiveList({ pageNum: 1, pageSize: 5, status: 1 })
- ]);
-
- console.log('所有数据加载完成');
- } catch (error) {
- console.error('数据加载失败:', error);
- wx.showToast({
- title: '数据加载失败',
- icon: 'none'
- });
- } finally {
- 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}功能`);
-
- // 根据不同功能跳转到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`);
- },
- /**
- * 查看资讯详情
- */
- 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');
- },
- /**
- * 导航到webview页面
- */
- navigateToWebview(path) {
- navigateToWebview(path);
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady() {
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow() {
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide() {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload() {
- },
- /**
- * 页面相关事件处理函数--监听用户下拉刷新
- */
- onPullDownRefresh() {
- this.initAllData().finally(() => {
- wx.stopPullDownRefresh();
- });
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom() {
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage() {
- return {
- title: '克拉玛依博物馆',
- path: '/pages/index/index?isFrom=weixin'
- };
- }
- })
|