index.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. // nova-werun/components/home/index.js
  2. const Parse = getApp().Parse;
  3. const company = getApp().globalData.company;
  4. Component({
  5. /**
  6. * 组件的属性列表
  7. */
  8. properties: {
  9. },
  10. /**
  11. * 组件的初始数据
  12. */
  13. data: {
  14. //屏幕高度
  15. statusBarHeight: 0, // 状态栏高度
  16. screenHeight: 0, // 屏幕高度
  17. customHeight: 0, // 自定义导航栏高度(如小程序右上角胶囊按钮)
  18. bottomNavHeight: 0, // 底部导航栏高度
  19. contentHeight: 0, // 可用内容高度
  20. contentpadding: 0, //顶部padding高度
  21. navheight: 0,
  22. percentage: 0,
  23. //选择
  24. rows: [{
  25. image: 'https://file-cloud.fmode.cn/qpFbRRSZrO/20241225/r5j1uc041211788.png',
  26. text: '签到打卡',
  27. url: '../../pages/home/signin/index'
  28. },
  29. {
  30. image: 'https://file-cloud.fmode.cn/qpFbRRSZrO/20241225/n1pin1040819673.png',
  31. text: '实时步行',
  32. url: '../../pages/home/sport/sport-home/index',
  33. },
  34. {
  35. image: 'https://file-cloud.fmode.cn/qpFbRRSZrO/20241225/9vfr1l040831635.png',
  36. text: '转发分享',
  37. url: '../../pages/home/share/index'
  38. },
  39. {
  40. image: 'https://file-cloud.fmode.cn/qpFbRRSZrO/20241225/it1rna040843179.png',
  41. text: '我的勋章',
  42. url: '../../pages/home/medal/index'
  43. },
  44. {
  45. image: 'https://file-cloud.fmode.cn/qpFbRRSZrO/20241225/r5193r040853498.png',
  46. text: '我的统计',
  47. url: '../../pages/home/statistics/index'
  48. },
  49. // {
  50. // image: 'https://file-cloud.fmode.cn/qpFbRRSZrO/20241102/v6p4fm041343296.png?imageView2/1/w/200/h/200',
  51. // text: '排行榜',
  52. // url: '../../pages/home/ranking/index'
  53. // },
  54. // {
  55. // image: 'https://file-cloud.fmode.cn/qpFbRRSZrO/20241102/71svpg041343669.png?imageView2/1/w/200/h/200',
  56. // text: '跑步',
  57. // url: '../../pages/home/sport/sport-home/index',
  58. // active: 1
  59. // },
  60. // {
  61. // image: 'https://file-cloud.fmode.cn/qpFbRRSZrO/20241104/k1q4no034958681.png?imageView2/1/w/200/h/200',
  62. // text: '积分',
  63. // url: '../../pages/home/integral/index'
  64. // },
  65. ],
  66. target: 0,
  67. sharList: [],
  68. address: "",
  69. //排行榜
  70. todayList: [],
  71. changetitle: 'today',
  72. steps:0,
  73. //正序
  74. rank: 'up'
  75. },
  76. lifetimes: {
  77. detached: function () {
  78. // 在组件实例被从页面节点树移除时执行
  79. },
  80. attached: async function () {
  81. // 在组件实例进入页面节点树时执行
  82. // 计算
  83. const systemInfo = wx.getSystemInfoSync();
  84. const statusBarHeight = systemInfo.statusBarHeight || 0;
  85. const screenHeight = systemInfo.screenHeight || 0;
  86. const custom = wx.getMenuButtonBoundingClientRect();
  87. const customHeight = custom.height + 10 + 2 || 0;
  88. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  89. const contentpadding = (statusBarHeight + customHeight) * 750 / systemInfo.windowWidth;
  90. const contentHeight = (screenHeight - bottomNavHeight - 50 - statusBarHeight - customHeight) * 750 / systemInfo.windowWidth;
  91. const navheight = (statusBarHeight + customHeight) * 750 / systemInfo.windowWidth;
  92. this.setData({
  93. statusBarHeight,
  94. screenHeight: (screenHeight - 50 - bottomNavHeight) * 750 / systemInfo.windowWidth,
  95. customHeight,
  96. bottomNavHeight,
  97. contentHeight,
  98. contentpadding,
  99. navheight,
  100. });
  101. this.gettarget()
  102. this.order()
  103. this.Getlocation()
  104. this.gettoday()
  105. this.getBackgroundColor(this.data.steps)
  106. },
  107. },
  108. /**
  109. * 组件的方法列表
  110. */
  111. methods: {
  112. //跳转
  113. gourl(e) {
  114. const url = e.currentTarget.dataset.url
  115. const active = e.currentTarget.dataset.active
  116. if (active) {
  117. wx.navigateTo({
  118. url: `${url}?id=` + active // 目标页面的路径
  119. });
  120. console.log(active);
  121. } else {
  122. wx.navigateTo({
  123. url: `${url}` // 目标页面的路径
  124. });
  125. }
  126. },
  127. //获取目标步数
  128. async gettarget() {
  129. const currentUser = Parse.User.current();
  130. let userquery = new Parse.Query('_User');
  131. userquery.equalTo('company', company);
  132. userquery.equalTo('objectId', currentUser.id);
  133. userquery.notEqualTo('isDeleted', true)
  134. let user = await userquery.find();
  135. let num = user.map(item => item.toJSON());
  136. if (num[0].num) {
  137. this.setData({
  138. target: num[0].num
  139. })
  140. console.log('当前步数', this.data.target);
  141. } else {
  142. const currentUser2 = Parse.User.current();
  143. let userquery2 = new Parse.Query('_User');
  144. userquery2.equalTo('company', company);
  145. userquery2.equalTo('objectId', currentUser2.id);
  146. userquery2.notEqualTo('isDeleted', true)
  147. let user2 = await userquery2.first();
  148. user2.set('num', 5000)
  149. try {
  150. let saveDate2 = await user2.save();
  151. console.log(saveDate2);
  152. this.setData({
  153. target: 5000
  154. })
  155. console.log("目标步数更改成功");
  156. } catch (error) {
  157. console.error("保存数据时出现错误:", error);
  158. }
  159. }
  160. },
  161. //获取当天运动数据
  162. async order() {
  163. const currentUser = Parse.User.current();
  164. let ActivityDataquery = new Parse.Query('ActivityData');
  165. ActivityDataquery.equalTo('user', currentUser.id);
  166. ActivityDataquery.equalTo('company', company);
  167. ActivityDataquery.notEqualTo('isDeleted', true);
  168. // 获取今天的日期
  169. const today = new Date();
  170. const todayStart = new Date(today.getFullYear(), today.getMonth(), today.getDate()); // 今天的开始时间
  171. const todayEnd = new Date(todayStart);
  172. todayEnd.setHours(23, 59, 59, 999); // 今天的结束时间
  173. // 在查询条件中添加对 createdAt 的限制
  174. ActivityDataquery.greaterThanOrEqualTo('createdAt', todayStart);
  175. ActivityDataquery.lessThanOrEqualTo('createdAt', todayEnd);
  176. ActivityDataquery.include('user');
  177. let r = await ActivityDataquery.find();
  178. let sharList = r.map(item => item.toJSON());
  179. let steps = 0
  180. sharList.forEach((item)=>{
  181. if(item.steps){
  182. steps = steps+item.steps
  183. }
  184. })
  185. this.setData({
  186. steps,
  187. })
  188. this.getBackgroundColor()
  189. console.log('sharList',steps);
  190. },
  191. //获取当前位置信息
  192. Getlocation() {
  193. // 获取当前位置信息
  194. wx.getLocation({
  195. type: 'wgs84',
  196. success: (res) => {
  197. const {
  198. latitude,
  199. longitude
  200. } = res;
  201. //调用api解析地址
  202. wx.request({
  203. url: 'https://api.map.baidu.com/reverse_geocoding/v3/?ak=sHZTomd7grslfP7sPKB8tRgT49FK9TEu&output=json&coordtype=gcj02&location=' + latitude + ',' + longitude,
  204. data: {},
  205. header: {
  206. 'Content-Type': 'application/json'
  207. },
  208. success: (ops) => { // 使用箭头函数
  209. console.log(ops);
  210. // const address = ops.data.result.formatted_address;//详细地址
  211. const address = ops.data.result.addressComponent.city; //市
  212. this.setData({
  213. address: address,
  214. });
  215. console.log(this.data.address);
  216. },
  217. fail: function (resq) {
  218. wx.showModal({
  219. title: '信息提示',
  220. content: '请求失败',
  221. showCancel: false,
  222. confirmColor: '#f37938'
  223. });
  224. },
  225. complete: function () {}
  226. })
  227. },
  228. fail: (err) => {
  229. console.error(err);
  230. wx.showToast({
  231. title: '获取位置失败',
  232. icon: 'none'
  233. });
  234. }
  235. });
  236. },
  237. // 判断日期是否是今天
  238. isToday(item) {
  239. const today = new Date();
  240. const date = new Date(item.timestamp * 1000); // 假设时间戳是以秒为单位
  241. // 比较年、月、日
  242. return date.getFullYear() === today.getFullYear() &&
  243. date.getMonth() === today.getMonth() &&
  244. date.getDate() === today.getDate();
  245. },
  246. //切换
  247. change() {
  248. // 使用数组来简化切换逻辑
  249. const titles = ['today', 'weekdday', 'month'];
  250. const currentIndex = titles.indexOf(this.data.changetitle); //获取index
  251. const nextIndex = (currentIndex + 1) % titles.length; // 循环切换
  252. this.setData({
  253. changetitle: titles[nextIndex]
  254. });
  255. if (this.data.changetitle == 'today') {
  256. this.setData({
  257. rank: 'up'
  258. })
  259. this.gettoday()
  260. }
  261. if (this.data.changetitle == 'weekdday') {
  262. this.setData({
  263. rank: 'up'
  264. })
  265. console.log('weekdday');
  266. }
  267. if (this.data.changetitle == 'month') {
  268. this.setData({
  269. rank: 'up'
  270. })
  271. console.log('month');
  272. }
  273. },
  274. // 获取本日排行
  275. async gettoday() {
  276. const currentUser = Parse.User.current();
  277. let ActivityDataquery = new Parse.Query('ActivityData');
  278. ActivityDataquery.equalTo('company', company);
  279. ActivityDataquery.notEqualTo('isDeleted', true);
  280. // 获取今天的日期
  281. const today = new Date();
  282. const todayStart = new Date(today.getFullYear(), today.getMonth(), today.getDate()); // 今天的开始时间
  283. const todayEnd = new Date(todayStart);
  284. todayEnd.setHours(23, 59, 59, 999); // 今天的结束时间
  285. console.log(todayStart, todayEnd);
  286. // 在查询条件中添加对 createdAt 的限制
  287. ActivityDataquery.greaterThanOrEqualTo('createdAt', todayStart);
  288. ActivityDataquery.lessThanOrEqualTo('createdAt', todayEnd);
  289. // 根据 steps 字段进行降序排序
  290. ActivityDataquery.include('user');
  291. try {
  292. let P = await ActivityDataquery.find();
  293. let todayList = P.map(item => item.toJSON());
  294. // 创建一个对象来存储用户的总步数
  295. const userStepsMap = {};
  296. // 遍历数据,累加相同用户的步数
  297. todayList.forEach(item => {
  298. const userId = item.user.objectId;
  299. const steps = item.steps;
  300. if (steps) {
  301. if (!userStepsMap[userId]) {
  302. userStepsMap[userId] = {
  303. ...item.user, // 包含用户信息
  304. totalSteps: 0 // 初始化总步数
  305. };
  306. }
  307. userStepsMap[userId].totalSteps += steps; // 累加步数
  308. }
  309. });
  310. // 将对象转换为数组
  311. const aggregatedList = Object.values(userStepsMap);
  312. // 按总步数降序排序
  313. aggregatedList.sort((a, b) => b.totalSteps - a.totalSteps);
  314. // 添加排名
  315. let currentRank = 1; // 当前排名
  316. for (let i = 0; i < aggregatedList.length; i++) {
  317. if (i > 0 && aggregatedList[i].totalSteps === aggregatedList[i - 1].totalSteps) {
  318. // 如果步数相同,排名相同
  319. aggregatedList[i].rank = aggregatedList[i - 1].rank;
  320. } else {
  321. // 否则,更新当前排名
  322. aggregatedList[i].rank = currentRank;
  323. }
  324. currentRank++;
  325. }
  326. // 更新页面数据
  327. this.setData({
  328. todayList: aggregatedList,
  329. });
  330. console.log(this.data.todayList);
  331. } catch (error) {
  332. console.error('Error fetching today\'s data:', error);
  333. }
  334. },
  335. //正序逆序
  336. changeup() {
  337. if (this.data.rank == 'up') {
  338. this.setData({
  339. rank: 'down'
  340. })
  341. if (this.data.changetitle == 'today') {
  342. this.setData({
  343. todayList: this.data.todayList.reverse(),
  344. })
  345. console.log('逆序');
  346. }
  347. if (this.data.changetitle == 'weekdday') {
  348. console.log('weekdday逆序');
  349. }
  350. if (this.data.changetitle == 'month') {
  351. console.log('month逆序');
  352. }
  353. } else {
  354. this.setData({
  355. rank: 'up'
  356. })
  357. if (this.data.changetitle == 'today') {
  358. this.setData({
  359. todayList: this.data.todayList.reverse(),
  360. })
  361. console.log('顺序');
  362. }
  363. if (this.data.changetitle == 'weekdday') {
  364. console.log('weekdday顺序');
  365. }
  366. if (this.data.changetitle == 'month') {
  367. console.log('month顺序');
  368. }
  369. }
  370. },
  371. //修改进度条
  372. getBackgroundColor() {
  373. let percentage = (this.data.steps / this.data.target) * 100;
  374. if (percentage > 100) {
  375. percentage = 100;
  376. }
  377. this.setData({
  378. percentage: `conic-gradient(from 0deg, #015EEA ${percentage}%, white 0%)`,
  379. })
  380. console.log('百分比', this.data.percentage);
  381. },
  382. }
  383. })