index.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. console.log('res',res);
  276. // if (res.data && res.data.steps) {
  277. // console.log(res.data.steps);
  278. // // resolve(res.data.steps); // 返回步数
  279. // // const steps = 123456
  280. // resolve(steps);
  281. // } else {
  282. // reject('解密返回数据格式错误');
  283. // }
  284. if (res.data.data) {
  285. const stepInfoList = res.data.data.stepInfoList
  286. console.log('时间戳',stepInfoList);
  287. const todaylist = stepInfoList.filter(item => {
  288. console.log(this.isToday(item));
  289. return this.isToday(item); // 使用 isToday 函数判断是否是今天
  290. });
  291. const steps = todaylist[0].step;
  292. resolve(steps);
  293. console.log('今日步数', steps);
  294. } else {
  295. reject('解密返回数据格式错误');
  296. }
  297. },
  298. fail: (err) => {
  299. reject(err);
  300. }
  301. });
  302. });
  303. },
  304. // 判断日期是否是今天
  305. isToday(item) {
  306. const today = new Date();
  307. const date = new Date(item.timestamp * 1000); // 假设时间戳是以秒为单位
  308. // 比较年、月、日
  309. return date.getFullYear() === today.getFullYear() &&
  310. date.getMonth() === today.getMonth() &&
  311. date.getDate() === today.getDate();
  312. },
  313. //更新今日步数
  314. async updatetoday(steps) {
  315. const currentUser = Parse.User.current();
  316. let ActivityDataquery = new Parse.Query('ActivityData');
  317. ActivityDataquery.equalTo('user', currentUser.id);
  318. ActivityDataquery.equalTo('company', company);
  319. ActivityDataquery.equalTo('type', 'today');
  320. ActivityDataquery.notEqualTo('isDeleted', true);
  321. // 获取今天的日期
  322. const today = new Date();
  323. const todayStart = new Date(today.getFullYear(), today.getMonth(), today.getDate()); // 今天的开始时间
  324. const todayEnd = new Date(todayStart);
  325. todayEnd.setHours(23, 59, 59, 999); // 今天的结束时间
  326. // 在查询条件中添加对 createdAt 的限制
  327. ActivityDataquery.greaterThanOrEqualTo('createdAt', todayStart);
  328. ActivityDataquery.lessThanOrEqualTo('createdAt', todayEnd);
  329. let r = await ActivityDataquery.first();
  330. if (r) {
  331. r.set('steps', steps)
  332. try {
  333. let saveDate = await r.save();
  334. this.order()
  335. this.gettoday()
  336. console.log("步数修改成功");
  337. } catch (error) {
  338. console.error("保存数据时出现错误:", error);
  339. }
  340. } else {
  341. const currentUser = Parse.User.current();
  342. let userquery = new Parse.Query('_User');
  343. userquery.equalTo('company', company);
  344. userquery.equalTo('objectId', currentUser.id);
  345. userquery.notEqualTo('isDeleted', true)
  346. let user = await userquery.first();
  347. let companyPointer = Parse.Object.extend('Company').createWithoutData(company);
  348. let Comment = new Parse.Object('ActivityData');
  349. Comment.set('company', companyPointer);
  350. Comment.set('type', 'today');
  351. Comment.set('user', user.toPointer());
  352. Comment.set('steps', steps);
  353. try {
  354. let saveDate2 = await Comment.save();
  355. this.order()
  356. this.gettoday()
  357. // console.log(saveDate2);
  358. console.log("步数保存成功");
  359. } catch (error) {
  360. console.error("保存数据时出现错误:", error);
  361. }
  362. }
  363. },
  364. //切换
  365. change() {
  366. // 使用数组来简化切换逻辑
  367. const titles = ['today', 'weekdday', 'month'];
  368. const currentIndex = titles.indexOf(this.data.changetitle); //获取index
  369. const nextIndex = (currentIndex + 1) % titles.length; // 循环切换
  370. this.setData({
  371. changetitle: titles[nextIndex]
  372. });
  373. if (this.data.changetitle == 'today') {
  374. this.setData({
  375. rank: 'up'
  376. })
  377. this.gettoday()
  378. }
  379. if (this.data.changetitle == 'weekdday') {
  380. this.setData({
  381. rank: 'up'
  382. })
  383. console.log('weekdday');
  384. }
  385. if (this.data.changetitle == 'month') {
  386. this.setData({
  387. rank: 'up'
  388. })
  389. console.log('month');
  390. }
  391. },
  392. // 获取本日排行
  393. async gettoday() {
  394. const currentUser = Parse.User.current();
  395. let ActivityDataquery = new Parse.Query('ActivityData');
  396. ActivityDataquery.equalTo('company', company);
  397. ActivityDataquery.equalTo('type', "today");
  398. ActivityDataquery.notEqualTo('isDeleted', true);
  399. // 获取今天的日期
  400. const today = new Date();
  401. const todayStart = new Date(today.getFullYear(), today.getMonth(), today.getDate()); // 今天的开始时间
  402. const todayEnd = new Date(todayStart);
  403. todayEnd.setHours(23, 59, 59, 999); // 今天的结束时间
  404. console.log(todayStart, todayEnd);
  405. // 在查询条件中添加对 createdAt 的限制
  406. ActivityDataquery.greaterThanOrEqualTo('createdAt', todayStart);
  407. ActivityDataquery.lessThanOrEqualTo('createdAt', todayEnd);
  408. // 根据 steps 字段进行降序排序
  409. ActivityDataquery.descending('steps');
  410. ActivityDataquery.include('user');
  411. try {
  412. let P = await ActivityDataquery.find();
  413. let todayList = P.map(item => item.toJSON());
  414. // // 初始化 myList
  415. // let myList = [];
  416. // // 找到当前用户的数据并计算排名
  417. // todayList.forEach((item, index) => {
  418. // if (item.user.objectId === currentUser.id) {
  419. // myList.push({
  420. // ...item, // 包含用户数据
  421. // rank: index + 1 // 计算排名(index 从 0 开始,所以加 1)
  422. // });
  423. // }
  424. // });
  425. // 更新页面数据
  426. this.setData({
  427. todayList,
  428. });
  429. console.log(this.data.todayList);
  430. } catch (error) {
  431. console.error('Error fetching today\'s data:', error);
  432. }
  433. },
  434. //正序逆序
  435. changeup() {
  436. if (this.data.rank == 'up') {
  437. this.setData({
  438. rank: 'down'
  439. })
  440. if (this.data.changetitle == 'today') {
  441. this.setData({
  442. todayList: this.data.todayList.reverse(),
  443. })
  444. console.log('逆序');
  445. }
  446. if (this.data.changetitle == 'weekdday') {
  447. console.log('weekdday逆序');
  448. }
  449. if (this.data.changetitle == 'month') {
  450. console.log('month逆序');
  451. }
  452. } else {
  453. this.setData({
  454. rank: 'up'
  455. })
  456. if (this.data.changetitle == 'today') {
  457. this.setData({
  458. todayList: this.data.todayList.reverse(),
  459. })
  460. console.log('顺序');
  461. }
  462. if (this.data.changetitle == 'weekdday') {
  463. console.log('weekdday顺序');
  464. }
  465. if (this.data.changetitle == 'month') {
  466. console.log('month顺序');
  467. }
  468. }
  469. },
  470. //修改进度条
  471. getBackgroundColor() {
  472. const steps = this.data.sharList[0].steps || 0;
  473. let percentage = (steps / this.data.target) * 100;
  474. if (percentage > 100) {
  475. percentage = 100;
  476. }
  477. this.setData({
  478. percentage: `conic-gradient(from 0deg, #015EEA ${percentage}%, white 0%)`,
  479. })
  480. console.log('百分比', this.data.percentage);
  481. },
  482. }
  483. })