// nova-werun/pages/home/sport/sport-start/index.js const Parse = getApp().Parse; const company = getApp().globalData.company; Page({ /** * 页面的初始数据 */ data: { //屏幕高度 statusBarHeight: 0, // 状态栏高度 screenHeight: 0, // 屏幕高度 customHeight: 0, // 自定义导航栏高度(如小程序右上角胶囊按钮) bottomNavHeight: 0, // 底部导航栏高度 contentHeight: 0, // 可用内容高度 contentHeight2: 0, contentpadding: 0, //顶部padding高度 navheight: 0, //地图 longitude: 0, latitude: 0, markers: [], //是否暂停 isstop: false, //标题 title: '', percentage: '', timer: null, startTime: 0, show: false, // timer: null, totalSeconds: 0, // 用于存储总秒数 formattedTime: '00:00:00', // 用于存储格式化后的时间 isRunning: false, // 计时器是否在运行 steps: 0, //运动步数 startsteps: 0, defferentstep: 0, }, /** * 生命周期函数--监听页面加载 */ onLoad: async function (options) { // 计算 const systemInfo = wx.getSystemInfoSync(); const statusBarHeight = systemInfo.statusBarHeight || 0; const screenHeight = systemInfo.screenHeight || 0; const custom = wx.getMenuButtonBoundingClientRect(); const customHeight = custom.height + 10 + 2 || 0; const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0; const navheight = (statusBarHeight + customHeight) * 750 / systemInfo.windowWidth; const contentHeight = (screenHeight - bottomNavHeight) * 750 / systemInfo.windowWidth; this.setData({ statusBarHeight, screenHeight, customHeight, bottomNavHeight, contentHeight, navheight, }); this.setData({ title: options.id }) // 获取初始步数 // 获取初始步数 this. getWeRunData2() console.log(this.data.startsteps); // 地图 await this.Getlocation(); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { this.setData({ totalSeconds: 0, // 重置总秒数 formattedTime: '00:00:00', // 重置格式化后的时间 isRunning: false // 初始化为未运行状态 }); this.startTimer() }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { // 页面隐藏时清除计时器 this.stopTimer(); }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { }, //获取当前位置信息 Getlocation() { // 获取当前位置信息 wx.getLocation({ type: 'wgs84', success: (res) => { const { latitude, longitude } = res; console.log('获取到的经纬度:', latitude, longitude); // 添加日志 //调用api解析地址 wx.request({ url: 'https://api.map.baidu.com/reverse_geocoding/v3/?ak=sHZTomd7grslfP7sPKB8tRgT49FK9TEu&output=json&coordtype=gcj02&location=' + latitude + ',' + longitude, data: {}, header: { 'Content-Type': 'application/json' }, success: (ops) => { // 使用箭头函数 console.log(ops); const address = ops.data.result.formatted_address; this.setData({ address: address, latitude: latitude, // 保证 latitude 被设置 longitude: longitude, // 保证 longitude 被设置 markers: [{ // 设置 markers id: 1, latitude: latitude, longitude: longitude, iconPath: 'https://file-cloud.fmode.cn/13WZ0W7u3l/20240724/7ebg0k104325941.png?imageView2/1/w/200/h/200', // 自定义标记图标 width: 40, height: 40, // callout: { // content: address, // 可以显示解析出的地址 // color: '#ffffff', // bgColor: '#7F56B2', // padding: 10, // borderRadius: 5, // display: 'ALWAYS' // } }] }); console.log(this.data.address); }, fail: function (resq) { wx.showModal({ title: '信息提示', content: '请求失败', showCancel: false, confirmColor: '#f37938' }); }, complete: function () {} }) }, fail: (err) => { console.error(err); wx.showToast({ title: '获取位置失败', icon: 'none' }); } }); }, stop() { this.setData({ isstop: !this.data.isstop }) if (this.data.isstop) { this.stopTimer() } else { this.startTimer() } console.log(this.data.isstop); }, //结束光环展示 startIncrease() { // 记录开始时间 this.setData({ startTime: Date.now(), }); // 清除之前的定时器 if (this.data.timer) { clearInterval(this.data.timer); } // 设置定时器,每隔 40 毫秒更新一次 percentage this.setData({ timer: setInterval(() => { const currentTime = Date.now(); const elapsedTime = currentTime - this.data.startTime; const percentage = Math.min((elapsedTime / 4000) * 100, 100); this.setData({ percentage: `conic-gradient(from 0deg, #015EEA ${percentage}%, white 0%)`, }); if (percentage >= 100) { wx.showToast({ title: '运动已结束', icon: 'success', duration: 500 }); setTimeout(() => { this.onConfirm() }, 500) } // 如果达到 100%,清除定时器 if (percentage >= 100) { clearInterval(this.data.timer); this.setData({ timer: null, }); } }, 40), }); }, stopIncrease() { // 清除定时器 if (this.data.timer) { clearInterval(this.data.timer); this.setData({ timer: null, }); // 如果未达到 100%,清零 percentage const elapsedTime = Date.now() - this.data.startTime; if (elapsedTime < 4000) { this.setData({ percentage: '', }); } } }, //点击返回按钮 goback() { this.setData({ show: true, isstop: false, }) }, //取消弹窗 onClose() { this.setData({ show: false }); }, //确认返回 onConfirm() { wx.navigateBack({ delta: 1 // 返回的页面层数,1 表示返回上一页 }); }, //开始计时或继续计时 startTimer: function () { if (this.data.isRunning) return; // 如果已经在运行,则不再启动 const that = this; this.data.timer = setInterval(function () { that.data.totalSeconds += 1; // 增加总秒数 that.setData({ formattedTime: that.formatTime(that.data.totalSeconds) // 更新格式化后的时间 }); // that.getWeRunData(); // 每5秒调用一次getWeRunData if (that.data.totalSeconds % 1 === 0) { that.getWeRunData(); // 调用获取微信步数的函数 } }, 1000); this.setData({ isRunning: true // 设置为运行状态 }); }, //暂停 stopTimer: function () { clearInterval(this.data.timer); this.setData({ isRunning: false // 设置为未运行状态 }); }, formatTime: function (seconds) { const hours = Math.floor(seconds / 3600); const minutes = Math.floor((seconds % 3600) / 60); const secs = seconds % 60; // 格式化为两位数 const formattedHours = String(hours).padStart(2, '0'); const formattedMinutes = String(minutes).padStart(2, '0'); const formattedSeconds = String(secs).padStart(2, '0'); return `${formattedHours}:${formattedMinutes}:${formattedSeconds}`; }, //获取初始步数 async getWeRunData2() { wx.getWeRunData({ success: (res) => { // 获取到的加密数据 const encryptedData = res.encryptedData; const iv = res.iv; const userInfo = wx.getStorageSync('userInfo'); const session_key = userInfo.session_key // 这里需要调用你的后端接口进行解密 // 假设你有一个解密函数 decryptData this.decryptData(encryptedData, iv, session_key).then(async steps => { await this.progressdate(steps) console.log('再次赋值'); await this.setData({ startsteps: steps, }); console.log('用户步数:', this.data.steps); }).catch(err => { console.error('解密失败:', err); }); }, fail: (err) => { console.error('获取运动数据失败:', err); } }); }, //获取微信步数 async getWeRunData() { wx.getWeRunData({ success: (res) => { // 获取到的加密数据 const encryptedData = res.encryptedData; const iv = res.iv; const userInfo = wx.getStorageSync('userInfo'); const session_key = userInfo.session_key // 这里需要调用你的后端接口进行解密 // 假设你有一个解密函数 decryptData this.decryptData(encryptedData, iv, session_key).then(async steps => { await this.progressdate(steps) console.log('再次赋值'); await this.setData({ steps: steps, defferentstep: steps - this.data.startsteps }); console.log('用户步数:', this.data.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) => { // 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 const todaylist = stepInfoList.filter(item => { console.log(this.isToday(item)); return this.isToday(item); // 使用 isToday 函数判断是否是今天 }); const steps = todaylist[0].step; resolve(steps); } else { reject('解密返回数据格式错误'); } }, fail: (err) => { reject(err); } }); }); }, // 判断日期是否是今天 isToday(item) { const today = new Date(); const date = new Date(item.timestamp * 1000); // 假设时间戳是以秒为单位 // 比较年、月、日 return date.getFullYear() === today.getFullYear() && date.getMonth() === today.getMonth() && date.getDate() === today.getDate(); }, //创建过程数据 async progressdate(steps) { if (steps == this.data.steps) { console.log('步数没变'); return } 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 query = new Parse.Object('ActivityRunLog'); query.set('user', user.toPointer()) query.set('company', companyPointer) query.set('stage', 'progress ') query.set('steps', this.data.steps) try { // let saveDate2 = await Comment.save(); console.log("新数据保存成功"); } catch (error) { console.error("保存数据时出现错误:", error); } }, })