index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. data: [
  37. 12000,
  38. {
  39. value: 12000,
  40. itemStyle: {
  41. color: '#a90000',
  42. }
  43. },
  44. 12000,
  45. 12000,
  46. 12000,
  47. 12000,
  48. 12000
  49. ],
  50. type: 'bar',
  51. barWidth: '10',
  52. itemStyle: {
  53. borderRadius: 15,
  54. }
  55. }],
  56. };
  57. chart.setOption(option);
  58. return chart;
  59. }
  60. Page({
  61. /**
  62. * 页面的初始数据
  63. */
  64. data: {
  65. //屏幕高度
  66. statusBarHeight: 0, // 状态栏高度
  67. screenHeight: 0, // 屏幕高度
  68. customHeight: 0, // 自定义导航栏高度(如小程序右上角胶囊按钮)
  69. bottomNavHeight: 0, // 底部导航栏高度
  70. contentHeight: 0, // 可用内容高度
  71. contentHeight2: 0,
  72. contentpadding: 0, //顶部padding高度
  73. active: 0,
  74. //
  75. day: '7',
  76. target: '',
  77. sharList: [],
  78. //生涯数据
  79. stardate: '',
  80. daysDifference: "",
  81. totalSteps: 0,
  82. totalDistance: 0,
  83. totalBurnCalories: 0,
  84. totalsportDate: 0,
  85. ec: {
  86. onInit: initChart
  87. },
  88. resultList: [],
  89. stepsData: [],
  90. percentage: '',
  91. percent: '',
  92. },
  93. /**
  94. * 生命周期函数--监听页面加载
  95. */
  96. onLoad: async function (options) {
  97. // 计算
  98. const systemInfo = wx.getSystemInfoSync();
  99. const statusBarHeight = systemInfo.statusBarHeight || 0;
  100. const screenHeight = systemInfo.screenHeight || 0;
  101. const custom = wx.getMenuButtonBoundingClientRect();
  102. const customHeight = custom.height + 10 + 2 || 0;
  103. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  104. const contentpadding = (statusBarHeight + customHeight) * 750 / systemInfo.windowWidth;
  105. const contentHeight = (screenHeight - 50 - bottomNavHeight - statusBarHeight - customHeight) * 750 / systemInfo.windowWidth;
  106. this.setData({
  107. statusBarHeight,
  108. screenHeight,
  109. customHeight,
  110. bottomNavHeight,
  111. contentpadding,
  112. contentHeight
  113. });
  114. await this.gettarget()
  115. await this.order()
  116. this.allorder()
  117. this.getweekday()
  118. },
  119. /**
  120. * 生命周期函数--监听页面初次渲染完成
  121. */
  122. onReady: function () {
  123. },
  124. /**
  125. * 生命周期函数--监听页面显示
  126. */
  127. onShow: function () {
  128. },
  129. /**
  130. * 生命周期函数--监听页面隐藏
  131. */
  132. onHide: function () {
  133. },
  134. /**
  135. * 生命周期函数--监听页面卸载
  136. */
  137. onUnload: function () {
  138. },
  139. /**
  140. * 页面相关事件处理函数--监听用户下拉动作
  141. */
  142. onPullDownRefresh: function () {
  143. },
  144. /**
  145. * 页面上拉触底事件的处理函数
  146. */
  147. onReachBottom: function () {
  148. },
  149. /**
  150. * 用户点击右上角分享
  151. */
  152. onShareAppMessage: function () {
  153. },
  154. //底部tab栏修改
  155. onChange(event) {
  156. this.setData({
  157. active: event.detail
  158. });
  159. },
  160. //修改30天或者7天数据
  161. changeday() {
  162. if (this.data.day == '7') {
  163. this.setData({
  164. day: '30'
  165. })
  166. } else {
  167. this.setData({
  168. day: '7'
  169. })
  170. }
  171. },
  172. //获取目标步数
  173. async gettarget() {
  174. const currentUser = Parse.User.current();
  175. let userquery = new Parse.Query('_User');
  176. userquery.equalTo('company', company);
  177. userquery.equalTo('objectId', currentUser.id);
  178. userquery.notEqualTo('isDeleted', true)
  179. let user = await userquery.find();
  180. let num = user.map(item => item.toJSON());
  181. if (num[0].num) {
  182. this.setData({
  183. target: num[0].num
  184. })
  185. console.log('当前步数', this.data.target);
  186. }
  187. },
  188. //获取当天运动数据
  189. async order() {
  190. const currentUser = Parse.User.current();
  191. let ActivityDataquery = new Parse.Query('ActivityData');
  192. ActivityDataquery.equalTo('user', currentUser.id);
  193. ActivityDataquery.equalTo('company', company);
  194. ActivityDataquery.equalTo('type', 'today');
  195. ActivityDataquery.notEqualTo('isDeleted', true);
  196. // 获取今天的日期
  197. const today = new Date();
  198. const todayStart = new Date(today.getFullYear(), today.getMonth(), today.getDate()); // 今天的开始时间
  199. const todayEnd = new Date(todayStart);
  200. todayEnd.setHours(23, 59, 59, 999); // 今天的结束时间
  201. // 在查询条件中添加对 createdAt 的限制
  202. ActivityDataquery.greaterThanOrEqualTo('createdAt', todayStart);
  203. ActivityDataquery.lessThanOrEqualTo('createdAt', todayEnd);
  204. ActivityDataquery.include('user');
  205. let r = await ActivityDataquery.find();
  206. let sharList = r.map(item => item.toJSON());
  207. this.setData({
  208. sharList
  209. });
  210. this.getBackgroundColor()
  211. console.log(this.data.sharList);
  212. },
  213. //获取生涯运动数据
  214. async allorder() {
  215. const currentUser = Parse.User.current();
  216. let ActivityDataquery = new Parse.Query('ActivityData');
  217. ActivityDataquery.equalTo('user', currentUser.id);
  218. ActivityDataquery.equalTo('company', company);
  219. ActivityDataquery.equalTo('type', 'today');
  220. ActivityDataquery.notEqualTo('isDeleted', true);
  221. let r = await ActivityDataquery.find();
  222. let allList = r.map(item => item.toJSON());
  223. // 根据 createdAt 日期从以前到现在排列
  224. allList.sort((a, b) => new Date(a.createdAt) - new Date(b.createdAt));
  225. // 获取最早的 createdAt 日期
  226. const earliestDate = new Date(allList[0].createdAt);
  227. // 格式化为 YYYY年MM月DD日
  228. const formattedDate = `${earliestDate.getFullYear()}年${String(earliestDate.getMonth() + 1).padStart(2, '0')}月${String(earliestDate.getDate()).padStart(2, '0')}日`;
  229. // 计算从最早日期到今天的天数
  230. const today = new Date();
  231. // 清零时间部分
  232. earliestDate.setHours(0, 0, 0, 0); // 将最早日期的时间部分清零
  233. today.setHours(0, 0, 0, 0); // 将今天的时间部分清零
  234. // 计算日期差值
  235. const timeDifference = today - earliestDate; // 时间差(毫秒)
  236. const daysDifference = Math.floor(timeDifference / (1000 * 60 * 60 * 24)); // 转换为天数
  237. // 计算 steps、distance 和 burnCalories 的总和
  238. let totalSteps = 0;
  239. let totalDistance = 0;
  240. let totalBurnCalories = 0;
  241. let totalsportDate = 0;
  242. allList.forEach(item => {
  243. totalSteps += Number(item.steps) || 0; // 确保为数字类型,避免 NaN
  244. totalDistance += Number(item.distance) || 0; // 确保为数字类型,避免 NaN
  245. totalBurnCalories += Number(item.burnCalories) || 0; // 确保为数字类型,避免 NaN
  246. totalsportDate += Number(item.sportDate) || 0;
  247. });
  248. // 将总运动时间转换为天、小时、分钟
  249. const days = Math.floor(totalsportDate / (60 * 24)); // 转换为天数
  250. const hours = Math.floor((totalsportDate % (60 * 24)) / 60); // 剩余小时
  251. const minutes = totalsportDate % 60; // 剩余分钟
  252. // 设置 stardate 和 daysDifference
  253. this.setData({
  254. stardate: formattedDate,
  255. daysDifference: daysDifference,
  256. totalSteps,
  257. totalDistance,
  258. totalBurnCalories,
  259. totalsportDate: `${days}天 ${hours}小时 ${minutes}分钟` // 格式化为字符串
  260. });
  261. console.log(allList, this.data.totalsportDate);
  262. },
  263. //修改光圈
  264. getBackgroundColor() {
  265. const steps = this.data.sharList[0].steps || 0;
  266. let percent = (steps / this.data.target) * 100;
  267. console.log('目标', percent);
  268. if (percent > 100) {
  269. percent = 100;
  270. }
  271. // 保留两位小数
  272. percent = parseFloat(percent.toFixed(2));
  273. this.setData({
  274. percent,
  275. percentage: `conic-gradient(from 0deg, #015EEA ${percent}%, white 0%)`,
  276. })
  277. console.log('百分比', this.data.percentage);
  278. },
  279. //获取本周记录
  280. async getweekday() {
  281. const currentUser = Parse.User.current();
  282. let ActivityDataquery = new Parse.Query('ActivityData');
  283. ActivityDataquery.equalTo('user', currentUser.id);
  284. ActivityDataquery.equalTo('company', company);
  285. ActivityDataquery.equalTo('type', 'today');
  286. ActivityDataquery.notEqualTo('isDeleted', true);
  287. let r = await ActivityDataquery.find();
  288. let allList = r.map(item => item.toJSON());
  289. // 按时间降序排序(从现在到以前)
  290. allList.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt));
  291. // 获取今天是星期几(0 = 周日, 1 = 周一, ..., 6 = 周六)
  292. const today = new Date();
  293. const dayOfWeek = today.getDay(); // 获取当前星期几
  294. // 根据星期几决定取多少条数据
  295. let numItemsToTake;
  296. if (dayOfWeek === 0) {
  297. numItemsToTake = 7; // 周日取7条数据
  298. } else {
  299. numItemsToTake = dayOfWeek; // 周一至周六取对应条数
  300. }
  301. // 取出对应数量的数据
  302. const resultList = allList.slice(0, numItemsToTake);
  303. const stepsData = resultList.map(item => item.steps);
  304. this.setData({
  305. resultList,
  306. stepsData
  307. })
  308. console.log('resultList', resultList);
  309. },
  310. })