index.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. // nova-werun/pages/home/sport/sport-start/index.js
  2. const Parse = getApp().Parse;
  3. const company = getApp().globalData.company;
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. //屏幕高度
  10. statusBarHeight: 0, // 状态栏高度
  11. screenHeight: 0, // 屏幕高度
  12. customHeight: 0, // 自定义导航栏高度(如小程序右上角胶囊按钮)
  13. bottomNavHeight: 0, // 底部导航栏高度
  14. contentHeight: 0, // 可用内容高度
  15. contentHeight2: 0,
  16. contentpadding: 0, //顶部padding高度
  17. navheight: 0,
  18. //地图
  19. longitude: 0,
  20. latitude: 0,
  21. markers: [],
  22. //是否暂停
  23. isstop: false,
  24. //标题
  25. title: '',
  26. percentage: '',
  27. timer: null,
  28. startTime: 0,
  29. show: false,
  30. //
  31. timer2: null,
  32. totalSeconds: 0, // 用于存储总秒数
  33. formattedTime: '00:00:00', // 用于存储格式化后的时间
  34. isRunning: false, // 计时器是否在运行
  35. steps: 0, //运动步数
  36. startsteps: 0,
  37. defferentstep: 0,
  38. distance: 0,
  39. calorie: 0,
  40. pace: 0,
  41. //
  42. activitdateid: '',
  43. },
  44. /**
  45. * 生命周期函数--监听页面加载
  46. */
  47. onLoad: async function (options) {
  48. // 计算
  49. const systemInfo = wx.getSystemInfoSync();
  50. const statusBarHeight = systemInfo.statusBarHeight || 0;
  51. const screenHeight = systemInfo.screenHeight || 0;
  52. const custom = wx.getMenuButtonBoundingClientRect();
  53. const customHeight = custom.height + 10 + 2 || 0;
  54. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  55. const navheight = (statusBarHeight + customHeight) * 750 / systemInfo.windowWidth;
  56. const contentHeight = (screenHeight - bottomNavHeight) * 750 / systemInfo.windowWidth;
  57. this.setData({
  58. statusBarHeight,
  59. screenHeight,
  60. customHeight,
  61. bottomNavHeight,
  62. contentHeight,
  63. navheight,
  64. });
  65. this.setData({
  66. title: options.id
  67. })
  68. // 获取初始步数
  69. // 获取初始步数
  70. this.getWeRunData2()
  71. console.log(this.data.startsteps);
  72. // 地图
  73. await this.Getlocation();
  74. //开启后台定位
  75. this.startbackgroumd()
  76. //创建开始数据
  77. this.startdate()
  78. },
  79. /**
  80. * 生命周期函数--监听页面初次渲染完成
  81. */
  82. onReady: function () {
  83. },
  84. /**
  85. * 生命周期函数--监听页面显示
  86. */
  87. onShow: function () {
  88. if (!this.data.isRunning && !this.data.isstop) {
  89. this.startTimer();
  90. }
  91. },
  92. /**
  93. * 生命周期函数--监听页面隐藏
  94. */
  95. onHide: function () {
  96. if (this.data.isRunning) {
  97. this.stopTimer();
  98. }
  99. },
  100. /**
  101. * 生命周期函数--监听页面卸载
  102. */
  103. onUnload: function () {
  104. this.stopTimer();
  105. },
  106. /**
  107. * 页面相关事件处理函数--监听用户下拉动作
  108. */
  109. onPullDownRefresh: function () {
  110. },
  111. /**
  112. * 页面上拉触底事件的处理函数
  113. */
  114. onReachBottom: function () {
  115. },
  116. /**
  117. * 用户点击右上角分享
  118. */
  119. onShareAppMessage: function () {
  120. },
  121. //获取当前位置信息
  122. Getlocation() {
  123. // 获取当前位置信息
  124. wx.getLocation({
  125. type: 'gcj02',
  126. success: (res) => {
  127. const {
  128. latitude,
  129. longitude
  130. } = res;
  131. console.log('获取到的经纬度:', latitude, longitude); // 添加日志
  132. //调用api解析地址
  133. wx.request({
  134. url: 'https://api.map.baidu.com/reverse_geocoding/v3/?ak=sHZTomd7grslfP7sPKB8tRgT49FK9TEu&output=json&coordtype=gcj02&location=' + latitude + ',' + longitude,
  135. data: {},
  136. header: {
  137. 'Content-Type': 'application/json'
  138. },
  139. success: (ops) => { // 使用箭头函数
  140. const address = ops.data.result.formatted_address;
  141. this.setData({
  142. address: address,
  143. latitude: latitude, // 保证 latitude 被设置
  144. longitude: longitude, // 保证 longitude 被设置
  145. markers: [{ // 设置 markers
  146. id: 1,
  147. latitude: latitude,
  148. longitude: longitude,
  149. iconPath: 'https://file-cloud.fmode.cn/13WZ0W7u3l/20240724/7ebg0k104325941.png?imageView2/1/w/200/h/200', // 自定义标记图标
  150. width: 40,
  151. height: 40,
  152. // callout: {
  153. // content: address, // 可以显示解析出的地址
  154. // color: '#ffffff',
  155. // bgColor: '#7F56B2',
  156. // padding: 10,
  157. // borderRadius: 5,
  158. // display: 'ALWAYS'
  159. // }
  160. }]
  161. });
  162. },
  163. fail: function (resq) {
  164. wx.showModal({
  165. title: '信息提示',
  166. content: '请求失败',
  167. showCancel: false,
  168. confirmColor: '#f37938'
  169. });
  170. },
  171. complete: function () {}
  172. })
  173. },
  174. fail: (err) => {
  175. console.error(err);
  176. wx.showToast({
  177. title: '获取位置失败',
  178. icon: 'none'
  179. });
  180. }
  181. });
  182. },
  183. stop() {
  184. this.setData({
  185. isstop: !this.data.isstop
  186. })
  187. if (this.data.isstop) {
  188. this.stopTimer()
  189. } else {
  190. this.startTimer()
  191. }
  192. console.log(this.data.isstop);
  193. },
  194. //结束光环展示
  195. startIncrease() {
  196. // 记录开始时间
  197. this.setData({
  198. startTime: Date.now(),
  199. });
  200. // 清除之前的定时器
  201. if (this.data.timer) {
  202. clearInterval(this.data.timer);
  203. }
  204. // 设置定时器,每隔 40 毫秒更新一次 percentage
  205. this.setData({
  206. timer: setInterval(() => {
  207. const currentTime = Date.now();
  208. const elapsedTime = currentTime - this.data.startTime;
  209. const percentage = Math.min((elapsedTime / 4000) * 100, 100);
  210. this.setData({
  211. percentage: `conic-gradient(from 0deg, #015EEA ${percentage}%, white 0%)`,
  212. });
  213. if (percentage >= 100) {
  214. wx.showToast({
  215. title: '运动已结束',
  216. icon: 'success',
  217. duration: 500
  218. });
  219. this.enddate() //结束数据
  220. this.endActivitydate()
  221. setTimeout(() => {
  222. this.onConfirm()
  223. }, 500)
  224. }
  225. // 如果达到 100%,清除定时器
  226. if (percentage >= 100) {
  227. clearInterval(this.data.timer);
  228. this.setData({
  229. timer: null,
  230. });
  231. }
  232. }, 40),
  233. });
  234. },
  235. stopIncrease() {
  236. // 清除定时器
  237. if (this.data.timer) {
  238. clearInterval(this.data.timer);
  239. this.setData({
  240. timer: null,
  241. });
  242. // 如果未达到 100%,清零 percentage
  243. const elapsedTime = Date.now() - this.data.startTime;
  244. if (elapsedTime < 4000) {
  245. this.setData({
  246. percentage: '',
  247. });
  248. }
  249. }
  250. },
  251. //点击返回按钮
  252. goback() {
  253. this.setData({
  254. show: true,
  255. isstop: false,
  256. })
  257. },
  258. //取消弹窗
  259. onClose() {
  260. this.setData({
  261. show: false
  262. });
  263. },
  264. //确认返回
  265. onConfirm() {
  266. wx.navigateBack({
  267. delta: 1 // 返回的页面层数,1 表示返回上一页
  268. });
  269. this.stopBackgroundLocation()// 停止后台定位和位置监听
  270. },
  271. //开始计时或继续计时
  272. startTimer: function () {
  273. if (this.data.isRunning) return; // 如果已经在运行,则不再启动
  274. const that = this;
  275. this.data.timer2 = setInterval(function () {
  276. that.data.totalSeconds += 1; // 增加总秒数
  277. that.setData({
  278. formattedTime: that.formatTime(that.data.totalSeconds) // 更新格式化后的时间
  279. });
  280. // 每30秒调用一次getWeRunData
  281. if (that.data.totalSeconds % 4 === 0) {
  282. that.getWeRunData(); // 调用获取微信步数的函数
  283. }
  284. that.onLocationChange((res) => {
  285. // 这里可以处理位置变化的逻辑
  286. console.log('位置已更新:', res);
  287. });
  288. }, 1000);
  289. this.setData({
  290. isRunning: true // 设置为运行状态
  291. });
  292. },
  293. //暂停
  294. stopTimer: function () {
  295. clearInterval(this.data.timer2);
  296. this.setData({
  297. isRunning: false // 设置为未运行状态
  298. });
  299. wx.offLocationChange();
  300. },
  301. formatTime: function (seconds) {
  302. const hours = Math.floor(seconds / 3600);
  303. const minutes = Math.floor((seconds % 3600) / 60);
  304. const secs = seconds % 60;
  305. // 格式化为两位数
  306. const formattedHours = String(hours).padStart(2, '0');
  307. const formattedMinutes = String(minutes).padStart(2, '0');
  308. const formattedSeconds = String(secs).padStart(2, '0');
  309. return `${formattedHours}:${formattedMinutes}:${formattedSeconds}`;
  310. },
  311. //获取初始步数
  312. async getWeRunData2() {
  313. wx.getWeRunData({
  314. success: (res) => {
  315. // 获取到的加密数据
  316. const encryptedData = res.encryptedData;
  317. const iv = res.iv;
  318. const userInfo = wx.getStorageSync('userInfo');
  319. const session_key = userInfo.session_key
  320. // 这里需要调用你的后端接口进行解密
  321. // 假设你有一个解密函数 decryptData
  322. this.decryptData(encryptedData, iv, session_key).then(async steps => {
  323. console.log('再次赋值');
  324. await this.setData({
  325. startsteps: steps,
  326. });
  327. console.log('用户步数:', this.data.steps);
  328. }).catch(err => {
  329. console.error('解密失败:', err);
  330. });
  331. },
  332. fail: (err) => {
  333. console.error('获取运动数据失败:', err);
  334. }
  335. });
  336. },
  337. //获取微信步数
  338. async getWeRunData() {
  339. wx.getWeRunData({
  340. success: (res) => {
  341. // 获取到的加密数据
  342. const encryptedData = res.encryptedData;
  343. const iv = res.iv;
  344. const userInfo = wx.getStorageSync('userInfo');
  345. const session_key = userInfo.session_key
  346. // 这里需要调用你的后端接口进行解密
  347. // 假设你有一个解密函数 decryptData
  348. this.decryptData(encryptedData, iv, session_key).then(async steps => {
  349. console.log('再次赋值');
  350. await this.setData({
  351. steps: steps,
  352. defferentstep: steps - this.data.startsteps
  353. });
  354. console.log('用户步数:', this.data.steps);
  355. }).catch(err => {
  356. console.error('解密失败:', err);
  357. });
  358. },
  359. fail: (err) => {
  360. console.error('获取运动数据失败:', err);
  361. }
  362. });
  363. },
  364. // 解密
  365. decryptData(encryptedData, iv, session_key) {
  366. return new Promise((resolve, reject) => {
  367. // 发送请求到后端进行解密
  368. wx.request({
  369. url: 'https://server.fmode.cn/api/wxapp/decrypt_phone', // 替换为你的后端解密接口
  370. method: 'get',
  371. data: {
  372. encryptedData: encryptedData,
  373. iv: iv,
  374. appId: 'wxe6ecc0193c09696c',
  375. sessionKey: session_key
  376. },
  377. success: (res) => {
  378. // if (res.data && res.data.steps) {
  379. // console.log(res.data.steps);
  380. // // resolve(res.data.steps); // 返回步数
  381. // // const steps = 123456
  382. // resolve(steps);
  383. // } else {
  384. // reject('解密返回数据格式错误');
  385. // }
  386. if (res.data.data) {
  387. const stepInfoList = res.data.data.stepInfoList
  388. const todaylist = stepInfoList.filter(item => {
  389. console.log(this.isToday(item));
  390. return this.isToday(item); // 使用 isToday 函数判断是否是今天
  391. });
  392. const steps = todaylist[0].step;
  393. resolve(steps);
  394. } else {
  395. reject('解密返回数据格式错误');
  396. }
  397. },
  398. fail: (err) => {
  399. reject(err);
  400. }
  401. });
  402. });
  403. },
  404. // 判断日期是否是今天
  405. isToday(item) {
  406. const today = new Date();
  407. const date = new Date(item.timestamp * 1000); // 假设时间戳是以秒为单位
  408. // 比较年、月、日
  409. return date.getFullYear() === today.getFullYear() &&
  410. date.getMonth() === today.getMonth() &&
  411. date.getDate() === today.getDate();
  412. },
  413. // 开启后台定位
  414. startbackgroumd() {
  415. wx.startLocationUpdateBackground({
  416. success: (res) => {
  417. // 开始监听GPS数据
  418. this.onLocationChange((res) => {
  419. // 在这里处理位置变化的逻辑
  420. console.log('位置已更新:', res);
  421. });
  422. },
  423. fail: (res) => {
  424. // 授权失败后引导用户打开定位信息
  425. // 可以添加提示或引导用户操作的代码
  426. }
  427. });
  428. },
  429. // 监听位置变化
  430. onLocationChange(callback) {
  431. console.log('运行了');
  432. // 监听位置变化
  433. // this.Getlocation2()
  434. wx.onLocationChange((res) => {
  435. // 计算距离
  436. let distance = this.haversineDistance(this.data.longitude, this.data.latitude, res.longitude, res.latitude);
  437. // 更新总距离
  438. const totalDistance = parseFloat((Number(this.data.distance) + Number(distance)).toFixed(3));
  439. if(totalDistance!=this.data.distance){
  440. this.progressdate()
  441. this.progressActivitydate()
  442. }
  443. // 计算卡路里
  444. const calorie = this.getCalorie(60, totalDistance); // 假设体重为60kg
  445. let pace =0
  446. if(res.speed>0){
  447. pace = parseFloat(Number((res.speed*3.6).toFixed(2))); // 配速(km/h)
  448. console.log(pace);
  449. }
  450. console.log('总距离', totalDistance, '段距离', distance, '总卡路里', calorie, '配速', pace);
  451. // 更新状态
  452. this.setData({
  453. pace,
  454. distance: totalDistance,
  455. calorie: calorie,
  456. latitude: res.latitude,
  457. longitude: res.longitude
  458. });
  459. // 打印更新后的经纬度(可选)
  460. console.log('更新后的经纬度:', this.data.latitude, this.data.longitude);
  461. // 调用回调函数(如果有提供的话)
  462. if (callback) {
  463. callback(res);
  464. }
  465. });
  466. },
  467. //位置变化
  468. Getlocation2() {
  469. // 获取当前位置信息
  470. wx.getLocation({
  471. type: 'gcj02',
  472. success: (res) => {
  473. const {
  474. latitude,
  475. longitude
  476. } = res;
  477. if (this.data.longitude == res.longitude && this.data.latitude == res.latitude) {
  478. console.log('位置没变');
  479. } else {
  480. // 计算距离
  481. let distance = this.haversineDistance(this.data.longitude, this.data.latitude, res.longitude, res.latitude);
  482. // 更新总距离
  483. const totalDistance = parseFloat((Number(this.data.distance) + Number(distance)).toFixed(3));
  484. // 计算卡路里
  485. const calorie = this.getCalorie(60, totalDistance); // 假设体重为60kg
  486. const pace = parseFloat(Number((totalDistance / (this.data.totalSeconds / 3600)).toFixed(2))); // 配速(km/h)
  487. console.log('总距离', totalDistance, '段距离', distance, '总卡路里', calorie, '配速', pace);
  488. // 更新状态
  489. this.setData({
  490. pace,
  491. distance: totalDistance,
  492. calorie: calorie,
  493. latitude: res.latitude,
  494. longitude: res.longitude
  495. });
  496. console.log('更新经纬度:', latitude, longitude); // 添加日志
  497. }
  498. },
  499. fail: (err) => {
  500. console.error(err);
  501. wx.showToast({
  502. title: '获取位置失败',
  503. icon: 'none'
  504. });
  505. }
  506. });
  507. },
  508. //计算两点距离
  509. haversineDistance(longitude1, latitude1, longitude2, latitude2) {
  510. let lat1 = latitude1
  511. let lon1 = longitude1
  512. let lat2 = latitude2
  513. let lon2 = longitude2
  514. function toRad(degree) {
  515. return degree * Math.PI / 180;
  516. }
  517. const R = 6371e3; // 地球半径,单位为米
  518. const φ1 = toRad(lat1);
  519. const φ2 = toRad(lat2);
  520. const Δφ = toRad(lat2 - lat1);
  521. const Δλ = toRad(lon2 - lon1);
  522. const a = Math.sin(Δφ / 2) * Math.sin(Δφ / 2) +
  523. Math.cos(φ1) * Math.cos(φ2) *
  524. Math.sin(Δλ / 2) * Math.sin(Δλ / 2);
  525. const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  526. const distance = (R * c) / 1000; // 距离,单位为公里
  527. console.log('计算出距离', distance);
  528. return parseFloat(distance.toFixed(3));
  529. },
  530. //计算卡路里
  531. getCalorie(weight, distance) {
  532. weight = weight || 60 // 国人平均体重 60kg
  533. const Calorie = weight * distance * 0.75
  534. console.log('卡路里为', Calorie);
  535. return parseFloat(Calorie.toFixed(2)); // 保留两位小数并转换为数字
  536. },
  537. //创建开始数据
  538. async startdate() {
  539. const currentUser = Parse.User.current();
  540. let Userquery = new Parse.Query('_User');
  541. Userquery.equalTo('company', company);
  542. Userquery.equalTo('objectId', currentUser.id);
  543. Userquery.notEqualTo('isDeleted', true)
  544. let user = await Userquery.first();
  545. let companyPointer = Parse.Object.extend('Company').createWithoutData(company);
  546. let query = new Parse.Object('ActivityRunLog');
  547. query.set('user', user.toPointer())
  548. query.set('company', companyPointer)
  549. query.set('stage', 'start ')
  550. query.set('steps', 0) //步数
  551. query.set('distance', 0) //距离
  552. query.set('matchSpeed', 0) //配速
  553. query.set('sportDate', 0) //运动时间
  554. query.set('burnCalories', 0) //卡路里
  555. try {
  556. let saveDate2 = await query.save();
  557. console.log(saveDate2);
  558. const id = saveDate2.id
  559. await this.startActivitdate(id)
  560. console.log("新数据保存成功");
  561. } catch (error) {
  562. console.error("保存数据时出现错误:", error);
  563. }
  564. },
  565. //创建ActivityData数据
  566. async startActivitdate(objectId) {
  567. const currentUser = Parse.User.current();
  568. let Userquery = new Parse.Query('_User');
  569. Userquery.equalTo('company', company);
  570. Userquery.equalTo('objectId', currentUser.id);
  571. Userquery.notEqualTo('isDeleted', true)
  572. let user = await Userquery.first();
  573. let Activitdate = new Parse.Query('ActivityRunLog');
  574. Activitdate.equalTo('objectId', objectId);
  575. Activitdate.notEqualTo('isDeleted', true)
  576. let Activit = await Activitdate.first();
  577. let companyPointer = Parse.Object.extend('Company').createWithoutData(company);
  578. let query = new Parse.Object('ActivityData');
  579. query.set('user', user.toPointer())
  580. query.set('company', companyPointer)
  581. query.set('runlog', Activit.toPointer())
  582. try {
  583. let saveDate2 = await query.save();
  584. this.setData({
  585. activitdateid: saveDate2.id
  586. })
  587. console.log("ActivityData数据", this.data.activitdate);
  588. } catch (error) {
  589. console.error("保存数据时出现错误:", error);
  590. }
  591. },
  592. //创建过程数据
  593. async progressdate() {
  594. if (this.data.totalSeconds % 4 != 0) {
  595. return
  596. }
  597. const currentUser = Parse.User.current();
  598. let Userquery = new Parse.Query('_User');
  599. Userquery.equalTo('company', company);
  600. Userquery.equalTo('objectId', currentUser.id);
  601. Userquery.notEqualTo('isDeleted', true)
  602. let user = await Userquery.first();
  603. let companyPointer = Parse.Object.extend('Company').createWithoutData(company);
  604. let query = new Parse.Object('ActivityRunLog');
  605. query.set('user', user.toPointer())
  606. query.set('company', companyPointer)
  607. query.set('stage', 'progress ')
  608. query.set('steps', this.data.defferentstep) //步数
  609. query.set('distance', this.data.distance) //距离
  610. query.set('matchSpeed', this.data.pace) //配速
  611. query.set('sportDate', this.data.totalSeconds) //运动时间
  612. query.set('burnCalories', this.data.calorie) //卡路里
  613. try {
  614. console.log('上传成功', this.data.defferentstep, this.data.distance, this.data.pace, this.data.totalSeconds, this.data.calorie);
  615. let saveDate2 = await query.save();
  616. console.log("新数据保存成功");
  617. } catch (error) {
  618. console.error("保存数据时出现错误:", error);
  619. }
  620. },
  621. //上传ActivityData过程数据
  622. async progressActivitydate() {
  623. if (this.data.totalSeconds % 4 != 0) {
  624. return
  625. }
  626. let query = new Parse.Query('ActivityData');
  627. query.equalTo('objectId', this.data.activitdateid);
  628. query.notEqualTo('isDeleted', true)
  629. let Activit = await query.first();
  630. console.log('Activit',Activit);
  631. if (Activit) {
  632. Activit.set('steps', this.data.defferentstep) //步数
  633. Activit.set('distance', this.data.distance) //距离
  634. Activit.set('matchSpeed', this.data.pace) //配速
  635. Activit.set('sportDate', this.data.totalSeconds) //运动时间
  636. Activit.set('burnCalories', this.data.calorie) //卡路里
  637. try {
  638. let saveDate2 = await Activit.save();
  639. console.log("ActivityData过程数据数据");
  640. } catch (error) {
  641. console.error("保存数据时出现错误:", error);
  642. }
  643. }
  644. },
  645. //创建结束数据
  646. async enddate() {
  647. const currentUser = Parse.User.current();
  648. let Userquery = new Parse.Query('_User');
  649. Userquery.equalTo('company', company);
  650. Userquery.equalTo('objectId', currentUser.id);
  651. Userquery.notEqualTo('isDeleted', true)
  652. let user = await Userquery.first();
  653. let companyPointer = Parse.Object.extend('Company').createWithoutData(company);
  654. let query = new Parse.Object('ActivityRunLog');
  655. query.set('user', user.toPointer())
  656. query.set('company', companyPointer)
  657. query.set('stage', 'end ')
  658. query.set('steps', this.data.defferentstep) //步数
  659. query.set('distance', this.data.distance) //距离
  660. query.set('matchSpeed', this.data.pace) //配速
  661. query.set('sportDate', this.data.totalSeconds) //运动时间
  662. query.set('burnCalories', this.data.calorie) //卡路里
  663. try {
  664. console.log('上传成功', this.data.defferentstep, this.data.distance, this.data.pace, this.data.totalSeconds, this.data.calorie);
  665. let saveDate2 = await query.save();
  666. console.log("新数据保存成功");
  667. } catch (error) {
  668. console.error("保存数据时出现错误:", error);
  669. }
  670. },
  671. //上传ActivityData结束数据
  672. async endActivitydate() {
  673. let query = new Parse.Query('ActivityData');
  674. query.equalTo('objectId', this.data.activitdateid);
  675. query.notEqualTo('isDeleted', true)
  676. let Activit = await query.first();
  677. if (Activit) {
  678. Activit.set('steps', this.data.defferentstep) //步数
  679. Activit.set('distance', this.data.distance) //距离
  680. Activit.set('matchSpeed', this.data.pace) //配速
  681. Activit.set('sportDate', this.data.totalSeconds) //运动时间
  682. Activit.set('burnCalories', this.data.calorie) //卡路里
  683. try {
  684. let saveDate2 = await Activit.save();
  685. console.log("ActivityData结束数据数据");
  686. } catch (error) {
  687. console.error("保存数据时出现错误:", error);
  688. }
  689. }
  690. },
  691. // 停止后台定位和位置监听
  692. stopBackgroundLocation() {
  693. wx.stopLocationUpdate({
  694. success: (res) => {
  695. console.log('后台定位已停止');
  696. },
  697. fail: (res) => {
  698. console.error('停止后台定位失败', res);
  699. }
  700. });
  701. wx.offLocationChange(); // 停止监听位置变化
  702. },
  703. })