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