index.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. //选择
  23. rows: [{
  24. image: 'https://file-cloud.fmode.cn/qpFbRRSZrO/20241225/r5j1uc041211788.png',
  25. text: '签到打卡',
  26. url: '../../pages/home/signin/index'
  27. },
  28. {
  29. image: 'https://file-cloud.fmode.cn/qpFbRRSZrO/20241225/n1pin1040819673.png',
  30. text: '实时步行',
  31. url: '../../pages/home/sport/sport-home/index',
  32. },
  33. {
  34. image: 'https://file-cloud.fmode.cn/qpFbRRSZrO/20241225/9vfr1l040831635.png',
  35. text: '转发分享',
  36. url: '../../pages/home/share/index'
  37. },
  38. {
  39. image: 'https://file-cloud.fmode.cn/qpFbRRSZrO/20241225/it1rna040843179.png',
  40. text: '我的勋章',
  41. url: '../../pages/home/medal/index'
  42. },
  43. {
  44. image: 'https://file-cloud.fmode.cn/qpFbRRSZrO/20241225/r5193r040853498.png',
  45. text: '我的统计',
  46. url: '../../pages/home/statistics/index'
  47. },
  48. // {
  49. // image: 'https://file-cloud.fmode.cn/qpFbRRSZrO/20241102/v6p4fm041343296.png?imageView2/1/w/200/h/200',
  50. // text: '排行榜',
  51. // url: '../../pages/home/ranking/index'
  52. // },
  53. // {
  54. // image: 'https://file-cloud.fmode.cn/qpFbRRSZrO/20241102/71svpg041343669.png?imageView2/1/w/200/h/200',
  55. // text: '跑步',
  56. // url: '../../pages/home/sport/sport-home/index',
  57. // active: 1
  58. // },
  59. // {
  60. // image: 'https://file-cloud.fmode.cn/qpFbRRSZrO/20241104/k1q4no034958681.png?imageView2/1/w/200/h/200',
  61. // text: '积分',
  62. // url: '../../pages/home/integral/index'
  63. // },
  64. ],
  65. target: 0,
  66. sharList: [],
  67. address: "",
  68. //排行榜
  69. todayList: [],
  70. changetitle: 'today',
  71. //点赞
  72. isclick:false,
  73. //正序
  74. rank:'up'
  75. },
  76. lifetimes: {
  77. detached: function () {
  78. // 在组件实例被从页面节点树移除时执行
  79. },
  80. attached: async function () {
  81. // 在组件实例进入页面节点树时执行
  82. // 计算
  83. const systemInfo = wx.getSystemInfoSync();
  84. const statusBarHeight = systemInfo.statusBarHeight || 0;
  85. const screenHeight = systemInfo.screenHeight || 0;
  86. const custom = wx.getMenuButtonBoundingClientRect();
  87. const customHeight = custom.height + 10 + 2 || 0;
  88. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  89. const contentpadding = (statusBarHeight + customHeight) * 750 / systemInfo.windowWidth;
  90. const contentHeight = (screenHeight - bottomNavHeight - 50 - statusBarHeight - customHeight) * 750 / systemInfo.windowWidth;
  91. const navheight = (statusBarHeight + customHeight) * 750 / systemInfo.windowWidth;
  92. this.setData({
  93. statusBarHeight,
  94. screenHeight: (screenHeight - 50 - bottomNavHeight) * 750 / systemInfo.windowWidth,
  95. customHeight,
  96. bottomNavHeight,
  97. contentHeight,
  98. contentpadding,
  99. navheight,
  100. });
  101. console.log('123', contentpadding);
  102. this.getWeRunData()
  103. this.gettarget()
  104. // this.order()
  105. this.Getlocation()
  106. this.gettoday()
  107. },
  108. },
  109. /**
  110. * 组件的方法列表
  111. */
  112. methods: {
  113. //跳转
  114. gourl(e) {
  115. const url = e.currentTarget.dataset.url
  116. const active = e.currentTarget.dataset.active
  117. if (active) {
  118. wx.navigateTo({
  119. url: `${url}?id=` + active // 目标页面的路径
  120. });
  121. console.log(active);
  122. } else {
  123. wx.navigateTo({
  124. url: `${url}` // 目标页面的路径
  125. });
  126. }
  127. },
  128. //获取目标步数
  129. async gettarget() {
  130. const currentUser = Parse.User.current();
  131. let userquery = new Parse.Query('_User');
  132. userquery.equalTo('company', company);
  133. userquery.equalTo('objectId', currentUser.id);
  134. userquery.notEqualTo('isDeleted', true)
  135. let user = await userquery.find();
  136. let num = user.map(item => item.toJSON());
  137. if (num[0].num) {
  138. this.setData({
  139. target: num[0].num
  140. })
  141. console.log('当前步数', this.data.target);
  142. } else {
  143. const currentUser2 = Parse.User.current();
  144. let userquery2 = new Parse.Query('_User');
  145. userquery2.equalTo('company', company);
  146. userquery2.equalTo('objectId', currentUser2.id);
  147. userquery2.notEqualTo('isDeleted', true)
  148. let user2 = await userquery2.first();
  149. user2.set('num', 5000)
  150. try {
  151. let saveDate2 = await user2.save();
  152. console.log(saveDate2);
  153. this.setData({
  154. target: 5000
  155. })
  156. console.log("目标步数更改成功");
  157. } catch (error) {
  158. console.error("保存数据时出现错误:", error);
  159. }
  160. }
  161. },
  162. //获取当天运动数据
  163. async order() {
  164. const currentUser = Parse.User.current();
  165. let ActivityDataquery = new Parse.Query('ActivityData');
  166. ActivityDataquery.equalTo('user', currentUser.id);
  167. ActivityDataquery.equalTo('company', company);
  168. ActivityDataquery.equalTo('type', 'today');
  169. ActivityDataquery.notEqualTo('isDeleted', true);
  170. // 获取今天的日期
  171. const today = new Date();
  172. const todayStart = new Date(today.getFullYear(), today.getMonth(), today.getDate()); // 今天的开始时间
  173. const todayEnd = new Date(todayStart);
  174. todayEnd.setHours(23, 59, 59, 999); // 今天的结束时间
  175. // 在查询条件中添加对 createdAt 的限制
  176. ActivityDataquery.greaterThanOrEqualTo('createdAt', todayStart);
  177. ActivityDataquery.lessThanOrEqualTo('createdAt', todayEnd);
  178. ActivityDataquery.include('user');
  179. let r = await ActivityDataquery.find();
  180. let sharList = r.map(item => item.toJSON());
  181. this.setData({
  182. sharList
  183. });
  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. // 这里需要调用你的后端接口进行解密
  240. // 假设你有一个解密函数 decryptData
  241. // this.decryptData(encryptedData, iv).then(steps => {
  242. // this.setData({
  243. // steps: `${steps}`
  244. // });
  245. // console.log('用户步数:', this.data.steps);
  246. // }).catch(err => {
  247. // console.error('解密失败:', err);
  248. // });
  249. const steps = this.decryptData(encryptedData, iv)
  250. console.log(steps);
  251. this.updatetoday(steps)
  252. },
  253. fail: (err) => {
  254. console.error('获取运动数据失败:', err);
  255. }
  256. });
  257. },
  258. decryptData(encryptedData, iv) {
  259. // return new Promise((resolve, reject) => {
  260. // // 发送请求到后端进行解密
  261. // wx.request({
  262. // url: '', // 替换为你的后端解密接口
  263. // method: 'POST',
  264. // data: {
  265. // encryptedData: encryptedData,
  266. // iv: iv,
  267. // // 需要传递 session_key,通常在用户登录时获取
  268. // session_key: wx.getStorageSync('session_key') // 假设你已经存储了 session_key
  269. // },
  270. // success: (res) => {
  271. // if (res.data && res.data.steps) {
  272. // // resolve(res.data.steps); // 返回步数
  273. // const steps = 123456
  274. // resolve(steps);
  275. // } else {
  276. // reject('解密返回数据格式错误');
  277. // }
  278. // },
  279. // fail: (err) => {
  280. // reject(err);
  281. // }
  282. // });
  283. // });
  284. const steps = 33456
  285. return steps
  286. },
  287. //更新今日步数
  288. async updatetoday(steps) {
  289. const currentUser = Parse.User.current();
  290. let ActivityDataquery = new Parse.Query('ActivityData');
  291. ActivityDataquery.equalTo('user', currentUser.id);
  292. ActivityDataquery.equalTo('company', company);
  293. ActivityDataquery.equalTo('type', 'today');
  294. ActivityDataquery.notEqualTo('isDeleted', true);
  295. // 获取今天的日期
  296. const today = new Date();
  297. const todayStart = new Date(today.getFullYear(), today.getMonth(), today.getDate()); // 今天的开始时间
  298. const todayEnd = new Date(todayStart);
  299. todayEnd.setHours(23, 59, 59, 999); // 今天的结束时间
  300. // 在查询条件中添加对 createdAt 的限制
  301. ActivityDataquery.greaterThanOrEqualTo('createdAt', todayStart);
  302. ActivityDataquery.lessThanOrEqualTo('createdAt', todayEnd);
  303. let r = await ActivityDataquery.first();
  304. if (r) {
  305. r.set('steps', steps)
  306. try {
  307. let saveDate = await r.save();
  308. this.order()
  309. console.log("步数修改成功");
  310. } catch (error) {
  311. console.error("保存数据时出现错误:", error);
  312. }
  313. } else {
  314. const currentUser = Parse.User.current();
  315. let userquery = new Parse.Query('_User');
  316. userquery.equalTo('company', company);
  317. userquery.equalTo('objectId', currentUser.id);
  318. userquery.notEqualTo('isDeleted', true)
  319. let user = await userquery.first();
  320. let companyPointer = Parse.Object.extend('Company').createWithoutData(company);
  321. let Comment = new Parse.Object('ActivityData');
  322. Comment.set('company', companyPointer);
  323. Comment.set('type', 'today');
  324. Comment.set('user', user.toPointer());
  325. Comment.set('steps', steps);
  326. try {
  327. let saveDate2 = await Comment.save();
  328. this.order()
  329. // console.log(saveDate2);
  330. console.log("步数保存成功");
  331. } catch (error) {
  332. console.error("保存数据时出现错误:", error);
  333. }
  334. }
  335. },
  336. //切换
  337. change() {
  338. // 使用数组来简化切换逻辑
  339. const titles = ['today', 'weekdday', 'month'];
  340. const currentIndex = titles.indexOf(this.data.changetitle); //获取index
  341. const nextIndex = (currentIndex + 1) % titles.length; // 循环切换
  342. this.setData({
  343. changetitle: titles[nextIndex]
  344. });
  345. if (this.data.changetitle == 'today') {
  346. this.setData({
  347. rank:'up'
  348. })
  349. this.gettoday()
  350. }
  351. if (this.data.changetitle == 'weekdday') {
  352. this.setData({
  353. rank:'up'
  354. })
  355. console.log('weekdday');
  356. }
  357. if (this.data.changetitle == 'month') {
  358. this.setData({
  359. rank:'up'
  360. })
  361. console.log('month');
  362. }
  363. },
  364. // 获取本日排行
  365. async gettoday() {
  366. const currentUser = Parse.User.current();
  367. let ActivityDataquery = new Parse.Query('ActivityData');
  368. ActivityDataquery.equalTo('company', company);
  369. ActivityDataquery.equalTo('type', "today");
  370. ActivityDataquery.notEqualTo('isDeleted', true);
  371. // 获取今天的日期
  372. const today = new Date();
  373. const todayStart = new Date(today.getFullYear(), today.getMonth(), today.getDate()); // 今天的开始时间
  374. const todayEnd = new Date(todayStart);
  375. todayEnd.setHours(23, 59, 59, 999); // 今天的结束时间
  376. console.log(todayStart, todayEnd);
  377. // 在查询条件中添加对 createdAt 的限制
  378. ActivityDataquery.greaterThanOrEqualTo('createdAt', todayStart);
  379. ActivityDataquery.lessThanOrEqualTo('createdAt', todayEnd);
  380. // 根据 steps 字段进行降序排序
  381. ActivityDataquery.descending('steps');
  382. ActivityDataquery.include('user');
  383. try {
  384. let P = await ActivityDataquery.find();
  385. let todayList = P.map(item => item.toJSON());
  386. // // 初始化 myList
  387. // let myList = [];
  388. // // 找到当前用户的数据并计算排名
  389. // todayList.forEach((item, index) => {
  390. // if (item.user.objectId === currentUser.id) {
  391. // myList.push({
  392. // ...item, // 包含用户数据
  393. // rank: index + 1 // 计算排名(index 从 0 开始,所以加 1)
  394. // });
  395. // }
  396. // });
  397. // 更新页面数据
  398. this.setData({
  399. todayList,
  400. });
  401. console.log(this.data.todayList);
  402. } catch (error) {
  403. console.error('Error fetching today\'s data:', error);
  404. }
  405. },
  406. //正序逆序
  407. changeup(){
  408. if(this.data.rank=='up'){
  409. this.setData({
  410. rank:'down'
  411. })
  412. if (this.data.changetitle == 'today') {
  413. this.setData({
  414. todayList:this.data.todayList.reverse(),
  415. })
  416. console.log('逆序');
  417. }
  418. if (this.data.changetitle == 'weekdday') {
  419. console.log('weekdday逆序');
  420. }
  421. if (this.data.changetitle == 'month') {
  422. console.log('month逆序');
  423. }
  424. }else{
  425. this.setData({
  426. rank:'up'
  427. })
  428. if (this.data.changetitle == 'today') {
  429. this.setData({
  430. todayList:this.data.todayList.reverse(),
  431. })
  432. console.log('顺序');
  433. }
  434. if (this.data.changetitle == 'weekdday') {
  435. console.log('weekdday顺序');
  436. }
  437. if (this.data.changetitle == 'month') {
  438. console.log('month顺序');
  439. }
  440. }
  441. },
  442. }
  443. })