index.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. timer: null,
  32. totalSeconds: 0, // 用于存储总秒数
  33. formattedTime: '00:00:00', // 用于存储格式化后的时间
  34. isRunning: false, // 计时器是否在运行
  35. steps: 0, //运动步数
  36. startsteps: 0,
  37. defferentstep: 0,
  38. },
  39. /**
  40. * 生命周期函数--监听页面加载
  41. */
  42. onLoad: async function (options) {
  43. // 计算
  44. const systemInfo = wx.getSystemInfoSync();
  45. const statusBarHeight = systemInfo.statusBarHeight || 0;
  46. const screenHeight = systemInfo.screenHeight || 0;
  47. const custom = wx.getMenuButtonBoundingClientRect();
  48. const customHeight = custom.height + 10 + 2 || 0;
  49. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  50. const navheight = (statusBarHeight + customHeight) * 750 / systemInfo.windowWidth;
  51. const contentHeight = (screenHeight - bottomNavHeight) * 750 / systemInfo.windowWidth;
  52. this.setData({
  53. statusBarHeight,
  54. screenHeight,
  55. customHeight,
  56. bottomNavHeight,
  57. contentHeight,
  58. navheight,
  59. });
  60. this.setData({
  61. title: options.id
  62. })
  63. // 获取初始步数
  64. // 获取初始步数
  65. this. getWeRunData2()
  66. console.log(this.data.startsteps);
  67. // 地图
  68. await this.Getlocation();
  69. },
  70. /**
  71. * 生命周期函数--监听页面初次渲染完成
  72. */
  73. onReady: function () {
  74. },
  75. /**
  76. * 生命周期函数--监听页面显示
  77. */
  78. onShow: function () {
  79. this.setData({
  80. totalSeconds: 0, // 重置总秒数
  81. formattedTime: '00:00:00', // 重置格式化后的时间
  82. isRunning: false // 初始化为未运行状态
  83. });
  84. this.startTimer()
  85. },
  86. /**
  87. * 生命周期函数--监听页面隐藏
  88. */
  89. onHide: function () {
  90. // 页面隐藏时清除计时器
  91. this.stopTimer();
  92. },
  93. /**
  94. * 生命周期函数--监听页面卸载
  95. */
  96. onUnload: function () {
  97. },
  98. /**
  99. * 页面相关事件处理函数--监听用户下拉动作
  100. */
  101. onPullDownRefresh: function () {
  102. },
  103. /**
  104. * 页面上拉触底事件的处理函数
  105. */
  106. onReachBottom: function () {
  107. },
  108. /**
  109. * 用户点击右上角分享
  110. */
  111. onShareAppMessage: function () {
  112. },
  113. //获取当前位置信息
  114. Getlocation() {
  115. // 获取当前位置信息
  116. wx.getLocation({
  117. type: 'wgs84',
  118. success: (res) => {
  119. const {
  120. latitude,
  121. longitude
  122. } = res;
  123. console.log('获取到的经纬度:', latitude, longitude); // 添加日志
  124. //调用api解析地址
  125. wx.request({
  126. url: 'https://api.map.baidu.com/reverse_geocoding/v3/?ak=sHZTomd7grslfP7sPKB8tRgT49FK9TEu&output=json&coordtype=gcj02&location=' + latitude + ',' + longitude,
  127. data: {},
  128. header: {
  129. 'Content-Type': 'application/json'
  130. },
  131. success: (ops) => { // 使用箭头函数
  132. console.log(ops);
  133. const address = ops.data.result.formatted_address;
  134. this.setData({
  135. address: address,
  136. latitude: latitude, // 保证 latitude 被设置
  137. longitude: longitude, // 保证 longitude 被设置
  138. markers: [{ // 设置 markers
  139. id: 1,
  140. latitude: latitude,
  141. longitude: longitude,
  142. iconPath: 'https://file-cloud.fmode.cn/13WZ0W7u3l/20240724/7ebg0k104325941.png?imageView2/1/w/200/h/200', // 自定义标记图标
  143. width: 40,
  144. height: 40,
  145. // callout: {
  146. // content: address, // 可以显示解析出的地址
  147. // color: '#ffffff',
  148. // bgColor: '#7F56B2',
  149. // padding: 10,
  150. // borderRadius: 5,
  151. // display: 'ALWAYS'
  152. // }
  153. }]
  154. });
  155. console.log(this.data.address);
  156. },
  157. fail: function (resq) {
  158. wx.showModal({
  159. title: '信息提示',
  160. content: '请求失败',
  161. showCancel: false,
  162. confirmColor: '#f37938'
  163. });
  164. },
  165. complete: function () {}
  166. })
  167. },
  168. fail: (err) => {
  169. console.error(err);
  170. wx.showToast({
  171. title: '获取位置失败',
  172. icon: 'none'
  173. });
  174. }
  175. });
  176. },
  177. stop() {
  178. this.setData({
  179. isstop: !this.data.isstop
  180. })
  181. if (this.data.isstop) {
  182. this.stopTimer()
  183. } else {
  184. this.startTimer()
  185. }
  186. console.log(this.data.isstop);
  187. },
  188. //结束光环展示
  189. startIncrease() {
  190. // 记录开始时间
  191. this.setData({
  192. startTime: Date.now(),
  193. });
  194. // 清除之前的定时器
  195. if (this.data.timer) {
  196. clearInterval(this.data.timer);
  197. }
  198. // 设置定时器,每隔 40 毫秒更新一次 percentage
  199. this.setData({
  200. timer: setInterval(() => {
  201. const currentTime = Date.now();
  202. const elapsedTime = currentTime - this.data.startTime;
  203. const percentage = Math.min((elapsedTime / 4000) * 100, 100);
  204. this.setData({
  205. percentage: `conic-gradient(from 0deg, #015EEA ${percentage}%, white 0%)`,
  206. });
  207. if (percentage >= 100) {
  208. wx.showToast({
  209. title: '运动已结束',
  210. icon: 'success',
  211. duration: 500
  212. });
  213. setTimeout(() => {
  214. this.onConfirm()
  215. }, 500)
  216. }
  217. // 如果达到 100%,清除定时器
  218. if (percentage >= 100) {
  219. clearInterval(this.data.timer);
  220. this.setData({
  221. timer: null,
  222. });
  223. }
  224. }, 40),
  225. });
  226. },
  227. stopIncrease() {
  228. // 清除定时器
  229. if (this.data.timer) {
  230. clearInterval(this.data.timer);
  231. this.setData({
  232. timer: null,
  233. });
  234. // 如果未达到 100%,清零 percentage
  235. const elapsedTime = Date.now() - this.data.startTime;
  236. if (elapsedTime < 4000) {
  237. this.setData({
  238. percentage: '',
  239. });
  240. }
  241. }
  242. },
  243. //点击返回按钮
  244. goback() {
  245. this.setData({
  246. show: true,
  247. isstop: false,
  248. })
  249. },
  250. //取消弹窗
  251. onClose() {
  252. this.setData({
  253. show: false
  254. });
  255. },
  256. //确认返回
  257. onConfirm() {
  258. wx.navigateBack({
  259. delta: 1 // 返回的页面层数,1 表示返回上一页
  260. });
  261. },
  262. //开始计时或继续计时
  263. startTimer: function () {
  264. if (this.data.isRunning) return; // 如果已经在运行,则不再启动
  265. const that = this;
  266. this.data.timer = setInterval(function () {
  267. that.data.totalSeconds += 1; // 增加总秒数
  268. that.setData({
  269. formattedTime: that.formatTime(that.data.totalSeconds) // 更新格式化后的时间
  270. });
  271. // that.getWeRunData();
  272. // 每5秒调用一次getWeRunData
  273. if (that.data.totalSeconds % 1 === 0) {
  274. that.getWeRunData(); // 调用获取微信步数的函数
  275. }
  276. }, 1000);
  277. this.setData({
  278. isRunning: true // 设置为运行状态
  279. });
  280. },
  281. //暂停
  282. stopTimer: function () {
  283. clearInterval(this.data.timer);
  284. this.setData({
  285. isRunning: false // 设置为未运行状态
  286. });
  287. },
  288. formatTime: function (seconds) {
  289. const hours = Math.floor(seconds / 3600);
  290. const minutes = Math.floor((seconds % 3600) / 60);
  291. const secs = seconds % 60;
  292. // 格式化为两位数
  293. const formattedHours = String(hours).padStart(2, '0');
  294. const formattedMinutes = String(minutes).padStart(2, '0');
  295. const formattedSeconds = String(secs).padStart(2, '0');
  296. return `${formattedHours}:${formattedMinutes}:${formattedSeconds}`;
  297. },
  298. //获取初始步数
  299. async getWeRunData2() {
  300. wx.getWeRunData({
  301. success: (res) => {
  302. // 获取到的加密数据
  303. const encryptedData = res.encryptedData;
  304. const iv = res.iv;
  305. const userInfo = wx.getStorageSync('userInfo');
  306. const session_key = userInfo.session_key
  307. // 这里需要调用你的后端接口进行解密
  308. // 假设你有一个解密函数 decryptData
  309. this.decryptData(encryptedData, iv, session_key).then(async steps => {
  310. await this.progressdate(steps)
  311. console.log('再次赋值');
  312. await this.setData({
  313. startsteps: steps,
  314. });
  315. console.log('用户步数:', this.data.steps);
  316. }).catch(err => {
  317. console.error('解密失败:', err);
  318. });
  319. },
  320. fail: (err) => {
  321. console.error('获取运动数据失败:', err);
  322. }
  323. });
  324. },
  325. //获取微信步数
  326. async getWeRunData() {
  327. wx.getWeRunData({
  328. success: (res) => {
  329. // 获取到的加密数据
  330. const encryptedData = res.encryptedData;
  331. const iv = res.iv;
  332. const userInfo = wx.getStorageSync('userInfo');
  333. const session_key = userInfo.session_key
  334. // 这里需要调用你的后端接口进行解密
  335. // 假设你有一个解密函数 decryptData
  336. this.decryptData(encryptedData, iv, session_key).then(async steps => {
  337. await this.progressdate(steps)
  338. console.log('再次赋值');
  339. await this.setData({
  340. steps: steps,
  341. defferentstep: steps - this.data.startsteps
  342. });
  343. console.log('用户步数:', this.data.steps);
  344. }).catch(err => {
  345. console.error('解密失败:', err);
  346. });
  347. },
  348. fail: (err) => {
  349. console.error('获取运动数据失败:', err);
  350. }
  351. });
  352. },
  353. // 解密
  354. decryptData(encryptedData, iv, session_key) {
  355. return new Promise((resolve, reject) => {
  356. // 发送请求到后端进行解密
  357. wx.request({
  358. url: 'https://server.fmode.cn/api/wxapp/decrypt_phone', // 替换为你的后端解密接口
  359. method: 'get',
  360. data: {
  361. encryptedData: encryptedData,
  362. iv: iv,
  363. appId: 'wxe6ecc0193c09696c',
  364. sessionKey: session_key
  365. },
  366. success: (res) => {
  367. // if (res.data && res.data.steps) {
  368. // console.log(res.data.steps);
  369. // // resolve(res.data.steps); // 返回步数
  370. // // const steps = 123456
  371. // resolve(steps);
  372. // } else {
  373. // reject('解密返回数据格式错误');
  374. // }
  375. if (res.data.data) {
  376. const stepInfoList = res.data.data.stepInfoList
  377. const todaylist = stepInfoList.filter(item => {
  378. console.log(this.isToday(item));
  379. return this.isToday(item); // 使用 isToday 函数判断是否是今天
  380. });
  381. const steps = todaylist[0].step;
  382. resolve(steps);
  383. } else {
  384. reject('解密返回数据格式错误');
  385. }
  386. },
  387. fail: (err) => {
  388. reject(err);
  389. }
  390. });
  391. });
  392. },
  393. // 判断日期是否是今天
  394. isToday(item) {
  395. const today = new Date();
  396. const date = new Date(item.timestamp * 1000); // 假设时间戳是以秒为单位
  397. // 比较年、月、日
  398. return date.getFullYear() === today.getFullYear() &&
  399. date.getMonth() === today.getMonth() &&
  400. date.getDate() === today.getDate();
  401. },
  402. //创建过程数据
  403. async progressdate(steps) {
  404. if (steps == this.data.steps) {
  405. console.log('步数没变');
  406. return
  407. }
  408. const currentUser = Parse.User.current();
  409. let Userquery = new Parse.Query('_User');
  410. Userquery.equalTo('company', company);
  411. Userquery.equalTo('objectId', currentUser.id);
  412. Userquery.notEqualTo('isDeleted', true)
  413. let user = await Userquery.first();
  414. let companyPointer = Parse.Object.extend('Company').createWithoutData(company);
  415. let query = new Parse.Object('ActivityRunLog');
  416. query.set('user', user.toPointer())
  417. query.set('company', companyPointer)
  418. query.set('stage', 'progress ')
  419. query.set('steps', this.data.steps)
  420. try {
  421. // let saveDate2 = await Comment.save();
  422. console.log("新数据保存成功");
  423. } catch (error) {
  424. console.error("保存数据时出现错误:", error);
  425. }
  426. },
  427. })