index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. lifetimes: {
  70. detached: function () {
  71. // 在组件实例被从页面节点树移除时执行
  72. },
  73. attached: async function () {
  74. // 在组件实例进入页面节点树时执行
  75. // 计算
  76. const systemInfo = wx.getSystemInfoSync();
  77. const statusBarHeight = systemInfo.statusBarHeight || 0;
  78. const screenHeight = systemInfo.screenHeight || 0;
  79. const custom = wx.getMenuButtonBoundingClientRect();
  80. const customHeight = custom.height + 10 + 2 || 0;
  81. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  82. const contentpadding = (statusBarHeight + customHeight) * 750 / systemInfo.windowWidth;
  83. const contentHeight = (screenHeight - bottomNavHeight - 50 - statusBarHeight - customHeight) * 750 / systemInfo.windowWidth;
  84. const navheight = (statusBarHeight+customHeight) * 750 / systemInfo.windowWidth;
  85. this.setData({
  86. statusBarHeight,
  87. screenHeight:(screenHeight-50-bottomNavHeight) * 750 / systemInfo.windowWidth,
  88. customHeight,
  89. bottomNavHeight,
  90. contentHeight,
  91. contentpadding,
  92. navheight,
  93. });
  94. console.log('123', contentpadding);
  95. this.getWeRunData()
  96. this.gettarget()
  97. // this.order()
  98. this.Getlocation()
  99. },
  100. },
  101. /**
  102. * 组件的方法列表
  103. */
  104. methods: {
  105. //跳转
  106. gourl(e) {
  107. const url = e.currentTarget.dataset.url
  108. const active = e.currentTarget.dataset.active
  109. if (active) {
  110. wx.navigateTo({
  111. url: `${url}?id=` + active // 目标页面的路径
  112. });
  113. console.log(active);
  114. } else {
  115. wx.navigateTo({
  116. url: `${url}` // 目标页面的路径
  117. });
  118. }
  119. },
  120. //获取目标步数
  121. async gettarget() {
  122. const currentUser = Parse.User.current();
  123. let userquery = new Parse.Query('_User');
  124. userquery.equalTo('company', company);
  125. userquery.equalTo('objectId', currentUser.id);
  126. userquery.notEqualTo('isDeleted', true)
  127. let user = await userquery.find();
  128. let num = user.map(item => item.toJSON());
  129. if (num[0].num) {
  130. this.setData({
  131. target: num[0].num
  132. })
  133. console.log('当前步数', this.data.target);
  134. } else {
  135. const currentUser2 = Parse.User.current();
  136. let userquery2 = new Parse.Query('_User');
  137. userquery2.equalTo('company', company);
  138. userquery2.equalTo('objectId', currentUser2.id);
  139. userquery2.notEqualTo('isDeleted', true)
  140. let user2 = await userquery2.first();
  141. user2.set('num', 5000)
  142. try {
  143. let saveDate2 = await user2.save();
  144. console.log(saveDate2);
  145. this.setData({
  146. target: 5000
  147. })
  148. console.log("目标步数更改成功");
  149. } catch (error) {
  150. console.error("保存数据时出现错误:", error);
  151. }
  152. }
  153. },
  154. //获取当天运动数据
  155. async order() {
  156. const currentUser = Parse.User.current();
  157. let ActivityDataquery = new Parse.Query('ActivityData');
  158. ActivityDataquery.equalTo('user', currentUser.id);
  159. ActivityDataquery.equalTo('company', company);
  160. ActivityDataquery.equalTo('type', 'today');
  161. ActivityDataquery.notEqualTo('isDeleted', true);
  162. // 获取今天的日期
  163. const today = new Date();
  164. const todayStart = new Date(today.getFullYear(), today.getMonth(), today.getDate()); // 今天的开始时间
  165. const todayEnd = new Date(todayStart);
  166. todayEnd.setHours(23, 59, 59, 999); // 今天的结束时间
  167. // 在查询条件中添加对 createdAt 的限制
  168. ActivityDataquery.greaterThanOrEqualTo('createdAt', todayStart);
  169. ActivityDataquery.lessThanOrEqualTo('createdAt', todayEnd);
  170. ActivityDataquery.include('user');
  171. let r = await ActivityDataquery.find();
  172. let sharList = r.map(item => item.toJSON());
  173. this.setData({
  174. sharList
  175. });
  176. console.log(this.data.sharList);
  177. },
  178. //获取当前位置信息
  179. Getlocation() {
  180. // 获取当前位置信息
  181. wx.getLocation({
  182. type: 'wgs84',
  183. success: (res) => {
  184. const {
  185. latitude,
  186. longitude
  187. } = res;
  188. //调用api解析地址
  189. wx.request({
  190. url: 'https://api.map.baidu.com/reverse_geocoding/v3/?ak=sHZTomd7grslfP7sPKB8tRgT49FK9TEu&output=json&coordtype=gcj02&location=' + latitude + ',' + longitude,
  191. data: {},
  192. header: {
  193. 'Content-Type': 'application/json'
  194. },
  195. success: (ops) => { // 使用箭头函数
  196. console.log(ops);
  197. // const address = ops.data.result.formatted_address;//详细地址
  198. const address = ops.data.result.addressComponent.city;//市
  199. this.setData({
  200. address: address,
  201. });
  202. console.log(this.data.address);
  203. },
  204. fail: function (resq) {
  205. wx.showModal({
  206. title: '信息提示',
  207. content: '请求失败',
  208. showCancel: false,
  209. confirmColor: '#f37938'
  210. });
  211. },
  212. complete: function () {}
  213. })
  214. },
  215. fail: (err) => {
  216. console.error(err);
  217. wx.showToast({
  218. title: '获取位置失败',
  219. icon: 'none'
  220. });
  221. }
  222. });
  223. },
  224. //获取微信步数
  225. getWeRunData() {
  226. wx.getWeRunData({
  227. success: (res) => {
  228. // 获取到的加密数据
  229. const encryptedData = res.encryptedData;
  230. const iv = res.iv;
  231. // 这里需要调用你的后端接口进行解密
  232. // 假设你有一个解密函数 decryptData
  233. // this.decryptData(encryptedData, iv).then(steps => {
  234. // this.setData({
  235. // steps: `${steps}`
  236. // });
  237. // console.log('用户步数:', this.data.steps);
  238. // }).catch(err => {
  239. // console.error('解密失败:', err);
  240. // });
  241. const steps = this.decryptData(encryptedData, iv)
  242. console.log(steps);
  243. this.updatetoday(steps)
  244. },
  245. fail: (err) => {
  246. console.error('获取运动数据失败:', err);
  247. }
  248. });
  249. },
  250. decryptData(encryptedData, iv) {
  251. // return new Promise((resolve, reject) => {
  252. // // 发送请求到后端进行解密
  253. // wx.request({
  254. // url: '', // 替换为你的后端解密接口
  255. // method: 'POST',
  256. // data: {
  257. // encryptedData: encryptedData,
  258. // iv: iv,
  259. // // 需要传递 session_key,通常在用户登录时获取
  260. // session_key: wx.getStorageSync('session_key') // 假设你已经存储了 session_key
  261. // },
  262. // success: (res) => {
  263. // if (res.data && res.data.steps) {
  264. // // resolve(res.data.steps); // 返回步数
  265. // const steps = 123456
  266. // resolve(steps);
  267. // } else {
  268. // reject('解密返回数据格式错误');
  269. // }
  270. // },
  271. // fail: (err) => {
  272. // reject(err);
  273. // }
  274. // });
  275. // });
  276. const steps = 33456
  277. return steps
  278. },
  279. //更新今日步数
  280. async updatetoday(steps) {
  281. const currentUser = Parse.User.current();
  282. let ActivityDataquery = new Parse.Query('ActivityData');
  283. ActivityDataquery.equalTo('user', currentUser.id);
  284. ActivityDataquery.equalTo('company', company);
  285. ActivityDataquery.equalTo('type', 'today');
  286. ActivityDataquery.notEqualTo('isDeleted', true);
  287. // 获取今天的日期
  288. const today = new Date();
  289. const todayStart = new Date(today.getFullYear(), today.getMonth(), today.getDate()); // 今天的开始时间
  290. const todayEnd = new Date(todayStart);
  291. todayEnd.setHours(23, 59, 59, 999); // 今天的结束时间
  292. // 在查询条件中添加对 createdAt 的限制
  293. ActivityDataquery.greaterThanOrEqualTo('createdAt', todayStart);
  294. ActivityDataquery.lessThanOrEqualTo('createdAt', todayEnd);
  295. let r = await ActivityDataquery.first();
  296. if (r) {
  297. r.set('steps', steps)
  298. try {
  299. let saveDate = await r.save();
  300. this.order()
  301. console.log("步数修改成功");
  302. } catch (error) {
  303. console.error("保存数据时出现错误:", error);
  304. }
  305. } else {
  306. const currentUser = Parse.User.current();
  307. let userquery = new Parse.Query('_User');
  308. userquery.equalTo('company', company);
  309. userquery.equalTo('objectId', currentUser.id);
  310. userquery.notEqualTo('isDeleted', true)
  311. let user = await userquery.first();
  312. let companyPointer = Parse.Object.extend('Company').createWithoutData(company);
  313. let Comment = new Parse.Object('ActivityData');
  314. Comment.set('company', companyPointer);
  315. Comment.set('type', 'today');
  316. Comment.set('user', user.toPointer());
  317. Comment.set('steps', steps);
  318. try {
  319. let saveDate2 = await Comment.save();
  320. this.order()
  321. // console.log(saveDate2);
  322. console.log("步数保存成功");
  323. } catch (error) {
  324. console.error("保存数据时出现错误:", error);
  325. }
  326. }
  327. }
  328. }
  329. })