index.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // nova-werun/pages/home/statistics/index.js
  2. import * as echarts from "../../../components/ec-canvas/echarts.min"
  3. const Parse = getApp().Parse;
  4. const company = getApp().globalData.company;
  5. function initChart(canvas, width, height, dpr) {
  6. const chart = echarts.init(canvas, null, {
  7. width: width,
  8. height: height,
  9. devicePixelRatio: dpr
  10. });
  11. canvas.setChart(chart);
  12. var option = {
  13. xAxis: {
  14. type: 'category',
  15. data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
  16. axisLabel: {
  17. formatter: function(value, index) {
  18. const dates = ['1号', '2号', '3号', '4号', '5号', '6号', '7号'];
  19. return index === 1 ? '今天' : value + '\n'+'\n' + dates[index];
  20. }
  21. }
  22. },
  23. yAxis: {
  24. type: 'value',
  25. max: 20000, // 设置Y轴最大值为20000
  26. axisLabel: {
  27. formatter: function(value) {
  28. if (value >= 10000) {
  29. return (value / 10000) + 'k'; // 格式化为20k
  30. }
  31. return value;
  32. }
  33. }
  34. },
  35. series: [
  36. {
  37. data: [
  38. 12000,
  39. {
  40. value: 12000,
  41. itemStyle: {
  42. color: '#a90000',
  43. }
  44. },
  45. 12000,
  46. 12000,
  47. 12000,
  48. 12000,
  49. 12000
  50. ],
  51. type: 'bar',
  52. barWidth:'10',
  53. itemStyle: {
  54. borderRadius:15,
  55. }
  56. }
  57. ],
  58. };
  59. chart.setOption(option);
  60. return chart;
  61. }
  62. Page({
  63. /**
  64. * 页面的初始数据
  65. */
  66. data: {
  67. //屏幕高度
  68. statusBarHeight: 0, // 状态栏高度
  69. screenHeight: 0, // 屏幕高度
  70. customHeight: 0, // 自定义导航栏高度(如小程序右上角胶囊按钮)
  71. bottomNavHeight: 0, // 底部导航栏高度
  72. contentHeight: 0, // 可用内容高度
  73. contentHeight2: 0,
  74. contentpadding: 0, //顶部padding高度
  75. active: 0,
  76. //
  77. day: '7',
  78. target: '',
  79. sharList: [],
  80. //生涯数据
  81. stardate: '',
  82. daysDifference: "",
  83. totalSteps: 0,
  84. totalDistance: 0,
  85. totalBurnCalories: 0,
  86. totalsportDate: 0,
  87. ec: {
  88. onInit: initChart
  89. },
  90. },
  91. /**
  92. * 生命周期函数--监听页面加载
  93. */
  94. onLoad: function (options) {
  95. // 计算
  96. const systemInfo = wx.getSystemInfoSync();
  97. const statusBarHeight = systemInfo.statusBarHeight || 0;
  98. const screenHeight = systemInfo.screenHeight || 0;
  99. const custom = wx.getMenuButtonBoundingClientRect();
  100. const customHeight = custom.height + 10 + 2 || 0;
  101. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  102. const contentpadding = (statusBarHeight + customHeight) * 750 / systemInfo.windowWidth;
  103. const contentHeight = (screenHeight - 50 - bottomNavHeight - statusBarHeight - customHeight) * 750 / systemInfo.windowWidth;
  104. this.setData({
  105. statusBarHeight,
  106. screenHeight,
  107. customHeight,
  108. bottomNavHeight,
  109. contentpadding,
  110. contentHeight
  111. });
  112. this.gettarget()
  113. this.order()
  114. this.allorder()
  115. },
  116. /**
  117. * 生命周期函数--监听页面初次渲染完成
  118. */
  119. onReady: function () {
  120. },
  121. /**
  122. * 生命周期函数--监听页面显示
  123. */
  124. onShow: function () {
  125. },
  126. /**
  127. * 生命周期函数--监听页面隐藏
  128. */
  129. onHide: function () {
  130. },
  131. /**
  132. * 生命周期函数--监听页面卸载
  133. */
  134. onUnload: function () {
  135. },
  136. /**
  137. * 页面相关事件处理函数--监听用户下拉动作
  138. */
  139. onPullDownRefresh: function () {
  140. },
  141. /**
  142. * 页面上拉触底事件的处理函数
  143. */
  144. onReachBottom: function () {
  145. },
  146. /**
  147. * 用户点击右上角分享
  148. */
  149. onShareAppMessage: function () {
  150. },
  151. onChange(event) {
  152. this.setData({
  153. active: event.detail
  154. });
  155. },
  156. changeday() {
  157. if (this.data.day == '7') {
  158. this.setData({
  159. day: '30'
  160. })
  161. } else {
  162. this.setData({
  163. day: '7'
  164. })
  165. }
  166. },
  167. async gettarget() {
  168. const currentUser = Parse.User.current();
  169. let userquery = new Parse.Query('_User');
  170. userquery.equalTo('company', company);
  171. userquery.equalTo('objectId', currentUser.id);
  172. userquery.notEqualTo('isDeleted', true)
  173. let user = await userquery.find();
  174. let num = user.map(item => item.toJSON());
  175. if (num[0].num) {
  176. this.setData({
  177. target: num[0].num
  178. })
  179. console.log('当前步数', this.data.target);
  180. }
  181. },
  182. //获取当天运动数据
  183. async order() {
  184. const currentUser = Parse.User.current();
  185. let ActivityDataquery = new Parse.Query('ActivityData');
  186. ActivityDataquery.equalTo('user', currentUser.id);
  187. ActivityDataquery.equalTo('company', company);
  188. ActivityDataquery.equalTo('type', 'today');
  189. ActivityDataquery.notEqualTo('isDeleted', true);
  190. // 获取今天的日期
  191. const today = new Date();
  192. const todayStart = new Date(today.getFullYear(), today.getMonth(), today.getDate()); // 今天的开始时间
  193. const todayEnd = new Date(todayStart);
  194. todayEnd.setHours(23, 59, 59, 999); // 今天的结束时间
  195. // 在查询条件中添加对 createdAt 的限制
  196. ActivityDataquery.greaterThanOrEqualTo('createdAt', todayStart);
  197. ActivityDataquery.lessThanOrEqualTo('createdAt', todayEnd);
  198. ActivityDataquery.include('user');
  199. let r = await ActivityDataquery.find();
  200. let sharList = r.map(item => item.toJSON());
  201. this.setData({
  202. sharList
  203. });
  204. console.log(this.data.sharList);
  205. },
  206. //获取生涯运动数据
  207. async allorder() {
  208. const currentUser = Parse.User.current();
  209. let ActivityDataquery = new Parse.Query('ActivityData');
  210. ActivityDataquery.equalTo('user', currentUser.id);
  211. ActivityDataquery.equalTo('company', company);
  212. ActivityDataquery.equalTo('type', 'today');
  213. ActivityDataquery.notEqualTo('isDeleted', true);
  214. let r = await ActivityDataquery.find();
  215. let allList = r.map(item => item.toJSON());
  216. // 根据 createdAt 日期从以前到现在排列
  217. allList.sort((a, b) => new Date(a.createdAt) - new Date(b.createdAt));
  218. // 获取最早的 createdAt 日期
  219. const earliestDate = new Date(allList[0].createdAt);
  220. // 格式化为 YYYY年MM月DD日
  221. const formattedDate = `${earliestDate.getFullYear()}年${String(earliestDate.getMonth() + 1).padStart(2, '0')}月${String(earliestDate.getDate()).padStart(2, '0')}日`;
  222. // 计算从最早日期到今天的天数
  223. const today = new Date();
  224. // 清零时间部分
  225. earliestDate.setHours(0, 0, 0, 0); // 将最早日期的时间部分清零
  226. today.setHours(0, 0, 0, 0); // 将今天的时间部分清零
  227. // 计算日期差值
  228. const timeDifference = today - earliestDate; // 时间差(毫秒)
  229. const daysDifference = Math.floor(timeDifference / (1000 * 60 * 60 * 24)); // 转换为天数
  230. // 计算 steps、distance 和 burnCalories 的总和
  231. let totalSteps = 0;
  232. let totalDistance = 0;
  233. let totalBurnCalories = 0;
  234. let totalsportDate = 0;
  235. allList.forEach(item => {
  236. totalSteps += Number(item.steps) || 0; // 确保为数字类型,避免 NaN
  237. totalDistance += Number(item.distance) || 0; // 确保为数字类型,避免 NaN
  238. totalBurnCalories += Number(item.burnCalories) || 0; // 确保为数字类型,避免 NaN
  239. totalsportDate += Number(item.sportDate) || 0;
  240. });
  241. // 将总运动时间转换为天、小时、分钟
  242. const days = Math.floor(totalsportDate / (60 * 24)); // 转换为天数
  243. const hours = Math.floor((totalsportDate % (60 * 24)) / 60); // 剩余小时
  244. const minutes = totalsportDate % 60; // 剩余分钟
  245. // 设置 stardate 和 daysDifference
  246. this.setData({
  247. stardate: formattedDate,
  248. daysDifference: daysDifference,
  249. totalSteps,
  250. totalDistance,
  251. totalBurnCalories,
  252. totalsportDate: `${days}天 ${hours}小时 ${minutes}分钟` // 格式化为字符串
  253. });
  254. console.log(allList, this.data.totalsportDate);
  255. },
  256. })