index.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. // 这里需要调用你的后端接口进行解密
  244. // 假设你有一个解密函数 decryptData
  245. // this.decryptData(encryptedData, iv).then(steps => {
  246. // this.setData({
  247. // steps: `${steps}`
  248. // });
  249. // console.log('用户步数:', this.data.steps);
  250. // }).catch(err => {
  251. // console.error('解密失败:', err);
  252. // });
  253. const steps = this.decryptData(encryptedData, iv)
  254. console.log(steps);
  255. this.updatetoday(steps)
  256. },
  257. fail: (err) => {
  258. console.error('获取运动数据失败:', err);
  259. }
  260. });
  261. },
  262. decryptData(encryptedData, iv) {
  263. // return new Promise((resolve, reject) => {
  264. // // 发送请求到后端进行解密
  265. // wx.request({
  266. // url: '', // 替换为你的后端解密接口
  267. // method: 'POST',
  268. // data: {
  269. // encryptedData: encryptedData,
  270. // iv: iv,
  271. // // 需要传递 session_key,通常在用户登录时获取
  272. // session_key: wx.getStorageSync('session_key') // 假设你已经存储了 session_key
  273. // },
  274. // success: (res) => {
  275. // if (res.data && res.data.steps) {
  276. // // resolve(res.data.steps); // 返回步数
  277. // const steps = 123456
  278. // resolve(steps);
  279. // } else {
  280. // reject('解密返回数据格式错误');
  281. // }
  282. // },
  283. // fail: (err) => {
  284. // reject(err);
  285. // }
  286. // });
  287. // });
  288. const steps = 1200
  289. return steps
  290. },
  291. //更新今日步数
  292. async updatetoday(steps) {
  293. const currentUser = Parse.User.current();
  294. let ActivityDataquery = new Parse.Query('ActivityData');
  295. ActivityDataquery.equalTo('user', currentUser.id);
  296. ActivityDataquery.equalTo('company', company);
  297. ActivityDataquery.equalTo('type', 'today');
  298. ActivityDataquery.notEqualTo('isDeleted', true);
  299. // 获取今天的日期
  300. const today = new Date();
  301. const todayStart = new Date(today.getFullYear(), today.getMonth(), today.getDate()); // 今天的开始时间
  302. const todayEnd = new Date(todayStart);
  303. todayEnd.setHours(23, 59, 59, 999); // 今天的结束时间
  304. // 在查询条件中添加对 createdAt 的限制
  305. ActivityDataquery.greaterThanOrEqualTo('createdAt', todayStart);
  306. ActivityDataquery.lessThanOrEqualTo('createdAt', todayEnd);
  307. let r = await ActivityDataquery.first();
  308. if (r) {
  309. r.set('steps', steps)
  310. try {
  311. let saveDate = await r.save();
  312. this.order()
  313. this.gettoday()
  314. console.log("步数修改成功");
  315. } catch (error) {
  316. console.error("保存数据时出现错误:", error);
  317. }
  318. } else {
  319. const currentUser = Parse.User.current();
  320. let userquery = new Parse.Query('_User');
  321. userquery.equalTo('company', company);
  322. userquery.equalTo('objectId', currentUser.id);
  323. userquery.notEqualTo('isDeleted', true)
  324. let user = await userquery.first();
  325. let companyPointer = Parse.Object.extend('Company').createWithoutData(company);
  326. let Comment = new Parse.Object('ActivityData');
  327. Comment.set('company', companyPointer);
  328. Comment.set('type', 'today');
  329. Comment.set('user', user.toPointer());
  330. Comment.set('steps', steps);
  331. try {
  332. let saveDate2 = await Comment.save();
  333. this.order()
  334. this.gettoday()
  335. // console.log(saveDate2);
  336. console.log("步数保存成功");
  337. } catch (error) {
  338. console.error("保存数据时出现错误:", error);
  339. }
  340. }
  341. },
  342. //切换
  343. change() {
  344. // 使用数组来简化切换逻辑
  345. const titles = ['today', 'weekdday', 'month'];
  346. const currentIndex = titles.indexOf(this.data.changetitle); //获取index
  347. const nextIndex = (currentIndex + 1) % titles.length; // 循环切换
  348. this.setData({
  349. changetitle: titles[nextIndex]
  350. });
  351. if (this.data.changetitle == 'today') {
  352. this.setData({
  353. rank:'up'
  354. })
  355. this.gettoday()
  356. }
  357. if (this.data.changetitle == 'weekdday') {
  358. this.setData({
  359. rank:'up'
  360. })
  361. console.log('weekdday');
  362. }
  363. if (this.data.changetitle == 'month') {
  364. this.setData({
  365. rank:'up'
  366. })
  367. console.log('month');
  368. }
  369. },
  370. // 获取本日排行
  371. async gettoday() {
  372. const currentUser = Parse.User.current();
  373. let ActivityDataquery = new Parse.Query('ActivityData');
  374. ActivityDataquery.equalTo('company', company);
  375. ActivityDataquery.equalTo('type', "today");
  376. ActivityDataquery.notEqualTo('isDeleted', true);
  377. // 获取今天的日期
  378. const today = new Date();
  379. const todayStart = new Date(today.getFullYear(), today.getMonth(), today.getDate()); // 今天的开始时间
  380. const todayEnd = new Date(todayStart);
  381. todayEnd.setHours(23, 59, 59, 999); // 今天的结束时间
  382. console.log(todayStart, todayEnd);
  383. // 在查询条件中添加对 createdAt 的限制
  384. ActivityDataquery.greaterThanOrEqualTo('createdAt', todayStart);
  385. ActivityDataquery.lessThanOrEqualTo('createdAt', todayEnd);
  386. // 根据 steps 字段进行降序排序
  387. ActivityDataquery.descending('steps');
  388. ActivityDataquery.include('user');
  389. try {
  390. let P = await ActivityDataquery.find();
  391. let todayList = P.map(item => item.toJSON());
  392. // // 初始化 myList
  393. // let myList = [];
  394. // // 找到当前用户的数据并计算排名
  395. // todayList.forEach((item, index) => {
  396. // if (item.user.objectId === currentUser.id) {
  397. // myList.push({
  398. // ...item, // 包含用户数据
  399. // rank: index + 1 // 计算排名(index 从 0 开始,所以加 1)
  400. // });
  401. // }
  402. // });
  403. // 更新页面数据
  404. this.setData({
  405. todayList,
  406. });
  407. console.log(this.data.todayList);
  408. } catch (error) {
  409. console.error('Error fetching today\'s data:', error);
  410. }
  411. },
  412. //正序逆序
  413. changeup(){
  414. if(this.data.rank=='up'){
  415. this.setData({
  416. rank:'down'
  417. })
  418. if (this.data.changetitle == 'today') {
  419. this.setData({
  420. todayList:this.data.todayList.reverse(),
  421. })
  422. console.log('逆序');
  423. }
  424. if (this.data.changetitle == 'weekdday') {
  425. console.log('weekdday逆序');
  426. }
  427. if (this.data.changetitle == 'month') {
  428. console.log('month逆序');
  429. }
  430. }else{
  431. this.setData({
  432. rank:'up'
  433. })
  434. if (this.data.changetitle == 'today') {
  435. this.setData({
  436. todayList:this.data.todayList.reverse(),
  437. })
  438. console.log('顺序');
  439. }
  440. if (this.data.changetitle == 'weekdday') {
  441. console.log('weekdday顺序');
  442. }
  443. if (this.data.changetitle == 'month') {
  444. console.log('month顺序');
  445. }
  446. }
  447. },
  448. //修改进度条
  449. getBackgroundColor() {
  450. const steps = this.data.sharList[0].steps || 0;
  451. let percentage = (steps / this.data.target) * 100;
  452. if (percentage > 100) {
  453. percentage = 100;
  454. }
  455. this.setData({
  456. percentage:`conic-gradient(from 0deg, #015EEA ${percentage}%, white 0%)`,
  457. })
  458. console.log('百分比',this.data.percentage);
  459. },
  460. }
  461. })