index.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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 % 30 === 0) {
  282. that.getWeRunData(); // 调用获取微信步数的函数
  283. }
  284. that.onLocationChange((res) => {
  285. // 这里可以处理位置变化的逻辑
  286. console.log('位置已更新:', res);
  287. });
  288. that.progressdate()
  289. that.progressActivitydate()
  290. }, 1000);
  291. this.setData({
  292. isRunning: true // 设置为运行状态
  293. });
  294. },
  295. //暂停
  296. stopTimer: function () {
  297. clearInterval(this.data.timer2);
  298. this.setData({
  299. isRunning: false // 设置为未运行状态
  300. });
  301. wx.offLocationChange();
  302. },
  303. formatTime: function (seconds) {
  304. const hours = Math.floor(seconds / 3600);
  305. const minutes = Math.floor((seconds % 3600) / 60);
  306. const secs = seconds % 60;
  307. // 格式化为两位数
  308. const formattedHours = String(hours).padStart(2, '0');
  309. const formattedMinutes = String(minutes).padStart(2, '0');
  310. const formattedSeconds = String(secs).padStart(2, '0');
  311. return `${formattedHours}:${formattedMinutes}:${formattedSeconds}`;
  312. },
  313. //获取初始步数
  314. async getWeRunData2() {
  315. wx.getWeRunData({
  316. success: (res) => {
  317. // 获取到的加密数据
  318. const encryptedData = res.encryptedData;
  319. const iv = res.iv;
  320. const userInfo = wx.getStorageSync('userInfo');
  321. const session_key = userInfo.session_key
  322. // 这里需要调用你的后端接口进行解密
  323. // 假设你有一个解密函数 decryptData
  324. this.decryptData(encryptedData, iv, session_key).then(async steps => {
  325. console.log('再次赋值');
  326. await this.setData({
  327. startsteps: steps,
  328. });
  329. console.log('用户步数:', this.data.steps);
  330. }).catch(err => {
  331. console.error('解密失败:', err);
  332. });
  333. },
  334. fail: (err) => {
  335. console.error('获取运动数据失败:', err);
  336. }
  337. });
  338. },
  339. //获取微信步数
  340. async getWeRunData() {
  341. wx.getWeRunData({
  342. success: (res) => {
  343. // 获取到的加密数据
  344. const encryptedData = res.encryptedData;
  345. const iv = res.iv;
  346. const userInfo = wx.getStorageSync('userInfo');
  347. const session_key = userInfo.session_key
  348. // 这里需要调用你的后端接口进行解密
  349. // 假设你有一个解密函数 decryptData
  350. this.decryptData(encryptedData, iv, session_key).then(async steps => {
  351. console.log('再次赋值');
  352. await this.setData({
  353. steps: steps,
  354. defferentstep: steps - this.data.startsteps
  355. });
  356. console.log('用户步数:', this.data.steps);
  357. }).catch(err => {
  358. console.error('解密失败:', err);
  359. });
  360. },
  361. fail: (err) => {
  362. console.error('获取运动数据失败:', err);
  363. }
  364. });
  365. },
  366. // 解密
  367. decryptData(encryptedData, iv, session_key) {
  368. return new Promise((resolve, reject) => {
  369. // 发送请求到后端进行解密
  370. wx.request({
  371. url: 'https://server.fmode.cn/api/wxapp/decrypt_phone', // 替换为你的后端解密接口
  372. method: 'get',
  373. data: {
  374. encryptedData: encryptedData,
  375. iv: iv,
  376. appId: 'wxe6ecc0193c09696c',
  377. sessionKey: session_key
  378. },
  379. success: (res) => {
  380. // if (res.data && res.data.steps) {
  381. // console.log(res.data.steps);
  382. // // resolve(res.data.steps); // 返回步数
  383. // // const steps = 123456
  384. // resolve(steps);
  385. // } else {
  386. // reject('解密返回数据格式错误');
  387. // }
  388. if (res.data.data) {
  389. const stepInfoList = res.data.data.stepInfoList
  390. const todaylist = stepInfoList.filter(item => {
  391. console.log(this.isToday(item));
  392. return this.isToday(item); // 使用 isToday 函数判断是否是今天
  393. });
  394. const steps = todaylist[0].step;
  395. resolve(steps);
  396. } else {
  397. reject('解密返回数据格式错误');
  398. }
  399. },
  400. fail: (err) => {
  401. reject(err);
  402. }
  403. });
  404. });
  405. },
  406. // 判断日期是否是今天
  407. isToday(item) {
  408. const today = new Date();
  409. const date = new Date(item.timestamp * 1000); // 假设时间戳是以秒为单位
  410. // 比较年、月、日
  411. return date.getFullYear() === today.getFullYear() &&
  412. date.getMonth() === today.getMonth() &&
  413. date.getDate() === today.getDate();
  414. },
  415. // 开启后台定位
  416. startbackgroumd() {
  417. wx.startLocationUpdateBackground({
  418. success: (res) => {
  419. // 开始监听GPS数据
  420. this.onLocationChange((res) => {
  421. // 在这里处理位置变化的逻辑
  422. console.log('位置已更新:', res);
  423. });
  424. },
  425. fail: (res) => {
  426. // 授权失败后引导用户打开定位信息
  427. // 可以添加提示或引导用户操作的代码
  428. }
  429. });
  430. },
  431. // 监听位置变化
  432. onLocationChange(callback) {
  433. console.log('运行了');
  434. // 监听位置变化
  435. // this.Getlocation2()
  436. wx.onLocationChange((res) => {
  437. // 计算距离
  438. let distance = this.haversineDistance(this.data.longitude, this.data.latitude, res.longitude, res.latitude);
  439. // 更新总距离
  440. const totalDistance = parseFloat((Number(this.data.distance) + Number(distance)).toFixed(3));
  441. // 计算卡路里
  442. const calorie = this.getCalorie(60, totalDistance); // 假设体重为60kg
  443. const pace = parseFloat(Number((totalDistance / (this.data.totalSeconds / 3600)).toFixed(2))); // 配速(km/h)
  444. console.log('总距离', totalDistance, '段距离', distance, '总卡路里', calorie, '配速', pace);
  445. // 更新状态
  446. this.setData({
  447. pace,
  448. distance: totalDistance,
  449. calorie: calorie,
  450. latitude: res.latitude,
  451. longitude: res.longitude
  452. });
  453. // 打印更新后的经纬度(可选)
  454. console.log('更新后的经纬度:', this.data.latitude, this.data.longitude);
  455. // 调用回调函数(如果有提供的话)
  456. if (callback) {
  457. callback(res);
  458. }
  459. });
  460. },
  461. //位置变化
  462. Getlocation2() {
  463. // 获取当前位置信息
  464. wx.getLocation({
  465. type: 'gcj02',
  466. success: (res) => {
  467. const {
  468. latitude,
  469. longitude
  470. } = res;
  471. if (this.data.longitude == res.longitude && this.data.latitude == res.latitude) {
  472. console.log('位置没变');
  473. } else {
  474. // 计算距离
  475. let distance = this.haversineDistance(this.data.longitude, this.data.latitude, res.longitude, res.latitude);
  476. // 更新总距离
  477. const totalDistance = parseFloat((Number(this.data.distance) + Number(distance)).toFixed(3));
  478. // 计算卡路里
  479. const calorie = this.getCalorie(60, totalDistance); // 假设体重为60kg
  480. const pace = parseFloat(Number((totalDistance / (this.data.totalSeconds / 3600)).toFixed(2))); // 配速(km/h)
  481. console.log('总距离', totalDistance, '段距离', distance, '总卡路里', calorie, '配速', pace);
  482. // 更新状态
  483. this.setData({
  484. pace,
  485. distance: totalDistance,
  486. calorie: calorie,
  487. latitude: res.latitude,
  488. longitude: res.longitude
  489. });
  490. console.log('更新经纬度:', latitude, longitude); // 添加日志
  491. }
  492. },
  493. fail: (err) => {
  494. console.error(err);
  495. wx.showToast({
  496. title: '获取位置失败',
  497. icon: 'none'
  498. });
  499. }
  500. });
  501. },
  502. //计算两点距离
  503. haversineDistance(longitude1, latitude1, longitude2, latitude2) {
  504. let lat1 = latitude1
  505. let lon1 = longitude1
  506. let lat2 = latitude2
  507. let lon2 = longitude2
  508. function toRad(degree) {
  509. return degree * Math.PI / 180;
  510. }
  511. const R = 6371e3; // 地球半径,单位为米
  512. const φ1 = toRad(lat1);
  513. const φ2 = toRad(lat2);
  514. const Δφ = toRad(lat2 - lat1);
  515. const Δλ = toRad(lon2 - lon1);
  516. const a = Math.sin(Δφ / 2) * Math.sin(Δφ / 2) +
  517. Math.cos(φ1) * Math.cos(φ2) *
  518. Math.sin(Δλ / 2) * Math.sin(Δλ / 2);
  519. const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  520. const distance = (R * c) / 1000; // 距离,单位为公里
  521. console.log('计算出距离', distance);
  522. return parseFloat(distance.toFixed(3));
  523. },
  524. //计算卡路里
  525. getCalorie(weight, distance) {
  526. weight = weight || 60 // 国人平均体重 60kg
  527. const Calorie = weight * distance * 0.75
  528. console.log('卡路里为', Calorie);
  529. return parseFloat(Calorie.toFixed(2)); // 保留两位小数并转换为数字
  530. },
  531. //创建开始数据
  532. async startdate() {
  533. const currentUser = Parse.User.current();
  534. let Userquery = new Parse.Query('_User');
  535. Userquery.equalTo('company', company);
  536. Userquery.equalTo('objectId', currentUser.id);
  537. Userquery.notEqualTo('isDeleted', true)
  538. let user = await Userquery.first();
  539. let companyPointer = Parse.Object.extend('Company').createWithoutData(company);
  540. let query = new Parse.Object('ActivityRunLog');
  541. query.set('user', user.toPointer())
  542. query.set('company', companyPointer)
  543. query.set('stage', 'start ')
  544. query.set('steps', 0) //步数
  545. query.set('distance', 0) //距离
  546. query.set('matchSpeed', 0) //配速
  547. query.set('sportDate', 0) //运动时间
  548. query.set('burnCalories', 0) //卡路里
  549. try {
  550. let saveDate2 = await query.save();
  551. console.log(saveDate2);
  552. const id = saveDate2.id
  553. await this.startActivitdate(id)
  554. console.log("新数据保存成功");
  555. } catch (error) {
  556. console.error("保存数据时出现错误:", error);
  557. }
  558. },
  559. //创建ActivityData数据
  560. async startActivitdate(objectId) {
  561. const currentUser = Parse.User.current();
  562. let Userquery = new Parse.Query('_User');
  563. Userquery.equalTo('company', company);
  564. Userquery.equalTo('objectId', currentUser.id);
  565. Userquery.notEqualTo('isDeleted', true)
  566. let user = await Userquery.first();
  567. let Activitdate = new Parse.Query('ActivityRunLog');
  568. Activitdate.equalTo('objectId', objectId);
  569. Activitdate.notEqualTo('isDeleted', true)
  570. let Activit = await Activitdate.first();
  571. let companyPointer = Parse.Object.extend('Company').createWithoutData(company);
  572. let query = new Parse.Object('ActivityData');
  573. query.set('user', user.toPointer())
  574. query.set('company', companyPointer)
  575. query.set('runlog', Activit.toPointer())
  576. try {
  577. let saveDate2 = await query.save();
  578. this.setData({
  579. activitdateid: saveDate2.id
  580. })
  581. console.log("ActivityData数据", this.data.activitdate);
  582. } catch (error) {
  583. console.error("保存数据时出现错误:", error);
  584. }
  585. },
  586. //创建过程数据
  587. async progressdate() {
  588. if (this.data.totalSeconds % 4 != 0) {
  589. return
  590. }
  591. const currentUser = Parse.User.current();
  592. let Userquery = new Parse.Query('_User');
  593. Userquery.equalTo('company', company);
  594. Userquery.equalTo('objectId', currentUser.id);
  595. Userquery.notEqualTo('isDeleted', true)
  596. let user = await Userquery.first();
  597. let companyPointer = Parse.Object.extend('Company').createWithoutData(company);
  598. let query = new Parse.Object('ActivityRunLog');
  599. query.set('user', user.toPointer())
  600. query.set('company', companyPointer)
  601. query.set('stage', 'progress ')
  602. query.set('steps', this.data.defferentstep) //步数
  603. query.set('distance', this.data.distance) //距离
  604. query.set('matchSpeed', this.data.pace) //配速
  605. query.set('sportDate', this.data.totalSeconds) //运动时间
  606. query.set('burnCalories', this.data.calorie) //卡路里
  607. try {
  608. console.log('上传成功', this.data.defferentstep, this.data.distance, this.data.pace, this.data.totalSeconds, this.data.calorie);
  609. let saveDate2 = await query.save();
  610. console.log("新数据保存成功");
  611. } catch (error) {
  612. console.error("保存数据时出现错误:", error);
  613. }
  614. },
  615. //上传ActivityData过程数据
  616. async progressActivitydate() {
  617. if (this.data.totalSeconds % 4 != 0) {
  618. return
  619. }
  620. let query = new Parse.Query('ActivityData');
  621. query.equalTo('objectId', this.data.activitdateid);
  622. query.notEqualTo('isDeleted', true)
  623. let Activit = await query.first();
  624. console.log('Activit',Activit);
  625. if (Activit) {
  626. Activit.set('steps', this.data.defferentstep) //步数
  627. Activit.set('distance', this.data.distance) //距离
  628. Activit.set('matchSpeed', this.data.pace) //配速
  629. Activit.set('sportDate', this.data.totalSeconds) //运动时间
  630. Activit.set('burnCalories', this.data.calorie) //卡路里
  631. try {
  632. let saveDate2 = await Activit.save();
  633. console.log("ActivityData过程数据数据");
  634. } catch (error) {
  635. console.error("保存数据时出现错误:", error);
  636. }
  637. }
  638. },
  639. //创建结束数据
  640. async enddate() {
  641. const currentUser = Parse.User.current();
  642. let Userquery = new Parse.Query('_User');
  643. Userquery.equalTo('company', company);
  644. Userquery.equalTo('objectId', currentUser.id);
  645. Userquery.notEqualTo('isDeleted', true)
  646. let user = await Userquery.first();
  647. let companyPointer = Parse.Object.extend('Company').createWithoutData(company);
  648. let query = new Parse.Object('ActivityRunLog');
  649. query.set('user', user.toPointer())
  650. query.set('company', companyPointer)
  651. query.set('stage', 'end ')
  652. query.set('steps', this.data.defferentstep) //步数
  653. query.set('distance', this.data.distance) //距离
  654. query.set('matchSpeed', this.data.pace) //配速
  655. query.set('sportDate', this.data.totalSeconds) //运动时间
  656. query.set('burnCalories', this.data.calorie) //卡路里
  657. try {
  658. console.log('上传成功', this.data.defferentstep, this.data.distance, this.data.pace, this.data.totalSeconds, this.data.calorie);
  659. let saveDate2 = await query.save();
  660. console.log("新数据保存成功");
  661. } catch (error) {
  662. console.error("保存数据时出现错误:", error);
  663. }
  664. },
  665. //上传ActivityData结束数据
  666. async endActivitydate() {
  667. let query = new Parse.Query('ActivityData');
  668. query.equalTo('objectId', this.data.activitdateid);
  669. query.notEqualTo('isDeleted', true)
  670. let Activit = await query.first();
  671. if (Activit) {
  672. Activit.set('steps', this.data.defferentstep) //步数
  673. Activit.set('distance', this.data.distance) //距离
  674. Activit.set('matchSpeed', this.data.pace) //配速
  675. Activit.set('sportDate', this.data.totalSeconds) //运动时间
  676. Activit.set('burnCalories', this.data.calorie) //卡路里
  677. try {
  678. let saveDate2 = await Activit.save();
  679. console.log("ActivityData结束数据数据");
  680. } catch (error) {
  681. console.error("保存数据时出现错误:", error);
  682. }
  683. }
  684. },
  685. // 停止后台定位和位置监听
  686. stopBackgroundLocation() {
  687. wx.stopLocationUpdate({
  688. success: (res) => {
  689. console.log('后台定位已停止');
  690. },
  691. fail: (res) => {
  692. console.error('停止后台定位失败', res);
  693. }
  694. });
  695. wx.offLocationChange(); // 停止监听位置变化
  696. },
  697. })