index.js 9.6 KB

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