index.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. isLoggedIn: false // 登录状态
  18. },
  19. /**
  20. * 生命周期函数--监听页面加载
  21. */
  22. onLoad(options) {
  23. // 检查登录状态
  24. this.checkLoginStatus();
  25. // 检查是否首次访问
  26. const hasVisited = wx.getStorageSync('hasVisited');
  27. if (!hasVisited) {
  28. // 首次访问,显示loading组件并隐藏导航栏
  29. // wx.hideNavigationBarLoading();
  30. // wx.setNavigationBarTitle({
  31. // title: ''
  32. // });
  33. wx.hideTabBar();
  34. this.setData({
  35. showLoading: true,
  36. isFirstOpen: true
  37. });
  38. this.initAllData();
  39. } else {
  40. // 非首次访问,立即显示页面内容
  41. this.setData({
  42. showLoading: false,
  43. isFirstOpen: false
  44. });
  45. // 设置已访问标记
  46. wx.setStorageSync('hasVisited', true);
  47. // 立即加载数据,但不显示loading状态
  48. this.initAllData();
  49. }
  50. },
  51. // loading组件"开始探索"事件处理
  52. onStartExplore() {
  53. // 设置已访问标记
  54. wx.setStorageSync('hasVisited', true);
  55. wx.showTabBar();
  56. this.setData({
  57. showLoading: false,
  58. isFirstOpen: false
  59. });
  60. // 恢复导航栏
  61. // wx.setNavigationBarTitle({
  62. // title: '克拉玛依博物馆'
  63. // });
  64. // 初始化数据
  65. // this.initAllData();
  66. },
  67. // 轮播图点击事件
  68. onBannerClick(e) {
  69. const url = e.currentTarget.dataset.url;
  70. if (url) {
  71. console.log('轮播图点击,跳转URL:', url);
  72. navigateToWebview(url);
  73. } else {
  74. console.log('轮播图URL为空,无法跳转');
  75. }
  76. },
  77. // 查看更多
  78. viewMore(e) {
  79. const section = e.currentTarget.dataset.section;
  80. console.log(`查看更多${section}`);
  81. switch (section) {
  82. case 'exhibition':
  83. wx.switchTab({
  84. url: '/pages/exhibition/index'
  85. });
  86. break;
  87. case 'recommended':
  88. wx.navigateTo({
  89. url: '/pages/index/news/news'
  90. });
  91. break;
  92. case 'activity':
  93. wx.navigateTo({
  94. url: '/pages/index/activity/activity'
  95. });
  96. break;
  97. }
  98. },
  99. /**
  100. * 初始化所有数据
  101. */
  102. async initAllData() {
  103. try {
  104. // 只有首次访问时才显示loading状态
  105. if (this.data.isFirstOpen) {
  106. this.setData({ loading: true });
  107. }
  108. // 并行请求所有数据
  109. const [bannerRes, newsRes, exhibitionRes, activeRes] = await Promise.all([
  110. this.getBannerData({ pageNum: 1, pageSize: 5, status: 1 }),
  111. this.getNewsList({ pageNum: 1, pageSize: 3, status: 1 }),
  112. this.getExhibitionList({ pageNum: 1, pageSize: 5, status: 1 , recommend: 1 }),
  113. this.getActiveList({ pageNum: 1, pageSize: 5, status: 1 })
  114. ]);
  115. // console.log('所有数据加载完成');
  116. } catch (error) {
  117. console.error('数据加载失败:', error);
  118. wx.showToast({
  119. title: '数据加载失败',
  120. icon: 'none'
  121. });
  122. } finally {
  123. // 只有首次访问时才需要关闭loading状态
  124. if (this.data.isFirstOpen) {
  125. this.setData({ loading: false });
  126. }
  127. }
  128. },
  129. /**
  130. * 获取轮播图数据
  131. */
  132. async getBannerData(params = {}) {
  133. try {
  134. const response = await museumApi.getCarouselList(params);
  135. console.log('轮播图数据:', response);
  136. const bannerData = response.records || response.list || response.data || response || [];
  137. this.setData({ bannerData });
  138. return response;
  139. } catch (error) {
  140. console.error('获取轮播图数据失败:', error);
  141. // 使用默认数据
  142. this.setData({
  143. bannerData: [{ carouselId: 1, title: '轮播图1', img: '' }]
  144. });
  145. throw error;
  146. }
  147. },
  148. /**
  149. * 获取资讯列表
  150. */
  151. async getNewsList(params = {}) {
  152. try {
  153. const response = await museumApi.getNewsList(params);
  154. console.log('资讯数据:', response);
  155. const newsList = response.records || response.list || response.data || response || [];
  156. this.setData({ newsList });
  157. return response;
  158. } catch (error) {
  159. console.error('获取资讯数据失败:', error);
  160. this.setData({ newsList: [] });
  161. throw error;
  162. }
  163. },
  164. /**
  165. * 获取展览列表
  166. */
  167. async getExhibitionList(params = {}) {
  168. try {
  169. const response = await museumApi.getExhibitionList(params);
  170. console.log('展览数据:', response);
  171. const exhibitionList = response.records || response.list || response.data || response || [];
  172. this.setData({ exhibitionList });
  173. return response;
  174. } catch (error) {
  175. console.error('获取展览数据失败:', error);
  176. this.setData({ exhibitionList: [] });
  177. throw error;
  178. }
  179. },
  180. /**
  181. * 获取活动列表
  182. */
  183. async getActiveList(params = {}) {
  184. try {
  185. const response = await museumApi.getSocialActivityList(params);
  186. console.log('活动数据:', response);
  187. const activeList = response.records || response.list || response.data || response || [];
  188. this.setData({ activeList });
  189. return response;
  190. } catch (error) {
  191. console.error('获取活动数据失败:', error);
  192. this.setData({ activeList: [] });
  193. throw error;
  194. }
  195. },
  196. /**
  197. * 功能点击处理
  198. */
  199. handleFunctionClick(e) {
  200. const { type } = e.currentTarget.dataset;
  201. console.log(`点击了${type}功能`);
  202. // 检查是否需要登录的功能
  203. if ((type === 'visit' || type === 'activity') && !this.data.isLoggedIn) {
  204. // 未登录,显示登录提示
  205. this.showLoginPrompt();
  206. return;
  207. }
  208. // 根据不同功能跳转到webview页面
  209. switch (type) {
  210. case 'visit':
  211. wx.navigateTo({
  212. url: '/pages/index/visit-preview/visit-preview'
  213. });
  214. break;
  215. case 'activity':
  216. wx.navigateTo({
  217. url: '/pages/index/active-preview/active-preview'
  218. });
  219. break;
  220. case 'map':
  221. this.handleMapClick();
  222. break;
  223. case 'introduce':
  224. this.navigateToWebview('/allDetailsShow?id=1&type=museum');
  225. break;
  226. }
  227. },
  228. /**
  229. * 查看展览详情
  230. */
  231. viewExhibition(e) {
  232. const { item } = e.currentTarget.dataset;
  233. console.log(`查看展览${item.exhibitId}详情`);
  234. this.navigateToWebview(`/allDetailsShow?isFrom=weixin&id=${item.exhibitId}&type=exhibition`);
  235. },
  236. /**
  237. * 查看活动详情
  238. */
  239. viewActivity(e) {
  240. const { item } = e.currentTarget.dataset;
  241. console.log(`查看活动${item.activityId}详情`);
  242. // this.navigateToWebview(`/allDetailsShow?isFrom=weixin&id=${item.activityId}&type=activity`);
  243. wx.navigateTo({
  244. url: `/pages/exhibition/activeDetails/index?isFrom=weixin&id=${item.activityId}&type=activity`
  245. });
  246. },
  247. /**
  248. * 查看资讯详情
  249. */
  250. viewNews(e) {
  251. const { item } = e.currentTarget.dataset;
  252. console.log(`查看资讯${item.informationId}详情`);
  253. this.navigateToWebview(`/allDetailsShow?isFrom=weixin&id=${item.informationId}&type=information`);
  254. },
  255. /**
  256. * 处理地图点击事件
  257. */
  258. async handleMapClick() {
  259. // this.navigateToWebview('/indexPage/map?isFrom=weixin');
  260. wx.navigateTo({
  261. url: '/pages/user/map/index'
  262. });
  263. },
  264. /**
  265. * 导航到webview页面
  266. */
  267. navigateToWebview(path) {
  268. navigateToWebview(path);
  269. },
  270. /**
  271. * 生命周期函数--监听页面初次渲染完成
  272. */
  273. onReady() {
  274. },
  275. /**
  276. * 生命周期函数--监听页面显示
  277. */
  278. onShow() {
  279. // 每次显示页面时检查登录状态
  280. this.checkLoginStatus();
  281. },
  282. /**
  283. * 检查登录状态
  284. */
  285. checkLoginStatus() {
  286. const app = getApp();
  287. const token = wx.getStorageSync('token');
  288. const isLoggedIn = !!(token && !app.globalData.isGuest);
  289. this.setData({
  290. isLoggedIn: isLoggedIn
  291. });
  292. },
  293. /**
  294. * 显示登录提示
  295. */
  296. showLoginPrompt() {
  297. const app = getApp();
  298. wx.showModal({
  299. title: '登录提示',
  300. content: '为了给您提供更好的服务,需要获取您的微信登录信息,是否同意?',
  301. confirmText: '立即登录',
  302. cancelText: '取消',
  303. success: (res) => {
  304. if (res.confirm) {
  305. // 用户确认登录,调用app的登录方法
  306. app.wxLogin();
  307. // 登录弹窗关闭后,页面会重新显示,onShow会被触发,从而更新登录状态
  308. }
  309. }
  310. });
  311. },
  312. /**
  313. * 生命周期函数--监听页面隐藏
  314. */
  315. onHide() {
  316. },
  317. /**
  318. * 生命周期函数--监听页面卸载
  319. */
  320. onUnload() {
  321. },
  322. /**
  323. * 页面相关事件处理函数--监听用户下拉刷新
  324. */
  325. onPullDownRefresh() {
  326. this.initAllData().finally(() => {
  327. wx.stopPullDownRefresh();
  328. });
  329. },
  330. /**
  331. * 页面上拉触底事件的处理函数
  332. */
  333. onReachBottom() {
  334. },
  335. /**
  336. * 用户点击右上角分享
  337. */
  338. onShareAppMessage() {
  339. return {
  340. title: '克拉玛依博物馆',
  341. path: '/pages/index/index?isFrom=weixin'
  342. };
  343. },
  344. /**
  345. * 用户点击右上角分享到朋友圈
  346. */
  347. onShareTimeline() {
  348. return {
  349. title: '克拉玛依博物馆 - 探索历史文化之美',
  350. query: 'isFrom=weixin',
  351. imageUrl: '' // 可以设置自定义分享图片
  352. };
  353. }
  354. })