index.js 14 KB

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