index.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // 小程序首页逻辑
  2. const { museumApi } = require('../../utils/api.js');
  3. const { navigateToWebview } = require('../../utils/util.js');
  4. const app = getApp();
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. showLoading: false, // 控制是否显示loading组件
  11. isFirstOpen: false, // 是否首次打开
  12. bannerData: [], // 轮播图数据
  13. newsList: [], // 资讯列表
  14. exhibitionList: [], // 展览列表
  15. activeList: [], // 活动列表
  16. loading: false // 加载状态
  17. },
  18. /**
  19. * 生命周期函数--监听页面加载
  20. */
  21. onLoad(options) {
  22. // 检查是否首次访问
  23. const hasVisited = wx.getStorageSync('hasVisited');
  24. if (!hasVisited) {
  25. // 首次访问,显示loading组件并隐藏导航栏
  26. // wx.hideNavigationBarLoading();
  27. // wx.setNavigationBarTitle({
  28. // title: ''
  29. // });
  30. wx.hideTabBar();
  31. this.setData({
  32. showLoading: true,
  33. isFirstOpen: true
  34. });
  35. } else {
  36. // 非首次访问,直接显示内容并初始化数据
  37. this.setData({
  38. showLoading: false,
  39. isFirstOpen: false
  40. });
  41. // 设置已访问标记
  42. wx.setStorageSync('hasVisited', true);
  43. this.initAllData();
  44. }
  45. },
  46. // loading组件"开始探索"事件处理
  47. onStartExplore() {
  48. // 设置已访问标记
  49. wx.setStorageSync('hasVisited', true);
  50. wx.showTabBar();
  51. this.setData({
  52. showLoading: false,
  53. isFirstOpen: false
  54. });
  55. // 恢复导航栏
  56. // wx.setNavigationBarTitle({
  57. // title: '克拉玛依博物馆'
  58. // });
  59. // 初始化数据
  60. this.initAllData();
  61. },
  62. // 查看更多
  63. viewMore(e) {
  64. const section = e.currentTarget.dataset.section;
  65. console.log(`查看更多${section}`);
  66. switch (section) {
  67. case 'exhibition':
  68. wx.switchTab({
  69. url: '/pages/exhibition/index'
  70. });
  71. break;
  72. case 'recommended':
  73. wx.navigateTo({
  74. url: '/pages/index/news/news'
  75. });
  76. break;
  77. case 'activity':
  78. wx.navigateTo({
  79. url: '/pages/index/activity/activity'
  80. });
  81. break;
  82. }
  83. },
  84. /**
  85. * 初始化所有数据
  86. */
  87. async initAllData() {
  88. try {
  89. this.setData({ loading: true });
  90. // 并行请求所有数据
  91. const [bannerRes, newsRes, exhibitionRes, activeRes] = await Promise.all([
  92. this.getBannerData({ pageNum: 1, pageSize: 10, status: 1 }),
  93. this.getNewsList({ pageNum: 1, pageSize: 3, status: 1 }),
  94. this.getExhibitionList({ pageNum: 1, pageSize: 5, status: 1 }),
  95. this.getActiveList({ pageNum: 1, pageSize: 5, status: 1 })
  96. ]);
  97. console.log('所有数据加载完成');
  98. } catch (error) {
  99. console.error('数据加载失败:', error);
  100. wx.showToast({
  101. title: '数据加载失败',
  102. icon: 'none'
  103. });
  104. } finally {
  105. this.setData({ loading: false });
  106. }
  107. },
  108. /**
  109. * 获取轮播图数据
  110. */
  111. async getBannerData(params = {}) {
  112. try {
  113. const response = await museumApi.getCarouselList(params);
  114. console.log('轮播图数据:', response);
  115. const bannerData = response.records || response.list || response.data || response || [];
  116. this.setData({ bannerData });
  117. return response;
  118. } catch (error) {
  119. console.error('获取轮播图数据失败:', error);
  120. // 使用默认数据
  121. this.setData({
  122. bannerData: [{ carouselId: 1, title: '轮播图1', img: '' }]
  123. });
  124. throw error;
  125. }
  126. },
  127. /**
  128. * 获取资讯列表
  129. */
  130. async getNewsList(params = {}) {
  131. try {
  132. const response = await museumApi.getNewsList(params);
  133. console.log('资讯数据:', response);
  134. const newsList = response.records || response.list || response.data || response || [];
  135. this.setData({ newsList });
  136. return response;
  137. } catch (error) {
  138. console.error('获取资讯数据失败:', error);
  139. this.setData({ newsList: [] });
  140. throw error;
  141. }
  142. },
  143. /**
  144. * 获取展览列表
  145. */
  146. async getExhibitionList(params = {}) {
  147. try {
  148. const response = await museumApi.getExhibitionList(params);
  149. console.log('展览数据:', response);
  150. const exhibitionList = response.records || response.list || response.data || response || [];
  151. this.setData({ exhibitionList });
  152. return response;
  153. } catch (error) {
  154. console.error('获取展览数据失败:', error);
  155. this.setData({ exhibitionList: [] });
  156. throw error;
  157. }
  158. },
  159. /**
  160. * 获取活动列表
  161. */
  162. async getActiveList(params = {}) {
  163. try {
  164. const response = await museumApi.getSocialActivityList(params);
  165. console.log('活动数据:', response);
  166. const activeList = response.records || response.list || response.data || response || [];
  167. this.setData({ activeList });
  168. return response;
  169. } catch (error) {
  170. console.error('获取活动数据失败:', error);
  171. this.setData({ activeList: [] });
  172. throw error;
  173. }
  174. },
  175. /**
  176. * 功能点击处理
  177. */
  178. handleFunctionClick(e) {
  179. const { type } = e.currentTarget.dataset;
  180. console.log(`点击了${type}功能`);
  181. // 根据不同功能跳转到webview页面
  182. switch (type) {
  183. case 'visit':
  184. wx.navigateTo({
  185. url: '/pages/index/visit-preview/visit-preview'
  186. });
  187. break;
  188. case 'activity':
  189. wx.navigateTo({
  190. url: '/pages/index/active-preview/active-preview'
  191. });
  192. break;
  193. case 'map':
  194. this.handleMapClick();
  195. break;
  196. case 'introduce':
  197. this.navigateToWebview('/allDetailsShow?id=1&type=museum');
  198. break;
  199. }
  200. },
  201. /**
  202. * 查看展览详情
  203. */
  204. viewExhibition(e) {
  205. const { item } = e.currentTarget.dataset;
  206. console.log(`查看展览${item.exhibitId}详情`);
  207. this.navigateToWebview(`/allDetailsShow?isFrom=weixin&id=${item.exhibitId}&type=exhibition`);
  208. },
  209. /**
  210. * 查看活动详情
  211. */
  212. viewActivity(e) {
  213. const { item } = e.currentTarget.dataset;
  214. console.log(`查看活动${item.activityId}详情`);
  215. this.navigateToWebview(`/allDetailsShow?isFrom=weixin&id=${item.activityId}&type=activity`);
  216. },
  217. /**
  218. * 查看资讯详情
  219. */
  220. viewNews(e) {
  221. const { item } = e.currentTarget.dataset;
  222. console.log(`查看资讯${item.informationId}详情`);
  223. this.navigateToWebview(`/allDetailsShow?isFrom=weixin&id=${item.informationId}&type=information`);
  224. },
  225. /**
  226. * 处理地图点击事件
  227. */
  228. async handleMapClick() {
  229. this.navigateToWebview('/indexPage/map?isFrom=weixin');
  230. },
  231. /**
  232. * 导航到webview页面
  233. */
  234. navigateToWebview(path) {
  235. navigateToWebview(path);
  236. },
  237. /**
  238. * 生命周期函数--监听页面初次渲染完成
  239. */
  240. onReady() {
  241. },
  242. /**
  243. * 生命周期函数--监听页面显示
  244. */
  245. onShow() {
  246. },
  247. /**
  248. * 生命周期函数--监听页面隐藏
  249. */
  250. onHide() {
  251. },
  252. /**
  253. * 生命周期函数--监听页面卸载
  254. */
  255. onUnload() {
  256. },
  257. /**
  258. * 页面相关事件处理函数--监听用户下拉刷新
  259. */
  260. onPullDownRefresh() {
  261. this.initAllData().finally(() => {
  262. wx.stopPullDownRefresh();
  263. });
  264. },
  265. /**
  266. * 页面上拉触底事件的处理函数
  267. */
  268. onReachBottom() {
  269. },
  270. /**
  271. * 用户点击右上角分享
  272. */
  273. onShareAppMessage() {
  274. return {
  275. title: '克拉玛依博物馆',
  276. path: '/pages/index/index?isFrom=weixin'
  277. };
  278. }
  279. })