index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // nova-werun/pages/home/ranking/index.js
  2. const Parse = getApp().Parse;
  3. const company = getApp().globalData.company;
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. //屏幕高度
  10. statusBarHeight: 0, // 状态栏高度
  11. screenHeight: 0, // 屏幕高度
  12. customHeight: 0, // 自定义导航栏高度(如小程序右上角胶囊按钮)
  13. bottomNavHeight: 0, // 底部导航栏高度
  14. contentHeight: 0, // 可用内容高度
  15. contentHeight2: 0,
  16. contentpadding: 0, //顶部padding高度
  17. //
  18. changetitle:'today',
  19. //本日排行
  20. todayList:[],
  21. myList:[]
  22. },
  23. /**
  24. * 生命周期函数--监听页面加载
  25. */
  26. onLoad: function (options) {
  27. // 计算
  28. const systemInfo = wx.getSystemInfoSync();
  29. const statusBarHeight = systemInfo.statusBarHeight || 0;
  30. const screenHeight = systemInfo.screenHeight || 0;
  31. const custom = wx.getMenuButtonBoundingClientRect();
  32. const customHeight = custom.height + 10 + 2 || 0;
  33. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  34. const contentpadding = (statusBarHeight + customHeight) * 750 / systemInfo.windowWidth;
  35. const contentHeight = (screenHeight - bottomNavHeight - statusBarHeight - customHeight) * 750 / systemInfo.windowWidth;
  36. this.setData({
  37. statusBarHeight,
  38. screenHeight,
  39. customHeight,
  40. bottomNavHeight,
  41. contentpadding,
  42. contentHeight
  43. });
  44. this.gettoday()
  45. },
  46. /**
  47. * 生命周期函数--监听页面初次渲染完成
  48. */
  49. onReady: function () {
  50. },
  51. /**
  52. * 生命周期函数--监听页面显示
  53. */
  54. onShow: function () {
  55. },
  56. /**
  57. * 生命周期函数--监听页面隐藏
  58. */
  59. onHide: function () {
  60. },
  61. /**
  62. * 生命周期函数--监听页面卸载
  63. */
  64. onUnload: function () {
  65. },
  66. /**
  67. * 页面相关事件处理函数--监听用户下拉动作
  68. */
  69. onPullDownRefresh: function () {
  70. },
  71. /**
  72. * 页面上拉触底事件的处理函数
  73. */
  74. onReachBottom: function () {
  75. },
  76. /**
  77. * 用户点击右上角分享
  78. */
  79. onShareAppMessage: function () {
  80. },
  81. change() {
  82. // 使用数组来简化切换逻辑
  83. const titles = ['today', 'weekdday', 'month'];
  84. const currentIndex = titles.indexOf(this.data.changetitle);//获取index
  85. const nextIndex = (currentIndex + 1) % titles.length; // 循环切换
  86. this.setData({
  87. changetitle: titles[nextIndex]
  88. });
  89. if(this.data.changetitle=='today'){
  90. this.gettoday()
  91. }
  92. if(this.data.changetitle=='weekdday'){
  93. console.log('weekdday');
  94. }
  95. if(this.data.changetitle=='month'){
  96. console.log('month');
  97. }
  98. },
  99. // 获取本日排行
  100. async gettoday() {
  101. const currentUser = Parse.User.current();
  102. let ActivityDataquery = new Parse.Query('ActivityData');
  103. ActivityDataquery.equalTo('company', company);
  104. ActivityDataquery.equalTo('type', "today");
  105. ActivityDataquery.notEqualTo('isDeleted', true);
  106. // 获取今天的日期
  107. const today = new Date();
  108. const todayStart = new Date(today.getFullYear(), today.getMonth(), today.getDate()); // 今天的开始时间
  109. const todayEnd = new Date(todayStart);
  110. todayEnd.setHours(23, 59, 59, 999); // 今天的结束时间
  111. // 在查询条件中添加对 createdAt 的限制
  112. ActivityDataquery.greaterThanOrEqualTo('createdAt', todayStart);
  113. ActivityDataquery.lessThanOrEqualTo('createdAt', todayEnd);
  114. // 根据 steps 字段进行降序排序
  115. ActivityDataquery.descending('steps');
  116. ActivityDataquery.include('user');
  117. try {
  118. let P = await ActivityDataquery.find();
  119. let todayList = P.map(item => item.toJSON());
  120. // 初始化 myList
  121. let myList = [];
  122. // 找到当前用户的数据并计算排名
  123. todayList.forEach((item, index) => {
  124. if (item.user.objectId === currentUser.id) {
  125. myList.push({
  126. ...item, // 包含用户数据
  127. rank: index + 1 // 计算排名(index 从 0 开始,所以加 1)
  128. });
  129. }
  130. });
  131. // 更新页面数据
  132. this.setData({
  133. todayList,
  134. myList
  135. });
  136. console.log(this.data.todayList, this.data.myList);
  137. } catch (error) {
  138. console.error('Error fetching today\'s data:', error);
  139. }
  140. },
  141. })