index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // nova-werun/pages/home/medal/index.js
  2. const Parse = getApp().Parse;
  3. const company = getApp().globalData.company;
  4. const uid = Parse.User.current()?.id
  5. let sev = require('../../../service/getSportData')
  6. let request = require('../../../../utils/request')
  7. let dateF = require('../../../../utils/date')
  8. Page({
  9. /**
  10. * 页面的初始数据
  11. */
  12. data: {
  13. alreadyList: [],
  14. MedalList: [],
  15. show: false,
  16. title: '',
  17. user: Parse.User.current(),
  18. },
  19. /**
  20. * 生命周期函数--监听页面加载
  21. */
  22. onLoad: function (options) {
  23. this.setData({
  24. user: Parse.User.current()?.toJSON()
  25. });
  26. this.refersh()
  27. },
  28. async refersh() {
  29. await this.getMedal()
  30. this.getCountSport()
  31. },
  32. /**获取勋章 */
  33. async getMedal() {
  34. let sql = `SELECT me."objectId",me."name",me."image",me."imageIn",me."condition",
  35. ml."user",ml."createdAt"
  36. FROM "Medal" me
  37. LEFT JOIN "MedalLog" ml ON me."objectId"=ml."medal" AND ml."user"='${uid}'
  38. WHERE me."company"='${company}'
  39. AND me."isDeleted" IS NOT TRUE
  40. ORDER BY me."order" ASC`
  41. let data = await request.customSQL(sql)
  42. let medalList = data?.map(item => {
  43. item.createdAt = dateF.formatTime('YYYY-MM-dd', item.createdAt)
  44. return item
  45. })
  46. this.setData({
  47. medalList
  48. });
  49. },
  50. /**获取用户累计运动步数-授予勋章 */
  51. async getCountSport() {
  52. let {
  53. medalList
  54. } = this.data
  55. let fromTo = {
  56. from: Parse.User.current()?.get('createdAt'),
  57. to: new Date()
  58. }
  59. let d = await sev.getwalk('distance', '', fromTo) || 0
  60. console.log(d)
  61. let logList = medalList?.filter(item => parseInt(item.condition.distance) <= d && !item.user)
  62. console.log(logList)
  63. if (logList?.length > 0) {
  64. wx.showToast({
  65. title: `太厉害啦!您已累计运动${ (d/1000).toFixed(2) ||0} km,为您解锁${logList?.length}枚勋章。`,
  66. icon: 'none',
  67. duration: 5000
  68. })
  69. for (let i in logList) {
  70. let item = logList[i]
  71. if (parseInt(item.condition.distance) <= d && !item.user) {
  72. let MedalLog = Parse.Object.extend('MedalLog')
  73. let log = new MedalLog()
  74. log.set('company', {
  75. __type: 'Pointer',
  76. className: 'Company',
  77. objectId: company
  78. })
  79. log.set('user', {
  80. __type: 'Pointer',
  81. className: '_User',
  82. objectId: uid
  83. })
  84. log.set('medal', {
  85. __type: 'Pointer',
  86. className: 'Medal',
  87. objectId: item.objectId
  88. })
  89. await log.save()
  90. }
  91. }
  92. this.getMedal()
  93. }
  94. this.setData({distance: (d/1000).toFixed(2) ||0})
  95. },
  96. /**勋章详情 */
  97. showPopup(e) {
  98. let {
  99. medalList
  100. } = this.data
  101. let {
  102. index
  103. } = e.currentTarget.dataset
  104. console.log(medalList[index])
  105. this.setData({
  106. chickMedal: medalList[index],
  107. show: true
  108. });
  109. },
  110. onClose() {
  111. this.setData({
  112. show: false
  113. });
  114. },
  115. /**
  116. * 生命周期函数--监听页面初次渲染完成
  117. */
  118. onReady: function () {
  119. },
  120. /**
  121. * 生命周期函数--监听页面显示
  122. */
  123. onShow: function () {
  124. },
  125. /**
  126. * 生命周期函数--监听页面隐藏
  127. */
  128. onHide: function () {
  129. },
  130. /**
  131. * 生命周期函数--监听页面卸载
  132. */
  133. onUnload: function () {
  134. },
  135. /**
  136. * 页面相关事件处理函数--监听用户下拉动作
  137. */
  138. onPullDownRefresh: function () {
  139. },
  140. /**
  141. * 页面上拉触底事件的处理函数
  142. */
  143. onReachBottom: function () {
  144. },
  145. /**
  146. * 用户点击右上角分享
  147. */
  148. onShareAppMessage: function () {
  149. },
  150. })