index.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. // nova-werun/components/home/index.js
  2. const Parse = getApp().Parse;
  3. const company = getApp().globalData.company;
  4. Component({
  5. /**
  6. * 组件的属性列表
  7. */
  8. properties: {
  9. },
  10. /**
  11. * 组件的初始数据
  12. */
  13. data: {
  14. //屏幕高度
  15. statusBarHeight: 0, // 状态栏高度
  16. screenHeight: 0, // 屏幕高度
  17. customHeight: 0, // 自定义导航栏高度(如小程序右上角胶囊按钮)
  18. bottomNavHeight: 0, // 底部导航栏高度
  19. contentHeight: 0, // 可用内容高度
  20. contentpadding: 0, //顶部padding高度
  21. navheight: 0,
  22. percentage: 0,
  23. //选择
  24. rows: [{
  25. image: 'https://file-cloud.fmode.cn/qpFbRRSZrO/20241225/r5j1uc041211788.png',
  26. text: '签到打卡',
  27. url: '../../pages/home/signin/index'
  28. },
  29. {
  30. image: 'https://file-cloud.fmode.cn/qpFbRRSZrO/20241225/n1pin1040819673.png',
  31. text: '实时步行',
  32. url: '../../pages/home/sport/sport-home/index',
  33. },
  34. {
  35. image: 'https://file-cloud.fmode.cn/qpFbRRSZrO/20241225/9vfr1l040831635.png',
  36. text: '转发分享',
  37. url: '../../pages/home/share/index'
  38. },
  39. {
  40. image: 'https://file-cloud.fmode.cn/qpFbRRSZrO/20241225/it1rna040843179.png',
  41. text: '我的勋章',
  42. url: '../../pages/home/medal/index'
  43. },
  44. {
  45. image: 'https://file-cloud.fmode.cn/qpFbRRSZrO/20241225/r5193r040853498.png',
  46. text: '我的统计',
  47. url: '../../pages/home/statistics/index'
  48. },
  49. // {
  50. // image: 'https://file-cloud.fmode.cn/qpFbRRSZrO/20241102/v6p4fm041343296.png?imageView2/1/w/200/h/200',
  51. // text: '排行榜',
  52. // url: '../../pages/home/ranking/index'
  53. // },
  54. // {
  55. // image: 'https://file-cloud.fmode.cn/qpFbRRSZrO/20241102/71svpg041343669.png?imageView2/1/w/200/h/200',
  56. // text: '跑步',
  57. // url: '../../pages/home/sport/sport-home/index',
  58. // active: 1
  59. // },
  60. // {
  61. // image: 'https://file-cloud.fmode.cn/qpFbRRSZrO/20241104/k1q4no034958681.png?imageView2/1/w/200/h/200',
  62. // text: '积分',
  63. // url: '../../pages/home/integral/index'
  64. // },
  65. ],
  66. target: 0,
  67. sharList: [],
  68. address: "",
  69. //排行榜
  70. todayList: [],
  71. changetitle: 'today',
  72. //正序
  73. rank: 'up'
  74. },
  75. lifetimes: {
  76. detached: function () {
  77. // 在组件实例被从页面节点树移除时执行
  78. },
  79. attached: async function () {
  80. // 在组件实例进入页面节点树时执行
  81. // 计算
  82. const systemInfo = wx.getSystemInfoSync();
  83. const statusBarHeight = systemInfo.statusBarHeight || 0;
  84. const screenHeight = systemInfo.screenHeight || 0;
  85. const custom = wx.getMenuButtonBoundingClientRect();
  86. const customHeight = custom.height + 10 + 2 || 0;
  87. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  88. const contentpadding = (statusBarHeight + customHeight) * 750 / systemInfo.windowWidth;
  89. const contentHeight = (screenHeight - bottomNavHeight - 50 - statusBarHeight - customHeight) * 750 / systemInfo.windowWidth;
  90. const navheight = (statusBarHeight + customHeight) * 750 / systemInfo.windowWidth;
  91. this.setData({
  92. statusBarHeight,
  93. screenHeight: (screenHeight - 50 - bottomNavHeight) * 750 / systemInfo.windowWidth,
  94. customHeight,
  95. bottomNavHeight,
  96. contentHeight,
  97. contentpadding,
  98. navheight,
  99. });
  100. console.log('123', contentpadding);
  101. this.getWeRunData()
  102. this.gettarget()
  103. // this.order()
  104. this.Getlocation()
  105. this.gettoday()
  106. },
  107. },
  108. /**
  109. * 组件的方法列表
  110. */
  111. methods: {
  112. //跳转
  113. gourl(e) {
  114. const url = e.currentTarget.dataset.url
  115. const active = e.currentTarget.dataset.active
  116. if (active) {
  117. wx.navigateTo({
  118. url: `${url}?id=` + active // 目标页面的路径
  119. });
  120. console.log(active);
  121. } else {
  122. wx.navigateTo({
  123. url: `${url}` // 目标页面的路径
  124. });
  125. }
  126. },
  127. //获取目标步数
  128. async gettarget() {
  129. const currentUser = Parse.User.current();
  130. let userquery = new Parse.Query('_User');
  131. userquery.equalTo('company', company);
  132. userquery.equalTo('objectId', currentUser.id);
  133. userquery.notEqualTo('isDeleted', true)
  134. let user = await userquery.find();
  135. let num = user.map(item => item.toJSON());
  136. if (num[0].num) {
  137. this.setData({
  138. target: num[0].num
  139. })
  140. console.log('当前步数', this.data.target);
  141. } else {
  142. const currentUser2 = Parse.User.current();
  143. let userquery2 = new Parse.Query('_User');
  144. userquery2.equalTo('company', company);
  145. userquery2.equalTo('objectId', currentUser2.id);
  146. userquery2.notEqualTo('isDeleted', true)
  147. let user2 = await userquery2.first();
  148. user2.set('num', 5000)
  149. try {
  150. let saveDate2 = await user2.save();
  151. console.log(saveDate2);
  152. this.setData({
  153. target: 5000
  154. })
  155. console.log("目标步数更改成功");
  156. } catch (error) {
  157. console.error("保存数据时出现错误:", error);
  158. }
  159. }
  160. },
  161. //获取当天运动数据
  162. async order() {
  163. const currentUser = Parse.User.current();
  164. let ActivityDataquery = new Parse.Query('ActivityData');
  165. ActivityDataquery.equalTo('user', currentUser.id);
  166. ActivityDataquery.equalTo('company', company);
  167. ActivityDataquery.equalTo('type', 'today');
  168. ActivityDataquery.notEqualTo('isDeleted', true);
  169. // 获取今天的日期
  170. const today = new Date();
  171. const todayStart = new Date(today.getFullYear(), today.getMonth(), today.getDate()); // 今天的开始时间
  172. const todayEnd = new Date(todayStart);
  173. todayEnd.setHours(23, 59, 59, 999); // 今天的结束时间
  174. // 在查询条件中添加对 createdAt 的限制
  175. ActivityDataquery.greaterThanOrEqualTo('createdAt', todayStart);
  176. ActivityDataquery.lessThanOrEqualTo('createdAt', todayEnd);
  177. ActivityDataquery.include('user');
  178. let r = await ActivityDataquery.find();
  179. let sharList = r.map(item => item.toJSON());
  180. this.setData({
  181. sharList
  182. });
  183. this.getBackgroundColor()
  184. console.log(this.data.sharList);
  185. },
  186. //获取当前位置信息
  187. Getlocation() {
  188. // 获取当前位置信息
  189. wx.getLocation({
  190. type: 'wgs84',
  191. success: (res) => {
  192. const {
  193. latitude,
  194. longitude
  195. } = res;
  196. //调用api解析地址
  197. wx.request({
  198. url: 'https://api.map.baidu.com/reverse_geocoding/v3/?ak=sHZTomd7grslfP7sPKB8tRgT49FK9TEu&output=json&coordtype=gcj02&location=' + latitude + ',' + longitude,
  199. data: {},
  200. header: {
  201. 'Content-Type': 'application/json'
  202. },
  203. success: (ops) => { // 使用箭头函数
  204. console.log(ops);
  205. // const address = ops.data.result.formatted_address;//详细地址
  206. const address = ops.data.result.addressComponent.city; //市
  207. this.setData({
  208. address: address,
  209. });
  210. console.log(this.data.address);
  211. },
  212. fail: function (resq) {
  213. wx.showModal({
  214. title: '信息提示',
  215. content: '请求失败',
  216. showCancel: false,
  217. confirmColor: '#f37938'
  218. });
  219. },
  220. complete: function () {}
  221. })
  222. },
  223. fail: (err) => {
  224. console.error(err);
  225. wx.showToast({
  226. title: '获取位置失败',
  227. icon: 'none'
  228. });
  229. }
  230. });
  231. },
  232. //获取微信步数
  233. getWeRunData() {
  234. wx.getWeRunData({
  235. success: (res) => {
  236. // 获取到的加密数据
  237. const encryptedData = res.encryptedData;
  238. const iv = res.iv;
  239. console.log('encryptedData', encryptedData);
  240. console.log('iv', iv);
  241. const userInfo = wx.getStorageSync('userInfo');
  242. const session_key = userInfo.session_key
  243. console.log('session_key', session_key);
  244. // 这里需要调用你的后端接口进行解密
  245. // 假设你有一个解密函数 decryptData
  246. this.decryptData(encryptedData, iv, session_key).then(steps => {
  247. this.setData({
  248. steps: steps
  249. });
  250. console.log('用户步数:', this.data.steps);
  251. this.updatetoday(steps)
  252. }).catch(err => {
  253. console.error('解密失败:', err);
  254. });
  255. },
  256. fail: (err) => {
  257. console.error('获取运动数据失败:', err);
  258. }
  259. });
  260. },
  261. // 解密
  262. decryptData(encryptedData, iv, session_key) {
  263. return new Promise((resolve, reject) => {
  264. // 发送请求到后端进行解密
  265. wx.request({
  266. url: 'https://server.fmode.cn/api/wxapp/decrypt_phone', // 替换为你的后端解密接口
  267. method: 'get',
  268. data: {
  269. encryptedData: encryptedData,
  270. iv: iv,
  271. appId: 'wxe6ecc0193c09696c',
  272. sessionKey: session_key
  273. },
  274. success: (res) => {
  275. // if (res.data && res.data.steps) {
  276. // console.log(res.data.steps);
  277. // // resolve(res.data.steps); // 返回步数
  278. // // const steps = 123456
  279. // resolve(steps);
  280. // } else {
  281. // reject('解密返回数据格式错误');
  282. // }
  283. if (res.data.data) {
  284. const stepInfoList = res.data.data.stepInfoList
  285. console.log('时间戳',stepInfoList);
  286. const todaylist = stepInfoList.filter(item => {
  287. console.log(this.isToday(item));
  288. return this.isToday(item); // 使用 isToday 函数判断是否是今天
  289. });
  290. const steps = todaylist[0].step;
  291. resolve(steps);
  292. console.log('今日步数', steps);
  293. } else {
  294. reject('解密返回数据格式错误');
  295. }
  296. },
  297. fail: (err) => {
  298. reject(err);
  299. }
  300. });
  301. });
  302. },
  303. // 判断日期是否是今天
  304. isToday(item) {
  305. const today = new Date();
  306. const date = new Date(item.timestamp * 1000); // 假设时间戳是以秒为单位
  307. // 比较年、月、日
  308. return date.getFullYear() === today.getFullYear() &&
  309. date.getMonth() === today.getMonth() &&
  310. date.getDate() === today.getDate();
  311. },
  312. //更新今日步数
  313. async updatetoday(steps) {
  314. const currentUser = Parse.User.current();
  315. let ActivityDataquery = new Parse.Query('ActivityData');
  316. ActivityDataquery.equalTo('user', currentUser.id);
  317. ActivityDataquery.equalTo('company', company);
  318. ActivityDataquery.equalTo('type', 'today');
  319. ActivityDataquery.notEqualTo('isDeleted', true);
  320. // 获取今天的日期
  321. const today = new Date();
  322. const todayStart = new Date(today.getFullYear(), today.getMonth(), today.getDate()); // 今天的开始时间
  323. const todayEnd = new Date(todayStart);
  324. todayEnd.setHours(23, 59, 59, 999); // 今天的结束时间
  325. // 在查询条件中添加对 createdAt 的限制
  326. ActivityDataquery.greaterThanOrEqualTo('createdAt', todayStart);
  327. ActivityDataquery.lessThanOrEqualTo('createdAt', todayEnd);
  328. let r = await ActivityDataquery.first();
  329. if (r) {
  330. r.set('steps', steps)
  331. try {
  332. let saveDate = await r.save();
  333. this.order()
  334. this.gettoday()
  335. console.log("步数修改成功");
  336. } catch (error) {
  337. console.error("保存数据时出现错误:", error);
  338. }
  339. } else {
  340. const currentUser = Parse.User.current();
  341. let userquery = new Parse.Query('_User');
  342. userquery.equalTo('company', company);
  343. userquery.equalTo('objectId', currentUser.id);
  344. userquery.notEqualTo('isDeleted', true)
  345. let user = await userquery.first();
  346. let companyPointer = Parse.Object.extend('Company').createWithoutData(company);
  347. let Comment = new Parse.Object('ActivityData');
  348. Comment.set('company', companyPointer);
  349. Comment.set('type', 'today');
  350. Comment.set('user', user.toPointer());
  351. Comment.set('steps', steps);
  352. try {
  353. let saveDate2 = await Comment.save();
  354. this.order()
  355. this.gettoday()
  356. // console.log(saveDate2);
  357. console.log("步数保存成功");
  358. } catch (error) {
  359. console.error("保存数据时出现错误:", error);
  360. }
  361. }
  362. },
  363. //切换
  364. change() {
  365. // 使用数组来简化切换逻辑
  366. const titles = ['today', 'weekdday', 'month'];
  367. const currentIndex = titles.indexOf(this.data.changetitle); //获取index
  368. const nextIndex = (currentIndex + 1) % titles.length; // 循环切换
  369. this.setData({
  370. changetitle: titles[nextIndex]
  371. });
  372. if (this.data.changetitle == 'today') {
  373. this.setData({
  374. rank: 'up'
  375. })
  376. this.gettoday()
  377. }
  378. if (this.data.changetitle == 'weekdday') {
  379. this.setData({
  380. rank: 'up'
  381. })
  382. console.log('weekdday');
  383. }
  384. if (this.data.changetitle == 'month') {
  385. this.setData({
  386. rank: 'up'
  387. })
  388. console.log('month');
  389. }
  390. },
  391. // 获取本日排行
  392. async gettoday() {
  393. const currentUser = Parse.User.current();
  394. let ActivityDataquery = new Parse.Query('ActivityData');
  395. ActivityDataquery.equalTo('company', company);
  396. ActivityDataquery.equalTo('type', "today");
  397. ActivityDataquery.notEqualTo('isDeleted', true);
  398. // 获取今天的日期
  399. const today = new Date();
  400. const todayStart = new Date(today.getFullYear(), today.getMonth(), today.getDate()); // 今天的开始时间
  401. const todayEnd = new Date(todayStart);
  402. todayEnd.setHours(23, 59, 59, 999); // 今天的结束时间
  403. console.log(todayStart, todayEnd);
  404. // 在查询条件中添加对 createdAt 的限制
  405. ActivityDataquery.greaterThanOrEqualTo('createdAt', todayStart);
  406. ActivityDataquery.lessThanOrEqualTo('createdAt', todayEnd);
  407. // 根据 steps 字段进行降序排序
  408. ActivityDataquery.descending('steps');
  409. ActivityDataquery.include('user');
  410. try {
  411. let P = await ActivityDataquery.find();
  412. let todayList = P.map(item => item.toJSON());
  413. // // 初始化 myList
  414. // let myList = [];
  415. // // 找到当前用户的数据并计算排名
  416. // todayList.forEach((item, index) => {
  417. // if (item.user.objectId === currentUser.id) {
  418. // myList.push({
  419. // ...item, // 包含用户数据
  420. // rank: index + 1 // 计算排名(index 从 0 开始,所以加 1)
  421. // });
  422. // }
  423. // });
  424. // 更新页面数据
  425. this.setData({
  426. todayList,
  427. });
  428. console.log(this.data.todayList);
  429. } catch (error) {
  430. console.error('Error fetching today\'s data:', error);
  431. }
  432. },
  433. //正序逆序
  434. changeup() {
  435. if (this.data.rank == 'up') {
  436. this.setData({
  437. rank: 'down'
  438. })
  439. if (this.data.changetitle == 'today') {
  440. this.setData({
  441. todayList: this.data.todayList.reverse(),
  442. })
  443. console.log('逆序');
  444. }
  445. if (this.data.changetitle == 'weekdday') {
  446. console.log('weekdday逆序');
  447. }
  448. if (this.data.changetitle == 'month') {
  449. console.log('month逆序');
  450. }
  451. } else {
  452. this.setData({
  453. rank: 'up'
  454. })
  455. if (this.data.changetitle == 'today') {
  456. this.setData({
  457. todayList: this.data.todayList.reverse(),
  458. })
  459. console.log('顺序');
  460. }
  461. if (this.data.changetitle == 'weekdday') {
  462. console.log('weekdday顺序');
  463. }
  464. if (this.data.changetitle == 'month') {
  465. console.log('month顺序');
  466. }
  467. }
  468. },
  469. //修改进度条
  470. getBackgroundColor() {
  471. const steps = this.data.sharList[0].steps || 0;
  472. let percentage = (steps / this.data.target) * 100;
  473. if (percentage > 100) {
  474. percentage = 100;
  475. }
  476. this.setData({
  477. percentage: `conic-gradient(from 0deg, #015EEA ${percentage}%, white 0%)`,
  478. })
  479. console.log('百分比', this.data.percentage);
  480. },
  481. }
  482. })