index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. // nova-werun/pages/home/statistics/index.js
  2. import * as echarts from "../../../components/ec-canvas/echarts"
  3. const Parse = getApp().Parse;
  4. const company = getApp().globalData.company;
  5. const getSportData = require("../../../service/getSportData.js");
  6. const uid = Parse.User.current()?.id
  7. const user = Parse.User.current()
  8. const dateF = require("../../../../utils/date")
  9. Page({
  10. /**
  11. * 页面的初始数据
  12. */
  13. data: {
  14. //屏幕高度
  15. statusBarHeight: 0, // 状态栏高度
  16. screenHeight: 0, // 屏幕高度
  17. customHeight: 0, // 自定义导航栏高度(如小程序右上角胶囊按钮)
  18. bottomNavHeight: 0, // 底部导航栏高度
  19. contentHeight: 0, // 可用内容高度
  20. contentHeight2: 0,
  21. contentpadding: 0, //顶部padding高度
  22. active: 0,
  23. sportsDate: null, //运动数据 卡路里/时间/公里/步数
  24. option: null, //图表数据
  25. careerData: null, //生涯数据 注册日/卡路里/时间/公里/步数
  26. rankOfAll: null, //全网排名
  27. rece: 7, //近期趋势 7天/30天
  28. receDate: null, //趋势数据 每日平均/总计
  29. //
  30. day: '7',
  31. target: '',
  32. sharList: [],
  33. //生涯数据
  34. stardate: '',
  35. daysDifference: "",
  36. totalSteps: 0,
  37. totalDistance: 0,
  38. totalBurnCalories: 0,
  39. totalsportDate: 0,
  40. resultList: [],
  41. stepsData: [], //统计图柱状
  42. xAxis: [], //x轴
  43. percentage: '',
  44. percent: '',
  45. //排名百分比
  46. percebtage: 0,
  47. //
  48. average: 0,
  49. totle: 0,
  50. //显示日历
  51. todatDate: null,
  52. minDate: null,
  53. maxDate: null,
  54. formatter(day) {
  55. day.topInfo = '11111'
  56. return day;
  57. },
  58. },
  59. /**
  60. * 生命周期函数--监听页面加载
  61. */
  62. onLoad: async function (options) {
  63. let that = this
  64. // 计算
  65. const systemInfo = wx.getSystemInfoSync();
  66. const statusBarHeight = systemInfo.statusBarHeight || 0;
  67. const screenHeight = systemInfo.screenHeight || 0;
  68. const custom = wx.getMenuButtonBoundingClientRect();
  69. const customHeight = custom.height + 10 + 2 || 0;
  70. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  71. const contentpadding = (statusBarHeight + customHeight) * 750 / systemInfo.windowWidth;
  72. const contentHeight = (screenHeight - 50 - bottomNavHeight - statusBarHeight - customHeight) * 750 / systemInfo.windowWidth;
  73. this.setData({
  74. statusBarHeight,
  75. screenHeight,
  76. customHeight,
  77. bottomNavHeight,
  78. contentpadding,
  79. contentHeight
  80. });
  81. this.refersh01()
  82. },
  83. /** 底部tab栏修改*/
  84. async onChange(event) {
  85. if (event.detail == 0) {
  86. this.refersh01()
  87. } else {
  88. this.refersh02()
  89. }
  90. await this.setData({
  91. active: event.detail
  92. });
  93. },
  94. /**今日步数页面刷新 */
  95. async refersh01() {
  96. let that = this
  97. await this.getSportDate()
  98. this.gettarget()
  99. await this.getWeekData()
  100. this.setData({
  101. ec: {
  102. onInit: that.initChart
  103. },
  104. })
  105. },
  106. /**周期统计页面刷新 */
  107. async refersh02() {
  108. this.getCareer()
  109. this.getRankingOfAll()
  110. this.getTrend()
  111. this.getTrendSum()
  112. },
  113. /**获取运动数据 */
  114. async getSportDate() {
  115. let burnCalories = await getSportData.getwalk('burnCalories') || 0
  116. let sportDate = await getSportData.getwalk('sportDate') || 0
  117. let distance = await getSportData.getwalk('distance') || 0
  118. let steps = await getSportData.getwalk('steps') || 0
  119. this.setData({
  120. sportsDate: {
  121. burnCalories,
  122. sportDate,
  123. distance,
  124. steps
  125. }
  126. })
  127. },
  128. /** 获取目标步数*/
  129. async gettarget() {
  130. const currentUser = Parse.User.current();
  131. let userquery = new Parse.Query('_User');
  132. userquery.equalTo('company', company);
  133. userquery.equalTo('objectId', currentUser.id);
  134. userquery.notEqualTo('isDeleted', true)
  135. let user = await userquery.first();
  136. let num = user?.toJSON()
  137. if (num.num) {
  138. await this.setData({
  139. target: num.num
  140. })
  141. this.getBackgroundColor()
  142. }
  143. },
  144. /** 修改光圈*/
  145. getBackgroundColor() {
  146. const steps = this.data.sportsDate.steps || 0;
  147. let percent = (steps / this.data.target) * 100;
  148. if (percent > 100) {
  149. percent = 100;
  150. }
  151. percent = parseFloat(percent.toFixed(2));
  152. this.setData({
  153. percent,
  154. percentage: `conic-gradient(from 0deg, #015EEA ${percent}%, white 0%)`,
  155. })
  156. },
  157. /**获取本周步数 */
  158. async getWeekData() {
  159. let dates = [];
  160. let now = new Date();
  161. let currentDate = now.getDate();
  162. let currentDay = now.getDay();
  163. let monday = new Date(now);
  164. monday.setDate(currentDate - currentDay + 1);
  165. for (let i = 0; i < 7; i++) {
  166. let date = new Date(monday);
  167. date.setDate(monday.getDate() + i);
  168. date.setHours(0, 0, 0, 0);
  169. let nextDate = new Date(monday);
  170. nextDate.setDate(monday.getDate() + i + 1);
  171. nextDate.setHours(0, 0, 0, 0);
  172. let fromto = {
  173. from: date,
  174. to: nextDate
  175. }
  176. let d = await getSportData.getwalk('steps', '', fromto) || 0
  177. let item = {
  178. value: d,
  179. itemStyle: {
  180. color: '#4F9AF7'
  181. }
  182. }
  183. dates.push(item);
  184. }
  185. let option = {
  186. graphic: {
  187. z: -1
  188. },
  189. xAxis: {
  190. type: 'category',
  191. data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
  192. },
  193. yAxis: {
  194. type: 'value'
  195. },
  196. series: [{
  197. data: dates,
  198. type: 'bar'
  199. }]
  200. }
  201. this.setData({
  202. option
  203. })
  204. },
  205. /**获取生涯合计 */
  206. async getCareer() {
  207. let startDate = dateF.formatTime("YYYY年mm月dd日", user?.get('createdAt'))
  208. let fromTo = {
  209. from: user?.get('createdAt'),
  210. to: new Date()
  211. }
  212. let diff = Math.abs(fromTo.from.getTime() - fromTo.to.getTime());
  213. let days = 1000 * 60 * 60 * 24;
  214. let difDays = parseInt(Math.floor(diff / days))
  215. let steps = await getSportData.getwalk('steps', '', fromTo) || 0
  216. let burnCalories = await getSportData.getwalk('burnCalories', '', fromTo) || 0
  217. let sportDate = await getSportData.getwalk('sportDate', '', fromTo) || 0
  218. let distance = await getSportData.getwalk('distance', '', fromTo) / 1000 || 0
  219. sportDate = this.convertSeconds(sportDate)
  220. distance = distance.toFixed(2)
  221. this.setData({
  222. careerData: {
  223. startDate,
  224. difDays,
  225. steps,
  226. burnCalories,
  227. sportDate,
  228. distance
  229. }
  230. })
  231. },
  232. /**秒换算天小时 */
  233. convertSeconds(seconds) {
  234. let secondsInAnHour = 3600;
  235. let hours = Math.floor(seconds / secondsInAnHour);
  236. let days = Math.floor(hours / 24);
  237. return {
  238. days: days,
  239. hours: hours % 24 // 计算剩余的小时数
  240. };
  241. },
  242. /**获取全网排名 */
  243. async getRankingOfAll() {
  244. let steps = await getSportData.getwalk('steps', '')
  245. let rd = await getSportData.getUserRank(uid, '', 'today')
  246. let rank = rd[0]?.rank || 0
  247. // console.log(steps, rd, rank)
  248. let percent = 0
  249. if (rank > 0) {
  250. let query = new Parse.Query('_User')
  251. query.equalTo('company', company)
  252. query.equalTo('type', 'user')
  253. query.notEqualTo('isDeleted', true)
  254. let uCount = await query.count()
  255. percent = (100 * ((uCount - parseInt(rank) + 1) / uCount)).toFixed(2)
  256. }
  257. let rankOfAll = {
  258. steps,
  259. percent
  260. }
  261. this.setData({
  262. rankOfAll
  263. })
  264. },
  265. /**获取近期趋势 平均/总计 */
  266. async getTrendSum() {
  267. let {
  268. rece
  269. } = this.data
  270. rece = parseInt(rece)
  271. let to = new Date();
  272. let from = new Date(to);
  273. from.setDate(to.getDate() - rece);
  274. from.setHours(0, 0, 0, 0);
  275. let count = await getSportData.getwalk('steps', '', {from,to}) || 0
  276. let svg = parseInt(count / rece)
  277. this.setData({
  278. receDate: {
  279. count,
  280. svg
  281. }
  282. })
  283. },
  284. /**获取近期趋势 */
  285. async getTrend() {
  286. this.setData({
  287. option: null
  288. })
  289. let {
  290. rece
  291. } = this.data
  292. let dates = [];
  293. let xList = []
  294. let today = new Date();
  295. today.setHours(0, 0, 0, 0);
  296. let tomorrow = new Date(today);
  297. tomorrow.setDate(today.getDate() + 1);
  298. tomorrow.setHours(0, 0, 0, 0);
  299. for (let i = 0; i < rece; i++) {
  300. let from = new Date(today);
  301. from.setDate(today.getDate() - i)
  302. let to = new Date(tomorrow);
  303. to.setDate(tomorrow.getDate() - i)
  304. let fromTo = {
  305. from,
  306. to
  307. }
  308. let d = await getSportData.getwalk('steps', '', fromTo) || 0
  309. let item = {
  310. value: d,
  311. itemStyle: {
  312. color: '#4F9AF7'
  313. }
  314. }
  315. xList.push(dateF.formatTime("mm-dd", from))
  316. dates.push(item);
  317. }
  318. let option = {
  319. xAxis: {
  320. type: 'category',
  321. data: xList
  322. },
  323. yAxis: {
  324. type: 'value'
  325. },
  326. series: [{
  327. data: dates,
  328. type: 'bar'
  329. }]
  330. }
  331. await this.setData({
  332. option
  333. })
  334. let that = this
  335. this.setData({
  336. ec: {
  337. onInit: that.initChart
  338. },
  339. })
  340. },
  341. /**修改近期7/30 天 */
  342. async changeTrend(e) {
  343. let {
  344. rece
  345. } = this.data
  346. let {
  347. t
  348. } = e.currentTarget.dataset
  349. t = parseInt(t)
  350. // console.log(t, rece)
  351. if (t == rece) return
  352. await this.setData({
  353. rece: t
  354. })
  355. this.getTrendSum()
  356. this.getTrend()
  357. },
  358. /**设置图表 */
  359. initChart(canvas, width, height, dpr) {
  360. let {
  361. option
  362. } = this.data
  363. let chart = echarts.init(canvas, null, {
  364. width: width,
  365. height: height,
  366. devicePixelRatio: dpr
  367. });
  368. canvas.setChart(chart);
  369. chart.setOption(option);
  370. return chart;
  371. },
  372. /**
  373. * 生命周期函数--监听页面初次渲染完成
  374. */
  375. onReady: function () {},
  376. /**
  377. * 生命周期函数--监听页面显示
  378. */
  379. onShow: function () {
  380. },
  381. /**
  382. * 生命周期函数--监听页面隐藏
  383. */
  384. onHide: function () {
  385. },
  386. /**
  387. * 生命周期函数--监听页面卸载
  388. */
  389. onUnload: function () {
  390. },
  391. /**
  392. * 页面相关事件处理函数--监听用户下拉动作
  393. */
  394. onPullDownRefresh: function () {
  395. },
  396. /**
  397. * 页面上拉触底事件的处理函数
  398. */
  399. onReachBottom: function () {
  400. },
  401. /**
  402. * 用户点击右上角分享
  403. */
  404. onShareAppMessage: function () {
  405. },
  406. })