邹能昇 2 月之前
父节点
当前提交
ccafaadee9
共有 2 个文件被更改,包括 61 次插入166 次删除
  1. 57 162
      nova-werun/components/home/index.js
  2. 4 4
      nova-werun/components/home/index.wxml

+ 57 - 162
nova-werun/components/home/index.js

@@ -77,7 +77,7 @@ Component({
         //排行榜
         todayList: [],
         changetitle: 'today',
-
+        steps:0,
         //正序
         rank: 'up'
     },
@@ -108,13 +108,11 @@ Component({
                 contentpadding,
                 navheight,
             });
-            console.log('123', contentpadding);
-            this.getWeRunData()
             this.gettarget()
-            // this.order()
+            this.order()
             this.Getlocation()
             this.gettoday()
-
+            this.getBackgroundColor(this.data.steps)
         },
     },
 
@@ -178,7 +176,6 @@ Component({
             let ActivityDataquery = new Parse.Query('ActivityData');
             ActivityDataquery.equalTo('user', currentUser.id);
             ActivityDataquery.equalTo('company', company);
-            ActivityDataquery.equalTo('type', 'today');
             ActivityDataquery.notEqualTo('isDeleted', true);
 
             // 获取今天的日期
@@ -194,12 +191,17 @@ Component({
 
             let r = await ActivityDataquery.find();
             let sharList = r.map(item => item.toJSON());
-
+            let steps = 0
+            sharList.forEach((item)=>{
+                if(item.steps){
+                    steps = steps+item.steps
+                }
+            })
             this.setData({
-                sharList
-            });
+                steps,
+            })
             this.getBackgroundColor()
-            console.log(this.data.sharList);
+            console.log('sharList',steps);
         },
         //获取当前位置信息
         Getlocation() {
@@ -248,79 +250,6 @@ Component({
                 }
             });
         },
-        //获取微信步数
-        getWeRunData() {
-            wx.getWeRunData({
-                success: (res) => {
-                    // 获取到的加密数据
-                    const encryptedData = res.encryptedData;
-                    const iv = res.iv;
-                    console.log('encryptedData', encryptedData);
-                    console.log('iv', iv);
-                    const userInfo = wx.getStorageSync('userInfo');
-                    const session_key = userInfo.session_key
-                    console.log('session_key', session_key);
-                    // 这里需要调用你的后端接口进行解密
-                    // 假设你有一个解密函数 decryptData
-                    this.decryptData(encryptedData, iv, session_key).then(steps => {
-                        this.setData({
-                            steps: steps
-                        });
-                        console.log('用户步数:', this.data.steps);
-                        this.updatetoday(steps)
-                    }).catch(err => {
-                        console.error('解密失败:', err);
-                    });   
-                },
-                fail: (err) => {
-                    console.error('获取运动数据失败:', err);
-                }
-            });
-        },
-        //  解密
-        decryptData(encryptedData, iv, session_key) {
-            return new Promise((resolve, reject) => {
-                // 发送请求到后端进行解密
-                wx.request({
-                    url: 'https://server.fmode.cn/api/wxapp/decrypt_phone', // 替换为你的后端解密接口
-                    method: 'get',
-                    data: {
-                        encryptedData: encryptedData,
-                        iv: iv,
-                        appId: 'wxe6ecc0193c09696c',
-                        sessionKey: session_key
-                    },
-                    success: (res) => {
-                        console.log('res',res);
-                        // if (res.data && res.data.steps) {
-                        //     console.log(res.data.steps);
-                        //     // resolve(res.data.steps); // 返回步数
-                        //     // const steps = 123456
-                        //     resolve(steps);
-                        // } else {
-                        //     reject('解密返回数据格式错误');
-                        // }
-                        if (res.data.data) {
-                            const stepInfoList = res.data.data.stepInfoList
-                            console.log('时间戳',stepInfoList);
-                            const todaylist = stepInfoList.filter(item => {
-                                console.log(this.isToday(item));
-                                return this.isToday(item); // 使用 isToday 函数判断是否是今天
-                            });
-                            const steps = todaylist[0].step;
-                            resolve(steps);
-                            console.log('今日步数', steps);
-                        } else {
-                            reject('解密返回数据格式错误');
-                        }
-
-                    },
-                    fail: (err) => {
-                        reject(err);
-                    }
-                });
-            });
-        },
         // 判断日期是否是今天
         isToday(item) {
             const today = new Date();
@@ -330,61 +259,6 @@ Component({
                 date.getMonth() === today.getMonth() &&
                 date.getDate() === today.getDate();
         },
-        //更新今日步数
-        async updatetoday(steps) {
-            const currentUser = Parse.User.current();
-            let ActivityDataquery = new Parse.Query('ActivityData');
-            ActivityDataquery.equalTo('user', currentUser.id);
-            ActivityDataquery.equalTo('company', company);
-            ActivityDataquery.equalTo('type', 'today');
-            ActivityDataquery.notEqualTo('isDeleted', true);
-            // 获取今天的日期
-            const today = new Date();
-            const todayStart = new Date(today.getFullYear(), today.getMonth(), today.getDate()); // 今天的开始时间
-            const todayEnd = new Date(todayStart);
-            todayEnd.setHours(23, 59, 59, 999); // 今天的结束时间
-
-            // 在查询条件中添加对 createdAt 的限制
-            ActivityDataquery.greaterThanOrEqualTo('createdAt', todayStart);
-            ActivityDataquery.lessThanOrEqualTo('createdAt', todayEnd);
-
-            let r = await ActivityDataquery.first();
-            if (r) {
-                r.set('steps', steps)
-                try {
-                    let saveDate = await r.save();
-                    this.order()
-                    this.gettoday()
-                    console.log("步数修改成功");
-                } catch (error) {
-                    console.error("保存数据时出现错误:", error);
-                }
-            } else {
-                const currentUser = Parse.User.current();
-                let userquery = new Parse.Query('_User');
-                userquery.equalTo('company', company);
-                userquery.equalTo('objectId', currentUser.id);
-                userquery.notEqualTo('isDeleted', true)
-                let user = await userquery.first();
-
-                let companyPointer = Parse.Object.extend('Company').createWithoutData(company);
-                let Comment = new Parse.Object('ActivityData');
-                Comment.set('company', companyPointer);
-                Comment.set('type', 'today');
-                Comment.set('user', user.toPointer());
-                Comment.set('steps', steps);
-                try {
-                    let saveDate2 = await Comment.save();
-                    this.order()
-                    this.gettoday()
-                    // console.log(saveDate2);
-                    console.log("步数保存成功");
-                } catch (error) {
-                    console.error("保存数据时出现错误:", error);
-                }
-            }
-
-        },
         //切换
         change() {
             // 使用数组来简化切换逻辑
@@ -418,45 +292,68 @@ Component({
             const currentUser = Parse.User.current();
             let ActivityDataquery = new Parse.Query('ActivityData');
             ActivityDataquery.equalTo('company', company);
-            ActivityDataquery.equalTo('type', "today");
             ActivityDataquery.notEqualTo('isDeleted', true);
-
+        
             // 获取今天的日期
             const today = new Date();
             const todayStart = new Date(today.getFullYear(), today.getMonth(), today.getDate()); // 今天的开始时间
             const todayEnd = new Date(todayStart);
             todayEnd.setHours(23, 59, 59, 999); // 今天的结束时间
             console.log(todayStart, todayEnd);
+            
             // 在查询条件中添加对 createdAt 的限制
             ActivityDataquery.greaterThanOrEqualTo('createdAt', todayStart);
             ActivityDataquery.lessThanOrEqualTo('createdAt', todayEnd);
-
+        
             // 根据 steps 字段进行降序排序
-            ActivityDataquery.descending('steps');
             ActivityDataquery.include('user');
-
+        
             try {
                 let P = await ActivityDataquery.find();
                 let todayList = P.map(item => item.toJSON());
-
-                // // 初始化 myList
-                // let myList = [];
-
-                // // 找到当前用户的数据并计算排名
-                // todayList.forEach((item, index) => {
-                //     if (item.user.objectId === currentUser.id) {
-                //         myList.push({
-                //             ...item, // 包含用户数据
-                //             rank: index + 1 // 计算排名(index 从 0 开始,所以加 1)
-                //         });
-                //     }
-                // });
-
+        
+                // 创建一个对象来存储用户的总步数
+                const userStepsMap = {};
+        
+                // 遍历数据,累加相同用户的步数
+                todayList.forEach(item => {
+                    const userId = item.user.objectId;
+                    const steps = item.steps;
+                    if (steps) {
+                        if (!userStepsMap[userId]) {
+                            userStepsMap[userId] = {
+                                ...item.user, // 包含用户信息
+                                totalSteps: 0 // 初始化总步数
+                            };
+                        }
+                        userStepsMap[userId].totalSteps += steps; // 累加步数
+                    }
+                });
+        
+                // 将对象转换为数组
+                const aggregatedList = Object.values(userStepsMap);
+        
+                // 按总步数降序排序
+                aggregatedList.sort((a, b) => b.totalSteps - a.totalSteps);
+        
+                // 添加排名
+                let currentRank = 1; // 当前排名
+                for (let i = 0; i < aggregatedList.length; i++) {
+                    if (i > 0 && aggregatedList[i].totalSteps === aggregatedList[i - 1].totalSteps) {
+                        // 如果步数相同,排名相同
+                        aggregatedList[i].rank = aggregatedList[i - 1].rank;
+                    } else {
+                        // 否则,更新当前排名
+                        aggregatedList[i].rank = currentRank;
+                    }
+                    currentRank++;
+                }
+        
                 // 更新页面数据
                 this.setData({
-                    todayList,
+                    todayList: aggregatedList,
                 });
-
+        
                 console.log(this.data.todayList);
             } catch (error) {
                 console.error('Error fetching today\'s data:', error);
@@ -500,9 +397,7 @@ Component({
         },
         //修改进度条
         getBackgroundColor() {
-            const steps = this.data.sharList[0].steps || 0;
-
-            let percentage = (steps / this.data.target) * 100;
+            let percentage = (this.data.steps / this.data.target) * 100;
             if (percentage > 100) {
                 percentage = 100;
             }

+ 4 - 4
nova-werun/components/home/index.wxml

@@ -25,7 +25,7 @@
             <view class="clockinbox2">
                 <view class="runbox" style="background:{{percentage}}">
                     <view class="run">
-                        <view class="step-num">{{sharList[0].steps||0}}</view>
+                        <view class="step-num">{{steps||0}}</view>
                         <view class="tody-steps">今日步数</view>
                     </view>
                 </view>
@@ -87,12 +87,12 @@
                     <view class="image2" wx:if="{{index==1}}">2</view>
                     <view class="image3" wx:if="{{index==2}}">3</view>
                     <view class="paiming" wx:if="{{index>=3}}">{{index+1}}</view>
-                    <image class="avarter" src="{{item.user.avatar}}"></image>
+                    <image class="avarter" src="{{item.avatar}}"></image>
                     <view class="name">
-                    {{item.user.nickname}}
+                    {{item.nickname}}
                     <view class="name2">已打卡12天</view>
                     </view>
-                    <view class="num">{{item.steps}}</view>
+                    <view class="num">{{item.totalSteps}}</view>
                 </view>
             </block>