index.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // nova-werun/pages/home/statistics/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. active: 0,
  18. //
  19. day: '7',
  20. target: '',
  21. sharList: [],
  22. //生涯数据
  23. stardate: '',
  24. daysDifference: "",
  25. totalSteps: 0,
  26. totalDistance: 0,
  27. totalBurnCalories: 0,
  28. totalsportDate:0
  29. },
  30. /**
  31. * 生命周期函数--监听页面加载
  32. */
  33. onLoad: function (options) {
  34. // 计算
  35. const systemInfo = wx.getSystemInfoSync();
  36. const statusBarHeight = systemInfo.statusBarHeight || 0;
  37. const screenHeight = systemInfo.screenHeight || 0;
  38. const custom = wx.getMenuButtonBoundingClientRect();
  39. const customHeight = custom.height + 10 + 2 || 0;
  40. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  41. const contentpadding = (statusBarHeight + customHeight) * 750 / systemInfo.windowWidth;
  42. const contentHeight = (screenHeight - 50 - bottomNavHeight - statusBarHeight - customHeight) * 750 / systemInfo.windowWidth;
  43. this.setData({
  44. statusBarHeight,
  45. screenHeight,
  46. customHeight,
  47. bottomNavHeight,
  48. contentpadding,
  49. contentHeight
  50. });
  51. this.gettarget()
  52. this.order()
  53. this.allorder()
  54. },
  55. /**
  56. * 生命周期函数--监听页面初次渲染完成
  57. */
  58. onReady: function () {
  59. },
  60. /**
  61. * 生命周期函数--监听页面显示
  62. */
  63. onShow: function () {
  64. },
  65. /**
  66. * 生命周期函数--监听页面隐藏
  67. */
  68. onHide: function () {
  69. },
  70. /**
  71. * 生命周期函数--监听页面卸载
  72. */
  73. onUnload: function () {
  74. },
  75. /**
  76. * 页面相关事件处理函数--监听用户下拉动作
  77. */
  78. onPullDownRefresh: function () {
  79. },
  80. /**
  81. * 页面上拉触底事件的处理函数
  82. */
  83. onReachBottom: function () {
  84. },
  85. /**
  86. * 用户点击右上角分享
  87. */
  88. onShareAppMessage: function () {
  89. },
  90. onChange(event) {
  91. this.setData({
  92. active: event.detail
  93. });
  94. },
  95. changeday() {
  96. if (this.data.day == '7') {
  97. this.setData({
  98. day: '30'
  99. })
  100. } else {
  101. this.setData({
  102. day: '7'
  103. })
  104. }
  105. },
  106. async gettarget() {
  107. const currentUser = Parse.User.current();
  108. let userquery = new Parse.Query('_User');
  109. userquery.equalTo('company', company);
  110. userquery.equalTo('objectId', currentUser.id);
  111. userquery.notEqualTo('isDeleted', true)
  112. let user = await userquery.find();
  113. let num = user.map(item => item.toJSON());
  114. if (num[0].num) {
  115. this.setData({
  116. target: num[0].num
  117. })
  118. console.log('当前步数', this.data.target);
  119. }
  120. },
  121. //获取当天运动数据
  122. async order() {
  123. const currentUser = Parse.User.current();
  124. let ActivityDataquery = new Parse.Query('ActivityData');
  125. ActivityDataquery.equalTo('user', currentUser.id);
  126. ActivityDataquery.equalTo('company', company);
  127. ActivityDataquery.equalTo('type', 'today');
  128. ActivityDataquery.notEqualTo('isDeleted', true);
  129. // 获取今天的日期
  130. const today = new Date();
  131. const todayStart = new Date(today.getFullYear(), today.getMonth(), today.getDate()); // 今天的开始时间
  132. const todayEnd = new Date(todayStart);
  133. todayEnd.setHours(23, 59, 59, 999); // 今天的结束时间
  134. // 在查询条件中添加对 createdAt 的限制
  135. ActivityDataquery.greaterThanOrEqualTo('createdAt', todayStart);
  136. ActivityDataquery.lessThanOrEqualTo('createdAt', todayEnd);
  137. ActivityDataquery.include('user');
  138. let r = await ActivityDataquery.find();
  139. let sharList = r.map(item => item.toJSON());
  140. this.setData({
  141. sharList
  142. });
  143. console.log(this.data.sharList);
  144. },
  145. //获取生涯运动数据
  146. async allorder() {
  147. const currentUser = Parse.User.current();
  148. let ActivityDataquery = new Parse.Query('ActivityData');
  149. ActivityDataquery.equalTo('user', currentUser.id);
  150. ActivityDataquery.equalTo('company', company);
  151. ActivityDataquery.equalTo('type', 'today');
  152. ActivityDataquery.notEqualTo('isDeleted', true);
  153. let r = await ActivityDataquery.find();
  154. let allList = r.map(item => item.toJSON());
  155. // 根据 createdAt 日期从以前到现在排列
  156. allList.sort((a, b) => new Date(a.createdAt) - new Date(b.createdAt));
  157. // 获取最早的 createdAt 日期
  158. const earliestDate = new Date(allList[0].createdAt);
  159. // 格式化为 YYYY年MM月DD日
  160. const formattedDate = `${earliestDate.getFullYear()}年${String(earliestDate.getMonth() + 1).padStart(2, '0')}月${String(earliestDate.getDate()).padStart(2, '0')}日`;
  161. // 计算从最早日期到今天的天数
  162. const today = new Date();
  163. // 清零时间部分
  164. earliestDate.setHours(0, 0, 0, 0); // 将最早日期的时间部分清零
  165. today.setHours(0, 0, 0, 0); // 将今天的时间部分清零
  166. // 计算日期差值
  167. const timeDifference = today - earliestDate; // 时间差(毫秒)
  168. const daysDifference = Math.floor(timeDifference / (1000 * 60 * 60 * 24)); // 转换为天数
  169. // 计算 steps、distance 和 burnCalories 的总和
  170. let totalSteps = 0;
  171. let totalDistance = 0;
  172. let totalBurnCalories = 0;
  173. let totalsportDate = 0;
  174. allList.forEach(item => {
  175. totalSteps += Number(item.steps) || 0; // 确保为数字类型,避免 NaN
  176. totalDistance += Number(item.distance) || 0; // 确保为数字类型,避免 NaN
  177. totalBurnCalories += Number(item.burnCalories) || 0; // 确保为数字类型,避免 NaN
  178. totalsportDate += Number(item.sportDate) || 0;
  179. });
  180. // 将总运动时间转换为天、小时、分钟
  181. const days = Math.floor(totalsportDate / (60 * 24)); // 转换为天数
  182. const hours = Math.floor((totalsportDate % (60 * 24)) / 60); // 剩余小时
  183. const minutes = totalsportDate % 60; // 剩余分钟
  184. // 设置 stardate 和 daysDifference
  185. this.setData({
  186. stardate: formattedDate,
  187. daysDifference: daysDifference,
  188. totalSteps,
  189. totalDistance,
  190. totalBurnCalories,
  191. totalsportDate: `${days}天 ${hours}小时 ${minutes}分钟` // 格式化为字符串
  192. });
  193. console.log(allList, this.data.totalsportDate);
  194. }
  195. })