index.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. //屏幕高度
  11. statusBarHeight: 0, // 状态栏高度
  12. screenHeight: 0, // 屏幕高度
  13. customHeight: 0, // 自定义导航栏高度(如小程序右上角胶囊按钮)
  14. bottomNavHeight: 0, // 底部导航栏高度
  15. contentHeight: 0, // 可用内容高度
  16. contentHeight2: 0,
  17. contentpadding: 0, //顶部padding高度
  18. active: 0,
  19. //
  20. day: '7',
  21. target: '',
  22. sharList: [],
  23. //生涯数据
  24. stardate: '',
  25. daysDifference: "",
  26. totalSteps: 0,
  27. totalDistance: 0,
  28. totalBurnCalories: 0,
  29. totalsportDate: 0,
  30. ec: {
  31. onInit: null
  32. },
  33. resultList: [],
  34. stepsData: [], //统计图柱状
  35. xAxis: [], //x轴
  36. percentage: '',
  37. percent: '',
  38. //排名百分比
  39. percebtage:0
  40. },
  41. /**
  42. * 生命周期函数--监听页面加载
  43. */
  44. onLoad: async function (options) {
  45. // 计算
  46. const systemInfo = wx.getSystemInfoSync();
  47. const statusBarHeight = systemInfo.statusBarHeight || 0;
  48. const screenHeight = systemInfo.screenHeight || 0;
  49. const custom = wx.getMenuButtonBoundingClientRect();
  50. const customHeight = custom.height + 10 + 2 || 0;
  51. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  52. const contentpadding = (statusBarHeight + customHeight) * 750 / systemInfo.windowWidth;
  53. const contentHeight = (screenHeight - 50 - bottomNavHeight - statusBarHeight - customHeight) * 750 / systemInfo.windowWidth;
  54. this.setData({
  55. statusBarHeight,
  56. screenHeight,
  57. customHeight,
  58. bottomNavHeight,
  59. contentpadding,
  60. contentHeight
  61. });
  62. await this.gettarget()
  63. await this.order()
  64. this.allorder()
  65. await this.getweekday()
  66. this.echartsComponnet = this.selectComponent('#mychart')
  67. await this.initChart()
  68. this.gettoday()
  69. },
  70. //图表
  71. // 将 initChart 定义在 Page 上下文中
  72. initChart() {
  73. if (this.chart) {
  74. this.chart.clear();
  75. }
  76. this.echartsComponnet.init((canvas, width, height, dpr) => {
  77. // 初始化图表
  78. this.chart = echarts.init(canvas, null, {
  79. width: width,
  80. height: height,
  81. devicePixelRatio: dpr // 清晰度 使canvas的图表更加清晰
  82. });
  83. this.chart.setOption(this.getOption())
  84. return this.chart;
  85. });
  86. },
  87. getOption() {
  88. var option = {
  89. xAxis: {
  90. type: 'category',
  91. data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
  92. axisLabel: {
  93. formatter: (value, index) => this.formatter(value, index)
  94. }
  95. },
  96. yAxis: {
  97. type: 'value',
  98. max: 20000, // 设置Y轴最大值为20000
  99. axisLabel: {
  100. formatter: function (value) {
  101. if (value >= 10000) {
  102. return (value / 1000) + 'k'; // 格式化为20k
  103. }
  104. return value;
  105. }
  106. }
  107. },
  108. series: [{
  109. // data: [
  110. // 12000,
  111. // {
  112. // value: 12000,
  113. // itemStyle: {
  114. // color: '#a90000',
  115. // }
  116. // },
  117. // 12000,
  118. // 12000,
  119. // 12000,
  120. // 12000,
  121. // 12000
  122. // ],
  123. data: this.data.stepsData,
  124. type: 'bar',
  125. barWidth: '10',
  126. itemStyle: {
  127. borderRadius: 15,
  128. }
  129. }],
  130. };
  131. return option; // 返回 option 对象
  132. },
  133. // 返回x轴底下的字
  134. formatter(value, index) {
  135. let daylist = this.data.resultList.map(item => {
  136. const day = new Date(item.createdAt); // 获取当前日期
  137. const dayOfMonth = day.getDate(); // 获取今天是几号
  138. return dayOfMonth
  139. })
  140. daylist.reverse()
  141. const dayindex = daylist.length-1
  142. if (daylist.length < 7) {
  143. for (let i = daylist.length; i < 7; i++) {
  144. const lastDay = daylist[daylist.length - 1];
  145. daylist.push(lastDay + (i - daylist.length + 1)); // 补充后续日期
  146. }
  147. }
  148. const dates = daylist.map(item=>{
  149. return `${item}号`
  150. })
  151. return index === dayindex ? '今天' : value + '\n' + '\n' + dates[index];
  152. },
  153. /**
  154. * 生命周期函数--监听页面初次渲染完成
  155. */
  156. onReady: function () {
  157. },
  158. /**
  159. * 生命周期函数--监听页面显示
  160. */
  161. onShow: function () {
  162. },
  163. /**
  164. * 生命周期函数--监听页面隐藏
  165. */
  166. onHide: function () {
  167. },
  168. /**
  169. * 生命周期函数--监听页面卸载
  170. */
  171. onUnload: function () {
  172. },
  173. /**
  174. * 页面相关事件处理函数--监听用户下拉动作
  175. */
  176. onPullDownRefresh: function () {
  177. },
  178. /**
  179. * 页面上拉触底事件的处理函数
  180. */
  181. onReachBottom: function () {
  182. },
  183. /**
  184. * 用户点击右上角分享
  185. */
  186. onShareAppMessage: function () {
  187. },
  188. //底部tab栏修改
  189. onChange(event) {
  190. this.setData({
  191. active: event.detail
  192. });
  193. this.echartsComponnet = this.selectComponent('#mychart')
  194. this.initChart()
  195. },
  196. //修改30天或者7天数据
  197. changeday() {
  198. if (this.data.day == '7') {
  199. this.setData({
  200. day: '30'
  201. })
  202. } else {
  203. this.setData({
  204. day: '7'
  205. })
  206. }
  207. },
  208. //获取目标步数
  209. async gettarget() {
  210. const currentUser = Parse.User.current();
  211. let userquery = new Parse.Query('_User');
  212. userquery.equalTo('company', company);
  213. userquery.equalTo('objectId', currentUser.id);
  214. userquery.notEqualTo('isDeleted', true)
  215. let user = await userquery.find();
  216. let num = user.map(item => item.toJSON());
  217. if (num[0].num) {
  218. this.setData({
  219. target: num[0].num
  220. })
  221. console.log('当前步数', this.data.target);
  222. }
  223. },
  224. //获取当天运动数据
  225. async order() {
  226. const currentUser = Parse.User.current();
  227. let ActivityDataquery = new Parse.Query('ActivityData');
  228. ActivityDataquery.equalTo('user', currentUser.id);
  229. ActivityDataquery.equalTo('company', company);
  230. ActivityDataquery.equalTo('type', 'today');
  231. ActivityDataquery.notEqualTo('isDeleted', true);
  232. // 获取今天的日期
  233. const today = new Date();
  234. const todayStart = new Date(today.getFullYear(), today.getMonth(), today.getDate()); // 今天的开始时间
  235. const todayEnd = new Date(todayStart);
  236. todayEnd.setHours(23, 59, 59, 999); // 今天的结束时间
  237. // 在查询条件中添加对 createdAt 的限制
  238. ActivityDataquery.greaterThanOrEqualTo('createdAt', todayStart);
  239. ActivityDataquery.lessThanOrEqualTo('createdAt', todayEnd);
  240. ActivityDataquery.include('user');
  241. let r = await ActivityDataquery.find();
  242. let sharList = r.map(item => item.toJSON());
  243. this.setData({
  244. sharList
  245. });
  246. this.getBackgroundColor()
  247. console.log(this.data.sharList);
  248. },
  249. //获取生涯运动数据
  250. async allorder() {
  251. const currentUser = Parse.User.current();
  252. let ActivityDataquery = new Parse.Query('ActivityData');
  253. ActivityDataquery.equalTo('user', currentUser.id);
  254. ActivityDataquery.equalTo('company', company);
  255. ActivityDataquery.equalTo('type', 'today');
  256. ActivityDataquery.notEqualTo('isDeleted', true);
  257. let r = await ActivityDataquery.find();
  258. let allList = r.map(item => item.toJSON());
  259. // 根据 createdAt 日期从以前到现在排列
  260. allList.sort((a, b) => new Date(a.createdAt) - new Date(b.createdAt));
  261. // 获取最早的 createdAt 日期
  262. const earliestDate = new Date(allList[0].createdAt);
  263. // 格式化为 YYYY年MM月DD日
  264. const formattedDate = `${earliestDate.getFullYear()}年${String(earliestDate.getMonth() + 1).padStart(2, '0')}月${String(earliestDate.getDate()).padStart(2, '0')}日`;
  265. // 计算从最早日期到今天的天数
  266. const today = new Date();
  267. // 清零时间部分
  268. earliestDate.setHours(0, 0, 0, 0); // 将最早日期的时间部分清零
  269. today.setHours(0, 0, 0, 0); // 将今天的时间部分清零
  270. // 计算日期差值
  271. const timeDifference = today - earliestDate; // 时间差(毫秒)
  272. const daysDifference = Math.floor(timeDifference / (1000 * 60 * 60 * 24)); // 转换为天数
  273. // 计算 steps、distance 和 burnCalories 的总和
  274. let totalSteps = 0;
  275. let totalDistance = 0;
  276. let totalBurnCalories = 0;
  277. let totalsportDate = 0;
  278. allList.forEach(item => {
  279. totalSteps += Number(item.steps) || 0; // 确保为数字类型,避免 NaN
  280. totalDistance += Number(item.distance) || 0; // 确保为数字类型,避免 NaN
  281. totalBurnCalories += Number(item.burnCalories) || 0; // 确保为数字类型,避免 NaN
  282. totalsportDate += Number(item.sportDate) || 0;
  283. });
  284. // 将总运动时间转换为天、小时、分钟
  285. const days = Math.floor(totalsportDate / (60 * 24)); // 转换为天数
  286. const hours = Math.floor((totalsportDate % (60 * 24)) / 60); // 剩余小时
  287. const minutes = totalsportDate % 60; // 剩余分钟
  288. // 设置 stardate 和 daysDifference
  289. this.setData({
  290. stardate: formattedDate,
  291. daysDifference: daysDifference,
  292. totalSteps,
  293. totalDistance,
  294. totalBurnCalories,
  295. totalsportDate: `${days}天 ${hours}小时 ${minutes}分钟` // 格式化为字符串
  296. });
  297. console.log(allList, this.data.totalsportDate);
  298. },
  299. //修改光圈
  300. getBackgroundColor() {
  301. const steps = this.data.sharList[0].steps || 0;
  302. let percent = (steps / this.data.target) * 100;
  303. console.log('目标', percent);
  304. if (percent > 100) {
  305. percent = 100;
  306. }
  307. // 保留两位小数
  308. percent = parseFloat(percent.toFixed(2));
  309. this.setData({
  310. percent,
  311. percentage: `conic-gradient(from 0deg, #015EEA ${percent}%, white 0%)`,
  312. })
  313. console.log('百分比', this.data.percentage);
  314. },
  315. //获取本周记录
  316. async getweekday() {
  317. const currentUser = Parse.User.current();
  318. let ActivityDataquery = new Parse.Query('ActivityData');
  319. ActivityDataquery.equalTo('user', currentUser.id);
  320. ActivityDataquery.equalTo('company', company);
  321. ActivityDataquery.equalTo('type', 'today');
  322. ActivityDataquery.notEqualTo('isDeleted', true);
  323. let r = await ActivityDataquery.find();
  324. let allList = r.map(item => item.toJSON());
  325. // 按时间降序排序(从现在到以前)
  326. allList.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt));
  327. // 获取今天是星期几(0 = 周日, 1 = 周一, ..., 6 = 周六)
  328. const today = new Date();
  329. const dayOfWeek = today.getDay(); // 获取当前星期几
  330. // 根据星期几决定取多少条数据
  331. let numItemsToTake;
  332. if (dayOfWeek === 0) {
  333. numItemsToTake = 7; // 周日取7条数据
  334. } else {
  335. numItemsToTake = dayOfWeek; // 周一至周六取对应条数
  336. }
  337. // 取出对应数量的数据
  338. const resultList = allList.slice(0, numItemsToTake);
  339. const stepsData = resultList.map((item, index) => {
  340. if (index === 0) {
  341. // 如果是第一条数据,设置特殊样式
  342. return {
  343. value: item.steps,
  344. itemStyle: {
  345. color: '#a90000',
  346. }
  347. };
  348. } else {
  349. // 其他数据直接返回 steps
  350. return item.steps;
  351. }
  352. });
  353. this.setData({
  354. resultList,
  355. stepsData
  356. })
  357. console.log('resultList', resultList);
  358. },
  359. //获取今日排名百分比
  360. async gettoday() {
  361. const currentUser = Parse.User.current();
  362. let ActivityDataquery = new Parse.Query('ActivityData');
  363. ActivityDataquery.equalTo('company', company);
  364. ActivityDataquery.equalTo('type', "today");
  365. ActivityDataquery.notEqualTo('isDeleted', true);
  366. // 获取今天的日期
  367. const today = new Date();
  368. const todayStart = new Date(today.getFullYear(), today.getMonth(), today.getDate()); // 今天的开始时间
  369. const todayEnd = new Date(todayStart);
  370. todayEnd.setHours(23, 59, 59, 999); // 今天的结束时间
  371. console.log(todayStart, todayEnd);
  372. // 在查询条件中添加对 createdAt 的限制
  373. ActivityDataquery.greaterThanOrEqualTo('createdAt', todayStart);
  374. ActivityDataquery.lessThanOrEqualTo('createdAt', todayEnd);
  375. // 根据 steps 字段进行降序排序
  376. ActivityDataquery.descending('steps');
  377. ActivityDataquery.include('user');
  378. try {
  379. let P = await ActivityDataquery.find();
  380. let todayList = P.map(item => item.toJSON());
  381. console.log('todayList',todayList);
  382. let rank = 0
  383. // 更新页面数据
  384. todayList.forEach((item,index)=>{
  385. if(item.user.objectId==currentUser.id){
  386. rank = index+1;
  387. return
  388. }
  389. })
  390. var ranper =100 - (rank/todayList.length)*100
  391. ranper = parseFloat(ranper.toFixed(2));
  392. this.setData({
  393. percebtage:ranper
  394. })
  395. console.log('percebtage',this.data.percebtage);
  396. } catch (error) {
  397. console.error('Error fetching today\'s data:', error);
  398. }
  399. },
  400. })